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: class Mage_Adminhtml_Block_Widget_Form extends Mage_Adminhtml_Block_Widget
35: {
36:
37: 38: 39: 40: 41:
42: protected $_form;
43:
44: 45: 46: 47:
48: protected function _construct()
49: {
50: parent::_construct();
51: $this->setTemplate('widget/form.phtml');
52: $this->setDestElementId('edit_form');
53: $this->setShowGlobalIcon(false);
54: }
55:
56: 57: 58: 59: 60: 61: 62:
63: protected function _prepareLayout()
64: {
65: Varien_Data_Form::setElementRenderer(
66: $this->getLayout()->createBlock('adminhtml/widget_form_renderer_element')
67: );
68: Varien_Data_Form::setFieldsetRenderer(
69: $this->getLayout()->createBlock('adminhtml/widget_form_renderer_fieldset')
70: );
71: Varien_Data_Form::setFieldsetElementRenderer(
72: $this->getLayout()->createBlock('adminhtml/widget_form_renderer_fieldset_element')
73: );
74:
75: return parent::_prepareLayout();
76: }
77:
78: 79: 80: 81: 82:
83: public function getForm()
84: {
85: return $this->_form;
86: }
87:
88: 89: 90: 91: 92: 93: 94:
95: public function getFormObject()
96: {
97: return $this->getForm();
98: }
99:
100: 101: 102: 103: 104:
105: public function getFormHtml()
106: {
107: if (is_object($this->getForm())) {
108: return $this->getForm()->getHtml();
109: }
110: return '';
111: }
112:
113: 114: 115: 116: 117: 118:
119: public function setForm(Varien_Data_Form $form)
120: {
121: $this->_form = $form;
122: $this->_form->setParent($this);
123: $this->_form->setBaseUrl(Mage::getBaseUrl());
124: return $this;
125: }
126:
127: 128: 129: 130: 131:
132: protected function _prepareForm()
133: {
134: return $this;
135: }
136:
137: 138: 139: 140: 141:
142: protected function _beforeToHtml()
143: {
144: $this->_prepareForm();
145: $this->_initFormValues();
146: return parent::_beforeToHtml();
147: }
148:
149: 150: 151: 152: 153: 154:
155: protected function _initFormValues()
156: {
157: return $this;
158: }
159:
160: 161: 162: 163: 164: 165: 166:
167: protected function _setFieldset($attributes, $fieldset, $exclude=array())
168: {
169: $this->_addElementTypes($fieldset);
170: foreach ($attributes as $attribute) {
171:
172: if (!$attribute || ($attribute->hasIsVisible() && !$attribute->getIsVisible())) {
173: continue;
174: }
175: if ( ($inputType = $attribute->getFrontend()->getInputType())
176: && !in_array($attribute->getAttributeCode(), $exclude)
177: && ('media_image' != $inputType)
178: ) {
179:
180: $fieldType = $inputType;
181: $rendererClass = $attribute->getFrontend()->getInputRendererClass();
182: if (!empty($rendererClass)) {
183: $fieldType = $inputType . '_' . $attribute->getAttributeCode();
184: $fieldset->addType($fieldType, $rendererClass);
185: }
186:
187: $element = $fieldset->addField($attribute->getAttributeCode(), $fieldType,
188: array(
189: 'name' => $attribute->getAttributeCode(),
190: 'label' => $attribute->getFrontend()->getLabel(),
191: 'class' => $attribute->getFrontend()->getClass(),
192: 'required' => $attribute->getIsRequired(),
193: 'note' => $attribute->getNote(),
194: )
195: )
196: ->setEntityAttribute($attribute);
197:
198: $element->setAfterElementHtml($this->_getAdditionalElementHtml($element));
199:
200: if ($inputType == 'select') {
201: $element->setValues($attribute->getSource()->getAllOptions(true, true));
202: } else if ($inputType == 'multiselect') {
203: $element->setValues($attribute->getSource()->getAllOptions(false, true));
204: $element->setCanBeEmpty(true);
205: } else if ($inputType == 'date') {
206: $element->setImage($this->getSkinUrl('images/grid-cal.gif'));
207: $element->setFormat(Mage::app()->getLocale()->getDateFormatWithLongYear());
208: } else if ($inputType == 'multiline') {
209: $element->setLineCount($attribute->getMultilineCount());
210: }
211: }
212: }
213: }
214:
215: 216: 217: 218: 219:
220: protected function _addElementTypes(Varien_Data_Form_Abstract $baseElement)
221: {
222: $types = $this->_getAdditionalElementTypes();
223: foreach ($types as $code => $className) {
224: $baseElement->addType($code, $className);
225: }
226: }
227:
228: 229: 230: 231: 232:
233: protected function _getAdditionalElementTypes()
234: {
235: return array();
236: }
237:
238: 239: 240: 241: 242: 243:
244: protected function _getAdditionalElementHtml($element)
245: {
246: return '';
247: }
248:
249: }
250: