Development

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

You must first sign up to be able to contribute.

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

Revision 30762, 3.9 kB (checked in by fabien, 4 years ago)

[1.3, 1.4] fixed phpdoc (closes #8931)

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