Development

/branches/1.2/lib/widget/sfWidgetFormSelectCheckbox.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/widget/sfWidgetFormSelectCheckbox.class.php

Revision 27989, 4.4 kB (checked in by Kris.Wallsmith, 4 years ago)

[1.2] backported r27738 xss fix

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
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$
18  */
19 class sfWidgetFormSelectCheckbox extends sfWidgetForm
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 sfWidgetForm
38    */
39   protected function configure($options = array(), $attributes = array())
40   {
41     $this->addRequiredOption('choices');
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    * @param  string $name        The element name
52    * @param  string $value       The value selected in this widget
53    * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
54    * @param  array  $errors      An array of errors for the field
55    *
56    * @return string An HTML tag string
57    *
58    * @see sfWidgetForm
59    */
60   public function render($name, $value = null, $attributes = array(), $errors = array())
61   {
62     if ('[]' != substr($name, -2))
63     {
64       $name .= '[]';
65     }
66
67     if (is_null($value))
68     {
69       $value = array();
70     }
71
72     $choices = $this->getOption('choices');
73     if ($choices instanceof sfCallable)
74     {
75       $choices = $choices->call();
76     }
77
78     // with groups?
79     if (count($choices) && is_array(current($choices)))
80     {
81       $parts = array();
82       foreach ($choices as $key => $option)
83       {
84         $parts[] = strtr($this->getOption('template'), array('%group%' => $key, '%options%' => $this->formatChoices($name, $value, $option, $attributes)));
85       }
86
87       return implode("\n", $parts);
88     }
89     else
90     {
91       return $this->formatChoices($name, $value, $choices, $attributes);;
92     }
93   }
94
95   protected function formatChoices($name, $value, $choices, $attributes)
96   {
97     $inputs = array();
98     foreach ($choices as $key => $option)
99     {
100       $baseAttributes = array(
101         'name'  => $name,
102         'type'  => 'checkbox',
103         'value' => self::escapeOnce($key),
104         'id'    => $id = $this->generateId($name, self::escapeOnce($key)),
105       );
106
107       if ((is_array($value) && in_array(strval($key), $value)) || strval($key) == strval($value))
108       {
109         $baseAttributes['checked'] = 'checked';
110       }
111
112       $inputs[] = array(
113         'input' => $this->renderTag('input', array_merge($baseAttributes, $attributes)),
114         'label' => $this->renderContentTag('label', self::escapeOnce($option), array('for' => $id)),
115       );
116     }
117
118     return call_user_func($this->getOption('formatter'), $this, $inputs);
119   }
120
121   public function formatter($widget, $inputs)
122   {
123     $rows = array();
124     foreach ($inputs as $input)
125     {
126       $rows[] = $this->renderContentTag('li', $input['input'].$this->getOption('label_separator').$input['label']);
127     }
128
129     return $this->renderContentTag('ul', implode($this->getOption('separator'), $rows), array('class' => $this->getOption('class')));
130   }
131
132   public function __clone()
133   {
134     if ($this->getOption('choices') instanceof sfCallable)
135     {
136       $callable = $this->getOption('choices')->getCallable();
137       $class = __CLASS__;
138       if (is_array($callable) && $callable[0] instanceof $class)
139       {
140         $callable[0] = $this;
141         $this->setOption('choices', new sfCallable($callable));
142       }
143     }
144   }
145 }
146
Note: See TracBrowser for help on using the browser.