PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/system/library/PEAR/HTML/QuickForm2/Container/Group.php

https://bitbucket.org/spekkionu/passworddb
PHP | 356 lines | 184 code | 33 blank | 139 comment | 25 complexity | 36fc5830e10a095d746a753dc9d77e74 MD5 | raw file
Possible License(s): BSD-2-Clause
  1. <?php
  2. /**
  3. * Base class for HTML_QuickForm2 groups
  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: Group.php 326287 2012-06-21 18:44:44Z avb $
  43. * @link http://pear.php.net/package/HTML_QuickForm2
  44. */
  45. /**
  46. * Base class for all HTML_QuickForm2 containers
  47. */
  48. require_once 'HTML/QuickForm2/Container.php';
  49. /**
  50. * Base class for QuickForm2 groups of elements
  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_Container_Group extends HTML_QuickForm2_Container
  61. {
  62. /**
  63. * Group name
  64. * If set, group name will be used as prefix for contained
  65. * element names, like groupname[elementname].
  66. * @var string
  67. */
  68. protected $name;
  69. /**
  70. * Previous group name
  71. * Stores the previous group name when the group name is changed.
  72. * Used to restore children names if necessary.
  73. * @var string
  74. */
  75. protected $previousName;
  76. public function getType()
  77. {
  78. return 'group';
  79. }
  80. protected function prependsName()
  81. {
  82. return strlen($this->name) > 0;
  83. }
  84. protected function getChildValues($filtered = false)
  85. {
  86. $value = parent::getChildValues($filtered);
  87. if (!$this->prependsName()) {
  88. return $value;
  89. } elseif (!strpos($this->getName(), '[')) {
  90. return isset($value[$this->getName()])? $value[$this->getName()]: null;
  91. } else {
  92. $tokens = explode('[', str_replace(']', '', $this->getName()));
  93. $valueAry =& $value;
  94. do {
  95. $token = array_shift($tokens);
  96. if (!isset($valueAry[$token])) {
  97. return null;
  98. }
  99. $valueAry =& $valueAry[$token];
  100. } while ($tokens);
  101. return $valueAry;
  102. }
  103. }
  104. public function setValue($value)
  105. {
  106. // Prepare a mapper for element names as array
  107. if ($this->prependsName()) {
  108. $prefix = explode('[', str_replace(']', '', $this->getName()));
  109. }
  110. $elements = array();
  111. foreach ($this as $child) {
  112. $tokens = explode('[', str_replace(']', '', $child->getName()));
  113. if (!empty($prefix)) {
  114. $tokens = array_slice($tokens, count($prefix));
  115. }
  116. $elements[] = $tokens;
  117. }
  118. // Iterate over values to find corresponding element
  119. $index = 0;
  120. foreach ($value as $k => $v) {
  121. $val = array($k => $v);
  122. foreach ($elements as $i => $tokens) {
  123. do {
  124. $token = array_shift($tokens);
  125. $numeric = false;
  126. if ($token == "") {
  127. // special case for a group of checkboxes
  128. if (empty($tokens) && is_array($val)
  129. && $this->elements[$i] instanceof HTML_QuickForm2_Element_InputCheckbox
  130. ) {
  131. if (in_array($this->elements[$i]->getAttribute('value'),
  132. array_map('strval', $val), true)
  133. ) {
  134. $this->elements[$i]->setAttribute('checked');
  135. // don't want to remove 'checked' on next iteration
  136. unset($elements[$i]);
  137. } else {
  138. $this->elements[$i]->removeAttribute('checked');
  139. }
  140. continue 2;
  141. }
  142. // Deal with numeric indexes in values
  143. $token = $index;
  144. $numeric = true;
  145. }
  146. if (!isset($val[$token])) {
  147. // Not found, skip next iterations
  148. continue 2;
  149. } else {
  150. // Found a value
  151. $val = $val[$token];
  152. if ($numeric) {
  153. $index += 1;
  154. }
  155. }
  156. } while (!empty($tokens));
  157. // Found a value corresponding to element name
  158. $child = $this->elements[$i];
  159. $child->setValue($val);
  160. unset($val);
  161. if (!($child instanceof HTML_QuickForm2_Container_Group)) {
  162. // Speed up next iterations
  163. unset($elements[$i]);
  164. }
  165. break;
  166. }
  167. }
  168. return $this;
  169. }
  170. public function getName()
  171. {
  172. return $this->name;
  173. }
  174. public function setName($name)
  175. {
  176. $this->previousName = $this->name;
  177. $this->name = $name;
  178. foreach ($this as $child) {
  179. $this->renameChild($child);
  180. }
  181. return $this;
  182. }
  183. /**
  184. * Prepends group's name to contained element's name
  185. *
  186. * Used when adding an element to the group or changing group's name
  187. *
  188. * @param HTML_QuickForm2_Node $element
  189. *
  190. * @return HTML_QuickForm2_Node
  191. */
  192. protected function renameChild(HTML_QuickForm2_Node $element)
  193. {
  194. $tokens = explode('[', str_replace(']', '', $element->getName()));
  195. // Child has already been renamed by its group before
  196. if ($this === $element->getContainer() && strlen($this->previousName)) {
  197. $gtokens = explode('[', str_replace(']', '', $this->previousName));
  198. if ($gtokens === array_slice($tokens, 0, count($gtokens))) {
  199. array_splice($tokens, 0, count($gtokens));
  200. }
  201. }
  202. if (strlen($this->name)) {
  203. $element->setName($this->name . '[' . implode('][', $tokens) . ']');
  204. } elseif (strlen($this->previousName)) {
  205. $elname = array_shift($tokens);
  206. foreach ($tokens as $token) {
  207. $elname .= '[' . $token . ']';
  208. }
  209. $element->setName($elname);
  210. }
  211. return $element;
  212. }
  213. /**
  214. * Appends an element to the container
  215. *
  216. * If the element was previously added to the container or to another
  217. * container, it is first removed there.
  218. *
  219. * @param HTML_QuickForm2_Node $element Element to add
  220. *
  221. * @return HTML_QuickForm2_Node Added element
  222. * @throws HTML_QuickForm2_InvalidArgumentException
  223. */
  224. public function appendChild(HTML_QuickForm2_Node $element)
  225. {
  226. if (null !== ($container = $element->getContainer())) {
  227. $container->removeChild($element);
  228. }
  229. // Element can be renamed only after being removed from container
  230. $this->renameChild($element);
  231. $element->setContainer($this);
  232. $this->elements[] = $element;
  233. return $element;
  234. }
  235. /**
  236. * Removes the element from this container
  237. *
  238. * If the reference object is not given, the element will be appended.
  239. *
  240. * @param HTML_QuickForm2_Node $element Element to remove
  241. *
  242. * @return HTML_QuickForm2_Node Removed object
  243. */
  244. public function removeChild(HTML_QuickForm2_Node $element)
  245. {
  246. $element = parent::removeChild($element);
  247. if ($this->prependsName()) {
  248. $name = preg_replace(
  249. '/^' . preg_quote($this->getName(), '/') . '\[([^\]]*)\]/',
  250. '\1', $element->getName()
  251. );
  252. $element->setName($name);
  253. }
  254. return $element;
  255. }
  256. /**
  257. * Inserts an element in the container
  258. *
  259. * If the reference object is not given, the element will be appended.
  260. *
  261. * @param HTML_QuickForm2_Node $element Element to insert
  262. * @param HTML_QuickForm2_Node $reference Reference to insert before
  263. *
  264. * @return HTML_QuickForm2_Node Inserted element
  265. */
  266. public function insertBefore(HTML_QuickForm2_Node $element, HTML_QuickForm2_Node $reference = null)
  267. {
  268. if (null === $reference) {
  269. return $this->appendChild($element);
  270. }
  271. return parent::insertBefore($this->renameChild($element), $reference);
  272. }
  273. /**
  274. * Sets string(s) to separate grouped elements
  275. *
  276. * @param string|array $separator Use a string for one separator, array for
  277. * alternating separators
  278. *
  279. * @return HTML_QuickForm2_Container_Group
  280. */
  281. public function setSeparator($separator)
  282. {
  283. $this->data['separator'] = $separator;
  284. return $this;
  285. }
  286. /**
  287. * Returns string(s) to separate grouped elements
  288. *
  289. * @return string|array Separator, null if not set
  290. */
  291. public function getSeparator()
  292. {
  293. return isset($this->data['separator'])? $this->data['separator']: null;
  294. }
  295. /**
  296. * Renders the group using the given renderer
  297. *
  298. * @param HTML_QuickForm2_Renderer $renderer
  299. *
  300. * @return HTML_QuickForm2_Renderer
  301. */
  302. public function render(HTML_QuickForm2_Renderer $renderer)
  303. {
  304. $renderer->startGroup($this);
  305. foreach ($this as $element) {
  306. $element->render($renderer);
  307. }
  308. $this->renderClientRules($renderer->getJavascriptBuilder());
  309. $renderer->finishGroup($this);
  310. return $renderer;
  311. }
  312. public function __toString()
  313. {
  314. HTML_QuickForm2_Loader::loadClass('HTML_QuickForm2_Renderer');
  315. $renderer = $this->render(
  316. HTML_QuickForm2_Renderer::factory('default')
  317. ->setTemplateForId($this->getId(), '{content}')
  318. );
  319. return $renderer->__toString()
  320. . $renderer->getJavascriptBuilder()->getSetupCode(null, true);
  321. }
  322. }
  323. ?>