sfWidgetFormSelect.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfWidgetFormSelect represents a select HTML tag.
  11. *
  12. * @package symfony
  13. * @subpackage widget
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfWidgetFormSelect.class.php 13578 2008-12-01 10:15:10Z FabianLange $
  16. */
  17. class sfWidgetFormSelect extends sfWidgetForm
  18. {
  19. /**
  20. * Constructor.
  21. *
  22. * Available options:
  23. *
  24. * * choices: An array of possible choices (required)
  25. * * multiple: true if the select tag must allow multiple selections
  26. *
  27. * @param array $options An array of options
  28. * @param array $attributes An array of default HTML attributes
  29. *
  30. * @see sfWidgetForm
  31. */
  32. protected function configure($options = array(), $attributes = array())
  33. {
  34. $this->addRequiredOption('choices');
  35. $this->addOption('multiple', false);
  36. }
  37. /**
  38. * @param string $name The element name
  39. * @param string $value The value selected in this widget
  40. * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
  41. * @param array $errors An array of errors for the field
  42. *
  43. * @return string An HTML tag string
  44. *
  45. * @see sfWidgetForm
  46. */
  47. public function render($name, $value = null, $attributes = array(), $errors = array())
  48. {
  49. if ($this->getOption('multiple'))
  50. {
  51. $attributes['multiple'] = 'multiple';
  52. if ('[]' != substr($name, -2))
  53. {
  54. $name .= '[]';
  55. }
  56. }
  57. $choices = $this->getOption('choices');
  58. if ($choices instanceof sfCallable)
  59. {
  60. $choices = $choices->call();
  61. }
  62. return $this->renderContentTag('select', "\n".implode("\n", $this->getOptionsForSelect($value, $choices))."\n", array_merge(array('name' => $name), $attributes));
  63. }
  64. /**
  65. * Returns an array of option tags for the given choices
  66. *
  67. * @param string $value The selected value
  68. * @param array $choices An array of choices
  69. *
  70. * @return array An array of option tags
  71. */
  72. protected function getOptionsForSelect($value, $choices)
  73. {
  74. $mainAttributes = $this->attributes;
  75. $this->attributes = array();
  76. if (!is_array($value))
  77. {
  78. $value = array($value);
  79. }
  80. $value = array_map('strval', array_values($value));
  81. $value_set = array_flip($value);
  82. $options = array();
  83. foreach ($choices as $key => $option)
  84. {
  85. if (is_array($option))
  86. {
  87. $options[] = $this->renderContentTag('optgroup', implode("\n", $this->getOptionsForSelect($value, $option)), array('label' => self::escapeOnce($key)));
  88. }
  89. else
  90. {
  91. $attributes = array('value' => self::escapeOnce($key));
  92. if (isset($value_set[strval($key)]))
  93. {
  94. $attributes['selected'] = 'selected';
  95. }
  96. $options[] = $this->renderContentTag('option', self::escapeOnce($option), $attributes);
  97. }
  98. }
  99. $this->attributes = $mainAttributes;
  100. return $options;
  101. }
  102. public function __clone()
  103. {
  104. if ($this->getOption('choices') instanceof sfCallable)
  105. {
  106. $callable = $this->getOption('choices')->getCallable();
  107. if (is_array($callable))
  108. {
  109. $callable[0] = $this;
  110. $this->setOption('choices', new sfCallable($callable));
  111. }
  112. }
  113. }
  114. }