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_Widget_Block_Adminhtml_Widget_Chooser extends Mage_Adminhtml_Block_Template
36: {
37: 38: 39: 40: 41:
42: public function getSourceUrl()
43: {
44: return $this->_getData('source_url');
45: }
46:
47: 48: 49: 50: 51:
52: public function getElement()
53: {
54: return $this->_getData('element');
55: }
56:
57: 58: 59: 60: 61:
62: public function getConfig()
63: {
64: if ($this->_getData('config') instanceof Varien_Object) {
65: return $this->_getData('config');
66: }
67:
68: $configArray = $this->_getData('config');
69: $config = new Varien_Object();
70: $this->setConfig($config);
71: if (!is_array($configArray)) {
72: return $this->_getData('config');
73: }
74:
75:
76: if (isset($configArray['label'])) {
77: $config->setData('label', $this->getTranslationHelper()->__($configArray['label']));
78: }
79:
80:
81: $buttons = array(
82: 'open' => Mage::helper('widget')->__('Choose...'),
83: 'close' => Mage::helper('widget')->__('Close')
84: );
85: if (isset($configArray['button']) && is_array($configArray['button'])) {
86: foreach ($configArray['button'] as $id => $label) {
87: $buttons[$id] = $this->getTranslationHelper()->__($label);
88: }
89: }
90: $config->setButtons($buttons);
91:
92: return $this->_getData('config');
93: }
94:
95: 96: 97: 98: 99:
100: public function getTranslationHelper()
101: {
102: if ($this->_getData('translation_helper') instanceof Mage_Core_Helper_Abstract) {
103: return $this->_getData('translation_helper');
104: }
105: return $this->helper('widget');
106: }
107:
108: 109: 110: 111: 112:
113: public function getUniqId()
114: {
115: return $this->_getData('uniq_id');
116: }
117:
118: 119: 120: 121: 122:
123: public function getFieldsetId()
124: {
125: return $this->_getData('fieldset_id');
126: }
127:
128: 129: 130: 131: 132:
133: public function getHiddenEnabled()
134: {
135: return $this->hasData('hidden_enabled') ? (bool)$this->_getData('hidden_enabled') : true;
136: }
137:
138: 139: 140: 141: 142:
143: protected function _toHtml()
144: {
145: $element = $this->getElement();
146:
147: $fieldset = $element->getForm()->getElement($this->getFieldsetId());
148: $chooserId = $this->getUniqId();
149: $config = $this->getConfig();
150:
151:
152: $chooser = $fieldset->addField('chooser' . $element->getId(), 'note', array(
153: 'label' => $config->getLabel() ? $config->getLabel() : '',
154: 'value_class' => 'value2',
155: ));
156: $hiddenHtml = '';
157: if ($this->getHiddenEnabled()) {
158: $hidden = new Varien_Data_Form_Element_Hidden($element->getData());
159: $hidden->setId("{$chooserId}value")->setForm($element->getForm());
160: if ($element->getRequired()) {
161: $hidden->addClass('required-entry');
162: }
163: $hiddenHtml = $hidden->getElementHtml();
164: $element->setValue('');
165: }
166:
167: $buttons = $config->getButtons();
168: $chooseButton = $this->getLayout()->createBlock('adminhtml/widget_button')
169: ->setType('button')
170: ->setId($chooserId . 'control')
171: ->setClass('btn-chooser')
172: ->setLabel($buttons['open'])
173: ->setOnclick($chooserId.'.choose()')
174: ->setDisabled($element->getReadonly());
175: $chooser->setData('after_element_html', $hiddenHtml . $chooseButton->toHtml());
176:
177:
178: $configJson = Mage::helper('core')->jsonEncode($config->getData());
179: return '
180: <label class="widget-option-label" id="' . $chooserId . 'label">'
181: . ($this->getLabel() ? $this->getLabel() : Mage::helper('widget')->__('Not Selected')) . '</label>
182: <div id="' . $chooserId . 'advice-container" class="hidden"></div>
183: <script type="text/javascript">//<![CDATA[
184: (function() {
185: var instantiateChooser = function() {
186: window.' . $chooserId . ' = new WysiwygWidget.chooser(
187: "' . $chooserId . '",
188: "' . $this->getSourceUrl() . '",
189: ' . $configJson . '
190: );
191: if ($("' . $chooserId . 'value")) {
192: $("' . $chooserId . 'value").advaiceContainer = "' . $chooserId . 'advice-container";
193: }
194: }
195:
196: if (document.loaded) { //allow load over ajax
197: instantiateChooser();
198: } else {
199: document.observe("dom:loaded", instantiateChooser);
200: }
201: })();
202: //]]></script>
203: ';
204: }
205: }
206: