PageRenderTime 33ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/speedupmate/Magento-CE-Mirror
PHP | 510 lines | 293 code | 37 blank | 180 comment | 39 complexity | ac6972053e9a3e9b6792a66023c7b6db 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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Adminhtml
  23. * @copyright Copyright (c) 2006-2020 Magento, Inc. (http://www.magento.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' => Mage::helper('core')->escapeHtml($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) .
  152. Mage::helper('core')->escapeHtml($store->getName()),
  153. 'value' => $store->getId()
  154. );
  155. }
  156. if ($groupShow) {
  157. $options[] = array(
  158. 'label' => str_repeat($nonEscapableNbspChar, 4) .
  159. Mage::helper('core')->escapeHtml($group->getName()),
  160. 'value' => $values
  161. );
  162. }
  163. }
  164. }
  165. return $options;
  166. }
  167. /**
  168. * Retrieve stores structure
  169. *
  170. * @param bool $isAll
  171. * @param array $storeIds
  172. * @param array $groupIds
  173. * @param array $websiteIds
  174. * @return array
  175. */
  176. public function getStoresStructure($isAll = false, $storeIds = array(), $groupIds = array(), $websiteIds = array())
  177. {
  178. $out = array();
  179. $websites = $this->getWebsiteCollection();
  180. if ($isAll) {
  181. $out[] = array(
  182. 'value' => 0,
  183. 'label' => Mage::helper('adminhtml')->__('All Store Views')
  184. );
  185. }
  186. foreach ($websites as $website) {
  187. $websiteId = $website->getId();
  188. if ($websiteIds && !in_array($websiteId, $websiteIds)) {
  189. continue;
  190. }
  191. $out[$websiteId] = array(
  192. 'value' => $websiteId,
  193. 'label' => $website->getName()
  194. );
  195. foreach ($website->getGroups() as $group) {
  196. $groupId = $group->getId();
  197. if ($groupIds && !in_array($groupId, $groupIds)) {
  198. continue;
  199. }
  200. $out[$websiteId]['children'][$groupId] = array(
  201. 'value' => $groupId,
  202. 'label' => $group->getName()
  203. );
  204. foreach ($group->getStores() as $store) {
  205. $storeId = $store->getId();
  206. if ($storeIds && !in_array($storeId, $storeIds)) {
  207. continue;
  208. }
  209. $out[$websiteId]['children'][$groupId]['children'][$storeId] = array(
  210. 'value' => $storeId,
  211. 'label' => $store->getName()
  212. );
  213. }
  214. if (empty($out[$websiteId]['children'][$groupId]['children'])) {
  215. unset($out[$websiteId]['children'][$groupId]);
  216. }
  217. }
  218. if (empty($out[$websiteId]['children'])) {
  219. unset($out[$websiteId]);
  220. }
  221. }
  222. return $out;
  223. }
  224. /**
  225. * Website label/value array getter, compatible with form dropdown options
  226. *
  227. * @param bool $empty
  228. * @param bool $all
  229. * @return array
  230. */
  231. public function getWebsiteValuesForForm($empty = false, $all = false)
  232. {
  233. $options = array();
  234. if ($empty) {
  235. $options[] = array(
  236. 'label' => Mage::helper('adminhtml')->__('-- Please Select --'),
  237. 'value' => ''
  238. );
  239. }
  240. if ($all && $this->_isAdminScopeAllowed) {
  241. $options[] = array(
  242. 'label' => Mage::helper('adminhtml')->__('Admin'),
  243. 'value' => 0
  244. );
  245. }
  246. foreach ($this->_websiteCollection as $website) {
  247. $options[] = array(
  248. 'label' => $website->getName(),
  249. 'value' => $website->getId(),
  250. );
  251. }
  252. return $options;
  253. }
  254. /**
  255. * Get websites as id => name associative array
  256. *
  257. * @param bool $withDefault
  258. * @param string $attribute
  259. * @return array
  260. */
  261. public function getWebsiteOptionHash($withDefault = false, $attribute = 'name')
  262. {
  263. $options = array();
  264. foreach (Mage::app()->getWebsites((bool)$withDefault && $this->_isAdminScopeAllowed) as $website) {
  265. $options[$website->getId()] = $website->getDataUsingMethod($attribute);
  266. }
  267. return $options;
  268. }
  269. /**
  270. * Get store views as id => name associative array
  271. *
  272. * @param bool $withDefault
  273. * @param string $attribute
  274. * @return array
  275. */
  276. public function getStoreOptionHash($withDefault = false, $attribute = 'name')
  277. {
  278. $options = array();
  279. foreach (Mage::app()->getStores((bool)$withDefault && $this->_isAdminScopeAllowed) as $store) {
  280. $options[$store->getId()] = $store->getDataUsingMethod($attribute);
  281. }
  282. return $options;
  283. }
  284. /**
  285. * Get store groups as id => name associative array
  286. *
  287. * @param string $attribute
  288. * @return array
  289. */
  290. public function getStoreGroupOptionHash($attribute = 'name')
  291. {
  292. foreach ($this->_groupCollection as $group) {
  293. $options[$group->getId()] = $group->getDataUsingMethod($attribute);
  294. }
  295. return $options;
  296. }
  297. /**
  298. * Retrieve Website name by Id
  299. *
  300. * @param int websiteId
  301. * @return string
  302. */
  303. public function getWebsiteName($websiteId)
  304. {
  305. foreach ($this->_websiteCollection as $website) {
  306. if ($website->getId() == $websiteId) {
  307. return $website->getName();
  308. }
  309. }
  310. return null;
  311. }
  312. /**
  313. * Retrieve Group name by Id
  314. *
  315. * @param int groupId
  316. * @return string
  317. */
  318. public function getGroupName($groupId)
  319. {
  320. foreach ($this->_groupCollection as $group) {
  321. if ($group->getId() == $groupId) {
  322. return $group->getName();
  323. }
  324. }
  325. return null;
  326. }
  327. /**
  328. * Retrieve Store name by Id
  329. *
  330. * @param int $storeId
  331. * @return string
  332. */
  333. public function getStoreName($storeId)
  334. {
  335. if (isset($this->_storeCollection[$storeId])) {
  336. return $this->_storeCollection[$storeId]->getName();
  337. }
  338. return null;
  339. }
  340. /**
  341. * Retrieve store name with website and website store
  342. *
  343. * @param int $storeId
  344. * @return Mage_Core_Model_Store
  345. **/
  346. public function getStoreData($storeId)
  347. {
  348. if (isset($this->_storeCollection[$storeId])) {
  349. return $this->_storeCollection[$storeId];
  350. }
  351. return null;
  352. }
  353. /**
  354. * Retrieve store name with website and website store
  355. *
  356. * @param int $storeId
  357. * @return string
  358. **/
  359. public function getStoreNameWithWebsite($storeId)
  360. {
  361. $name = '';
  362. if (is_array($storeId)) {
  363. $names = array();
  364. foreach ($storeId as $id) {
  365. $names[]= $this->getStoreNameWithWebsite($id);
  366. }
  367. $name = implode(', ', $names);
  368. }
  369. else {
  370. if (isset($this->_storeCollection[$storeId])) {
  371. $data = $this->_storeCollection[$storeId];
  372. $name .= $this->getWebsiteName($data->getWebsiteId());
  373. $name .= ($name ? '/' : '').$this->getGroupName($data->getGroupId());
  374. $name .= ($name ? '/' : '').$data->getName();
  375. }
  376. }
  377. return $name;
  378. }
  379. /**
  380. * Retrieve Website collection as array
  381. *
  382. * @return array
  383. */
  384. public function getWebsiteCollection()
  385. {
  386. return $this->_websiteCollection;
  387. }
  388. /**
  389. * Retrieve Group collection as array
  390. *
  391. * @return array
  392. */
  393. public function getGroupCollection()
  394. {
  395. return $this->_groupCollection;
  396. }
  397. /**
  398. * Retrieve Store collection as array
  399. *
  400. * @return array
  401. */
  402. public function getStoreCollection()
  403. {
  404. return $this->_storeCollection;
  405. }
  406. /**
  407. * Load/Reload collection(s) by type
  408. * Allowed types: website, group, store or null for all
  409. *
  410. * @param string $type
  411. * @return Mage_Adminhtml_Model_System_Store
  412. */
  413. public function reload($type = null)
  414. {
  415. if (is_null($type)) {
  416. $this->_loadWebsiteCollection();
  417. $this->_loadGroupCollection();
  418. $this->_loadStoreCollection();
  419. }
  420. else {
  421. switch ($type) {
  422. case 'website':
  423. $this->_loadWebsiteCollection();
  424. break;
  425. case 'group':
  426. $this->_loadGroupCollection();
  427. break;
  428. case 'store':
  429. $this->_loadStoreCollection();
  430. break;
  431. default:
  432. break;
  433. }
  434. }
  435. return $this;
  436. }
  437. /**
  438. * Retrieve store path with website and website store
  439. *
  440. * @param int $storeId
  441. * @return string
  442. **/
  443. public function getStoreNamePath($storeId)
  444. {
  445. $name = '';
  446. if (is_array($storeId)) {
  447. $names = array();
  448. foreach ($storeId as $id) {
  449. $names[]= $this->getStoreNamePath($id);
  450. }
  451. $name = implode(', ', $names);
  452. }
  453. else {
  454. if (isset($this->_storeCollection[$storeId])) {
  455. $data = $this->_storeCollection[$storeId];
  456. $name .= $this->getWebsiteName($data->getWebsiteId());
  457. $name .= ($name ? '/' : '') . $this->getGroupName($data->getGroupId());
  458. }
  459. }
  460. return $name;
  461. }
  462. /**
  463. * Specify whether to show admin-scope options
  464. *
  465. * @param bool $value
  466. * @return Mage_Adminhtml_Model_System_Store
  467. */
  468. public function setIsAdminScopeAllowed($value)
  469. {
  470. $this->_isAdminScopeAllowed = (bool)$value;
  471. return $this;
  472. }
  473. }