PageRenderTime 122ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/Nette/Forms/Controls/RadioList.php

https://github.com/DocX/nette
PHP | 197 lines | 78 code | 46 blank | 73 comment | 10 complexity | b71f6203e93d4f5589d754ef14640e7c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  6. *
  7. * This source file is subject to the "Nette license" that is bundled
  8. * with this package in the file license.txt.
  9. *
  10. * For more information please see http://nettephp.com
  11. *
  12. * @copyright Copyright (c) 2004, 2009 David Grudl
  13. * @license http://nettephp.com/license Nette license
  14. * @link http://nettephp.com
  15. * @category Nette
  16. * @package Nette\Forms
  17. */
  18. /*namespace Nette\Forms;*/
  19. require_once dirname(__FILE__) . '/../../Forms/Controls/FormControl.php';
  20. /**
  21. * Set of radio button controls.
  22. *
  23. * @author David Grudl
  24. * @copyright Copyright (c) 2004, 2009 David Grudl
  25. * @package Nette\Forms
  26. *
  27. * @property array $items
  28. * @property-read Nette\Web\Html $separatorPrototype
  29. * @property-read Nette\Web\Html $containerPrototype
  30. */
  31. class RadioList extends FormControl
  32. {
  33. /** @var Nette\Web\Html separator element template */
  34. protected $separator;
  35. /** @var Nette\Web\Html container element template */
  36. protected $container;
  37. /** @var array */
  38. protected $items = array();
  39. /**
  40. * @param string label
  41. * @param array options from which to choose
  42. */
  43. public function __construct($label = NULL, array $items = NULL)
  44. {
  45. parent::__construct($label);
  46. $this->control->type = 'radio';
  47. $this->container = /*Nette\Web\*/Html::el();
  48. $this->separator = /*Nette\Web\*/Html::el('br');
  49. if ($items !== NULL) $this->setItems($items);
  50. }
  51. /**
  52. * Returns selected radio value.
  53. * @param bool
  54. * @return mixed
  55. */
  56. public function getValue($raw = FALSE)
  57. {
  58. return is_scalar($this->value) && ($raw || isset($this->items[$this->value])) ? $this->value : NULL;
  59. }
  60. /**
  61. * Sets options from which to choose.
  62. * @param array
  63. * @return RadioList provides a fluent interface
  64. */
  65. public function setItems(array $items)
  66. {
  67. $this->items = $items;
  68. return $this;
  69. }
  70. /**
  71. * Returns options from which to choose.
  72. * @return array
  73. */
  74. final public function getItems()
  75. {
  76. return $this->items;
  77. }
  78. /**
  79. * Returns separator HTML element template.
  80. * @return Nette\Web\Html
  81. */
  82. final public function getSeparatorPrototype()
  83. {
  84. return $this->separator;
  85. }
  86. /**
  87. * Returns container HTML element template.
  88. * @return Nette\Web\Html
  89. */
  90. final public function getContainerPrototype()
  91. {
  92. return $this->container;
  93. }
  94. /**
  95. * Generates control's HTML element.
  96. * @param mixed
  97. * @return Nette\Web\Html
  98. */
  99. public function getControl($key = NULL)
  100. {
  101. if ($key === NULL) {
  102. $container = clone $this->container;
  103. $separator = (string) $this->separator;
  104. } elseif (!isset($this->items[$key])) {
  105. return NULL;
  106. }
  107. $control = parent::getControl();
  108. $id = $control->id;
  109. $counter = -1;
  110. $value = $this->value === NULL ? NULL : (string) $this->getValue();
  111. $label = /*Nette\Web\*/Html::el('label');
  112. foreach ($this->items as $k => $val) {
  113. $counter++;
  114. if ($key !== NULL && $key != $k) continue; // intentionally ==
  115. $control->id = $label->for = $id . '-' . $counter;
  116. $control->checked = (string) $k === $value;
  117. $control->value = $k;
  118. if ($val instanceof /*Nette\Web\*/Html) {
  119. $label->setHtml($val);
  120. } else {
  121. $label->setText($this->translate($val));
  122. }
  123. if ($key !== NULL) {
  124. return (string) $control . (string) $label;
  125. }
  126. $container->add((string) $control . (string) $label . $separator);
  127. // TODO: separator after last item?
  128. }
  129. return $container;
  130. }
  131. /**
  132. * Generates label's HTML element.
  133. * @param string
  134. * @return void
  135. */
  136. public function getLabel($caption = NULL)
  137. {
  138. $label = parent::getLabel($caption);
  139. $label->for = NULL;
  140. return $label;
  141. }
  142. /**
  143. * Filled validator: has been any radio button selected?
  144. * @param IFormControl
  145. * @return bool
  146. */
  147. public static function validateFilled(IFormControl $control)
  148. {
  149. return $control->getValue() !== NULL;
  150. }
  151. }