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_Options extends Mage_Adminhtml_Block_Widget_Form
36: {
37: 38: 39: 40:
41: protected $_defaultElementType = 'text';
42:
43: 44: 45: 46:
47: protected $_translationHelper = null;
48:
49: 50: 51: 52: 53: 54:
55: protected function _prepareForm()
56: {
57: $this->getForm()->setUseContainer(false);
58: $this->addFields();
59: return $this;
60: }
61:
62: 63: 64: 65: 66:
67: public function getForm()
68: {
69: if ($this->_form instanceof Varien_Data_Form) {
70: return $this->_form;
71: }
72: $form = new Varien_Data_Form();
73: $this->setForm($form);
74: return $form;
75: }
76:
77: 78: 79: 80: 81:
82: public function getMainFieldset()
83: {
84: if ($this->_getData('main_fieldset') instanceof Varien_Data_Form_Element_Fieldset) {
85: return $this->_getData('main_fieldset');
86: }
87: $mainFieldsetHtmlId = 'options_fieldset' . md5($this->getWidgetType());
88: $this->setMainFieldsetHtmlId($mainFieldsetHtmlId);
89: $fieldset = $this->getForm()->addFieldset($mainFieldsetHtmlId, array(
90: 'legend' => $this->helper('widget')->__('Widget Options'),
91: 'class' => 'fieldset-wide',
92: ));
93: $this->setData('main_fieldset', $fieldset);
94:
95:
96: $block = $this->getLayout()->createBlock('adminhtml/widget_form_element_dependence');
97: $this->setChild('form_after', $block);
98:
99: return $fieldset;
100: }
101:
102: 103: 104: 105: 106:
107: public function addFields()
108: {
109:
110: if (!$this->getWidgetType()) {
111: Mage::throwException($this->__('Widget Type is not specified'));
112: }
113: $config = Mage::getSingleton('widget/widget')->getConfigAsObject($this->getWidgetType());
114: if (!$config->getParameters()) {
115: return $this;
116: }
117: $module = $config->getModule();
118: $this->_translationHelper = Mage::helper($module ? $module : 'widget');
119: foreach ($config->getParameters() as $parameter) {
120: $this->_addField($parameter);
121: }
122:
123: return $this;
124: }
125:
126: 127: 128: 129: 130: 131:
132: protected function _addField($parameter)
133: {
134: $form = $this->getForm();
135: $fieldset = $this->getMainFieldset();
136:
137:
138: $fieldName = $parameter->getKey();
139: $data = array(
140: 'name' => $form->addSuffixToName($fieldName, 'parameters'),
141: 'label' => $this->_translationHelper->__($parameter->getLabel()),
142: 'required' => $parameter->getRequired(),
143: 'class' => 'widget-option',
144: 'note' => $this->_translationHelper->__($parameter->getDescription()),
145: );
146:
147: if ($values = $this->getWidgetValues()) {
148: $data['value'] = (isset($values[$fieldName]) ? $values[$fieldName] : '');
149: }
150: else {
151: $data['value'] = $parameter->getValue();
152:
153: if ($fieldName == 'unique_id' && $data['value'] == '') {
154: $data['value'] = md5(microtime(1));
155: }
156: }
157:
158:
159: if ($values = $parameter->getValues()) {
160:
161: $data['values'] = array();
162: foreach ($values as $option) {
163: $data['values'][] = array(
164: 'label' => $this->_translationHelper->__($option['label']),
165: 'value' => $option['value']
166: );
167: }
168: }
169:
170: elseif ($sourceModel = $parameter->getSourceModel()) {
171: $data['values'] = Mage::getModel($sourceModel)->toOptionArray();
172: }
173:
174:
175: $fieldRenderer = null;
176: $fieldType = $parameter->getType();
177:
178: if (!$parameter->getVisible()) {
179: $fieldType = 'hidden';
180: }
181:
182: elseif (false !== strpos($fieldType, '/')) {
183: $fieldRenderer = $this->getLayout()->createBlock($fieldType);
184: $fieldType = $this->_defaultElementType;
185: }
186:
187:
188: $field = $fieldset->addField($this->getMainFieldsetHtmlId() . '_' . $fieldName, $fieldType, $data);
189: if ($fieldRenderer) {
190: $field->setRenderer($fieldRenderer);
191: }
192:
193:
194: if ($helper = $parameter->getHelperBlock()) {
195: $helperBlock = $this->getLayout()->createBlock($helper->getType(), '', $helper->getData());
196: if ($helperBlock instanceof Varien_Object) {
197: $helperBlock->setConfig($helper->getData())
198: ->setFieldsetId($fieldset->getId())
199: ->setTranslationHelper($this->_translationHelper)
200: ->prepareElementHtml($field);
201: }
202: }
203:
204:
205: $dependenceBlock = $this->getChild('form_after');
206: $dependenceBlock->addFieldMap($field->getId(), $fieldName);
207: if ($parameter->getDepends()) {
208: foreach ($parameter->getDepends() as $from => $row) {
209: $values = isset($row['values']) ? array_values($row['values']) : (string)$row['value'];
210: $dependenceBlock->addFieldDependence($fieldName, $from, $values);
211: }
212: }
213:
214: return $field;
215: }
216: }
217: