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: /**
28: * Store group model
29: *
30: * @method Mage_Core_Model_Resource_Store_Group _getResource()
31: * @method Mage_Core_Model_Resource_Store_Group getResource()
32: * @method Mage_Core_Model_Store_Group setWebsiteId(int $value)
33: * @method string getName()
34: * @method Mage_Core_Model_Store_Group setName(string $value)
35: * @method Mage_Core_Model_Store_Group setRootCategoryId(int $value)
36: * @method Mage_Core_Model_Store_Group setDefaultStoreId(int $value)
37: *
38: * @category Mage
39: * @package Mage_Core
40: * @author Magento Core Team <core@magentocommerce.com>
41: */
42:
43: class Mage_Core_Model_Store_Group extends Mage_Core_Model_Abstract
44: {
45: const ENTITY = 'store_group';
46: const CACHE_TAG = 'store_group';
47:
48: protected $_cacheTag = true;
49:
50: /**
51: * @var string
52: */
53: protected $_eventPrefix = 'store_group';
54:
55: /**
56: * @var string
57: */
58: protected $_eventObject = 'store_group';
59:
60: /**
61: * Group Store collection array
62: *
63: * @var array
64: */
65: protected $_stores;
66:
67: /**
68: * Group store ids array
69: *
70: * @var array
71: */
72: protected $_storeIds = array();
73:
74: /**
75: * Group store codes array
76: *
77: * @var array
78: */
79: protected $_storeCodes = array();
80:
81: /**
82: * The number of stores in a group
83: *
84: * @var int
85: */
86: protected $_storesCount = 0;
87:
88: /**
89: * Group default store
90: *
91: * @var Mage_Core_Model_Store
92: */
93: protected $_defaultStore;
94:
95: /**
96: * Website model
97: *
98: * @var Mage_Core_Model_Website
99: */
100: protected $_website;
101:
102: /**
103: * @var bool
104: */
105: private $_isReadOnly = false;
106:
107: /**
108: * init model
109: *
110: */
111: protected function _construct()
112: {
113: $this->_init('core/store_group');
114: }
115:
116: /**
117: * Load store collection and set internal data
118: *
119: */
120: protected function _loadStores()
121: {
122: $this->_stores = array();
123: $this->_storesCount = 0;
124: foreach ($this->getStoreCollection() as $store) {
125: $this->_stores[$store->getId()] = $store;
126: $this->_storeIds[$store->getId()] = $store->getId();
127: $this->_storeCodes[$store->getId()] = $store->getCode();
128: if ($this->getDefaultStoreId() == $store->getId()) {
129: $this->_defaultStore = $store;
130: }
131: $this->_storesCount ++;
132: }
133: }
134:
135: /**
136: * Set website stores
137: *
138: * @param array $stores
139: */
140: public function setStores($stores)
141: {
142: $this->_stores = array();
143: $this->_storesCount = 0;
144: foreach ($stores as $store) {
145: $this->_stores[$store->getId()] = $store;
146: $this->_storeIds[$store->getId()] = $store->getId();
147: $this->_storeCodes[$store->getId()] = $store->getCode();
148: if ($this->getDefaultStoreId() == $store->getId()) {
149: $this->_defaultStore = $store;
150: }
151: $this->_storesCount ++;
152: }
153: }
154:
155: /**
156: * Retrieve new (not loaded) Store collection object with group filter
157: *
158: * @return Mage_Core_Model_Mysql4_Store_Collection
159: */
160: public function getStoreCollection()
161: {
162: return Mage::getModel('core/store')
163: ->getCollection()
164: ->addGroupFilter($this->getId());
165: }
166:
167: /**
168: * Retrieve wersite store objects
169: *
170: * @return array
171: */
172: public function getStores()
173: {
174: if (is_null($this->_stores)) {
175: $this->_loadStores();
176: }
177: return $this->_stores;
178: }
179:
180: /**
181: * Retrieve website store ids
182: *
183: * @return array
184: */
185: public function getStoreIds()
186: {
187: if (is_null($this->_stores)) {
188: $this->_loadStores();
189: }
190: return $this->_storeIds;
191: }
192:
193: /**
194: * Retrieve website store codes
195: *
196: * @return array
197: */
198: public function getStoreCodes()
199: {
200: if (is_null($this->_stores)) {
201: $this->_loadStores();
202: }
203: return $this->_storeCodes;
204: }
205:
206: public function getStoresCount()
207: {
208: if (is_null($this->_stores)) {
209: $this->_loadStores();
210: }
211: return $this->_storesCount;
212: }
213:
214: /**
215: * Retrieve default store model
216: *
217: * @return Mage_Core_Model_Store
218: */
219: public function getDefaultStore()
220: {
221: if (!$this->hasDefaultStoreId()) {
222: return false;
223: }
224: if (is_null($this->_stores)) {
225: $this->_loadStores();
226: }
227: return $this->_defaultStore;
228: }
229:
230: /**
231: * Get most suitable store by locale
232: * If no store with given locale is found - default store is returned
233: * If group has no stores - null is returned
234: *
235: * @param string $locale
236: * @return Mage_Core_Model_Store|null
237: */
238: public function getDefaultStoreByLocale($locale)
239: {
240: if ($this->getDefaultStore() && $this->getDefaultStore()->getLocaleCode() == $locale) {
241: return $this->getDefaultStore();
242: } else {
243: $stores = $this->getStoresByLocale($locale);
244: if (count($stores)) {
245: return $stores[0];
246: } else {
247: return $this->getDefaultStore() ? $this->getDefaultStore() : null;
248: }
249: }
250: }
251:
252: /**
253: * Retrieve list of stores with given locale
254: *
255: * @param $locale
256: * @return array
257: */
258: public function getStoresByLocale($locale)
259: {
260: $stores = array();
261: foreach ($this->getStores() as $store) {
262: /* @var $store Mage_Core_Model_Store */
263: if ($store->getLocaleCode() == $locale) {
264: array_push($stores, $store);
265: }
266: }
267: return $stores;
268: }
269:
270: /**
271: * Set website model
272: *
273: * @param Mage_Core_Model_Website $website
274: */
275: public function setWebsite(Mage_Core_Model_Website $website)
276: {
277: $this->_website = $website;
278: }
279:
280: /**
281: * Retrieve website model
282: *
283: * @return Mage_Core_Model_Website
284: */
285: public function getWebsite()
286: {
287: if (is_null($this->getWebsiteId())) {
288: return false;
289: }
290: if (is_null($this->_website)) {
291: $this->_website = Mage::app()->getWebsite($this->getWebsiteId());
292: }
293: return $this->_website;
294: }
295:
296: /**
297: * Is can delete group
298: *
299: * @return bool
300: */
301: public function isCanDelete()
302: {
303: if (!$this->getId()) {
304: return false;
305: }
306:
307: return $this->getWebsite()->getDefaultGroupId() != $this->getId();
308: }
309:
310: public function getDefaultStoreId()
311: {
312: return $this->_getData('default_store_id');
313: }
314:
315: public function getRootCategoryId()
316: {
317: return $this->_getData('root_category_id');
318: }
319:
320: public function getWebsiteId()
321: {
322: return $this->_getData('website_id');
323: }
324:
325: protected function _beforeDelete()
326: {
327: $this->_protectFromNonAdmin();
328: return parent::_beforeDelete();
329: }
330:
331: /**
332: * Get/Set isReadOnly flag
333: *
334: * @param bool $value
335: * @return bool
336: */
337: public function isReadOnly($value = null)
338: {
339: if (null !== $value) {
340: $this->_isReadOnly = (bool)$value;
341: }
342: return $this->_isReadOnly;
343: }
344: }
345: