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) 2012 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: /**
28: * Form element dependencies mapper
29: * Assumes that one element may depend on other element values.
30: * Will toggle as "enabled" only if all elements it depends from toggle as true.
31: */
32: class Mage_Adminhtml_Block_Widget_Form_Element_Dependence extends Mage_Adminhtml_Block_Abstract
33: {
34: /**
35: * name => id mapper
36: * @var array
37: */
38: protected $_fields = array();
39:
40: /**
41: * Dependencies mapper (by names)
42: * array(
43: * 'dependent_name' => array(
44: * 'depends_from_1_name' => 'mixed value',
45: * 'depends_from_2_name' => 'some another value',
46: * ...
47: * )
48: * )
49: * @var array
50: */
51: protected $_depends = array();
52:
53: /**
54: * Additional configuration options for the dependencies javascript controller
55: *
56: * @var array
57: */
58: protected $_configOptions = array();
59:
60: /**
61: * Add name => id mapping
62: *
63: * @param string $fieldId - element ID in DOM
64: * @param string $fieldName - element name in their fieldset/form namespace
65: * @return Mage_Adminhtml_Block_Widget_Form_Element_Dependence
66: */
67: public function addFieldMap($fieldId, $fieldName)
68: {
69: $this->_fields[$fieldName] = $fieldId;
70: return $this;
71: }
72:
73: /**
74: * Register field name dependence one from each other by specified values
75: *
76: * @param string $fieldName
77: * @param string $fieldNameFrom
78: * @param string|array $refValues
79: * @return Mage_Adminhtml_Block_Widget_Form_Element_Dependence
80: */
81: public function addFieldDependence($fieldName, $fieldNameFrom, $refValues)
82: {
83: $this->_depends[$fieldName][$fieldNameFrom] = $refValues;
84: return $this;
85: }
86:
87: /**
88: * Add misc configuration options to the javascript dependencies controller
89: *
90: * @param array $options
91: * @return Mage_Adminhtml_Block_Widget_Form_Element_Dependence
92: */
93: public function addConfigOptions(array $options)
94: {
95: $this->_configOptions = array_merge($this->_configOptions, $options);
96: return $this;
97: }
98:
99: /**
100: * HTML output getter
101: * @return string
102: */
103: protected function _toHtml()
104: {
105: if (!$this->_depends) {
106: return '';
107: }
108: return '<script type="text/javascript"> new FormElementDependenceController('
109: . $this->_getDependsJson()
110: . ($this->_configOptions ? ', ' . Mage::helper('core')->jsonEncode($this->_configOptions) : '')
111: . '); </script>';
112: }
113:
114: /**
115: * Field dependences JSON map generator
116: * @return string
117: */
118: protected function _getDependsJson()
119: {
120: $result = array();
121: foreach ($this->_depends as $to => $row) {
122: foreach ($row as $from => $value) {
123: $result[$this->_fields[$to]][$this->_fields[$from]] = $value;
124: }
125: }
126: return Mage::helper('core')->jsonEncode($result);
127: }
128: }
129: