00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 abstract class Varien_Data_Form_Element_Abstract extends Varien_Data_Form_Abstract
00036 {
00037 protected $_id;
00038 protected $_type;
00039 protected $_form;
00040 protected $_elements;
00041 protected $_renderer;
00042
00043 public function __construct($attributes = array())
00044 {
00045 parent::__construct($attributes);
00046 $this->_renderer = Varien_Data_Form::getElementRenderer();
00047 }
00048
00049
00050
00051
00052
00053
00054
00055 public function addElement(Varien_Data_Form_Element_Abstract $element, $after=false)
00056 {
00057 if ($this->getForm()) {
00058 $this->getForm()->checkElementId($element->getId());
00059 $this->getForm()->addElementToCollection($element);
00060 }
00061
00062 parent::addElement($element, $after);
00063 return $this;
00064 }
00065
00066 public function getId()
00067 {
00068 return $this->_id;
00069 }
00070
00071 public function getType()
00072 {
00073 return $this->_type;
00074 }
00075
00076 public function getForm()
00077 {
00078 return $this->_form;
00079 }
00080
00081 public function setId($id)
00082 {
00083 $this->_id = $id;
00084 $this->setData('html_id', $id);
00085 return $this;
00086 }
00087
00088 public function getHtmlId()
00089 {
00090 return $this->getForm()->getHtmlIdPrefix() . $this->getData('html_id') . $this->getForm()->getHtmlIdSuffix();
00091 }
00092
00093 public function getName()
00094 {
00095 $name = $this->getData('name');
00096 if ($suffix = $this->getForm()->getFieldNameSuffix()) {
00097 $name = $this->getForm()->addSuffixToName($name, $suffix);
00098 }
00099 return $name;
00100 }
00101
00102 public function setType($type)
00103 {
00104 $this->_type = $type;
00105 $this->setData('type', $type);
00106 return $this;
00107 }
00108
00109 public function setForm($form)
00110 {
00111 $this->_form = $form;
00112 return $this;
00113 }
00114
00115 public function removeField($elementId)
00116 {
00117 $this->getForm()->removeField($elementId);
00118 return parent::removeField($elementId);
00119 }
00120
00121 public function getHtmlAttributes()
00122 {
00123 return array('type', 'title', 'class', 'style', 'onclick', 'onchange', 'disabled', 'readonly');
00124 }
00125
00126 public function addClass($class)
00127 {
00128 $oldClass = $this->getClass();
00129 $this->setClass($oldClass.' '.$class);
00130 return $this;
00131 }
00132
00133 protected function _escape($string)
00134 {
00135 return htmlspecialchars($string, ENT_COMPAT);
00136 }
00137
00138 public function getEscapedValue($index=null)
00139 {
00140 $value = $this->getValue($index);
00141
00142 if ($filter = $this->getValueFilter()) {
00143 $value = $filter->filter($value);
00144 }
00145 return $this->_escape($value);
00146 }
00147
00148 public function setRenderer(Varien_Data_Form_Element_Renderer_Interface $renderer)
00149 {
00150 $this->_renderer = $renderer;
00151 return $this;
00152 }
00153
00154 public function getRenderer()
00155 {
00156 return $this->_renderer;
00157 }
00158
00159 public function getElementHtml()
00160 {
00161 $html = '<input id="'.$this->getHtmlId().'" name="'.$this->getName()
00162 .'" value="'.$this->getEscapedValue().'" '.$this->serialize($this->getHtmlAttributes()).'/>'."\n";
00163 $html.= $this->getAfterElementHtml();
00164 return $html;
00165 }
00166
00167 public function getAfterElementHtml()
00168 {
00169 return $this->getData('after_element_html');
00170 }
00171
00172 public function getLabelHtml($idSuffix = '')
00173 {
00174 if (!is_null($this->getLabel())) {
00175 $html = '<label for="'.$this->getHtmlId() . $idSuffix . '">'.$this->getLabel()
00176 . ( $this->getRequired() ? ' <span class="required">*</span>' : '' ).'</label>'."\n";
00177 }
00178 else {
00179 $html = '';
00180 }
00181 return $html;
00182 }
00183
00184 public function getDefaultHtml()
00185 {
00186 $html = $this->getData('default_html');
00187 if (is_null($html)) {
00188 $html = ( $this->getNoSpan() === true ) ? '' : '<span class="field-row">'."\n";
00189 $html.= $this->getLabelHtml();
00190 $html.= $this->getElementHtml();
00191 $html.= ( $this->getNoSpan() === true ) ? '' : '</span>'."\n";
00192 }
00193 return $html;
00194 }
00195
00196 public function getHtml()
00197 {
00198 if ($this->_renderer) {
00199 $html = $this->_renderer->render($this);
00200 }
00201 else {
00202 $html = $this->getDefaultHtml();
00203 }
00204 return $html;
00205 }
00206
00207 public function toHtml()
00208 {
00209 return $this->getHtml();
00210 }
00211
00212 public function serialize($attributes = array(), $valueSeparator='=', $fieldSeparator=' ', $quote='"')
00213 {
00214 if (in_array('disabled', $attributes) && !empty($this->_data['disabled'])) {
00215 $this->_data['disabled'] = 'disabled';
00216 }
00217 else {
00218 unset($this->_data['disabled']);
00219 }
00220 if (in_array('checked', $attributes) && !empty($this->_data['checked'])) {
00221 $this->_data['checked'] = 'checked';
00222 }
00223 else {
00224 unset($this->_data['checked']);
00225 }
00226 return parent::serialize($attributes, $valueSeparator, $fieldSeparator, $quote);
00227 }
00228
00229 public function setReadonly($readonly, $useDisabled = false)
00230 {
00231 if ($useDisabled) {
00232 $this->setDisabled($readonly);
00233 $this->setData('readonly_disabled', $readonly);
00234 } else {
00235 $this->setData('readonly', $readonly);
00236 }
00237
00238 return $this;
00239 }
00240
00241 public function getReadonly()
00242 {
00243 if ($this->hasData('readonly_disabled')) {
00244 return $this->_getData('readonly_disabled');
00245 }
00246
00247 return $this->_getData('readonly');
00248 }
00249
00250 }