123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- /*
- * This file is part of the symfony package.
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- /**
- * sfWidgetFormSelect represents a select HTML tag.
- *
- * @package symfony
- * @subpackage widget
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- * @version SVN: $Id: sfWidgetFormSelect.class.php 17068 2009-04-07 08:24:53Z fabien $
- */
- class sfWidgetFormSelect extends sfWidgetForm
- {
- /**
- * Constructor.
- *
- * Available options:
- *
- * * choices: An array of possible choices (required)
- * * multiple: true if the select tag must allow multiple selections
- *
- * @param array $options An array of options
- * @param array $attributes An array of default HTML attributes
- *
- * @see sfWidgetForm
- */
- protected function configure($options = array(), $attributes = array())
- {
- $this->addRequiredOption('choices');
- $this->addOption('multiple', false);
- }
- /**
- * @param string $name The element name
- * @param string $value The value selected in this widget
- * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
- * @param array $errors An array of errors for the field
- *
- * @return string An HTML tag string
- *
- * @see sfWidgetForm
- */
- public function render($name, $value = null, $attributes = array(), $errors = array())
- {
- if ($this->getOption('multiple'))
- {
- $attributes['multiple'] = 'multiple';
- if ('[]' != substr($name, -2))
- {
- $name .= '[]';
- }
- }
- $choices = $this->getOption('choices');
- if ($choices instanceof sfCallable)
- {
- $choices = $choices->call();
- }
- return $this->renderContentTag('select', "\n".implode("\n", $this->getOptionsForSelect($value, $choices))."\n", array_merge(array('name' => $name), $attributes));
- }
- /**
- * Returns an array of option tags for the given choices
- *
- * @param string $value The selected value
- * @param array $choices An array of choices
- *
- * @return array An array of option tags
- */
- protected function getOptionsForSelect($value, $choices)
- {
- $mainAttributes = $this->attributes;
- $this->attributes = array();
- $options = array();
- foreach ($choices as $key => $option)
- {
- if (is_array($option))
- {
- $options[] = $this->renderContentTag('optgroup', implode("\n", $this->getOptionsForSelect($value, $option)), array('label' => self::escapeOnce($key)));
- }
- else
- {
- $attributes = array('value' => self::escapeOnce($key));
- if ((is_array($value) && in_array(strval($key), $value)) || strval($key) == strval($value))
- {
- $attributes['selected'] = 'selected';
- }
- $options[] = $this->renderContentTag('option', self::escapeOnce($option), $attributes);
- }
- }
- $this->attributes = $mainAttributes;
- return $options;
- }
- /**
- * @see sfWidget
- *
- * We always generate an attribute for the value.
- */
- protected function attributesToHtmlCallback($k, $v)
- {
- return is_null($v) || ('' === $v && 'value' != $k) ? '' : sprintf(' %s="%s"', $k, $this->escapeOnce($v));
- }
- public function __clone()
- {
- if ($this->getOption('choices') instanceof sfCallable)
- {
- $callable = $this->getOption('choices')->getCallable();
- $class = __CLASS__;
- if (is_array($callable) && $callable[0] instanceof $class)
- {
- $callable[0] = $this;
- $this->setOption('choices', new sfCallable($callable));
- }
- }
- }
- }
|