-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathForm.php
More file actions
257 lines (235 loc) · 7.54 KB
/
Form.php
File metadata and controls
257 lines (235 loc) · 7.54 KB
1
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magento.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magento.com for more information.
*
* @category Mage
* @package Mage_Adminhtml
* @copyright Copyright (c) 2006-2020 Magento, Inc. (http://www.magento.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Admin form widget
*
* @category Mage
* @package Mage_Adminhtml
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Adminhtml_Block_Widget_Form extends Mage_Adminhtml_Block_Widget
{
/**
* Form Object
*
* @var Varien_Data_Form
*/
protected $_form;
/**
* Class constructor
*
*/
protected function _construct()
{
parent::_construct();
$this->setTemplate('widget/form.phtml');
$this->setDestElementId('edit_form');
$this->setShowGlobalIcon(false);
}
/**
* Preparing global layout
*
* You can redefine this method in child classes for changin layout
*
* @return Mage_Core_Block_Abstract
*/
protected function _prepareLayout()
{
Varien_Data_Form::setElementRenderer(
$this->getLayout()->createBlock('adminhtml/widget_form_renderer_element')
);
Varien_Data_Form::setFieldsetRenderer(
$this->getLayout()->createBlock('adminhtml/widget_form_renderer_fieldset')
);
Varien_Data_Form::setFieldsetElementRenderer(
$this->getLayout()->createBlock('adminhtml/widget_form_renderer_fieldset_element')
);
return parent::_prepareLayout();
}
/**
* Get form object
*
* @return Varien_Data_Form
*/
public function getForm()
{
return $this->_form;
}
/**
* Get form object
*
* @deprecated deprecated since version 1.2
* @see getForm()
* @return Varien_Data_Form
*/
public function getFormObject()
{
return $this->getForm();
}
/**
* Get form HTML
*
* @return string
*/
public function getFormHtml()
{
if (is_object($this->getForm())) {
return $this->getForm()->getHtml();
}
return '';
}
/**
* Set form object
*
* @param Varien_Data_Form $form
* @return Mage_Adminhtml_Block_Widget_Form
*/
public function setForm(Varien_Data_Form $form)
{
$this->_form = $form;
$this->_form->setParent($this);
$this->_form->setBaseUrl(Mage::getBaseUrl());
return $this;
}
/**
* Prepare form before rendering HTML
*
* @return Mage_Adminhtml_Block_Widget_Form
*/
protected function _prepareForm()
{
return $this;
}
/**
* This method is called before rendering HTML
*
* @return Mage_Adminhtml_Block_Widget_Form
*/
protected function _beforeToHtml()
{
$this->_prepareForm();
$this->_initFormValues();
return parent::_beforeToHtml();
}
/**
* Initialize form fields values
* Method will be called after prepareForm and can be used for field values initialization
*
* @return Mage_Adminhtml_Block_Widget_Form
*/
protected function _initFormValues()
{
return $this;
}
/**
* Set Fieldset to Form
*
* @param array $attributes attributes that are to be added
* @param Varien_Data_Form_Element_Fieldset $fieldset
* @param array $exclude attributes that should be skipped
*/
protected function _setFieldset($attributes, $fieldset, $exclude=array())
{
$this->_addElementTypes($fieldset);
foreach ($attributes as $attribute) {
/* @var $attribute Mage_Eav_Model_Entity_Attribute */
if (!$attribute || ($attribute->hasIsVisible() && !$attribute->getIsVisible())) {
continue;
}
if ( ($inputType = $attribute->getFrontend()->getInputType())
&& !in_array($attribute->getAttributeCode(), $exclude)
&& ('media_image' != $inputType)
) {
$fieldType = $inputType;
$rendererClass = $attribute->getFrontend()->getInputRendererClass();
if (!empty($rendererClass)) {
$fieldType = $inputType . '_' . $attribute->getAttributeCode();
$fieldset->addType($fieldType, $rendererClass);
}
$element = $fieldset->addField($attribute->getAttributeCode(), $fieldType,
array(
'name' => $attribute->getAttributeCode(),
'label' => $attribute->getFrontend()->getLabel(),
'class' => $attribute->getFrontend()->getClass(),
'required' => $attribute->getIsRequired(),
'note' => $attribute->getNote(),
)
)
->setEntityAttribute($attribute);
$element->setAfterElementHtml($this->_getAdditionalElementHtml($element));
if ($inputType == 'select') {
$element->setValues($attribute->getSource()->getAllOptions(true, true));
} else if ($inputType == 'multiselect') {
$element->setValues($attribute->getSource()->getAllOptions(false, true));
$element->setCanBeEmpty(true);
} else if ($inputType == 'date') {
$element->setImage($this->getSkinUrl('images/grid-cal.gif'));
$element->setFormat(Mage::app()->getLocale()->getDateFormatWithLongYear());
} else if ($inputType == 'datetime') {
$element->setImage($this->getSkinUrl('images/grid-cal.gif'));
$element->setTime(true);
$element->setStyle('width:50%;');
$element->setFormat(
Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT)
);
} else if ($inputType == 'multiline') {
$element->setLineCount($attribute->getMultilineCount());
}
}
}
}
/**
* Add new element type
*
* @param Varien_Data_Form_Abstract $baseElement
*/
protected function _addElementTypes(Varien_Data_Form_Abstract $baseElement)
{
$types = $this->_getAdditionalElementTypes();
foreach ($types as $code => $className) {
$baseElement->addType($code, $className);
}
}
/**
* Retrieve predefined additional element types
*
* @return array
*/
protected function _getAdditionalElementTypes()
{
return array();
}
/**
* Enter description here...
*
* @param Varien_Data_Form_Element_Abstract $element
* @return string
*/
protected function _getAdditionalElementHtml($element)
{
return '';
}
}