00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Core_Model_Website extends Mage_Core_Model_Abstract
00036 {
00037 const CACHE_TAG = 'website';
00038 protected $_cacheTag = true;
00039
00040
00041
00042
00043 protected $_eventPrefix = 'website';
00044
00045
00046
00047
00048 protected $_eventObject = 'website';
00049
00050
00051
00052
00053
00054
00055 protected $_configCache = array();
00056
00057
00058
00059
00060
00061
00062 protected $_groups;
00063
00064
00065
00066
00067
00068
00069 protected $_groupIds = array();
00070
00071
00072
00073
00074
00075
00076 protected $_groupsCount;
00077
00078
00079
00080
00081
00082
00083 protected $_stores;
00084
00085
00086
00087
00088
00089
00090 protected $_storeIds = array();
00091
00092
00093
00094
00095
00096
00097 protected $_storeCodes = array();
00098
00099
00100
00101
00102
00103
00104 protected $_storesCount = 0;
00105
00106
00107
00108
00109
00110
00111 protected $_defaultGroup;
00112
00113
00114
00115
00116
00117
00118 protected $_defaultStore;
00119
00120
00121
00122
00123
00124
00125 protected $_isCanDelete;
00126
00127
00128
00129
00130 private $_isReadOnly = false;
00131
00132
00133
00134
00135
00136 protected function _construct()
00137 {
00138 $this->_init('core/website');
00139 }
00140
00141
00142
00143
00144
00145
00146
00147
00148 public function load($id, $field = null)
00149 {
00150 if (!is_numeric($id) && is_null($field)) {
00151 $this->_getResource()->load($this, $id, 'code');
00152 return $this;
00153 }
00154 return parent::load($id, $field);
00155 }
00156
00157
00158
00159
00160
00161
00162
00163 public function loadConfig($code)
00164 {
00165 if (!Mage::getConfig()->getNode('websites')) {
00166 return $this;
00167 }
00168 if (is_numeric($code)) {
00169 foreach (Mage::getConfig()->getNode('websites')->children() as $websiteCode=>$website) {
00170 if ((int)$website->system->website->id==$code) {
00171 $code = $websiteCode;
00172 break;
00173 }
00174 }
00175 } else {
00176 $website = Mage::getConfig()->getNode('websites/'.$code);
00177 }
00178 if (!empty($website)) {
00179 $this->setCode($code);
00180 $id = (int)$website->system->website->id;
00181 $this->setId($id)->setStoreId($id);
00182 }
00183 return $this;
00184 }
00185
00186
00187
00188
00189
00190
00191
00192 public function getConfig($path) {
00193 if (!isset($this->_configCache[$path])) {
00194
00195 $config = Mage::getConfig()->getNode('websites/'.$this->getCode().'/'.$path);
00196 if (!$config) {
00197 return false;
00198 #throw Mage::exception('Mage_Core', Mage::helper('core')->__('Invalid websites configuration path: %s', $path));
00199 }
00200 if ($config->hasChildren()) {
00201 $value = array();
00202 foreach ($config->children() as $k=>$v) {
00203 $value[$k] = $v;
00204 }
00205 } else {
00206 $value = (string)$config;
00207 }
00208 $this->_configCache[$path] = $value;
00209 }
00210 return $this->_configCache[$path];
00211 }
00212
00213
00214
00215
00216
00217 protected function _loadGroups()
00218 {
00219 $this->_groups = array();
00220 $this->_groupsCount = 0;
00221 foreach ($this->getGroupCollection() as $group) {
00222 $this->_groups[$group->getId()] = $group;
00223 $this->_groupIds[$group->getId()] = $group->getId();
00224 if ($this->getDefaultGroupId() == $group->getId()) {
00225 $this->_defaultGroup = $group;
00226 }
00227 $this->_groupsCount ++;
00228 }
00229 }
00230
00231
00232
00233
00234
00235
00236 public function setGroups($groups)
00237 {
00238 $this->_groups = array();
00239 $this->_groupsCount = 0;
00240 foreach ($groups as $group) {
00241 $this->_groups[$group->getId()] = $group;
00242 $this->_groupIds[$group->getId()] = $group->getId();
00243 if ($this->getDefaultGroupId() == $group->getId()) {
00244 $this->_defaultGroup = $group;
00245 }
00246 $this->_groupsCount ++;
00247 }
00248 return $this;
00249 }
00250
00251
00252
00253
00254
00255
00256 public function getGroupCollection()
00257 {
00258 return Mage::getModel('core/store_group')
00259 ->getCollection()
00260 ->addWebsiteFilter($this->getId());
00261 }
00262
00263
00264
00265
00266
00267
00268 public function getGroups()
00269 {
00270 if (is_null($this->_groups)) {
00271 $this->_loadGroups();
00272 }
00273 return $this->_groups;
00274 }
00275
00276
00277
00278
00279
00280
00281 public function getGroupIds()
00282 {
00283 if (is_null($this->_groups)) {
00284 $this->_loadGroups();
00285 }
00286 return $this->_groupIds;
00287 }
00288
00289
00290
00291
00292
00293
00294 public function getGroupsCount()
00295 {
00296 if (is_null($this->_groups)) {
00297 $this->_loadGroups();
00298 }
00299 return $this->_groupsCount;
00300 }
00301
00302
00303
00304
00305
00306
00307 public function getDefaultGroup()
00308 {
00309 if (!$this->getDefaultGroupId()) {
00310 return false;
00311 }
00312 if (is_null($this->_groups)) {
00313 $this->_loadGroups();
00314 }
00315 return $this->_defaultGroup;
00316 }
00317
00318
00319
00320
00321
00322 protected function _loadStores()
00323 {
00324 $this->_stores = array();
00325 $this->_storesCount = 0;
00326 foreach ($this->getStoreCollection() as $store) {
00327 $this->_stores[$store->getId()] = $store;
00328 $this->_storeIds[$store->getId()] = $store->getId();
00329 $this->_storeCodes[$store->getId()] = $store->getCode();
00330 if ($this->getDefaultGroup() && $this->getDefaultGroup()->getDefaultStoreId() == $store->getId()) {
00331 $this->_defaultStore = $store;
00332 }
00333 $this->_storesCount ++;
00334 }
00335 }
00336
00337
00338
00339
00340
00341
00342 public function setStores($stores)
00343 {
00344 $this->_stores = array();
00345 $this->_storesCount = 0;
00346 foreach ($stores as $store) {
00347 $this->_stores[$store->getId()] = $store;
00348 $this->_storeIds[$store->getId()] = $store->getId();
00349 $this->_storeCodes[$store->getId()] = $store->getCode();
00350 if ($this->getDefaultGroup() && $this->getDefaultGroup()->getDefaultStoreId() == $store->getId()) {
00351 $this->_defaultStore = $store;
00352 }
00353 $this->_storesCount ++;
00354 }
00355 }
00356
00357
00358
00359
00360
00361
00362 public function getStoreCollection()
00363 {
00364 return Mage::getModel('core/store')
00365 ->getCollection()
00366 ->addWebsiteFilter($this->getId());
00367 }
00368
00369
00370
00371
00372
00373
00374 public function getStores()
00375 {
00376 if (is_null($this->_stores)) {
00377 $this->_loadStores();
00378 }
00379 return $this->_stores;
00380 }
00381
00382
00383
00384
00385
00386
00387 public function getStoreIds()
00388 {
00389 if (is_null($this->_stores)) {
00390 $this->_loadStores();
00391 }
00392 return $this->_storeIds;
00393 }
00394
00395
00396
00397
00398
00399
00400 public function getStoreCodes()
00401 {
00402 if (is_null($this->_stores)) {
00403 $this->_loadStores();
00404 }
00405 return $this->_storeCodes;
00406 }
00407
00408
00409
00410
00411
00412
00413 public function getStoresCount()
00414 {
00415 if (is_null($this->_stores)) {
00416 $this->_loadStores();
00417 }
00418 return $this->_storesCount;
00419 }
00420
00421
00422
00423
00424
00425
00426 public function isCanDelete()
00427 {
00428 if ($this->_isReadOnly || !$this->getId()) {
00429 return false;
00430 }
00431 if (is_null($this->_isCanDelete)) {
00432 $this->_isCanDelete = (Mage::getModel('core/website')->getCollection()->getSize() > 2)
00433 && !$this->getIsDefault();
00434 }
00435 return $this->_isCanDelete;
00436 }
00437
00438
00439
00440
00441
00442
00443 public function getWebsiteGroupStore()
00444 {
00445 return join('-', array($this->getWebsiteId(), $this->getGroupId(), $this->getStoreId()));
00446 }
00447
00448 public function getDefaultGroupId()
00449 {
00450 return $this->_getData('default_group_id');
00451 }
00452
00453 public function getCode()
00454 {
00455 return $this->_getData('code');
00456 }
00457
00458 protected function _beforeDelete()
00459 {
00460 $this->_protectFromNonAdmin();
00461 return parent::_beforeDelete();
00462 }
00463
00464
00465
00466
00467
00468
00469 protected function _afterDelete()
00470 {
00471 parent::_afterDelete();
00472 Mage::getConfig()->removeCache();
00473 return $this;
00474 }
00475
00476
00477
00478
00479
00480
00481 public function getBaseCurrencyCode()
00482 {
00483 if ($this->getConfig(Mage_Core_Model_Store::XML_PATH_PRICE_SCOPE) == Mage_Core_Model_Store::PRICE_SCOPE_GLOBAL) {
00484 return Mage::app()->getBaseCurrencyCode();
00485 } else {
00486 return $this->getConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE);
00487 }
00488 }
00489
00490
00491
00492
00493
00494
00495 public function getBaseCurrency()
00496 {
00497 $currency = $this->getData('base_currency');
00498 if (is_null($currency)) {
00499 $currency = Mage::getModel('directory/currency')->load($this->getBaseCurrencyCode());
00500 $this->setData('base_currency', $currency);
00501 }
00502 return $currency;
00503 }
00504
00505
00506
00507
00508
00509
00510 public function getDefaultStore()
00511 {
00512
00513 $this->getStores();
00514 return $this->_defaultStore;
00515 }
00516
00517
00518
00519
00520
00521
00522
00523
00524 public function getDefaultStoresSelect($withDefault = false)
00525 {
00526 return $this->getResource()->getDefaultStoresSelect($withDefault);
00527 }
00528
00529
00530
00531
00532
00533
00534
00535 public function isReadOnly($value = null)
00536 {
00537 if (null !== $value) {
00538 $this->_isReadOnly = (bool)$value;
00539 }
00540 return $this->_isReadOnly;
00541 }
00542 }