PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/HTML/QuickForm2/Container/Group.php

https://github.com/CodeYellowBV/piwik
PHP | 328 lines | 176 code | 29 blank | 123 comment | 26 complexity | af6598a96ab2505936156cf3c82eb231 MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Base class for HTML_QuickForm2 groups
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE:
  8. *
  9. * Copyright (c) 2006-2010, 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 294057 2010-01-26 21:10:28Z 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. * @version Release: @package_version@
  57. */
  58. class HTML_QuickForm2_Container_Group extends HTML_QuickForm2_Container
  59. {
  60. /**
  61. * Group name
  62. * If set, group name will be used as prefix for contained
  63. * element names, like groupname[elementname].
  64. * @var string
  65. */
  66. protected $name;
  67. /**
  68. * Previous group name
  69. * Stores the previous group name when the group name is changed.
  70. * Used to restore children names if necessary.
  71. * @var string
  72. */
  73. protected $previousName;
  74. public function getType()
  75. {
  76. return 'group';
  77. }
  78. protected function prependsName()
  79. {
  80. return strlen($this->name) > 0;
  81. }
  82. public function getValue()
  83. {
  84. $value = parent::getValue();
  85. if (!$this->prependsName()) {
  86. return $value;
  87. } elseif (!strpos($this->getName(), '[')) {
  88. return isset($value[$this->getName()])? $value[$this->getName()]: null;
  89. } else {
  90. $tokens = explode('[', str_replace(']', '', $this->getName()));
  91. $valueAry =& $value;
  92. do {
  93. $token = array_shift($tokens);
  94. if (!isset($valueAry[$token])) {
  95. return null;
  96. }
  97. $valueAry =& $valueAry[$token];
  98. } while ($tokens);
  99. return $valueAry;
  100. }
  101. }
  102. public function setValue($value)
  103. {
  104. // Prepare a mapper for element names as array
  105. if ($this->prependsName()) {
  106. $prefix = explode('[', str_replace(']', '', $this->getName()));
  107. }
  108. $elements = array();
  109. foreach ($this as $child) {
  110. $tokens = explode('[', str_replace(']', '', $child->getName()));
  111. if (!empty($prefix)) {
  112. $tokens = array_slice($tokens, count($prefix));
  113. }
  114. $elements[] = $tokens;
  115. }
  116. // Iterate over values to find corresponding element
  117. $index = 0;
  118. foreach ($value as $k => $v) {
  119. $val = array($k => $v);
  120. $found = null;
  121. foreach ($elements as $i => $tokens) {
  122. do {
  123. $token = array_shift($tokens);
  124. $numeric = false;
  125. if ($token == "") {
  126. // Deal with numeric indexes in values
  127. $token = $index;
  128. $numeric = true;
  129. }
  130. if (isset($val[$token])) {
  131. // Found a value
  132. $val = $val[$token];
  133. $found = $val;
  134. if ($numeric) {
  135. $index += 1;
  136. }
  137. } else {
  138. // Not found, skip next iterations
  139. $found = null;
  140. break;
  141. }
  142. } while (!empty($tokens));
  143. if (!is_null($found)) {
  144. // Found a value corresponding to element name
  145. $child = $this->elements[$i];
  146. $child->setValue($val);
  147. unset($val);
  148. if (!($child instanceof HTML_QuickForm2_Container_Group)) {
  149. // Speed up next iterations
  150. unset($elements[$i]);
  151. }
  152. break;
  153. }
  154. }
  155. }
  156. }
  157. public function getName()
  158. {
  159. return $this->name;
  160. }
  161. public function setName($name)
  162. {
  163. $this->previousName = $this->name;
  164. $this->name = $name;
  165. foreach ($this as $child) {
  166. $this->renameChild($child);
  167. }
  168. return $this;
  169. }
  170. protected function renameChild(HTML_QuickForm2_Node $element)
  171. {
  172. $tokens = explode('[', str_replace(']', '', $element->getName()));
  173. if ($this === $element->getContainer()) {
  174. // Child has already been renamed by its group before
  175. if (!is_null($this->previousName) &&
  176. $this->previousName !== '') {
  177. $gtokens = explode('[', str_replace(']', '', $this->previousName));
  178. $pos = array_search(end($gtokens), $tokens);
  179. if (!is_null($pos)) {
  180. $tokens = array_slice($tokens, $pos+1);
  181. }
  182. }
  183. }
  184. if (is_null($this->name) || $this->name === '') {
  185. if (is_null($this->previousName) || $this->previousName === '') {
  186. return $element;
  187. } else {
  188. $elname = $tokens[0];
  189. unset($tokens[0]);
  190. foreach ($tokens as $v) {
  191. $elname .= '['.$v.']';
  192. }
  193. }
  194. } else {
  195. $elname = $this->getName().'['.implode('][', $tokens).']';
  196. }
  197. $element->setName($elname);
  198. return $element;
  199. }
  200. /**
  201. * Appends an element to the container
  202. *
  203. * If the element was previously added to the container or to another
  204. * container, it is first removed there.
  205. *
  206. * @param HTML_QuickForm2_Node Element to add
  207. * @return HTML_QuickForm2_Node Added element
  208. * @throws HTML_QuickForm2_InvalidArgumentException
  209. */
  210. public function appendChild(HTML_QuickForm2_Node $element)
  211. {
  212. if (null !== ($container = $element->getContainer())) {
  213. $container->removeChild($element);
  214. }
  215. // Element can be renamed only after being removed from container
  216. $this->renameChild($element);
  217. $element->setContainer($this);
  218. $this->elements[] = $element;
  219. return $element;
  220. }
  221. /**
  222. * Removes the element from this container
  223. *
  224. * If the reference object is not given, the element will be appended.
  225. *
  226. * @param HTML_QuickForm2_Node Element to remove
  227. * @return HTML_QuickForm2_Node Removed object
  228. */
  229. public function removeChild(HTML_QuickForm2_Node $element)
  230. {
  231. $element = parent::removeChild($element);
  232. if ($this->prependsName()) {
  233. $name = preg_replace('/^' . $this->getName() . '\[([^\]]*)\]/', '\1', $element->getName());
  234. $element->setName($name);
  235. }
  236. return $element;
  237. }
  238. /**
  239. * Inserts an element in the container
  240. *
  241. * If the reference object is not given, the element will be appended.
  242. *
  243. * @param HTML_QuickForm2_Node Element to insert
  244. * @param HTML_QuickForm2_Node Reference to insert before
  245. * @return HTML_QuickForm2_Node Inserted element
  246. */
  247. public function insertBefore(HTML_QuickForm2_Node $element, HTML_QuickForm2_Node $reference = null)
  248. {
  249. if (null === $reference) {
  250. return $this->appendChild($element);
  251. }
  252. return parent::insertBefore($this->renameChild($element), $reference);
  253. }
  254. /**
  255. * Sets string(s) to separate grouped elements
  256. *
  257. * @param string|array Use a string for one separator, array for
  258. * alternating separators
  259. * @return HTML_QuickForm2_Container_Group
  260. */
  261. public function setSeparator($separator)
  262. {
  263. $this->data['separator'] = $separator;
  264. return $this;
  265. }
  266. /**
  267. * Returns string(s) to separate grouped elements
  268. *
  269. * @return string|array Separator, null if not set
  270. */
  271. public function getSeparator()
  272. {
  273. return isset($this->data['separator'])? $this->data['separator']: null;
  274. }
  275. /**
  276. * Renders the group using the given renderer
  277. *
  278. * @param HTML_QuickForm2_Renderer Renderer instance
  279. * @return HTML_QuickForm2_Renderer
  280. */
  281. public function render(HTML_QuickForm2_Renderer $renderer)
  282. {
  283. $renderer->startGroup($this);
  284. foreach ($this as $element) {
  285. $element->render($renderer);
  286. }
  287. $renderer->finishGroup($this);
  288. return $renderer;
  289. }
  290. public function __toString()
  291. {
  292. // require_once 'HTML/QuickForm2/Renderer.php';
  293. return $this->render(
  294. HTML_QuickForm2_Renderer::factory('default')
  295. ->setTemplateForId($this->getId(), '{content}')
  296. )->__toString();
  297. }
  298. }
  299. ?>