PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Core/Model/Website.php

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