PageRenderTime 53ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Varien/Data/Form/Element/Abstract.php

https://gitlab.com/blingbang2016/shop
PHP | 301 lines | 211 code | 34 blank | 56 comment | 27 complexity | ebcf3d7ad4fa41066bb579e6eed4efc3 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magento.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category Varien
  22. * @package Varien_Data
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Data form abstract class
  28. *
  29. * @category Varien
  30. * @package Varien_Data
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. abstract class Varien_Data_Form_Element_Abstract extends Varien_Data_Form_Abstract
  34. {
  35. protected $_id;
  36. protected $_type;
  37. protected $_form;
  38. protected $_elements;
  39. protected $_renderer;
  40. public function __construct($attributes = array())
  41. {
  42. parent::__construct($attributes);
  43. $this->_renderer = Varien_Data_Form::getElementRenderer();
  44. }
  45. /**
  46. * Add form element
  47. *
  48. * @param Varien_Data_Form_Element_Abstract $element
  49. * @return Varien_Data_Form
  50. */
  51. public function addElement(Varien_Data_Form_Element_Abstract $element, $after=false)
  52. {
  53. if ($this->getForm()) {
  54. $this->getForm()->checkElementId($element->getId());
  55. $this->getForm()->addElementToCollection($element);
  56. }
  57. parent::addElement($element, $after);
  58. return $this;
  59. }
  60. public function getId()
  61. {
  62. return $this->_id;
  63. }
  64. public function getType()
  65. {
  66. return $this->_type;
  67. }
  68. public function getForm()
  69. {
  70. return $this->_form;
  71. }
  72. public function setId($id)
  73. {
  74. $this->_id = $id;
  75. $this->setData('html_id', $id);
  76. return $this;
  77. }
  78. public function getHtmlId()
  79. {
  80. return $this->getForm()->getHtmlIdPrefix() . $this->getData('html_id') . $this->getForm()->getHtmlIdSuffix();
  81. }
  82. public function getName()
  83. {
  84. $name = $this->getData('name');
  85. if ($suffix = $this->getForm()->getFieldNameSuffix()) {
  86. $name = $this->getForm()->addSuffixToName($name, $suffix);
  87. }
  88. return $name;
  89. }
  90. public function setType($type)
  91. {
  92. $this->_type = $type;
  93. $this->setData('type', $type);
  94. return $this;
  95. }
  96. public function setForm($form)
  97. {
  98. $this->_form = $form;
  99. return $this;
  100. }
  101. public function removeField($elementId)
  102. {
  103. $this->getForm()->removeField($elementId);
  104. return parent::removeField($elementId);
  105. }
  106. public function getHtmlAttributes()
  107. {
  108. return array('type', 'title', 'class', 'style', 'onclick', 'onchange', 'disabled', 'readonly', 'tabindex');
  109. }
  110. public function addClass($class)
  111. {
  112. $oldClass = $this->getClass();
  113. $this->setClass($oldClass.' '.$class);
  114. return $this;
  115. }
  116. /**
  117. * Remove CSS class
  118. *
  119. * @param string $class
  120. * @return Varien_Data_Form_Element_Abstract
  121. */
  122. public function removeClass($class)
  123. {
  124. $classes = array_unique(explode(' ', $this->getClass()));
  125. if (false !== ($key = array_search($class, $classes))) {
  126. unset($classes[$key]);
  127. }
  128. $this->setClass(implode(' ', $classes));
  129. return $this;
  130. }
  131. protected function _escape($string)
  132. {
  133. return htmlspecialchars($string, ENT_COMPAT);
  134. }
  135. public function getEscapedValue($index=null)
  136. {
  137. $value = $this->getValue($index);
  138. if ($filter = $this->getValueFilter()) {
  139. $value = $filter->filter($value);
  140. }
  141. return $this->_escape($value);
  142. }
  143. public function setRenderer(Varien_Data_Form_Element_Renderer_Interface $renderer)
  144. {
  145. $this->_renderer = $renderer;
  146. return $this;
  147. }
  148. public function getRenderer()
  149. {
  150. return $this->_renderer;
  151. }
  152. public function getElementHtml()
  153. {
  154. $html = '<input id="'.$this->getHtmlId().'" name="'.$this->getName()
  155. .'" value="'.$this->getEscapedValue().'" '.$this->serialize($this->getHtmlAttributes()).'/>'."\n";
  156. $html.= $this->getAfterElementHtml();
  157. return $html;
  158. }
  159. public function getAfterElementHtml()
  160. {
  161. return $this->getData('after_element_html');
  162. }
  163. /**
  164. * Render HTML for element's label
  165. *
  166. * @param string $idSuffix
  167. * @return string
  168. */
  169. public function getLabelHtml($idSuffix = '')
  170. {
  171. if (!is_null($this->getLabel())) {
  172. $html = '<label for="'.$this->getHtmlId() . $idSuffix . '">' . $this->_escape($this->getLabel())
  173. . ( $this->getRequired() ? ' <span class="required">*</span>' : '' ) . '</label>' . "\n";
  174. } else {
  175. $html = '';
  176. }
  177. return $html;
  178. }
  179. public function getDefaultHtml()
  180. {
  181. $html = $this->getData('default_html');
  182. if (is_null($html)) {
  183. $html = ( $this->getNoSpan() === true ) ? '' : '<span class="field-row">'."\n";
  184. $html.= $this->getLabelHtml();
  185. $html.= $this->getElementHtml();
  186. $html.= ( $this->getNoSpan() === true ) ? '' : '</span>'."\n";
  187. }
  188. return $html;
  189. }
  190. public function getHtml()
  191. {
  192. if ($this->getRequired()) {
  193. $this->addClass('required-entry');
  194. }
  195. if ($this->_renderer) {
  196. $html = $this->_renderer->render($this);
  197. }
  198. else {
  199. $html = $this->getDefaultHtml();
  200. }
  201. return $html;
  202. }
  203. public function toHtml()
  204. {
  205. return $this->getHtml();
  206. }
  207. public function serialize($attributes = array(), $valueSeparator='=', $fieldSeparator=' ', $quote='"')
  208. {
  209. if (in_array('disabled', $attributes) && !empty($this->_data['disabled'])) {
  210. $this->_data['disabled'] = 'disabled';
  211. }
  212. else {
  213. unset($this->_data['disabled']);
  214. }
  215. if (in_array('checked', $attributes) && !empty($this->_data['checked'])) {
  216. $this->_data['checked'] = 'checked';
  217. }
  218. else {
  219. unset($this->_data['checked']);
  220. }
  221. return parent::serialize($attributes, $valueSeparator, $fieldSeparator, $quote);
  222. }
  223. public function getReadonly()
  224. {
  225. if ($this->hasData('readonly_disabled')) {
  226. return $this->_getData('readonly_disabled');
  227. }
  228. return $this->_getData('readonly');
  229. }
  230. public function getHtmlContainerId()
  231. {
  232. if ($this->hasData('container_id')) {
  233. return $this->getData('container_id');
  234. } elseif ($idPrefix = $this->getForm()->getFieldContainerIdPrefix()) {
  235. return $idPrefix . $this->getId();
  236. }
  237. return '';
  238. }
  239. /**
  240. * Add specified values to element values
  241. *
  242. * @param string|int|array $values
  243. * @param bool $overwrite
  244. * @return Varien_Data_Form_Element_Abstract
  245. */
  246. public function addElementValues($values, $overwrite = false)
  247. {
  248. if (empty($values) || (is_string($values) && trim($values) == '')) {
  249. return $this;
  250. }
  251. if (!is_array($values)) {
  252. $values = Mage::helper('core')->escapeHtml(trim($values));
  253. $values = array($values => $values);
  254. }
  255. $elementValues = $this->getValues();
  256. if (!empty($elementValues)) {
  257. foreach ($values as $key => $value) {
  258. if ((isset($elementValues[$key]) && $overwrite) || !isset($elementValues[$key])) {
  259. $elementValues[$key] = Mage::helper('core')->escapeHtml($value);
  260. }
  261. }
  262. $values = $elementValues;
  263. }
  264. $this->setValues($values);
  265. return $this;
  266. }
  267. }