sfWidgetFormSelect.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 17068 2009-04-07 08:24:53Z fabien $
  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. $options = array();
  77. foreach ($choices as $key => $option)
  78. {
  79. if (is_array($option))
  80. {
  81. $options[] = $this->renderContentTag('optgroup', implode("\n", $this->getOptionsForSelect($value, $option)), array('label' => self::escapeOnce($key)));
  82. }
  83. else
  84. {
  85. $attributes = array('value' => self::escapeOnce($key));
  86. if ((is_array($value) && in_array(strval($key), $value)) || strval($key) == strval($value))
  87. {
  88. $attributes['selected'] = 'selected';
  89. }
  90. $options[] = $this->renderContentTag('option', self::escapeOnce($option), $attributes);
  91. }
  92. }
  93. $this->attributes = $mainAttributes;
  94. return $options;
  95. }
  96. /**
  97. * @see sfWidget
  98. *
  99. * We always generate an attribute for the value.
  100. */
  101. protected function attributesToHtmlCallback($k, $v)
  102. {
  103. return is_null($v) || ('' === $v && 'value' != $k) ? '' : sprintf(' %s="%s"', $k, $this->escapeOnce($v));
  104. }
  105. public function __clone()
  106. {
  107. if ($this->getOption('choices') instanceof sfCallable)
  108. {
  109. $callable = $this->getOption('choices')->getCallable();
  110. $class = __CLASS__;
  111. if (is_array($callable) && $callable[0] instanceof $class)
  112. {
  113. $callable[0] = $this;
  114. $this->setOption('choices', new sfCallable($callable));
  115. }
  116. }
  117. }
  118. }