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_Block_System_Config_Form extends Mage_Adminhtml_Block_Widget_Form
36: {
37:
38: const SCOPE_DEFAULT = 'default';
39: const SCOPE_WEBSITES = 'websites';
40: const SCOPE_STORES = 'stores';
41:
42: 43: 44: 45: 46:
47: protected $_configData;
48:
49: 50: 51: 52: 53:
54: protected $_configDataObject;
55:
56: 57: 58: 59: 60:
61: protected $_configRoot;
62:
63: 64: 65: 66: 67:
68: protected $_configFields;
69:
70: 71: 72: 73: 74:
75: protected $_defaultFieldsetRenderer;
76:
77: 78: 79: 80: 81:
82: protected $_defaultFieldRenderer;
83:
84: 85: 86: 87: 88:
89: protected $_fieldsets = array();
90:
91: 92: 93: 94: 95:
96: protected $_scopeLabels = array();
97:
98: 99: 100: 101:
102: public function __construct()
103: {
104: parent::__construct();
105: $this->_scopeLabels = array(
106: self::SCOPE_DEFAULT => Mage::helper('adminhtml')->__('[GLOBAL]'),
107: self::SCOPE_WEBSITES => Mage::helper('adminhtml')->__('[WEBSITE]'),
108: self::SCOPE_STORES => Mage::helper('adminhtml')->__('[STORE VIEW]'),
109: );
110: }
111:
112: 113: 114: 115: 116:
117: protected function _initObjects()
118: {
119:
120: $this->_configDataObject = Mage::getSingleton('adminhtml/config_data');
121: $this->_configRoot = $this->_configDataObject->getConfigRoot();
122: $this->_configData = $this->_configDataObject->load();
123:
124: $this->_configFields = Mage::getSingleton('adminhtml/config');
125:
126: $this->_defaultFieldsetRenderer = Mage::getBlockSingleton('adminhtml/system_config_form_fieldset');
127: $this->_defaultFieldRenderer = Mage::getBlockSingleton('adminhtml/system_config_form_field');
128: return $this;
129: }
130:
131: 132: 133: 134: 135:
136: public function initForm()
137: {
138: $this->_initObjects();
139:
140: $form = new Varien_Data_Form();
141:
142: $sections = $this->_configFields->getSection(
143: $this->getSectionCode(),
144: $this->getWebsiteCode(),
145: $this->getStoreCode()
146: );
147: if (empty($sections)) {
148: $sections = array();
149: }
150: foreach ($sections as $section) {
151:
152: if (!$this->_canShowField($section)) {
153: continue;
154: }
155: foreach ($section->groups as $groups){
156: $groups = (array)$groups;
157: usort($groups, array($this, '_sortForm'));
158:
159: foreach ($groups as $group){
160:
161: if (!$this->_canShowField($group)) {
162: continue;
163: }
164: $this->_initGroup($form, $group, $section);
165: }
166: }
167: }
168:
169: $this->setForm($form);
170: return $this;
171: }
172:
173: 174: 175: 176: 177: 178: 179: 180:
181: protected function _initGroup($form, $group, $section, $parentElement = null)
182: {
183: if ($group->frontend_model) {
184: $fieldsetRenderer = Mage::getBlockSingleton((string)$group->frontend_model);
185: } else {
186: $fieldsetRenderer = $this->_defaultFieldsetRenderer;
187: }
188:
189: $fieldsetRenderer->setForm($this)
190: ->setConfigData($this->_configData);
191:
192: if ($this->_configFields->hasChildren($group, $this->getWebsiteCode(), $this->getStoreCode())) {
193: $helperName = $this->_configFields->getAttributeModule($section, $group);
194: $fieldsetConfig = array('legend' => Mage::helper($helperName)->__((string)$group->label));
195: if (!empty($group->comment)) {
196: $fieldsetConfig['comment'] = Mage::helper($helperName)->__((string)$group->comment);
197: }
198: if (!empty($group->expanded)) {
199: $fieldsetConfig['expanded'] = (bool)$group->expanded;
200: }
201:
202: $fieldset = new Varien_Data_Form_Element_Fieldset($fieldsetConfig);
203: $fieldset->setId($section->getName() . '_' . $group->getName())
204: ->setRenderer($fieldsetRenderer)
205: ->setGroup($group);
206:
207: if ($parentElement) {
208: $fieldset->setIsNested(true);
209: $parentElement->addElement($fieldset);
210: } else {
211: $form->addElement($fieldset);
212: }
213:
214: $this->_prepareFieldOriginalData($fieldset, $group);
215: $this->_addElementTypes($fieldset);
216:
217: $this->_fieldsets[$group->getName()] = $fieldset;
218:
219: if ($group->clone_fields) {
220: if ($group->clone_model) {
221: $cloneModel = Mage::getModel((string)$group->clone_model);
222: } else {
223: Mage::throwException($this->__('Config form fieldset clone model required to be able to clone fields'));
224: }
225: foreach ($cloneModel->getPrefixes() as $prefix) {
226: $this->initFields($fieldset, $group, $section, $prefix['field'], $prefix['label']);
227: }
228: } else {
229: $this->initFields($fieldset, $group, $section);
230: }
231: }
232: }
233:
234: 235: 236: 237: 238:
239: protected function _getDependence()
240: {
241: if (!$this->getChild('element_dependense')){
242: $this->setChild('element_dependense',
243: $this->getLayout()->createBlock('adminhtml/widget_form_element_dependence'));
244: }
245: return $this->getChild('element_dependense');
246: }
247:
248: 249: 250: 251: 252: 253: 254: 255: 256: 257:
258: public function initFields($fieldset, $group, $section, $fieldPrefix='', $labelPrefix='')
259: {
260: if (!$this->_configDataObject) {
261: $this->_initObjects();
262: }
263:
264:
265: $configDataAdditionalGroups = array();
266:
267: foreach ($group->fields as $elements) {
268: $elements = (array)$elements;
269:
270: if ($group->sort_fields && $group->sort_fields->by) {
271: $fieldset->setSortElementsByAttribute(
272: (string)$group->sort_fields->by,
273: $group->sort_fields->direction_desc ? SORT_DESC : SORT_ASC
274: );
275: } else {
276: usort($elements, array($this, '_sortForm'));
277: }
278:
279: foreach ($elements as $element) {
280: if (!$this->_canShowField($element)) {
281: continue;
282: }
283:
284: if ((string)$element->getAttribute('type') == 'group') {
285: $this->_initGroup($fieldset->getForm(), $element, $section, $fieldset);
286: continue;
287: }
288:
289: 290: 291:
292: $path = (string)$element->config_path;
293: if (empty($path)) {
294: $path = $section->getName() . '/' . $group->getName() . '/' . $fieldPrefix . $element->getName();
295: } elseif (strrpos($path, '/') > 0) {
296:
297: $groupPath = substr($path, 0, strrpos($path, '/'));
298: if (!isset($configDataAdditionalGroups[$groupPath])) {
299: $this->_configData = $this->_configDataObject->extendConfig(
300: $groupPath,
301: false,
302: $this->_configData
303: );
304: $configDataAdditionalGroups[$groupPath] = true;
305: }
306: }
307:
308: $data = $this->_configDataObject->getConfigDataValue($path, $inherit, $this->_configData);
309: if ($element->frontend_model) {
310: $fieldRenderer = Mage::getBlockSingleton((string)$element->frontend_model);
311: } else {
312: $fieldRenderer = $this->_defaultFieldRenderer;
313: }
314:
315: $fieldRenderer->setForm($this);
316: $fieldRenderer->setConfigData($this->_configData);
317:
318: $helperName = $this->_configFields->getAttributeModule($section, $group, $element);
319: $fieldType = (string)$element->frontend_type ? (string)$element->frontend_type : 'text';
320: $name = 'groups[' . $group->getName() . '][fields][' . $fieldPrefix.$element->getName() . '][value]';
321: $label = Mage::helper($helperName)->__($labelPrefix) . ' '
322: . Mage::helper($helperName)->__((string)$element->label);
323: $hint = (string)$element->hint ? Mage::helper($helperName)->__((string)$element->hint) : '';
324:
325: if ($element->backend_model) {
326: $model = Mage::getModel((string)$element->backend_model);
327: if (!$model instanceof Mage_Core_Model_Config_Data) {
328: Mage::throwException('Invalid config field backend model: '.(string)$element->backend_model);
329: }
330: $model->setPath($path)
331: ->setValue($data)
332: ->setWebsite($this->getWebsiteCode())
333: ->setStore($this->getStoreCode())
334: ->afterLoad();
335: $data = $model->getValue();
336: }
337:
338: $comment = $this->_prepareFieldComment($element, $helperName, $data);
339: $tooltip = $this->_prepareFieldTooltip($element, $helperName);
340: $id = $section->getName() . '_' . $group->getName() . '_' . $fieldPrefix . $element->getName();
341:
342: if ($element->depends) {
343: foreach ($element->depends->children() as $dependent) {
344:
345:
346: if (isset($dependent->fieldset)) {
347: $dependentFieldGroupName = (string)$dependent->fieldset;
348: if (!isset($this->_fieldsets[$dependentFieldGroupName])) {
349: $dependentFieldGroupName = $group->getName();
350: }
351: } else {
352: $dependentFieldGroupName = $group->getName();
353: }
354:
355: $dependentFieldNameValue = $dependent->getName();
356: $dependentFieldGroup = $dependentFieldGroupName == $group->getName()
357: ? $group
358: : $this->_fieldsets[$dependentFieldGroupName]->getGroup();
359:
360: $dependentId = $section->getName()
361: . '_' . $dependentFieldGroupName
362: . '_' . $fieldPrefix
363: . $dependentFieldNameValue;
364: $shouldBeAddedDependence = true;
365: $dependentValue = (string)(isset($dependent->value) ? $dependent->value : $dependent);
366: if (isset($dependent['separator'])) {
367: $dependentValue = explode((string)$dependent['separator'], $dependentValue);
368: }
369: $dependentFieldName = $fieldPrefix . $dependent->getName();
370: $dependentField = $dependentFieldGroup->fields->$dependentFieldName;
371: 372: 373: 374: 375:
376: if (!$this->_canShowField($dependentField)) {
377: $dependentFullPath = $section->getName()
378: . '/' . $dependentFieldGroupName
379: . '/' . $fieldPrefix
380: . $dependent->getName();
381: $dependentValueInStore = Mage::getStoreConfig($dependentFullPath, $this->getStoreCode());
382: if (is_array($dependentValue)) {
383: $shouldBeAddedDependence = !in_array($dependentValueInStore, $dependentValue);
384: } else {
385: $shouldBeAddedDependence = $dependentValue != $dependentValueInStore;
386: }
387: }
388: if ($shouldBeAddedDependence) {
389: $this->_getDependence()
390: ->addFieldMap($id, $id)
391: ->addFieldMap($dependentId, $dependentId)
392: ->addFieldDependence($id, $dependentId, $dependentValue);
393: }
394: }
395: }
396: $sharedClass = '';
397: if ($element->shared && $element->config_path) {
398: $sharedClass = ' shared shared-' . str_replace('/', '-', $element->config_path);
399: }
400:
401: $requiresClass = '';
402: if ($element->requires) {
403: $requiresClass = ' requires';
404: foreach (explode(',', $element->requires) as $groupName) {
405: $requiresClass .= ' requires-' . $section->getName() . '_' . $groupName;
406: }
407: }
408:
409: $field = $fieldset->addField($id, $fieldType, array(
410: 'name' => $name,
411: 'label' => $label,
412: 'comment' => $comment,
413: 'tooltip' => $tooltip,
414: 'hint' => $hint,
415: 'value' => $data,
416: 'inherit' => $inherit,
417: 'class' => $element->frontend_class . $sharedClass . $requiresClass,
418: 'field_config' => $element,
419: 'scope' => $this->getScope(),
420: 'scope_id' => $this->getScopeId(),
421: 'scope_label' => $this->getScopeLabel($element),
422: 'can_use_default_value' => $this->canUseDefaultValue((int)$element->show_in_default),
423: 'can_use_website_value' => $this->canUseWebsiteValue((int)$element->show_in_website),
424: ));
425: $this->_prepareFieldOriginalData($field, $element);
426:
427: if (isset($element->validate)) {
428: $field->addClass($element->validate);
429: }
430:
431: if (isset($element->frontend_type)
432: && 'multiselect' === (string)$element->frontend_type
433: && isset($element->can_be_empty)
434: ) {
435: $field->setCanBeEmpty(true);
436: }
437:
438: $field->setRenderer($fieldRenderer);
439:
440: if ($element->source_model) {
441:
442: $factoryName = (string)$element->source_model;
443: $method = false;
444: if (preg_match('/^([^:]+?)::([^:]+?)$/', $factoryName, $matches)) {
445: array_shift($matches);
446: list($factoryName, $method) = array_values($matches);
447: }
448:
449: $sourceModel = Mage::getSingleton($factoryName);
450: if ($sourceModel instanceof Varien_Object) {
451: $sourceModel->setPath($path);
452: }
453: if ($method) {
454: if ($fieldType == 'multiselect') {
455: $optionArray = $sourceModel->$method();
456: } else {
457: $optionArray = array();
458: foreach ($sourceModel->$method() as $value => $label) {
459: $optionArray[] = array('label' => $label, 'value' => $value);
460: }
461: }
462: } else {
463: $optionArray = $sourceModel->toOptionArray($fieldType == 'multiselect');
464: }
465: $field->setValues($optionArray);
466: }
467: }
468: }
469: return $this;
470: }
471:
472: 473: 474: 475: 476:
477: public function getConfigRoot()
478: {
479: if (empty($this->_configRoot)) {
480: $this->_configRoot = Mage::getSingleton('adminhtml/config_data')->getConfigRoot();
481: }
482: return $this->_configRoot;
483: }
484:
485: 486: 487: 488: 489: 490:
491: protected function _prepareFieldOriginalData($field, $xmlElement)
492: {
493: $originalData = array();
494: foreach ($xmlElement as $key => $value) {
495: if (!$value->hasChildren()) {
496: $originalData[$key] = (string)$value;
497: }
498: }
499: $field->setOriginalData($originalData);
500: }
501:
502: 503: 504: 505: 506: 507: 508:
509: protected function ($element, $helper, $currentValue)
510: {
511: $comment = '';
512: if ($element->comment) {
513: $commentInfo = $element->comment->asArray();
514: if (is_array($commentInfo)) {
515: if (isset($commentInfo['model'])) {
516: $model = Mage::getModel($commentInfo['model']);
517: if (method_exists($model, 'getCommentText')) {
518: $comment = $model->getCommentText($element, $currentValue);
519: }
520: }
521: } else {
522: $comment = Mage::helper($helper)->__($commentInfo);
523: }
524: }
525: return $comment;
526: }
527:
528: 529: 530: 531: 532: 533: 534:
535: protected function _prepareFieldTooltip($element, $helper)
536: {
537: if ($element->tooltip) {
538: return Mage::helper($helper)->__((string)$element->tooltip);
539: } elseif ($element->tooltip_block) {
540: return $this->getLayout()->createBlock((string)$element->tooltip_block)->toHtml();
541: }
542: return '';
543: }
544:
545: 546: 547: 548: 549:
550: protected function _afterToHtml($html)
551: {
552: if ($this->_getDependence()) {
553: $html .= $this->_getDependence()->toHtml();
554: }
555: $html = parent::_afterToHtml($html);
556: return $html;
557: }
558:
559: 560: 561: 562: 563: 564: 565:
566: protected function _sortForm($a, $b)
567: {
568: return (int)$a->sort_order < (int)$b->sort_order ? -1 : ((int)$a->sort_order > (int)$b->sort_order ? 1 : 0);
569:
570: }
571:
572: 573: 574: 575: 576: 577:
578: public function canUseDefaultValue($field)
579: {
580: if ($this->getScope() == self::SCOPE_STORES && $field) {
581: return true;
582: }
583: if ($this->getScope() == self::SCOPE_WEBSITES && $field) {
584: return true;
585: }
586: return false;
587: }
588:
589: 590: 591: 592: 593: 594:
595: public function canUseWebsiteValue($field)
596: {
597: if ($this->getScope() == self::SCOPE_STORES && $field) {
598: return true;
599: }
600: return false;
601: }
602:
603: 604: 605: 606: 607: 608:
609: protected function _canShowField($field)
610: {
611: $ifModuleEnabled = trim((string)$field->if_module_enabled);
612: if ($ifModuleEnabled && !Mage::helper('Core')->isModuleEnabled($ifModuleEnabled)) {
613: return false;
614: }
615:
616: switch ($this->getScope()) {
617: case self::SCOPE_DEFAULT:
618: return (int)$field->show_in_default;
619: break;
620: case self::SCOPE_WEBSITES:
621: return (int)$field->show_in_website;
622: break;
623: case self::SCOPE_STORES:
624: return (int)$field->show_in_store;
625: break;
626: }
627: return true;
628: }
629:
630: 631: 632: 633: 634:
635: public function getScope()
636: {
637: $scope = $this->getData('scope');
638: if (is_null($scope)) {
639: if ($this->getStoreCode()) {
640: $scope = self::SCOPE_STORES;
641: } elseif ($this->getWebsiteCode()) {
642: $scope = self::SCOPE_WEBSITES;
643: } else {
644: $scope = self::SCOPE_DEFAULT;
645: }
646: $this->setScope($scope);
647: }
648:
649: return $scope;
650: }
651:
652: 653: 654: 655: 656: 657:
658: public function getScopeLabel($element)
659: {
660: if ($element->show_in_store == 1) {
661: return $this->_scopeLabels[self::SCOPE_STORES];
662: } elseif ($element->show_in_website == 1) {
663: return $this->_scopeLabels[self::SCOPE_WEBSITES];
664: }
665: return $this->_scopeLabels[self::SCOPE_DEFAULT];
666: }
667:
668: 669: 670: 671: 672:
673: public function getScopeCode()
674: {
675: $scopeCode = $this->getData('scope_code');
676: if (is_null($scopeCode)) {
677: if ($this->getStoreCode()) {
678: $scopeCode = $this->getStoreCode();
679: } elseif ($this->getWebsiteCode()) {
680: $scopeCode = $this->getWebsiteCode();
681: } else {
682: $scopeCode = '';
683: }
684: $this->setScopeCode($scopeCode);
685: }
686:
687: return $scopeCode;
688: }
689:
690: 691: 692: 693: 694:
695: public function getScopeId()
696: {
697: $scopeId = $this->getData('scope_id');
698: if (is_null($scopeId)) {
699: if ($this->getStoreCode()) {
700: $scopeId = Mage::app()->getStore($this->getStoreCode())->getId();
701: } elseif ($this->getWebsiteCode()) {
702: $scopeId = Mage::app()->getWebsite($this->getWebsiteCode())->getId();
703: } else {
704: $scopeId = '';
705: }
706: $this->setScopeId($scopeId);
707: }
708: return $scopeId;
709: }
710:
711: 712: 713: 714: 715:
716: protected function _getAdditionalElementTypes()
717: {
718: return array(
719: 'export' => Mage::getConfig()->getBlockClassName('adminhtml/system_config_form_field_export'),
720: 'import' => Mage::getConfig()->getBlockClassName('adminhtml/system_config_form_field_import'),
721: 'allowspecific' => Mage::getConfig()
722: ->getBlockClassName('adminhtml/system_config_form_field_select_allowspecific'),
723: 'image' => Mage::getConfig()->getBlockClassName('adminhtml/system_config_form_field_image'),
724: 'file' => Mage::getConfig()->getBlockClassName('adminhtml/system_config_form_field_file')
725: );
726: }
727:
728: 729: 730: 731:
732: 733: 734: 735: 736: 737:
738: public function getSectionCode()
739: {
740: return $this->getRequest()->getParam('section', '');
741: }
742:
743: 744: 745: 746: 747: 748:
749: public function getWebsiteCode()
750: {
751: return $this->getRequest()->getParam('website', '');
752: }
753:
754: 755: 756: 757: 758: 759:
760: public function getStoreCode()
761: {
762: return $this->getRequest()->getParam('store', '');
763: }
764:
765: }
766: