Development

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

You must first sign up to be able to contribute.

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

Revision 17749, 6.4 kB (checked in by fabien, 6 years ago)

[1.2, 1.3] fixed some PHPDoc (closes #6371 - patch from gimler)

  • 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  * sfWidgetForm is the base class for all form widgets.
13  *
14  * @package    symfony
15  * @subpackage widget
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 abstract class sfWidgetForm extends sfWidget
20 {
21   /**
22    * Constructor.
23    *
24    * Available options:
25    *
26    *  * id_format:       The format for the generated HTML id attributes (%s by default)
27    *  * is_hidden:       true if the form widget must be hidden, false otherwise (false by default)
28    *  * needs_multipart: true if the form widget needs a multipart form, false otherwise (false by default)
29    *  * default:         The default value to use when rendering the widget
30    *  * label:           The label to use when the widget is rendered by a widget schema
31    *
32    * @param array $options     An array of options
33    * @param array $attributes  An array of default HTML attributes
34    *
35    * @see sfWidget
36    */
37   public function __construct($options = array(), $attributes = array())
38   {
39     $this->addOption('id_format', '%s');
40     $this->addOption('is_hidden', false);
41     $this->addOption('needs_multipart', false);
42     $this->addOption('default', null);
43     $this->addOption('label', null);
44
45     parent::__construct($options, $attributes);
46   }
47
48   /**
49    * Sets the default value for the widget.
50    *
51    * @param string $value The default value
52    */
53   public function setDefault($value)
54   {
55     $this->setOption('default', $value);
56   }
57
58   /**
59    * Returns the default value for the widget.
60    *
61    * @return string The default value
62    */
63   public function getDefault()
64   {
65     return $this->getOption('default');
66   }
67
68   /**
69    * Sets the label for the widget.
70    *
71    * @param string $value The label
72    */
73   public function setLabel($value)
74   {
75     $this->setOption('label', $value);
76   }
77
78   /**
79    * Returns the label for the widget.
80    *
81    * @return string The label
82    */
83   public function getLabel()
84   {
85     return $this->getOption('label');
86   }
87
88   /**
89    * Sets the format for HTML id attributes.
90    *
91    * @param string $format  The format string (must contain a %s for the id placeholder)
92    */
93   public function setIdFormat($format)
94   {
95     $this->setOption('id_format', $format);
96   }
97
98   /**
99    * Gets the HTML format string for id attributes.
100    *
101    * @return string The format string
102    */
103   public function getIdFormat()
104   {
105     return $this->getOption('id_format');
106   }
107
108   /**
109    * Returns true if the widget is hidden.
110    *
111    * @return Boolean true if the widget is hidden, false otherwise
112    */
113   public function isHidden()
114   {
115     return $this->getOption('is_hidden');
116   }
117
118   /**
119    * Sets the hidden flag for the widget.
120    *
121    * @param bool $boolean  true if the widget must be hidden, false otherwise
122    */
123   public function setHidden($boolean)
124   {
125     $this->setOption('is_hidden', (boolean) $boolean);
126   }
127
128   /**
129    * Returns true if the widget needs a multipart form.
130    *
131    * @return bool true if the widget needs a multipart form, false otherwise
132    */
133   public function needsMultipartForm()
134   {
135     return $this->getOption('needs_multipart');
136   }
137
138   /**
139    * Renders a HTML tag.
140    *
141    * The id attribute is added automatically to the array of attributes if none is specified.
142    * If uses for "id_format" option to generate the id.
143    *
144    * @param  string $tag        The tag name
145    * @param  array  $attributes An array of HTML attributes to be merged with the default HTML attributes
146    *
147    * @return string An HTML tag string
148    */
149   public function renderTag($tag, $attributes = array())
150   {
151     if (empty($tag))
152     {
153       return '';
154     }
155
156     $attributes = $this->fixFormId($attributes);
157
158     return parent::renderTag($tag, $attributes);
159   }
160
161   /**
162    * Renders a HTML content tag.
163    *
164    * The id attribute is added automatically to the array of attributes if none is specified.
165    * If uses for "id_format" option to generate the id.
166    *
167    * @param  string $tag         The tag name
168    * @param  string $content     The content of the tag
169    * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
170    *
171    * @return string An HTML tag string
172    */
173   public function renderContentTag($tag, $content = null, $attributes = array())
174   {
175     return parent::renderContentTag($tag, $content, $this->fixFormId($attributes));
176   }
177
178   /**
179    * Adds an HTML id attributes to the array of attributes if none is given and a name attribute exists.
180    *
181    * @param  array $attributes  An array of attributes
182    *
183    * @return array An array of attributes with an id.
184    */
185   protected function fixFormId($attributes)
186   {
187     if (!isset($attributes['id']) && isset($attributes['name']))
188     {
189       $attributes['id'] = $this->generateId($attributes['name'], isset($attributes['value']) ? $attributes['value'] : null);
190     }
191
192     return $attributes;
193   }
194
195   /**
196    * Returns a formatted id based on the field name and optionally on the field value.
197    *
198    * This function determines the proper form field id name based on the parameters. If a form field has an
199    * array value as a name we need to convert them to proper and unique ids like so:
200    *
201    * <samp>
202    *  name[] => name (if value == null)
203    *  name[] => name_value (if value != null)
204    *  name[bob] => name_bob
205    *  name[item][total] => name_item_total
206    * </samp>
207    *
208    * @param  string $name   The field name
209    * @param  string $value  The field value
210    *
211    * @return string The field id or null.
212    */
213   public function generateId($name, $value = null)
214   {
215     if (false === $this->getOption('id_format'))
216     {
217       return null;
218     }
219
220     // check to see if we have an array variable for a field name
221     if (strstr($name, '['))
222     {
223       $name = str_replace(array('[]', '][', '[', ']'), array((!is_null($value) ? '_'.$value : ''), '_', '_', ''), $name);
224     }
225
226     if (false !== strpos($this->getOption('id_format'), '%s'))
227     {
228       return sprintf($this->getOption('id_format'), $name);
229     }
230
231     return $name;
232   }
233  
234   /**
235    * Generates a two chars range
236    *
237    * @param  int  $start
238    * @param  int  $stop
239    * @return array
240    */
241   static protected function generateTwoCharsRange($start, $stop)
242   {
243     $results = array();
244     for ($i = $start; $i <= $stop; $i++)
245     {
246       $results[$i] = sprintf('%02d', $i);
247     }
248     return $results;
249   }
250 }
251
Note: See TracBrowser for help on using the browser.