*/
abstract class Varien_Data_Form_Element_Abstract extends Varien_Data_Form_Abstract
{
protected $_id;
protected $_type;
protected $_form;
protected $_elements;
protected $_renderer;
public function __construct($attributes = array())
{
parent::__construct($attributes);
$this->_renderer = Varien_Data_Form::getElementRenderer();
}
/**
* Add form element
*
* @param Varien_Data_Form_Element_Abstract $element
* @return Varien_Data_Form
*/
public function addElement(Varien_Data_Form_Element_Abstract $element, $after=false)
{
if ($this->getForm()) {
$this->getForm()->checkElementId($element->getId());
$this->getForm()->addElementToCollection($element);
}
parent::addElement($element, $after);
return $this;
}
public function getId()
{
return $this->_id;
}
public function getType()
{
return $this->_type;
}
public function getForm()
{
return $this->_form;
}
public function setId($id)
{
$this->_id = $id;
$this->setData('html_id', $id);
return $this;
}
public function getHtmlId()
{
return $this->getForm()->getHtmlIdPrefix() . $this->getData('html_id') . $this->getForm()->getHtmlIdSuffix();
}
public function getName()
{
$name = $this->getData('name');
if ($suffix = $this->getForm()->getFieldNameSuffix()) {
$name = $this->getForm()->addSuffixToName($name, $suffix);
}
return $name;
}
public function setType($type)
{
$this->_type = $type;
$this->setData('type', $type);
return $this;
}
public function setForm($form)
{
$this->_form = $form;
return $this;
}
public function removeField($elementId)
{
$this->getForm()->removeField($elementId);
return parent::removeField($elementId);
}
public function getHtmlAttributes()
{
return array('type', 'title', 'class', 'style', 'onclick', 'onchange', 'disabled', 'readonly', 'tabindex');
}
public function addClass($class)
{
$oldClass = $this->getClass();
$this->setClass($oldClass.' '.$class);
return $this;
}
/**
* Remove CSS class
*
* @param string $class
* @return Varien_Data_Form_Element_Abstract
*/
public function removeClass($class)
{
$classes = array_unique(explode(' ', $this->getClass()));
if (false !== ($key = array_search($class, $classes))) {
unset($classes[$key]);
}
$this->setClass(implode(' ', $classes));
return $this;
}
protected function _escape($string)
{
return htmlspecialchars($string, ENT_COMPAT);
}
public function getEscapedValue($index=null)
{
$value = $this->getValue($index);
if ($filter = $this->getValueFilter()) {
$value = $filter->filter($value);
}
return $this->_escape($value);
}
public function setRenderer(Varien_Data_Form_Element_Renderer_Interface $renderer)
{
$this->_renderer = $renderer;
return $this;
}
public function getRenderer()
{
return $this->_renderer;
}
public function getElementHtml()
{
$html = 'serialize($this->getHtmlAttributes()).'/>'."\n";
$html.= $this->getAfterElementHtml();
return $html;
}
public function getAfterElementHtml()
{
return $this->getData('after_element_html');
}
public function getLabelHtml($idSuffix = '')
{
if (!is_null($this->getLabel())) {
$html = ''."\n";
}
else {
$html = '';
}
return $html;
}
public function getDefaultHtml()
{
$html = $this->getData('default_html');
if (is_null($html)) {
$html = ( $this->getNoSpan() === true ) ? '' : ''."\n";
$html.= $this->getLabelHtml();
$html.= $this->getElementHtml();
$html.= ( $this->getNoSpan() === true ) ? '' : ''."\n";
}
return $html;
}
public function getHtml()
{
if ($this->getRequired()) {
$this->addClass('required-entry');
}
if ($this->_renderer) {
$html = $this->_renderer->render($this);
}
else {
$html = $this->getDefaultHtml();
}
return $html;
}
public function toHtml()
{
return $this->getHtml();
}
public function serialize($attributes = array(), $valueSeparator='=', $fieldSeparator=' ', $quote='"')
{
if (in_array('disabled', $attributes) && !empty($this->_data['disabled'])) {
$this->_data['disabled'] = 'disabled';
}
else {
unset($this->_data['disabled']);
}
if (in_array('checked', $attributes) && !empty($this->_data['checked'])) {
$this->_data['checked'] = 'checked';
}
else {
unset($this->_data['checked']);
}
return parent::serialize($attributes, $valueSeparator, $fieldSeparator, $quote);
}
public function setReadonly($readonly, $useDisabled = false)
{
if ($useDisabled) {
$this->setDisabled($readonly);
$this->setData('readonly_disabled', $readonly);
} else {
$this->setData('readonly', $readonly);
}
return $this;
}
public function getReadonly()
{
if ($this->hasData('readonly_disabled')) {
return $this->_getData('readonly_disabled');
}
return $this->_getData('readonly');
}
public function getHtmlContainerId()
{
if ($this->hasData('container_id')) {
return $this->getData('container_id');
} elseif ($idPrefix = $this->getForm()->getFieldContainerIdPrefix()) {
return $idPrefix . $this->getId();
}
return '';
}
/**
* Add specified values to element values
*
* @param string|int|array $values
* @param bool $overwrite
* @return Varien_Data_Form_Element_Abstract
*/
public function addElementValues($values, $overwrite = false)
{
if (empty($values) || (is_string($values) && trim($values) == '')) {
return $this;
}
if (!is_array($values)) {
$values = Mage::helper('core')->escapeHtml(trim($values));
$values = array($values => $values);
}
$elementValues = $this->getValues();
if (!empty($elementValues)) {
foreach ($values as $key => $value) {
if ((isset($elementValues[$key]) && $overwrite) || !isset($elementValues[$key])) {
$elementValues[$key] = Mage::helper('core')->escapeHtml($value);
}
}
$values = $elementValues;
}
$this->setValues($values);
return $this;
}
}