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
 * sfWidgetFormSelectCheckbox represents an array of checkboxes.
13
 *
14
 * @package    symfony
15
 * @subpackage widget
16
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17
 * @version    SVN: $Id: sfWidgetFormSelectCheckbox.class.php 30762 2010-08-25 12:33:33Z fabien $
18
 */
19
class sfWidgetFormSelectCheckbox extends sfWidgetFormChoiceBase
20
{
21
  /**
22
   * Constructor.
23
   *
24
   * Available options:
25
   *
26
   *  * choices:         An array of possible choices (required)
27
   *  * label_separator: The separator to use between the input checkbox and the label
28
   *  * class:           The class to use for the main <ul> tag
29
   *  * separator:       The separator to use between each input checkbox
30
   *  * formatter:       A callable to call to format the checkbox choices
31
   *                     The formatter callable receives the widget and the array of inputs as arguments
32
   *  * template:        The template to use when grouping option in groups (%group% %options%)
33
   *
34
   * @param array $options     An array of options
35
   * @param array $attributes  An array of default HTML attributes
36
   *
37
   * @see sfWidgetFormChoiceBase
38
   */
39
  protected function configure($options = array(), $attributes = array())
40
  {
41
    parent::configure($options, $attributes);
42
 
43
    $this->addOption('class', 'checkbox_list');
44
    $this->addOption('label_separator', '&nbsp;');
45
    $this->addOption('separator', "\n");
46
    $this->addOption('formatter', array($this, 'formatter'));
47
    $this->addOption('template', '%group% %options%');
48
  }
49
 
50
  /**
51
   * Renders the widget.
52
   *
53
   * @param  string $name        The element name
54
   * @param  string $value       The value selected in this widget
55
   * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
56
   * @param  array  $errors      An array of errors for the field
57
   *
58
   * @return string An HTML tag string
59
   *
60
   * @see sfWidgetForm
61
   */
62
  public function render($name, $value = null, $attributes = array(), $errors = array())
63
  {
64
    if ('[]' != substr($name, -2))
65
    {
66
      $name .= '[]';
67
    }
68
 
69
    if (null === $value)
70
    {
71
      $value = array();
72
    }
73
 
74
    $choices = $this->getChoices();
75
 
76
    // with groups?
77
    if (count($choices) && is_array(current($choices)))
78
    {
79
      $parts = array();
80
      foreach ($choices as $key => $option)
81
      {
82
        $parts[] = strtr($this->getOption('template'), array('%group%' => $key, '%options%' => $this->formatChoices($name, $value, $option, $attributes)));
83
      }
84
 
85
      return implode("\n", $parts);
86
    }
87
    else
88
    {
89
      return $this->formatChoices($name, $value, $choices, $attributes);
90
    }
91
  }
92
 
93
  protected function formatChoices($name, $value, $choices, $attributes)
94
  {
95
    $inputs = array();
96
    foreach ($choices as $key => $option)
97
    {
98
      $baseAttributes = array(
99
        'name'  => $name,
100
        'type'  => 'checkbox',
101
        'value' => self::escapeOnce($key),
102
        'id'    => $id = $this->generateId($name, self::escapeOnce($key)),
103
      );
104
 
105
      if ((is_array($value) && in_array(strval($key), $value)) || strval($key) == strval($value))
106
      {
107
        $baseAttributes['checked'] = 'checked';
108
      }
109
 
110
      $inputs[$id] = array(
111
        'input' => $this->renderTag('input', array_merge($baseAttributes, $attributes)),
112
        'label' => $this->renderContentTag('label', self::escapeOnce($option), array('for' => $id)),
113
      );
114
    }
115
 
116
    return call_user_func($this->getOption('formatter'), $this, $inputs);
117
  }
118
 
119
  public function formatter($widget, $inputs)
120
  {
121
    $rows = array();
122
    foreach ($inputs as $input)
123
    {
124
      $rows[] = $this->renderContentTag('li', $input['input'].$this->getOption('label_separator').$input['label']);
125
    }
126
 
127
    return !$rows ? '' : $this->renderContentTag('ul', implode($this->getOption('separator'), $rows), array('class' => $this->getOption('class')));
128
  }
129
}