Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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