PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Adminhtml/Model/System/Store.php

https://github.com/weburnit/magento-lite
PHP | 508 lines | 291 code | 37 blank | 180 comment | 39 complexity | 1d192cac15d178eb0362cf2fffa97fc8 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_Adminhtml
  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. * Adminhtml System Store Model
  28. *
  29. * @category Mage
  30. * @package Mage_Adminhtml
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Adminhtml_Model_System_Store extends Varien_Object
  34. {
  35. /**
  36. * Website collection
  37. * websiteId => Mage_Core_Model_Website
  38. *
  39. * @var array
  40. */
  41. protected $_websiteCollection = array();
  42. /**
  43. * Group collection
  44. * groupId => Mage_Core_Model_Store_Group
  45. *
  46. * @var array
  47. */
  48. protected $_groupCollection = array();
  49. /**
  50. * Store collection
  51. * storeId => Mage_Core_Model_Store
  52. *
  53. * @var array
  54. */
  55. protected $_storeCollection;
  56. /**
  57. * @var bool
  58. */
  59. private $_isAdminScopeAllowed = true;
  60. /**
  61. * Init model
  62. * Load Website, Group and Store collections
  63. *
  64. * @return Mage_Adminhtml_Model_System_Store
  65. */
  66. public function __construct()
  67. {
  68. return $this->reload();
  69. }
  70. /**
  71. * Load/Reload Website collection
  72. *
  73. * @return array
  74. */
  75. protected function _loadWebsiteCollection()
  76. {
  77. $this->_websiteCollection = Mage::app()->getWebsites();
  78. return $this;
  79. }
  80. /**
  81. * Load/Reload Group collection
  82. *
  83. * @return array
  84. */
  85. protected function _loadGroupCollection()
  86. {
  87. $this->_groupCollection = array();
  88. foreach (Mage::app()->getWebsites() as $website) {
  89. foreach ($website->getGroups() as $group) {
  90. $this->_groupCollection[$group->getId()] = $group;
  91. }
  92. }
  93. return $this;
  94. }
  95. /**
  96. * Load/Reload Store collection
  97. *
  98. * @return array
  99. */
  100. protected function _loadStoreCollection()
  101. {
  102. $this->_storeCollection = Mage::app()->getStores();
  103. return $this;
  104. }
  105. /**
  106. * Retrieve store values for form
  107. *
  108. * @param bool $empty
  109. * @param bool $all
  110. * @return array
  111. */
  112. public function getStoreValuesForForm($empty = false, $all = false)
  113. {
  114. $options = array();
  115. if ($empty) {
  116. $options[] = array(
  117. 'label' => '',
  118. 'value' => ''
  119. );
  120. }
  121. if ($all && $this->_isAdminScopeAllowed) {
  122. $options[] = array(
  123. 'label' => Mage::helper('adminhtml')->__('All Store Views'),
  124. 'value' => 0
  125. );
  126. }
  127. $nonEscapableNbspChar = html_entity_decode('&#160;', ENT_NOQUOTES, 'UTF-8');
  128. foreach ($this->_websiteCollection as $website) {
  129. $websiteShow = false;
  130. foreach ($this->_groupCollection as $group) {
  131. if ($website->getId() != $group->getWebsiteId()) {
  132. continue;
  133. }
  134. $groupShow = false;
  135. foreach ($this->_storeCollection as $store) {
  136. if ($group->getId() != $store->getGroupId()) {
  137. continue;
  138. }
  139. if (!$websiteShow) {
  140. $options[] = array(
  141. 'label' => $website->getName(),
  142. 'value' => array()
  143. );
  144. $websiteShow = true;
  145. }
  146. if (!$groupShow) {
  147. $groupShow = true;
  148. $values = array();
  149. }
  150. $values[] = array(
  151. 'label' => str_repeat($nonEscapableNbspChar, 4) . $store->getName(),
  152. 'value' => $store->getId()
  153. );
  154. }
  155. if ($groupShow) {
  156. $options[] = array(
  157. 'label' => str_repeat($nonEscapableNbspChar, 4) . $group->getName(),
  158. 'value' => $values
  159. );
  160. }
  161. }
  162. }
  163. return $options;
  164. }
  165. /**
  166. * Retrieve stores structure
  167. *
  168. * @param bool $isAll
  169. * @param array $storeIds
  170. * @param array $groupIds
  171. * @param array $websiteIds
  172. * @return array
  173. */
  174. public function getStoresStructure($isAll = false, $storeIds = array(), $groupIds = array(), $websiteIds = array())
  175. {
  176. $out = array();
  177. $websites = $this->getWebsiteCollection();
  178. if ($isAll) {
  179. $out[] = array(
  180. 'value' => 0,
  181. 'label' => Mage::helper('adminhtml')->__('All Store Views')
  182. );
  183. }
  184. foreach ($websites as $website) {
  185. $websiteId = $website->getId();
  186. if ($websiteIds && !in_array($websiteId, $websiteIds)) {
  187. continue;
  188. }
  189. $out[$websiteId] = array(
  190. 'value' => $websiteId,
  191. 'label' => $website->getName()
  192. );
  193. foreach ($website->getGroups() as $group) {
  194. $groupId = $group->getId();
  195. if ($groupIds && !in_array($groupId, $groupIds)) {
  196. continue;
  197. }
  198. $out[$websiteId]['children'][$groupId] = array(
  199. 'value' => $groupId,
  200. 'label' => $group->getName()
  201. );
  202. foreach ($group->getStores() as $store) {
  203. $storeId = $store->getId();
  204. if ($storeIds && !in_array($storeId, $storeIds)) {
  205. continue;
  206. }
  207. $out[$websiteId]['children'][$groupId]['children'][$storeId] = array(
  208. 'value' => $storeId,
  209. 'label' => $store->getName()
  210. );
  211. }
  212. if (empty($out[$websiteId]['children'][$groupId]['children'])) {
  213. unset($out[$websiteId]['children'][$groupId]);
  214. }
  215. }
  216. if (empty($out[$websiteId]['children'])) {
  217. unset($out[$websiteId]);
  218. }
  219. }
  220. return $out;
  221. }
  222. /**
  223. * Website label/value array getter, compatible with form dropdown options
  224. *
  225. * @param bool $empty
  226. * @param bool $all
  227. * @return array
  228. */
  229. public function getWebsiteValuesForForm($empty = false, $all = false)
  230. {
  231. $options = array();
  232. if ($empty) {
  233. $options[] = array(
  234. 'label' => Mage::helper('adminhtml')->__('-- Please Select --'),
  235. 'value' => ''
  236. );
  237. }
  238. if ($all && $this->_isAdminScopeAllowed) {
  239. $options[] = array(
  240. 'label' => Mage::helper('adminhtml')->__('Admin'),
  241. 'value' => 0
  242. );
  243. }
  244. foreach ($this->_websiteCollection as $website) {
  245. $options[] = array(
  246. 'label' => $website->getName(),
  247. 'value' => $website->getId(),
  248. );
  249. }
  250. return $options;
  251. }
  252. /**
  253. * Get websites as id => name associative array
  254. *
  255. * @param bool $withDefault
  256. * @param string $attribute
  257. * @return array
  258. */
  259. public function getWebsiteOptionHash($withDefault = false, $attribute = 'name')
  260. {
  261. $options = array();
  262. foreach (Mage::app()->getWebsites((bool)$withDefault && $this->_isAdminScopeAllowed) as $website) {
  263. $options[$website->getId()] = $website->getDataUsingMethod($attribute);
  264. }
  265. return $options;
  266. }
  267. /**
  268. * Get store views as id => name associative array
  269. *
  270. * @param bool $withDefault
  271. * @param string $attribute
  272. * @return array
  273. */
  274. public function getStoreOptionHash($withDefault = false, $attribute = 'name')
  275. {
  276. $options = array();
  277. foreach (Mage::app()->getStores((bool)$withDefault && $this->_isAdminScopeAllowed) as $store) {
  278. $options[$store->getId()] = $store->getDataUsingMethod($attribute);
  279. }
  280. return $options;
  281. }
  282. /**
  283. * Get store groups as id => name associative array
  284. *
  285. * @param string $attribute
  286. * @return array
  287. */
  288. public function getStoreGroupOptionHash($attribute = 'name')
  289. {
  290. foreach ($this->_groupCollection as $group) {
  291. $options[$group->getId()] = $group->getDataUsingMethod($attribute);
  292. }
  293. return $options;
  294. }
  295. /**
  296. * Retrieve Website name by Id
  297. *
  298. * @param int websiteId
  299. * @return string
  300. */
  301. public function getWebsiteName($websiteId)
  302. {
  303. foreach ($this->_websiteCollection as $website) {
  304. if ($website->getId() == $websiteId) {
  305. return $website->getName();
  306. }
  307. }
  308. return null;
  309. }
  310. /**
  311. * Retrieve Group name by Id
  312. *
  313. * @param int groupId
  314. * @return string
  315. */
  316. public function getGroupName($groupId)
  317. {
  318. foreach ($this->_groupCollection as $group) {
  319. if ($group->getId() == $groupId) {
  320. return $group->getName();
  321. }
  322. }
  323. return null;
  324. }
  325. /**
  326. * Retrieve Store name by Id
  327. *
  328. * @param int $storeId
  329. * @return string
  330. */
  331. public function getStoreName($storeId)
  332. {
  333. if (isset($this->_storeCollection[$storeId])) {
  334. return $this->_storeCollection[$storeId]->getName();
  335. }
  336. return null;
  337. }
  338. /**
  339. * Retrieve store name with website and website store
  340. *
  341. * @param int $storeId
  342. * @return Mage_Core_Model_Store
  343. **/
  344. public function getStoreData($storeId)
  345. {
  346. if (isset($this->_storeCollection[$storeId])) {
  347. return $this->_storeCollection[$storeId];
  348. }
  349. return null;
  350. }
  351. /**
  352. * Retrieve store name with website and website store
  353. *
  354. * @param int $storeId
  355. * @return string
  356. **/
  357. public function getStoreNameWithWebsite($storeId)
  358. {
  359. $name = '';
  360. if (is_array($storeId)) {
  361. $names = array();
  362. foreach ($storeId as $id) {
  363. $names[]= $this->getStoreNameWithWebsite($id);
  364. }
  365. $name = implode(', ', $names);
  366. }
  367. else {
  368. if (isset($this->_storeCollection[$storeId])) {
  369. $data = $this->_storeCollection[$storeId];
  370. $name .= $this->getWebsiteName($data->getWebsiteId());
  371. $name .= ($name ? '/' : '').$this->getGroupName($data->getGroupId());
  372. $name .= ($name ? '/' : '').$data->getName();
  373. }
  374. }
  375. return $name;
  376. }
  377. /**
  378. * Retrieve Website collection as array
  379. *
  380. * @return array
  381. */
  382. public function getWebsiteCollection()
  383. {
  384. return $this->_websiteCollection;
  385. }
  386. /**
  387. * Retrieve Group collection as array
  388. *
  389. * @return array
  390. */
  391. public function getGroupCollection()
  392. {
  393. return $this->_groupCollection;
  394. }
  395. /**
  396. * Retrieve Store collection as array
  397. *
  398. * @return array
  399. */
  400. public function getStoreCollection()
  401. {
  402. return $this->_storeCollection;
  403. }
  404. /**
  405. * Load/Reload collection(s) by type
  406. * Allowed types: website, group, store or null for all
  407. *
  408. * @param string $type
  409. * @return Mage_Adminhtml_Model_System_Store
  410. */
  411. public function reload($type = null)
  412. {
  413. if (is_null($type)) {
  414. $this->_loadWebsiteCollection();
  415. $this->_loadGroupCollection();
  416. $this->_loadStoreCollection();
  417. }
  418. else {
  419. switch ($type) {
  420. case 'website':
  421. $this->_loadWebsiteCollection();
  422. break;
  423. case 'group':
  424. $this->_loadGroupCollection();
  425. break;
  426. case 'store':
  427. $this->_loadStoreCollection();
  428. break;
  429. default:
  430. break;
  431. }
  432. }
  433. return $this;
  434. }
  435. /**
  436. * Retrieve store path with website and website store
  437. *
  438. * @param int $storeId
  439. * @return string
  440. **/
  441. public function getStoreNamePath($storeId)
  442. {
  443. $name = '';
  444. if (is_array($storeId)) {
  445. $names = array();
  446. foreach ($storeId as $id) {
  447. $names[]= $this->getStoreNamePath($id);
  448. }
  449. $name = implode(', ', $names);
  450. }
  451. else {
  452. if (isset($this->_storeCollection[$storeId])) {
  453. $data = $this->_storeCollection[$storeId];
  454. $name .= $this->getWebsiteName($data->getWebsiteId());
  455. $name .= ($name ? '/' : '').$this->getGroupName($data->getGroupId());
  456. }
  457. }
  458. return $name;
  459. }
  460. /**
  461. * Specify whether to show admin-scope options
  462. *
  463. * @param bool $value
  464. * @return Mage_Adminhtml_Model_System_Store
  465. */
  466. public function setIsAdminScopeAllowed($value)
  467. {
  468. $this->_isAdminScopeAllowed = (bool)$value;
  469. return $this;
  470. }
  471. }