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