group.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * HTML class for a form element group
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.01 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_01.txt If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category HTML
  15. * @package HTML_QuickForm
  16. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  17. * @author Bertrand Mansion <bmansion@mamasam.com>
  18. * @author Alexey Borzov <avb@php.net>
  19. * @copyright 2001-2009 The PHP Group
  20. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  21. * @version CVS: $Id: group.php,v 1.40 2009/04/04 21:34:03 avb Exp $
  22. * @link http://pear.php.net/package/HTML_QuickForm
  23. */
  24. /**
  25. * HTML class for a form element group
  26. *
  27. * @category HTML
  28. * @package HTML_QuickForm
  29. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  30. * @author Bertrand Mansion <bmansion@mamasam.com>
  31. * @author Alexey Borzov <avb@php.net>
  32. * @version Release: 3.2.11
  33. * @since 1.0
  34. */
  35. class HTML_QuickForm_group extends HTML_QuickForm_element
  36. {
  37. /**
  38. * Name of the element
  39. * @var string
  40. * @since 1.0
  41. * @access private
  42. */
  43. private $_name = '';
  44. /**
  45. * Array of grouped elements
  46. * @var array
  47. * @since 1.0
  48. * @access private
  49. */
  50. private $_elements = array();
  51. /**
  52. * String to separate elements
  53. * @var mixed
  54. * @since 2.5
  55. * @access public
  56. */
  57. public $_separator = null;
  58. /**
  59. * Required elements in this group
  60. * @var array
  61. * @since 2.5
  62. * @access private
  63. */
  64. private $_required = array();
  65. /**
  66. * Whether to change elements' names to $groupName[$elementName] or leave them as is
  67. * @var bool
  68. * @since 3.0
  69. * @access private
  70. */
  71. private $_appendName = true;
  72. /**
  73. * Class constructor
  74. *
  75. * @param string $elementName (optional)Group name
  76. * @param array $elementLabel (optional)Group label
  77. * @param array $elements (optional)Group elements
  78. * @param mixed $separator (optional)Use a string for one separator,
  79. * use an array to alternate the separators.
  80. * @param bool $appendName (optional)whether to change elements' names to
  81. * the form $groupName[$elementName] or leave
  82. * them as is.
  83. * @since 1.0
  84. * @access public
  85. * @return void
  86. */
  87. public function __construct(
  88. $elementName = null,
  89. $elementLabel = null,
  90. $elements = null,
  91. $separator = null,
  92. $appendName = true
  93. ) {
  94. parent::__construct($elementName, $elementLabel);
  95. $this->_type = 'group';
  96. if (isset($elements) && is_array($elements)) {
  97. $this->setElements($elements);
  98. }
  99. if (isset($separator)) {
  100. $this->_separator = $separator;
  101. } else {
  102. $this->_separator = '';
  103. }
  104. if (isset($appendName)) {
  105. $this->_appendName = $appendName;
  106. }
  107. }
  108. /**
  109. * Sets the group name
  110. *
  111. * @param string $name Group name
  112. * @since 1.0
  113. * @access public
  114. * @return void
  115. */
  116. function setName($name)
  117. {
  118. $this->_name = $name;
  119. }
  120. /**
  121. * Returns the group name
  122. *
  123. * @since 1.0
  124. * @access public
  125. * @return string
  126. */
  127. function getName()
  128. {
  129. return $this->_name;
  130. }
  131. /**
  132. * Sets values for group's elements
  133. *
  134. * @param mixed Values for group's elements
  135. * @since 1.0
  136. * @access public
  137. * @return void
  138. */
  139. function setValue($value)
  140. {
  141. $this->_createElementsIfNotExist();
  142. foreach (array_keys($this->_elements) as $key) {
  143. if (!$this->_appendName) {
  144. $v = $this->_elements[$key]->_findValue($value);
  145. if (null !== $v) {
  146. $this->_elements[$key]->onQuickFormEvent('setGroupValue', $v, $this);
  147. }
  148. } else {
  149. $elementName = $this->_elements[$key]->getName();
  150. $index = strlen($elementName) ? $elementName : $key;
  151. if (is_array($value)) {
  152. if (isset($value[$index])) {
  153. $this->_elements[$key]->onQuickFormEvent('setGroupValue', $value[$index], $this);
  154. }
  155. } elseif (isset($value)) {
  156. $this->_elements[$key]->onQuickFormEvent('setGroupValue', $value, $this);
  157. }
  158. }
  159. }
  160. }
  161. /**
  162. * Returns the value of the group
  163. *
  164. * @since 1.0
  165. * @access public
  166. * @return mixed
  167. */
  168. function getValue()
  169. {
  170. $value = null;
  171. foreach (array_keys($this->_elements) as $key) {
  172. $element =& $this->_elements[$key];
  173. switch ($element->getType()) {
  174. case 'radio':
  175. $v = $element->getChecked()? $element->getValue(): null;
  176. break;
  177. case 'checkbox':
  178. $v = $element->getChecked()? true: null;
  179. break;
  180. default:
  181. $v = $element->getValue();
  182. }
  183. if (null !== $v) {
  184. $elementName = $element->getName();
  185. if (is_null($elementName)) {
  186. $value = $v;
  187. } else {
  188. if (!is_array($value)) {
  189. $value = is_null($value)? array(): array($value);
  190. }
  191. if ('' === $elementName) {
  192. $value[] = $v;
  193. } else {
  194. $value[$elementName] = $v;
  195. }
  196. }
  197. }
  198. }
  199. return $value;
  200. }
  201. /**
  202. * Sets the grouped elements
  203. *
  204. * @param array $elements Array of elements
  205. * @since 1.1
  206. * @access public
  207. * @return void
  208. */
  209. function setElements($elements)
  210. {
  211. $this->_elements = array_values($elements);
  212. if ($this->_flagFrozen) {
  213. $this->freeze();
  214. }
  215. }
  216. /**
  217. * Gets the grouped elements
  218. *
  219. * @since 2.4
  220. * @access public
  221. * @return array
  222. */
  223. function &getElements()
  224. {
  225. $this->_createElementsIfNotExist();
  226. return $this->_elements;
  227. }
  228. /**
  229. * Gets the group type based on its elements
  230. * Will return 'mixed' if elements contained in the group
  231. * are of different types.
  232. *
  233. * @access public
  234. * @return string group elements type
  235. */
  236. function getGroupType()
  237. {
  238. $this->_createElementsIfNotExist();
  239. $prevType = '';
  240. foreach (array_keys($this->_elements) as $key) {
  241. $type = $this->_elements[$key]->getType();
  242. if ($type != $prevType && $prevType != '') {
  243. return 'mixed';
  244. }
  245. $prevType = $type;
  246. }
  247. return $type;
  248. }
  249. /**
  250. * Returns Html for the group
  251. *
  252. * @since 1.0
  253. * @access public
  254. * @return string
  255. */
  256. public function toHtml()
  257. {
  258. $renderer = new HTML_QuickForm_Renderer_Default();
  259. $renderer->setElementTemplate('{element}');
  260. $this->accept($renderer);
  261. return $renderer->toHtml();
  262. }
  263. /**
  264. * Returns the element name inside the group such as found in the html form
  265. *
  266. * @param mixed $index Element name or element index in the group
  267. * @since 3.0
  268. * @access public
  269. * @return mixed string with element name, false if not found
  270. */
  271. function getElementName($index)
  272. {
  273. $this->_createElementsIfNotExist();
  274. $elementName = false;
  275. if (is_int($index) && isset($this->_elements[$index])) {
  276. $elementName = $this->_elements[$index]->getName();
  277. if (isset($elementName) && $elementName == '') {
  278. $elementName = $index;
  279. }
  280. if ($this->_appendName) {
  281. if (is_null($elementName)) {
  282. $elementName = $this->getName();
  283. } else {
  284. $elementName = $this->getName().'['.$elementName.']';
  285. }
  286. }
  287. } elseif (is_string($index)) {
  288. foreach (array_keys($this->_elements) as $key) {
  289. $elementName = $this->_elements[$key]->getName();
  290. if ($index == $elementName) {
  291. if ($this->_appendName) {
  292. $elementName = $this->getName().'['.$elementName.']';
  293. }
  294. break;
  295. } elseif ($this->_appendName && $this->getName().'['.$elementName.']' == $index) {
  296. break;
  297. }
  298. }
  299. }
  300. return $elementName;
  301. }
  302. /**
  303. * Returns the value of field without HTML tags
  304. *
  305. * @since 1.3
  306. * @access public
  307. * @return string
  308. */
  309. function getFrozenHtml()
  310. {
  311. $flags = array();
  312. $this->_createElementsIfNotExist();
  313. foreach (array_keys($this->_elements) as $key) {
  314. if (false === ($flags[$key] = $this->_elements[$key]->isFrozen())) {
  315. $this->_elements[$key]->freeze();
  316. }
  317. }
  318. $html = $this->toHtml();
  319. foreach (array_keys($this->_elements) as $key) {
  320. if (!$flags[$key]) {
  321. $this->_elements[$key]->unfreeze();
  322. }
  323. }
  324. return $html;
  325. }
  326. /**
  327. * Called by HTML_QuickForm whenever form event is made on this element
  328. *
  329. * @param string $event Name of event
  330. * @param mixed $arg event arguments
  331. * @param object &$caller calling object
  332. * @since 1.0
  333. * @access public
  334. * @return void
  335. */
  336. public function onQuickFormEvent($event, $arg, &$caller)
  337. {
  338. switch ($event) {
  339. case 'updateValue':
  340. $this->_createElementsIfNotExist();
  341. foreach (array_keys($this->_elements) as $key) {
  342. if ($this->_appendName) {
  343. $elementName = $this->_elements[$key]->getName();
  344. if (is_null($elementName)) {
  345. $this->_elements[$key]->setName($this->getName());
  346. } elseif ('' === $elementName) {
  347. $this->_elements[$key]->setName($this->getName() . '[' . $key . ']');
  348. } else {
  349. $this->_elements[$key]->setName($this->getName() . '[' . $elementName . ']');
  350. }
  351. }
  352. $this->_elements[$key]->onQuickFormEvent('updateValue', $arg, $caller);
  353. if ($this->_appendName) {
  354. $this->_elements[$key]->setName($elementName);
  355. }
  356. }
  357. break;
  358. default:
  359. parent::onQuickFormEvent($event, $arg, $caller);
  360. }
  361. return true;
  362. }
  363. /**
  364. * Accepts a renderer
  365. *
  366. * @param HTML_QuickForm_Renderer renderer object
  367. * @param bool Whether a group is required
  368. * @param string An error message associated with a group
  369. * @access public
  370. * @return void
  371. */
  372. public function accept(&$renderer, $required = false, $error = null)
  373. {
  374. $this->_createElementsIfNotExist();
  375. $renderer->startGroup($this, $required, $error);
  376. $name = $this->getName();
  377. foreach (array_keys($this->_elements) as $key) {
  378. $element =& $this->_elements[$key];
  379. if ($this->_appendName) {
  380. $elementName = $element->getName();
  381. if (isset($elementName)) {
  382. $element->setName($name . '['. (strlen($elementName)? $elementName: $key) .']');
  383. } else {
  384. $element->setName($name);
  385. }
  386. }
  387. $required = !$element->isFrozen() && in_array($element->getName(), $this->_required);
  388. $element->accept($renderer, $required);
  389. // restore the element's name
  390. if ($this->_appendName) {
  391. $element->setName($elementName);
  392. }
  393. }
  394. $renderer->finishGroup($this);
  395. }
  396. /**
  397. * As usual, to get the group's value we access its elements and call
  398. * their exportValue() methods
  399. */
  400. function exportValue(&$submitValues, $assoc = false)
  401. {
  402. $value = null;
  403. foreach (array_keys($this->_elements) as $key) {
  404. $elementName = $this->_elements[$key]->getName();
  405. if ($this->_appendName) {
  406. if (is_null($elementName)) {
  407. $this->_elements[$key]->setName($this->getName());
  408. } elseif ('' === $elementName) {
  409. $this->_elements[$key]->setName($this->getName() . '[' . $key . ']');
  410. } else {
  411. $this->_elements[$key]->setName($this->getName() . '[' . $elementName . ']');
  412. }
  413. }
  414. $v = $this->_elements[$key]->exportValue($submitValues, $assoc);
  415. if ($this->_appendName) {
  416. $this->_elements[$key]->setName($elementName);
  417. }
  418. if (null !== $v) {
  419. // Make $value an array, we will use it like one
  420. if (null === $value) {
  421. $value = array();
  422. }
  423. if ($assoc) {
  424. // just like HTML_QuickForm::exportValues()
  425. $value = HTML_QuickForm::arrayMerge($value, $v);
  426. } else {
  427. // just like getValue(), but should work OK every time here
  428. if (is_null($elementName)) {
  429. $value = $v;
  430. } elseif ('' === $elementName) {
  431. $value[] = $v;
  432. } else {
  433. $value[$elementName] = $v;
  434. }
  435. }
  436. }
  437. }
  438. // do not pass the value through _prepareValue, we took care of this already
  439. return $value;
  440. }
  441. /**
  442. * Creates the group's elements.
  443. *
  444. * This should be overriden by child classes that need to create their
  445. * elements. The method will be called automatically when needed, calling
  446. * it from the constructor is discouraged as the constructor is usually
  447. * called _twice_ on element creation, first time with _no_ parameters.
  448. *
  449. * @access private
  450. * @abstract
  451. */
  452. function _createElements()
  453. {
  454. // abstract
  455. }
  456. /**
  457. * A wrapper around _createElements()
  458. *
  459. * This method calls _createElements() if the group's _elements array
  460. * is empty. It also performs some updates, e.g. freezes the created
  461. * elements if the group is already frozen.
  462. *
  463. * @access private
  464. */
  465. function _createElementsIfNotExist()
  466. {
  467. if (empty($this->_elements)) {
  468. $this->_createElements();
  469. if ($this->_flagFrozen) {
  470. $this->freeze();
  471. }
  472. }
  473. }
  474. function freeze()
  475. {
  476. parent::freeze();
  477. foreach (array_keys($this->_elements) as $key) {
  478. $this->_elements[$key]->freezeSeeOnlySelected = $this->freezeSeeOnlySelected;
  479. $this->_elements[$key]->freeze();
  480. }
  481. }
  482. function unfreeze()
  483. {
  484. parent::unfreeze();
  485. foreach (array_keys($this->_elements) as $key) {
  486. $this->_elements[$key]->unfreeze();
  487. }
  488. }
  489. function setPersistantFreeze($persistant = false)
  490. {
  491. parent::setPersistantFreeze($persistant);
  492. foreach (array_keys($this->_elements) as $key) {
  493. $this->_elements[$key]->setPersistantFreeze($persistant);
  494. }
  495. }
  496. /**
  497. * @param string $layout
  498. *
  499. * @return string
  500. */
  501. public function getTemplate($layout)
  502. {
  503. $size = $this->calculateSize();
  504. switch ($layout) {
  505. case FormValidator::LAYOUT_INLINE:
  506. return '
  507. <div class="input-group">
  508. <label {label-for} >
  509. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  510. {label}
  511. </label>
  512. </div>
  513. <div class="input-group {error_class}">
  514. {element}
  515. </div>
  516. ';
  517. break;
  518. case FormValidator::LAYOUT_HORIZONTAL:
  519. return '
  520. <div class="form-group {error_class}" id="'.$this->getName().'-group">
  521. <label {label-for} class="col-sm-'.$size[0].' control-label {extra_label_class}" >
  522. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  523. {label}
  524. </label>
  525. <div class="col-sm-'.$size[1].'">
  526. {icon}
  527. {element}
  528. <!-- BEGIN label_2 -->
  529. <p class="help-block">{label_2}</p>
  530. <!-- END label_2 -->
  531. <!-- BEGIN error -->
  532. <span class="help-inline help-block">{error}</span>
  533. <!-- END error -->
  534. </div>
  535. <div class="col-sm-'.$size[2].'">
  536. <!-- BEGIN label_3 -->
  537. {label_3}
  538. <!-- END label_3 -->
  539. </div>
  540. </div>';
  541. break;
  542. case FormValidator::LAYOUT_BOX_NO_LABEL:
  543. return '
  544. <div class="input-group">
  545. {icon}
  546. {element}
  547. </div>';
  548. break;
  549. }
  550. }
  551. }