/app/code/core/Mage/Core/Model/Store/Group.php

https://bitbucket.org/spenna/alexoo_produzione · PHP · 344 lines · 160 code · 32 blank · 152 comment · 23 complexity · 3b5c1b6f11a780d380ee5c7d66588440 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Core
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Store group model
  28. *
  29. * @method Mage_Core_Model_Resource_Store_Group _getResource()
  30. * @method Mage_Core_Model_Resource_Store_Group getResource()
  31. * @method Mage_Core_Model_Store_Group setWebsiteId(int $value)
  32. * @method string getName()
  33. * @method Mage_Core_Model_Store_Group setName(string $value)
  34. * @method Mage_Core_Model_Store_Group setRootCategoryId(int $value)
  35. * @method Mage_Core_Model_Store_Group setDefaultStoreId(int $value)
  36. *
  37. * @category Mage
  38. * @package Mage_Core
  39. * @author Magento Core Team <core@magentocommerce.com>
  40. */
  41. class Mage_Core_Model_Store_Group extends Mage_Core_Model_Abstract
  42. {
  43. const ENTITY = 'store_group';
  44. const CACHE_TAG = 'store_group';
  45. protected $_cacheTag = true;
  46. /**
  47. * @var string
  48. */
  49. protected $_eventPrefix = 'store_group';
  50. /**
  51. * @var string
  52. */
  53. protected $_eventObject = 'store_group';
  54. /**
  55. * Group Store collection array
  56. *
  57. * @var array
  58. */
  59. protected $_stores;
  60. /**
  61. * Group store ids array
  62. *
  63. * @var array
  64. */
  65. protected $_storeIds = array();
  66. /**
  67. * Group store codes array
  68. *
  69. * @var array
  70. */
  71. protected $_storeCodes = array();
  72. /**
  73. * The number of stores in a group
  74. *
  75. * @var int
  76. */
  77. protected $_storesCount = 0;
  78. /**
  79. * Group default store
  80. *
  81. * @var Mage_Core_Model_Store
  82. */
  83. protected $_defaultStore;
  84. /**
  85. * Website model
  86. *
  87. * @var Mage_Core_Model_Website
  88. */
  89. protected $_website;
  90. /**
  91. * @var bool
  92. */
  93. private $_isReadOnly = false;
  94. /**
  95. * init model
  96. *
  97. */
  98. protected function _construct()
  99. {
  100. $this->_init('core/store_group');
  101. }
  102. /**
  103. * Load store collection and set internal data
  104. *
  105. */
  106. protected function _loadStores()
  107. {
  108. $this->_stores = array();
  109. $this->_storesCount = 0;
  110. foreach ($this->getStoreCollection() as $store) {
  111. $this->_stores[$store->getId()] = $store;
  112. $this->_storeIds[$store->getId()] = $store->getId();
  113. $this->_storeCodes[$store->getId()] = $store->getCode();
  114. if ($this->getDefaultStoreId() == $store->getId()) {
  115. $this->_defaultStore = $store;
  116. }
  117. $this->_storesCount ++;
  118. }
  119. }
  120. /**
  121. * Set website stores
  122. *
  123. * @param array $stores
  124. */
  125. public function setStores($stores)
  126. {
  127. $this->_stores = array();
  128. $this->_storesCount = 0;
  129. foreach ($stores as $store) {
  130. $this->_stores[$store->getId()] = $store;
  131. $this->_storeIds[$store->getId()] = $store->getId();
  132. $this->_storeCodes[$store->getId()] = $store->getCode();
  133. if ($this->getDefaultStoreId() == $store->getId()) {
  134. $this->_defaultStore = $store;
  135. }
  136. $this->_storesCount ++;
  137. }
  138. }
  139. /**
  140. * Retrieve new (not loaded) Store collection object with group filter
  141. *
  142. * @return Mage_Core_Model_Mysql4_Store_Collection
  143. */
  144. public function getStoreCollection()
  145. {
  146. return Mage::getModel('core/store')
  147. ->getCollection()
  148. ->addGroupFilter($this->getId());
  149. }
  150. /**
  151. * Retrieve wersite store objects
  152. *
  153. * @return array
  154. */
  155. public function getStores()
  156. {
  157. if (is_null($this->_stores)) {
  158. $this->_loadStores();
  159. }
  160. return $this->_stores;
  161. }
  162. /**
  163. * Retrieve website store ids
  164. *
  165. * @return array
  166. */
  167. public function getStoreIds()
  168. {
  169. if (is_null($this->_stores)) {
  170. $this->_loadStores();
  171. }
  172. return $this->_storeIds;
  173. }
  174. /**
  175. * Retrieve website store codes
  176. *
  177. * @return array
  178. */
  179. public function getStoreCodes()
  180. {
  181. if (is_null($this->_stores)) {
  182. $this->_loadStores();
  183. }
  184. return $this->_storeCodes;
  185. }
  186. public function getStoresCount()
  187. {
  188. if (is_null($this->_stores)) {
  189. $this->_loadStores();
  190. }
  191. return $this->_storesCount;
  192. }
  193. /**
  194. * Retrieve default store model
  195. *
  196. * @return Mage_Core_Model_Store
  197. */
  198. public function getDefaultStore()
  199. {
  200. if (!$this->hasDefaultStoreId()) {
  201. return false;
  202. }
  203. if (is_null($this->_stores)) {
  204. $this->_loadStores();
  205. }
  206. return $this->_defaultStore;
  207. }
  208. /**
  209. * Get most suitable store by locale
  210. * If no store with given locale is found - default store is returned
  211. * If group has no stores - null is returned
  212. *
  213. * @param string $locale
  214. * @return Mage_Core_Model_Store|null
  215. */
  216. public function getDefaultStoreByLocale($locale)
  217. {
  218. if ($this->getDefaultStore() && $this->getDefaultStore()->getLocaleCode() == $locale) {
  219. return $this->getDefaultStore();
  220. } else {
  221. $stores = $this->getStoresByLocale($locale);
  222. if (count($stores)) {
  223. return $stores[0];
  224. } else {
  225. return $this->getDefaultStore() ? $this->getDefaultStore() : null;
  226. }
  227. }
  228. }
  229. /**
  230. * Retrieve list of stores with given locale
  231. *
  232. * @param $locale
  233. * @return array
  234. */
  235. public function getStoresByLocale($locale)
  236. {
  237. $stores = array();
  238. foreach ($this->getStores() as $store) {
  239. /* @var $store Mage_Core_Model_Store */
  240. if ($store->getLocaleCode() == $locale) {
  241. array_push($stores, $store);
  242. }
  243. }
  244. return $stores;
  245. }
  246. /**
  247. * Set website model
  248. *
  249. * @param Mage_Core_Model_Website $website
  250. */
  251. public function setWebsite(Mage_Core_Model_Website $website)
  252. {
  253. $this->_website = $website;
  254. }
  255. /**
  256. * Retrieve website model
  257. *
  258. * @return Mage_Core_Model_Website
  259. */
  260. public function getWebsite()
  261. {
  262. if (is_null($this->getWebsiteId())) {
  263. return false;
  264. }
  265. if (is_null($this->_website)) {
  266. $this->_website = Mage::app()->getWebsite($this->getWebsiteId());
  267. }
  268. return $this->_website;
  269. }
  270. /**
  271. * Is can delete group
  272. *
  273. * @return bool
  274. */
  275. public function isCanDelete()
  276. {
  277. if (!$this->getId()) {
  278. return false;
  279. }
  280. return $this->getWebsite()->getDefaultGroupId() != $this->getId();
  281. }
  282. public function getDefaultStoreId()
  283. {
  284. return $this->_getData('default_store_id');
  285. }
  286. public function getRootCategoryId()
  287. {
  288. return $this->_getData('root_category_id');
  289. }
  290. public function getWebsiteId()
  291. {
  292. return $this->_getData('website_id');
  293. }
  294. protected function _beforeDelete()
  295. {
  296. $this->_protectFromNonAdmin();
  297. return parent::_beforeDelete();
  298. }
  299. /**
  300. * Get/Set isReadOnly flag
  301. *
  302. * @param bool $value
  303. * @return bool
  304. */
  305. public function isReadOnly($value = null)
  306. {
  307. if (null !== $value) {
  308. $this->_isReadOnly = (bool)$value;
  309. }
  310. return $this->_isReadOnly;
  311. }
  312. }