PageRenderTime 65ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/system/library/PEAR/HTML/QuickForm2/Element/Static.php

https://bitbucket.org/spekkionu/passworddb
PHP | 284 lines | 111 code | 20 blank | 153 comment | 15 complexity | 5627f7145791966941112bff876db6b1 MD5 | raw file
Possible License(s): BSD-2-Clause
  1. <?php
  2. /**
  3. * Class for static elements that only contain text or markup
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE:
  8. *
  9. * Copyright (c) 2006-2012, Alexey Borzov <avb@php.net>,
  10. * Bertrand Mansion <golgote@mamasam.com>
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * * Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * * Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. * * The names of the authors may not be used to endorse or promote products
  23. * derived from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  26. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  33. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @category HTML
  38. * @package HTML_QuickForm2
  39. * @author Alexey Borzov <avb@php.net>
  40. * @author Bertrand Mansion <golgote@mamasam.com>
  41. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  42. * @version SVN: $Id: Static.php 323363 2012-02-19 15:09:07Z avb $
  43. * @link http://pear.php.net/package/HTML_QuickForm2
  44. */
  45. /**
  46. * Base class for simple HTML_QuickForm2 elements (not Containers)
  47. */
  48. require_once 'HTML/QuickForm2/Element.php';
  49. /**
  50. * Class for static elements that only contain text or markup
  51. *
  52. * @category HTML
  53. * @package HTML_QuickForm2
  54. * @author Alexey Borzov <avb@php.net>
  55. * @author Bertrand Mansion <golgote@mamasam.com>
  56. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  57. * @version Release: 2.0.0
  58. * @link http://pear.php.net/package/HTML_QuickForm2
  59. */
  60. class HTML_QuickForm2_Element_Static extends HTML_QuickForm2_Element
  61. {
  62. /**
  63. * Name of the tag to wrap around static element's content
  64. * @var string
  65. */
  66. protected $tagName = null;
  67. /**
  68. * Whether to output closing tag when $tagName is set and element's content is empty
  69. * @var bool
  70. */
  71. protected $forceClosingTag = true;
  72. /**
  73. * Contains options and data used for the element creation
  74. * - content: Content of the static element
  75. * @var array
  76. */
  77. protected $data = array('content' => '');
  78. /**
  79. * Class constructor
  80. *
  81. * Static element can understand the following keys in $data parameter:
  82. * - 'content': content of the static element, e.g. text or markup
  83. * - 'tagName': name of the tag to wrap around content, e.g. 'div'.
  84. * Using tag names corresponding to form elements will cause an Exception
  85. * - 'forceClosingTag': whether to output closing tag in case of empty
  86. * content, &lt;foo&gt;&lt;/foo&gt; vs. &lt;foo /&gt;
  87. *
  88. * @param string $name Element name
  89. * @param string|array $attributes Attributes (either a string or an array)
  90. * @param array $data Additional element data
  91. */
  92. public function __construct($name = null, $attributes = null, array $data = array())
  93. {
  94. if (!empty($data['tagName'])) {
  95. $this->setTagName(
  96. $data['tagName'],
  97. !array_key_exists('forceClosingTag', $data) || $data['forceClosingTag']
  98. );
  99. }
  100. unset($data['tagName'], $data['forceClosingTag']);
  101. parent::__construct($name, $attributes, $data);
  102. }
  103. /**
  104. * Intercepts setting 'name' and 'id' attributes
  105. *
  106. * Overrides parent method to allow removal of 'name' attribute on Static
  107. * elements
  108. *
  109. * @param string $name Attribute name
  110. * @param string $value Attribute value, null if attribute is being removed
  111. *
  112. * @throws HTML_QuickForm2_InvalidArgumentException if trying to
  113. * remove a required attribute
  114. */
  115. protected function onAttributeChange($name, $value = null)
  116. {
  117. if ('name' == $name && null === $value) {
  118. unset($this->attributes['name']);
  119. } else {
  120. parent::onAttributeChange($name, $value);
  121. }
  122. }
  123. /**
  124. * Sets the element's name
  125. *
  126. * Passing null here will remove the name attribute
  127. *
  128. * @param string|null $name
  129. *
  130. * @return HTML_QuickForm2_Element_Static
  131. */
  132. public function setName($name)
  133. {
  134. if (null !== $name) {
  135. return parent::setName($name);
  136. } else {
  137. return $this->removeAttribute('name');
  138. }
  139. }
  140. public function getType()
  141. {
  142. return 'static';
  143. }
  144. /**
  145. * Static element can not be frozen
  146. *
  147. * @param bool $freeze Whether element should be frozen or editable. This
  148. * parameter is ignored in case of static elements
  149. *
  150. * @return bool Always returns false
  151. */
  152. public function toggleFrozen($freeze = null)
  153. {
  154. return false;
  155. }
  156. /**
  157. * Sets the contents of the static element
  158. *
  159. * @param string $content Static content
  160. *
  161. * @return HTML_QuickForm2_Element_Static
  162. */
  163. function setContent($content)
  164. {
  165. $this->data['content'] = $content;
  166. return $this;
  167. }
  168. /**
  169. * Returns the contents of the static element
  170. *
  171. * @return string
  172. */
  173. function getContent()
  174. {
  175. return $this->data['content'];
  176. }
  177. /**
  178. * Static element's content can also be set via this method
  179. *
  180. * @param mixed $value
  181. *
  182. * @return HTML_QuickForm2_Element_Static
  183. */
  184. public function setValue($value)
  185. {
  186. $this->setContent($value);
  187. return $this;
  188. }
  189. /**
  190. * Static elements have no value
  191. *
  192. * @return null
  193. */
  194. public function getRawValue()
  195. {
  196. return null;
  197. }
  198. public function __toString()
  199. {
  200. $prefix = $this->getIndent();
  201. if ($comment = $this->getComment()) {
  202. $prefix .= '<!-- ' . $comment . ' -->'
  203. . HTML_Common2::getOption('linebreak') . $this->getIndent();
  204. }
  205. if (!$this->tagName) {
  206. return $prefix . $this->getContent();
  207. } elseif ('' != $this->getContent()) {
  208. return $prefix . '<' . $this->tagName . $this->getAttributes(true)
  209. . '>' . $this->getContent() . '</' . $this->tagName . '>';
  210. } else {
  211. return $prefix . '<' . $this->tagName . $this->getAttributes(true)
  212. . ($this->forceClosingTag ? '></' . $this->tagName . '>' : ' />');
  213. }
  214. }
  215. public function getJavascriptValue($inContainer = false)
  216. {
  217. return '';
  218. }
  219. public function getJavascriptTriggers()
  220. {
  221. return array();
  222. }
  223. /**
  224. * Called when the element needs to update its value from form's data sources
  225. *
  226. * Static elements content can be updated with default form values.
  227. */
  228. protected function updateValue()
  229. {
  230. foreach ($this->getDataSources() as $ds) {
  231. if (!$ds instanceof HTML_QuickForm2_DataSource_Submit
  232. && null !== ($value = $ds->getValue($this->getName()))
  233. ) {
  234. $this->setContent($value);
  235. return;
  236. }
  237. }
  238. }
  239. /**
  240. * Sets the name of the HTML tag to wrap around static element's content
  241. *
  242. * @param string $name tag name
  243. * @param bool $forceClosing whether to output closing tag in case of empty contents
  244. *
  245. * @throws HTML_QuickForm2_InvalidArgumentException when trying to set a tag
  246. * name corresponding to a form element
  247. * @return HTML_QuickForm2_Element_Static
  248. */
  249. public function setTagName($name, $forceClosing = true)
  250. {
  251. // Prevent people shooting themselves in the proverbial foot
  252. if (in_array(strtolower($name),
  253. array('form', 'fieldset', 'button', 'input', 'select', 'textarea'))
  254. ) {
  255. throw new HTML_QuickForm2_InvalidArgumentException(
  256. "Do not use tag name '{$name}' with Static element, use proper element class"
  257. );
  258. }
  259. $this->tagName = (string)$name;
  260. $this->forceClosingTag = (bool)$forceClosing;
  261. return $this;
  262. }
  263. }
  264. ?>