Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Form / View / Helper / AbstractHelper.php
blob97b9568de40b71bab9eb4a4e99fd3d9a9d5582c4
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Form\View\Helper;
12 use Zend\Form\ElementInterface;
13 use Zend\I18n\View\Helper\AbstractTranslatorHelper as BaseAbstractHelper;
14 use Zend\View\Helper\Doctype;
15 use Zend\View\Helper\EscapeHtml;
16 use Zend\View\Helper\EscapeHtmlAttr;
18 /**
19 * Base functionality for all form view helpers
21 abstract class AbstractHelper extends BaseAbstractHelper
23 /**
24 * Standard boolean attributes, with expected values for enabling/disabling
26 * @var array
28 protected $booleanAttributes = array(
29 'autocomplete' => array('on' => 'on', 'off' => 'off'),
30 'autofocus' => array('on' => 'autofocus', 'off' => ''),
31 'checked' => array('on' => 'checked', 'off' => ''),
32 'disabled' => array('on' => 'disabled', 'off' => ''),
33 'multiple' => array('on' => 'multiple', 'off' => ''),
34 'readonly' => array('on' => 'readonly', 'off' => ''),
35 'required' => array('on' => 'required', 'off' => ''),
36 'selected' => array('on' => 'selected', 'off' => ''),
39 /**
40 * Translatable attributes
42 * @var array
44 protected $translatableAttributes = array(
45 'placeholder' => true,
46 'title' => true,
49 /**
50 * @var Doctype
52 protected $doctypeHelper;
54 /**
55 * @var EscapeHtml
57 protected $escapeHtmlHelper;
59 /**
60 * @var EscapeHtmlAttr
62 protected $escapeHtmlAttrHelper;
64 /**
65 * Attributes globally valid for all tags
67 * @var array
69 protected $validGlobalAttributes = array(
70 'accesskey' => true,
71 'class' => true,
72 'contenteditable' => true,
73 'contextmenu' => true,
74 'dir' => true,
75 'draggable' => true,
76 'dropzone' => true,
77 'hidden' => true,
78 'id' => true,
79 'lang' => true,
80 'onabort' => true,
81 'onblur' => true,
82 'oncanplay' => true,
83 'oncanplaythrough' => true,
84 'onchange' => true,
85 'onclick' => true,
86 'oncontextmenu' => true,
87 'ondblclick' => true,
88 'ondrag' => true,
89 'ondragend' => true,
90 'ondragenter' => true,
91 'ondragleave' => true,
92 'ondragover' => true,
93 'ondragstart' => true,
94 'ondrop' => true,
95 'ondurationchange' => true,
96 'onemptied' => true,
97 'onended' => true,
98 'onerror' => true,
99 'onfocus' => true,
100 'oninput' => true,
101 'oninvalid' => true,
102 'onkeydown' => true,
103 'onkeypress' => true,
104 'onkeyup' => true,
105 'onload' => true,
106 'onloadeddata' => true,
107 'onloadedmetadata' => true,
108 'onloadstart' => true,
109 'onmousedown' => true,
110 'onmousemove' => true,
111 'onmouseout' => true,
112 'onmouseover' => true,
113 'onmouseup' => true,
114 'onmousewheel' => true,
115 'onpause' => true,
116 'onplay' => true,
117 'onplaying' => true,
118 'onprogress' => true,
119 'onratechange' => true,
120 'onreadystatechange' => true,
121 'onreset' => true,
122 'onscroll' => true,
123 'onseeked' => true,
124 'onseeking' => true,
125 'onselect' => true,
126 'onshow' => true,
127 'onstalled' => true,
128 'onsubmit' => true,
129 'onsuspend' => true,
130 'ontimeupdate' => true,
131 'onvolumechange' => true,
132 'onwaiting' => true,
133 'role' => true,
134 'spellcheck' => true,
135 'style' => true,
136 'tabindex' => true,
137 'title' => true,
138 'xml:base' => true,
139 'xml:lang' => true,
140 'xml:space' => true,
144 * Attributes valid for the tag represented by this helper
146 * This should be overridden in extending classes
148 * @var array
150 protected $validTagAttributes = array(
154 * Set value for doctype
156 * @param string $doctype
157 * @return AbstractHelper
159 public function setDoctype($doctype)
161 $this->getDoctypeHelper()->setDoctype($doctype);
162 return $this;
166 * Get value for doctype
168 * @return string
170 public function getDoctype()
172 return $this->getDoctypeHelper()->getDoctype();
176 * Set value for character encoding
178 * @param string $encoding
179 * @return AbstractHelper
181 public function setEncoding($encoding)
183 $this->getEscapeHtmlHelper()->setEncoding($encoding);
184 return $this;
188 * Get character encoding
190 * @return string
192 public function getEncoding()
194 return $this->getEscapeHtmlHelper()->getEncoding();
198 * Create a string of all attribute/value pairs
200 * Escapes all attribute values
202 * @param array $attributes
203 * @return string
205 public function createAttributesString(array $attributes)
207 $attributes = $this->prepareAttributes($attributes);
208 $escape = $this->getEscapeHtmlHelper();
209 $strings = array();
210 foreach ($attributes as $key => $value) {
211 $key = strtolower($key);
212 if (!$value && isset($this->booleanAttributes[$key])) {
213 // Skip boolean attributes that expect empty string as false value
214 if ('' === $this->booleanAttributes[$key]['off']) {
215 continue;
219 //check if attribute is translatable
220 if (isset($this->translatableAttributes[$key]) && !empty($value)) {
221 if (($translator = $this->getTranslator()) !== null) {
222 $value = $translator->translate(
223 $value, $this->getTranslatorTextDomain()
228 //@TODO Escape event attributes like AbstractHtmlElement view helper does in htmlAttribs ??
229 $strings[] = sprintf('%s="%s"', $escape($key), $escape($value));
231 return implode(' ', $strings);
235 * Get the ID of an element
237 * If no ID attribute present, attempts to use the name attribute.
238 * If no name attribute is present, either, returns null.
240 * @param ElementInterface $element
241 * @return null|string
243 public function getId(ElementInterface $element)
245 $id = $element->getAttribute('id');
246 if (null !== $id) {
247 return $id;
250 return $element->getName();
254 * Get the closing bracket for an inline tag
256 * Closes as either "/>" for XHTML doctypes or ">" otherwise.
258 * @return string
260 public function getInlineClosingBracket()
262 $doctypeHelper = $this->getDoctypeHelper();
263 if ($doctypeHelper->isXhtml()) {
264 return '/>';
266 return '>';
270 * Retrieve the doctype helper
272 * @return Doctype
274 protected function getDoctypeHelper()
276 if ($this->doctypeHelper) {
277 return $this->doctypeHelper;
280 if (method_exists($this->view, 'plugin')) {
281 $this->doctypeHelper = $this->view->plugin('doctype');
284 if (!$this->doctypeHelper instanceof Doctype) {
285 $this->doctypeHelper = new Doctype();
288 return $this->doctypeHelper;
292 * Retrieve the escapeHtml helper
294 * @return EscapeHtml
296 protected function getEscapeHtmlHelper()
298 if ($this->escapeHtmlHelper) {
299 return $this->escapeHtmlHelper;
302 if (method_exists($this->view, 'plugin')) {
303 $this->escapeHtmlHelper = $this->view->plugin('escapehtml');
306 if (!$this->escapeHtmlHelper instanceof EscapeHtml) {
307 $this->escapeHtmlHelper = new EscapeHtml();
310 return $this->escapeHtmlHelper;
314 * Retrieve the escapeHtmlAttr helper
316 * @return EscapeHtmlAttr
318 protected function getEscapeHtmlAttrHelper()
320 if ($this->escapeHtmlAttrHelper) {
321 return $this->escapeHtmlAttrHelper;
324 if (method_exists($this->view, 'plugin')) {
325 $this->escapeHtmlAttrHelper = $this->view->plugin('escapehtmlattr');
328 if (!$this->escapeHtmlAttrHelper instanceof EscapeHtmlAttr) {
329 $this->escapeHtmlAttrHelper = new EscapeHtmlAttr();
332 return $this->escapeHtmlAttrHelper;
336 * Prepare attributes for rendering
338 * Ensures appropriate attributes are present (e.g., if "name" is present,
339 * but no "id", sets the latter to the former).
341 * Removes any invalid attributes
343 * @param array $attributes
344 * @return array
346 protected function prepareAttributes(array $attributes)
348 foreach ($attributes as $key => $value) {
349 $attribute = strtolower($key);
351 if (!isset($this->validGlobalAttributes[$attribute])
352 && !isset($this->validTagAttributes[$attribute])
353 && 'data-' != substr($attribute, 0, 5)
355 // Invalid attribute for the current tag
356 unset($attributes[$key]);
357 continue;
360 // Normalize attribute key, if needed
361 if ($attribute != $key) {
362 unset($attributes[$key]);
363 $attributes[$attribute] = $value;
366 // Normalize boolean attribute values
367 if (isset($this->booleanAttributes[$attribute])) {
368 $attributes[$attribute] = $this->prepareBooleanAttributeValue($attribute, $value);
372 return $attributes;
376 * Prepare a boolean attribute value
378 * Prepares the expected representation for the boolean attribute specified.
380 * @param string $attribute
381 * @param mixed $value
382 * @return string
384 protected function prepareBooleanAttributeValue($attribute, $value)
386 if (!is_bool($value) && in_array($value, $this->booleanAttributes[$attribute])) {
387 return $value;
390 $value = (bool) $value;
391 return ($value
392 ? $this->booleanAttributes[$attribute]['on']
393 : $this->booleanAttributes[$attribute]['off']