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: * Core Website model
29: *
30: * @method Mage_Core_Model_Resource_Website _getResource()
31: * @method Mage_Core_Model_Resource_Website getResource()
32: * @method Mage_Core_Model_Website setCode(string $value)
33: * @method string getName()
34: * @method Mage_Core_Model_Website setName(string $value)
35: * @method int getSortOrder()
36: * @method Mage_Core_Model_Website setSortOrder(int $value)
37: * @method Mage_Core_Model_Website setDefaultGroupId(int $value)
38: * @method int getIsDefault()
39: * @method Mage_Core_Model_Website setIsDefault(int $value)
40: *
41: * @category Mage
42: * @package Mage_Core
43: * @author Magento Core Team <core@magentocommerce.com>
44: */
45:
46: class Mage_Core_Model_Website extends Mage_Core_Model_Abstract
47: {
48: const ENTITY = 'core_website';
49: const CACHE_TAG = 'website';
50: protected $_cacheTag = true;
51:
52: /**
53: * @var string
54: */
55: protected $_eventPrefix = 'website';
56:
57: /**
58: * @var string
59: */
60: protected $_eventObject = 'website';
61:
62: /**
63: * Cache configuration array
64: *
65: * @var array
66: */
67: protected $_configCache = array();
68:
69: /**
70: * Website Group Coleection array
71: *
72: * @var array
73: */
74: protected $_groups;
75:
76: /**
77: * Website group ids array
78: *
79: * @var array
80: */
81: protected $_groupIds = array();
82:
83: /**
84: * The number of groups in a website
85: *
86: * @var int
87: */
88: protected $_groupsCount;
89:
90: /**
91: * Website Store collection array
92: *
93: * @var array
94: */
95: protected $_stores;
96:
97: /**
98: * Website store ids array
99: *
100: * @var array
101: */
102: protected $_storeIds = array();
103:
104: /**
105: * Website store codes array
106: *
107: * @var array
108: */
109: protected $_storeCodes = array();
110:
111: /**
112: * The number of stores in a website
113: *
114: * @var int
115: */
116: protected $_storesCount = 0;
117:
118: /**
119: * Website default group
120: *
121: * @var Mage_Core_Model_Store_Group
122: */
123: protected $_defaultGroup;
124:
125: /**
126: * Website default store
127: *
128: * @var Mage_Core_Model_Store
129: */
130: protected $_defaultStore;
131:
132: /**
133: * is can delete website
134: *
135: * @var bool
136: */
137: protected $_isCanDelete;
138:
139: /**
140: * @var bool
141: */
142: private $_isReadOnly = false;
143:
144: /**
145: * init model
146: *
147: */
148: protected function _construct()
149: {
150: $this->_init('core/website');
151: }
152:
153: /**
154: * Custom load
155: *
156: * @param int|string $id
157: * @param string $field
158: * @return Mage_Core_Model_Website
159: */
160: public function load($id, $field = null)
161: {
162: if (!is_numeric($id) && is_null($field)) {
163: $this->_getResource()->load($this, $id, 'code');
164: return $this;
165: }
166: return parent::load($id, $field);
167: }
168:
169: /**
170: * Load website configuration
171: *
172: * @param string $code
173: * @return Mage_Core_Model_Website
174: */
175: public function loadConfig($code)
176: {
177: if (!Mage::getConfig()->getNode('websites')) {
178: return $this;
179: }
180: if (is_numeric($code)) {
181: foreach (Mage::getConfig()->getNode('websites')->children() as $websiteCode=>$website) {
182: if ((int)$website->system->website->id==$code) {
183: $code = $websiteCode;
184: break;
185: }
186: }
187: } else {
188: $website = Mage::getConfig()->getNode('websites/'.$code);
189: }
190: if (!empty($website)) {
191: $this->setCode($code);
192: $id = (int)$website->system->website->id;
193: $this->setId($id)->setStoreId($id);
194: }
195: return $this;
196: }
197:
198: /**
199: * Get website config data
200: *
201: * @param string $path
202: * @return mixed
203: */
204: public function getConfig($path) {
205: if (!isset($this->_configCache[$path])) {
206:
207: $config = Mage::getConfig()->getNode('websites/'.$this->getCode().'/'.$path);
208: if (!$config) {
209: return false;
210: #throw Mage::exception('Mage_Core', Mage::helper('core')->__('Invalid website\'s configuration path: %s', $path));
211: }
212: if ($config->hasChildren()) {
213: $value = array();
214: foreach ($config->children() as $k=>$v) {
215: $value[$k] = $v;
216: }
217: } else {
218: $value = (string)$config;
219: }
220: $this->_configCache[$path] = $value;
221: }
222: return $this->_configCache[$path];
223: }
224:
225: /**
226: * Load group collection and set internal data
227: *
228: */
229: protected function _loadGroups()
230: {
231: $this->_groups = array();
232: $this->_groupsCount = 0;
233: foreach ($this->getGroupCollection() as $group) {
234: $this->_groups[$group->getId()] = $group;
235: $this->_groupIds[$group->getId()] = $group->getId();
236: if ($this->getDefaultGroupId() == $group->getId()) {
237: $this->_defaultGroup = $group;
238: }
239: $this->_groupsCount ++;
240: }
241: }
242:
243: /**
244: * Set website groups
245: *
246: * @param array $groups
247: */
248: public function setGroups($groups)
249: {
250: $this->_groups = array();
251: $this->_groupsCount = 0;
252: foreach ($groups as $group) {
253: $this->_groups[$group->getId()] = $group;
254: $this->_groupIds[$group->getId()] = $group->getId();
255: if ($this->getDefaultGroupId() == $group->getId()) {
256: $this->_defaultGroup = $group;
257: }
258: $this->_groupsCount ++;
259: }
260: return $this;
261: }
262:
263: /**
264: * Retrieve new (not loaded) Group collection object with website filter
265: *
266: * @return Mage_Core_Model_Mysql4_Store_Group_Collection
267: */
268: public function getGroupCollection()
269: {
270: return Mage::getModel('core/store_group')
271: ->getCollection()
272: ->addWebsiteFilter($this->getId());
273: }
274:
275: /**
276: * Retrieve website groups
277: *
278: * @return array
279: */
280: public function getGroups()
281: {
282: if (is_null($this->_groups)) {
283: $this->_loadGroups();
284: }
285: return $this->_groups;
286: }
287:
288: /**
289: * Retrieve website group ids
290: *
291: * @return array
292: */
293: public function getGroupIds()
294: {
295: if (is_null($this->_groups)) {
296: $this->_loadGroups();
297: }
298: return $this->_groupIds;
299: }
300:
301: /**
302: * Retrieve number groups in a website
303: *
304: * @return int
305: */
306: public function getGroupsCount()
307: {
308: if (is_null($this->_groups)) {
309: $this->_loadGroups();
310: }
311: return $this->_groupsCount;
312: }
313:
314: /**
315: * Retrieve default group model
316: *
317: * @return Mage_Core_Model_Store_Group
318: */
319: public function getDefaultGroup()
320: {
321: if (!$this->hasDefaultGroupId()) {
322: return false;
323: }
324: if (is_null($this->_groups)) {
325: $this->_loadGroups();
326: }
327: return $this->_defaultGroup;
328: }
329:
330: /**
331: * Load store collection and set internal data
332: *
333: */
334: protected function _loadStores()
335: {
336: $this->_stores = array();
337: $this->_storesCount = 0;
338: foreach ($this->getStoreCollection() as $store) {
339: $this->_stores[$store->getId()] = $store;
340: $this->_storeIds[$store->getId()] = $store->getId();
341: $this->_storeCodes[$store->getId()] = $store->getCode();
342: if ($this->getDefaultGroup() && $this->getDefaultGroup()->getDefaultStoreId() == $store->getId()) {
343: $this->_defaultStore = $store;
344: }
345: $this->_storesCount ++;
346: }
347: }
348:
349: /**
350: * Set website stores
351: *
352: * @param array $stores
353: */
354: public function setStores($stores)
355: {
356: $this->_stores = array();
357: $this->_storesCount = 0;
358: foreach ($stores as $store) {
359: $this->_stores[$store->getId()] = $store;
360: $this->_storeIds[$store->getId()] = $store->getId();
361: $this->_storeCodes[$store->getId()] = $store->getCode();
362: if ($this->getDefaultGroup() && $this->getDefaultGroup()->getDefaultStoreId() == $store->getId()) {
363: $this->_defaultStore = $store;
364: }
365: $this->_storesCount ++;
366: }
367: }
368:
369: /**
370: * Retrieve new (not loaded) Store collection object with website filter
371: *
372: * @return Mage_Core_Model_Mysql4_Store_Collection
373: */
374: public function getStoreCollection()
375: {
376: return Mage::getModel('core/store')
377: ->getCollection()
378: ->addWebsiteFilter($this->getId());
379: }
380:
381: /**
382: * Retrieve wersite store objects
383: *
384: * @return array
385: */
386: public function getStores()
387: {
388: if (is_null($this->_stores)) {
389: $this->_loadStores();
390: }
391: return $this->_stores;
392: }
393:
394: /**
395: * Retrieve website store ids
396: *
397: * @return array
398: */
399: public function getStoreIds()
400: {
401: if (is_null($this->_stores)) {
402: $this->_loadStores();
403: }
404: return $this->_storeIds;
405: }
406:
407: /**
408: * Retrieve website store codes
409: *
410: * @return array
411: */
412: public function getStoreCodes()
413: {
414: if (is_null($this->_stores)) {
415: $this->_loadStores();
416: }
417: return $this->_storeCodes;
418: }
419:
420: /**
421: * Retrieve number stores in a website
422: *
423: * @return int
424: */
425: public function getStoresCount()
426: {
427: if (is_null($this->_stores)) {
428: $this->_loadStores();
429: }
430: return $this->_storesCount;
431: }
432:
433: /**
434: * is can delete website
435: *
436: * @return bool
437: */
438: public function isCanDelete()
439: {
440: if ($this->_isReadOnly || !$this->getId()) {
441: return false;
442: }
443: if (is_null($this->_isCanDelete)) {
444: $this->_isCanDelete = (Mage::getModel('core/website')->getCollection()->getSize() > 2)
445: && !$this->getIsDefault();
446: }
447: return $this->_isCanDelete;
448: }
449:
450: /**
451: * Retrieve unique website-group-store key for collection with groups and stores
452: *
453: * @return string
454: */
455: public function getWebsiteGroupStore()
456: {
457: return join('-', array($this->getWebsiteId(), $this->getGroupId(), $this->getStoreId()));
458: }
459:
460: public function getDefaultGroupId()
461: {
462: return $this->_getData('default_group_id');
463: }
464:
465: public function getCode()
466: {
467: return $this->_getData('code');
468: }
469:
470: protected function _beforeDelete()
471: {
472: $this->_protectFromNonAdmin();
473: return parent::_beforeDelete();
474: }
475:
476: /**
477: * rewrite in order to clear configuration cache
478: *
479: * @return Mage_Core_Model_Website
480: */
481: protected function _afterDelete()
482: {
483: Mage::app()->clearWebsiteCache($this->getId());
484:
485: parent::_afterDelete();
486: Mage::getConfig()->removeCache();
487: return $this;
488: }
489:
490: /**
491: * Retrieve website base currency code
492: *
493: * @return string
494: */
495: public function getBaseCurrencyCode()
496: {
497: if ($this->getConfig(Mage_Core_Model_Store::XML_PATH_PRICE_SCOPE)
498: == Mage_Core_Model_Store::PRICE_SCOPE_GLOBAL
499: ) {
500: return Mage::app()->getBaseCurrencyCode();
501: } else {
502: return $this->getConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE);
503: }
504: }
505:
506: /**
507: * Retrieve website base currency
508: *
509: * @return Mage_Directory_Model_Currency
510: */
511: public function getBaseCurrency()
512: {
513: $currency = $this->getData('base_currency');
514: if (is_null($currency)) {
515: $currency = Mage::getModel('directory/currency')->load($this->getBaseCurrencyCode());
516: $this->setData('base_currency', $currency);
517: }
518: return $currency;
519: }
520:
521: /**
522: * Retrieve Default Website Store or null
523: *
524: * @return Mage_Core_Model_Store
525: */
526: public function getDefaultStore()
527: {
528: // init stores if not loaded
529: $this->getStores();
530: return $this->_defaultStore;
531: }
532:
533: /**
534: * Retrieve default stores select object
535: * Select fields website_id, store_id
536: *
537: * @param $withDefault include/exclude default admin website
538: * @return Varien_Db_Select
539: */
540: public function getDefaultStoresSelect($withDefault = false)
541: {
542: return $this->getResource()->getDefaultStoresSelect($withDefault);
543: }
544:
545: /**
546: * Get/Set isReadOnly flag
547: *
548: * @param bool $value
549: * @return bool
550: */
551: public function isReadOnly($value = null)
552: {
553: if (null !== $value) {
554: $this->_isReadOnly = (bool)$value;
555: }
556: return $this->_isReadOnly;
557: }
558: }
559: