PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/typo3/sysext/fluid/Classes/ViewHelpers/Form/SelectViewHelper.php

https://bitbucket.org/linxpinx/mercurial
PHP | 278 lines | 132 code | 21 blank | 125 comment | 31 complexity | d6d135f96e314901fee533249ce94609 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, Unlicense, LGPL-2.1, Apache-2.0
  1. <?php
  2. /* *
  3. * This script belongs to the FLOW3 package "Fluid". *
  4. * *
  5. * It is free software; you can redistribute it and/or modify it under *
  6. * the terms of the GNU Lesser General Public License as published by the *
  7. * Free Software Foundation, either version 3 of the License, or (at your *
  8. * option) any later version. *
  9. * *
  10. * This script is distributed in the hope that it will be useful, but *
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
  12. * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
  13. * General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU Lesser General Public *
  16. * License along with the script. *
  17. * If not, see http://www.gnu.org/licenses/lgpl.html *
  18. * *
  19. * The TYPO3 project - inspiring people to share! *
  20. * */
  21. /**
  22. * This view helper generates a <select> dropdown list for the use with a form.
  23. *
  24. * = Basic usage =
  25. *
  26. * The most straightforward way is to supply an associative array as the "options" parameter.
  27. * The array key is used as option key, and the value is used as human-readable name.
  28. *
  29. * <code title="Basic usage">
  30. * <f:form.select name="paymentOptions" options="{payPal: 'PayPal International Services', visa: 'VISA Card'}" />
  31. * </code>
  32. *
  33. * = Pre-select a value =
  34. *
  35. * To pre-select a value, set "value" to the option key which should be selected.
  36. * <code title="Default value">
  37. * <f:form.select name="paymentOptions" options="{payPal: 'PayPal International Services', visa: 'VISA Card'}" value="visa" />
  38. * </code>
  39. * Generates a dropdown box like above, except that "VISA Card" is selected.
  40. *
  41. * If the select box is a multi-select box (multiple="true"), then "value" can be an array as well.
  42. *
  43. * = Usage on domain objects =
  44. *
  45. * If you want to output domain objects, you can just pass them as array into the "options" parameter.
  46. * To define what domain object value should be used as option key, use the "optionValueField" variable. Same goes for optionLabelField.
  47. * If neither is given, the Identifier (UID/uid) and the __toString() method are tried as fallbacks.
  48. *
  49. * If the optionValueField variable is set, the getter named after that value is used to retrieve the option key.
  50. * If the optionLabelField variable is set, the getter named after that value is used to retrieve the option value.
  51. *
  52. * <code title="Domain objects">
  53. * <f:form.select name="users" options="{userArray}" optionValueField="id" optionLabelField="firstName" />
  54. * </code>
  55. * In the above example, the userArray is an array of "User" domain objects, with no array key specified.
  56. *
  57. * So, in the above example, the method $user->getId() is called to retrieve the key, and $user->getFirstName() to retrieve the displayed value of each entry.
  58. *
  59. * The "value" property now expects a domain object, and tests for object equivalence.
  60. *
  61. * @version $Id: SelectViewHelper.php 2043 2010-03-16 08:49:45Z sebastian $
  62. * @package Fluid
  63. * @subpackage ViewHelpers\Form
  64. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
  65. * @api
  66. * @scope prototype
  67. */
  68. class Tx_Fluid_ViewHelpers_Form_SelectViewHelper extends Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper {
  69. /**
  70. * @var string
  71. */
  72. protected $tagName = 'select';
  73. /**
  74. * @var mixed the selected value
  75. */
  76. protected $selectedValue = NULL;
  77. /**
  78. * Initialize arguments.
  79. *
  80. * @return void
  81. * @author Sebastian Kurf?rst <sebastian@typo3.org>
  82. * @api
  83. */
  84. public function initializeArguments() {
  85. parent::initializeArguments();
  86. $this->registerUniversalTagAttributes();
  87. $this->registerTagAttribute('multiple', 'string', 'if set, multiple select field');
  88. $this->registerTagAttribute('size', 'string', 'Size of input field');
  89. $this->registerTagAttribute('disabled', 'string', 'Specifies that the input element should be disabled when the page loads');
  90. $this->registerArgument('options', 'array', 'Associative array with internal IDs as key, and the values are displayed in the select box', TRUE);
  91. $this->registerArgument('optionValueField', 'string', 'If specified, will call the appropriate getter on each object to determine the value.');
  92. $this->registerArgument('optionLabelField', 'string', 'If specified, will call the appropriate getter on each object to determine the label.');
  93. $this->registerArgument('sortByOptionLabel', 'boolean', 'If true, List will be sorted by label.', FALSE, FALSE);
  94. $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this view helper', FALSE, 'f3-form-error');
  95. }
  96. /**
  97. * Render the tag.
  98. *
  99. * @return string rendered tag.
  100. * @author Sebastian Kurf?rst <sebastian@typo3.org>
  101. * @author Bastian Waidelich <bastian@typo3.org>
  102. * @api
  103. */
  104. public function render() {
  105. $name = $this->getName();
  106. if ($this->arguments->hasArgument('multiple')) {
  107. $name .= '[]';
  108. }
  109. $this->tag->addAttribute('name', $name);
  110. $options = $this->getOptions();
  111. if (empty($options)) {
  112. $options = array('' => '');
  113. }
  114. $this->tag->setContent($this->renderOptionTags($options));
  115. $this->setErrorClassAttribute();
  116. // register field name for token generation.
  117. // in case it is a multi-select, we need to register the field name
  118. // as often as there are elements in the box
  119. if ($this->arguments->hasArgument('multiple')) {
  120. for ($i=0; $i<count($options); $i++) {
  121. $this->registerFieldNameForFormTokenGeneration($name);
  122. }
  123. } else {
  124. $this->registerFieldNameForFormTokenGeneration($name);
  125. }
  126. return $this->tag->render();
  127. }
  128. /**
  129. * Render the option tags.
  130. *
  131. * @param array $options the options for the form.
  132. * @return string rendered tags.
  133. * @author Bastian Waidelich <bastian@typo3.org>
  134. */
  135. protected function renderOptionTags($options) {
  136. $output = '';
  137. foreach ($options as $value => $label) {
  138. $isSelected = $this->isSelected($value);
  139. $output.= $this->renderOptionTag($value, $label, $isSelected) . chr(10);
  140. }
  141. return $output;
  142. }
  143. /**
  144. * Render the option tags.
  145. *
  146. * @return array an associative array of options, key will be the value of the option tag
  147. * @author Bastian Waidelich <bastian@typo3.org>
  148. * @author Karsten Dambekalns <karsten@typo3.org>
  149. */
  150. protected function getOptions() {
  151. if (!is_array($this->arguments['options']) && !($this->arguments['options'] instanceof Traversable)) {
  152. return array();
  153. }
  154. $options = array();
  155. foreach ($this->arguments['options'] as $key => $value) {
  156. if (is_object($value)) {
  157. if ($this->arguments->hasArgument('optionValueField')) {
  158. $key = Tx_Extbase_Reflection_ObjectAccess::getProperty($value, $this->arguments['optionValueField']);
  159. if (is_object($key)) {
  160. if (method_exists($key, '__toString')) {
  161. $key = (string)$key;
  162. } else {
  163. throw new Tx_Fluid_Core_ViewHelper_Exception('Identifying value for object of class "' . get_class($value) . '" was an object.' , 1247827428);
  164. }
  165. }
  166. } elseif ($this->persistenceManager->getBackend()->getIdentifierByObject($value) !== NULL) {
  167. $key = $this->persistenceManager->getBackend()->getIdentifierByObject($value);
  168. } elseif (method_exists($value, '__toString')) {
  169. $key = (string)$value;
  170. } else {
  171. throw new Tx_Fluid_Core_ViewHelper_Exception('No identifying value for object of class "' . get_class($value) . '" found.' , 1247826696);
  172. }
  173. if ($this->arguments->hasArgument('optionLabelField')) {
  174. $value = Tx_Extbase_Reflection_ObjectAccess::getProperty($value, $this->arguments['optionLabelField']);
  175. if (is_object($value)) {
  176. if (method_exists($value, '__toString')) {
  177. $value = (string)$value;
  178. } else {
  179. throw new Tx_Fluid_Core_ViewHelper_Exception('Label value for object of class "' . get_class($value) . '" was an object without a __toString() method.' , 1247827553);
  180. }
  181. }
  182. } elseif (method_exists($value, '__toString')) {
  183. $value = (string)$value;
  184. } elseif ($this->persistenceManager->getBackend()->getIdentifierByObject($value) !== NULL) {
  185. $value = $this->persistenceManager->getBackend()->getIdentifierByObject($value);
  186. }
  187. }
  188. $options[$key] = $value;
  189. }
  190. if ($this->arguments['sortByOptionLabel']) {
  191. asort($options);
  192. }
  193. return $options;
  194. }
  195. /**
  196. * Render the option tags.
  197. *
  198. * @return boolean true if the
  199. * @author Bastian Waidelich <bastian@typo3.org>
  200. */
  201. protected function isSelected($value) {
  202. $selectedValue = $this->getSelectedValue();
  203. if ($value === $selectedValue || (string)$value === $selectedValue) {
  204. return TRUE;
  205. }
  206. if ($this->arguments->hasArgument('multiple') && is_array($selectedValue) && in_array($value, $selectedValue)) {
  207. return TRUE;
  208. }
  209. return FALSE;
  210. }
  211. /**
  212. * Retrieves the selected value(s)
  213. *
  214. * @return mixed value string or an array of strings
  215. * @author Bastian Waidelich <bastian@typo3.org>
  216. */
  217. protected function getSelectedValue() {
  218. $value = $this->getValue();
  219. if (!$this->arguments->hasArgument('optionValueField')) {
  220. return $value;
  221. }
  222. if (!is_array($value) && !($value instanceof Iterator)) {
  223. if (is_object($value)) {
  224. return Tx_Extbase_Reflection_ObjectAccess::getProperty($value, $this->arguments['optionValueField']);
  225. } else {
  226. return $value;
  227. }
  228. }
  229. $selectedValues = array();
  230. foreach($value as $selectedValueElement) {
  231. if (is_object($selectedValueElement)) {
  232. $selectedValues[] = Tx_Extbase_Reflection_ObjectAccess::getProperty($selectedValueElement, $this->arguments['optionValueField']);
  233. } else {
  234. $selectedValues[] = $selectedValueElement;
  235. }
  236. }
  237. return $selectedValues;
  238. }
  239. /**
  240. * Render one option tag
  241. *
  242. * @param string $value value attribute of the option tag (will be escaped)
  243. * @param string $label content of the option tag (will be escaped)
  244. * @param boolean $isSelected specifies wheter or not to add selected attribute
  245. * @return string the rendered option tag
  246. * @author Bastian Waidelich <bastian@typo3.org>
  247. */
  248. protected function renderOptionTag($value, $label, $isSelected) {
  249. $output = '<option value="' . htmlspecialchars($value) . '"';
  250. if ($isSelected) {
  251. $output.= ' selected="selected"';
  252. }
  253. $output.= '>' . htmlspecialchars($label) . '</option>';
  254. return $output;
  255. }
  256. }
  257. ?>