Development

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

You must first sign up to be able to contribute.

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

Revision 20301, 8.1 kB (checked in by fabien, 5 years ago)

[1.2, 1.3] added exception phpdocs to all widget classes and change function signature (closes #6840 - 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  * sfWidgetFormSchemaFormatter allows to format a form schema with HTML formats.
13  *
14  * @package    symfony
15  * @subpackage widget
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 abstract class sfWidgetFormSchemaFormatter
20 {
21   protected static
22     $translationCallable       = null;
23
24   protected
25     $rowFormat                 = '',
26     $helpFormat                = '%help%',
27     $errorRowFormat            = '',
28     $errorListFormatInARow     = "  <ul class=\"error_list\">\n%errors%  </ul>\n",
29     $errorRowFormatInARow      = "    <li>%error%</li>\n",
30     $namedErrorRowFormatInARow = "    <li>%name%: %error%</li>\n",
31     $decoratorFormat           = '',
32     $widgetSchema              = null,
33     $translationCatalogue      = null;
34
35   /**
36    * Constructor
37    *
38    * @param sfWidgetFormSchema $widgetSchema
39    */
40   public function __construct(sfWidgetFormSchema $widgetSchema)
41   {
42     $this->setWidgetSchema($widgetSchema);
43   }
44
45   public function formatRow($label, $field, $errors = array(), $help = '', $hiddenFields = null)
46   {
47     return strtr($this->getRowFormat(), array(
48       '%label%'         => $label,
49       '%field%'         => $field,
50       '%error%'         => $this->formatErrorsForRow($errors),
51       '%help%'          => $this->formatHelp($help),
52       '%hidden_fields%' => is_null($hiddenFields) ? '%hidden_fields%' : $hiddenFields,
53     ));
54   }
55
56   /**
57    * Translates a string using an i18n callable, if it has been provided
58    *
59    * @param  mixed  $subject     The subject to translate
60    * @param  array  $parameters  Additional parameters to pass back to the callable
61    * @return string
62    */
63   public function translate($subject, $parameters = array())
64   {
65     if (false === $subject)
66     {
67       return false;
68     }
69
70     if (is_null(self::$translationCallable))
71     {
72       // replace object with strings
73       foreach ($parameters as $key => $value)
74       {
75         if (is_object($value) && method_exists($value, '__toString'))
76         {
77           $parameters[$key] = $value->__toString();
78         }
79       }
80
81       return strtr($subject, $parameters);
82     }
83
84     $catalogue = $this->getTranslationCatalogue();
85
86     if (self::$translationCallable instanceof sfCallable)
87     {
88       return self::$translationCallable->call($subject, $parameters, $catalogue);
89     }
90
91     return call_user_func(self::$translationCallable, $subject, $parameters, $catalogue);
92   }
93
94   /**
95    * Returns the current i18n callable
96    *
97    * @return mixed
98    */
99   static public function getTranslationCallable()
100   {
101     return self::$translationCallable;
102   }
103
104   /**
105    * Sets a callable which aims to translate form labels, errors and help messages
106    *
107    * @param  mixed  $callable
108    *
109    * @throws InvalidArgumentException if an invalid php callable or sfCallable has been provided
110    */
111   static public function setTranslationCallable($callable)
112   {
113     if (!$callable instanceof sfCallable && !is_callable($callable))
114     {
115       throw new InvalidArgumentException('Provided i18n callable should be either an instance of sfCallable or a valid PHP callable');
116     }
117
118     self::$translationCallable = $callable;
119   }
120
121   public function formatHelp($help)
122   {
123     if (!$help)
124     {
125       return '';
126     }
127
128     return strtr($this->getHelpFormat(), array('%help%' => $this->translate($help)));
129   }
130
131   public function formatErrorRow($errors)
132   {
133     if (is_null($errors) || !$errors)
134     {
135       return '';
136     }
137
138     return strtr($this->getErrorRowFormat(), array('%errors%' => $this->formatErrorsForRow($errors)));
139   }
140
141   public function formatErrorsForRow($errors)
142   {
143     if (is_null($errors) || !$errors)
144     {
145       return '';
146     }
147
148     if (!is_array($errors))
149     {
150       $errors = array($errors);
151     }
152
153     return strtr($this->getErrorListFormatInARow(), array('%errors%' => implode('', $this->unnestErrors($errors))));
154   }
155
156   /**
157    * Generates a label for the given field name.
158    *
159    * @param  string $name        The field name
160    * @param  array  $attributes  Optional html attributes for the label tag
161    *
162    * @return string The label tag
163    */
164   public function generateLabel($name, $attributes = array())
165   {
166     $labelName = $this->generateLabelName($name);
167
168     if (false === $labelName)
169     {
170       return '';
171     }
172
173     if (!isset($attributes['for']))
174     {
175       $attributes['for'] = $this->widgetSchema->generateId($this->widgetSchema->generateName($name));
176     }
177
178     return $this->widgetSchema->renderContentTag('label', $labelName, $attributes);
179   }
180
181   /**
182    * Generates the label name for the given field name.
183    *
184    * @param  string $name  The field name
185    *
186    * @return string The label name
187    */
188   public function generateLabelName($name)
189   {
190     $label = $this->widgetSchema->getLabel($name);
191
192     if (!$label && false !== $label)
193     {
194       $label = str_replace('_', ' ', ucfirst($name));
195     }
196
197     return $this->translate($label);
198   }
199
200   /**
201    * Get i18n catalogue name
202    *
203    * @return string
204    */
205   public function getTranslationCatalogue()
206   {
207     return $this->translationCatalogue;
208   }
209
210   /**
211    * Set an i18n catalogue name
212    *
213    * @param  string  $catalogue
214    *
215    * @throws InvalidArgumentException when the catalogue is not a string
216    */
217   public function setTranslationCatalogue($catalogue)
218   {
219     if (!is_string($catalogue))
220     {
221       throw new InvalidArgumentException('Catalogue name must be a string');
222     }
223
224     $this->translationCatalogue = $catalogue;
225   }
226
227   protected function unnestErrors($errors, $prefix = '')
228   {
229     $newErrors = array();
230
231     foreach ($errors as $name => $error)
232     {
233       if ($error instanceof ArrayAccess || is_array($error))
234       {
235         $newErrors = array_merge($newErrors, $this->unnestErrors($error, ($prefix ? $prefix.' > ' : '').$name));
236       }
237       else
238       {
239         if ($error instanceof sfValidatorError)
240         {
241           $err = $this->translate($error->getMessageFormat(), $error->getArguments());
242         }
243         else
244         {
245           $err = $this->translate($error);
246         }
247
248         if (!is_integer($name))
249         {
250           $newErrors[] = strtr($this->getNamedErrorRowFormatInARow(), array('%error%' => $err, '%name%' => ($prefix ? $prefix.' > ' : '').$name));
251         }
252         else
253         {
254           $newErrors[] = strtr($this->getErrorRowFormatInARow(), array('%error%' => $err));
255         }
256       }
257     }
258
259     return $newErrors;
260   }
261
262   public function setRowFormat($format)
263   {
264     $this->rowFormat = $format;
265   }
266
267   public function getRowFormat()
268   {
269     return $this->rowFormat;
270   }
271
272   public function setErrorRowFormat($format)
273   {
274     $this->errorRowFormat = $format;
275   }
276
277   public function getErrorRowFormat()
278   {
279     return $this->errorRowFormat;
280   }
281
282   public function setErrorListFormatInARow($format)
283   {
284     $this->errorListFormatInARow = $format;
285   }
286
287   public function getErrorListFormatInARow()
288   {
289     return $this->errorListFormatInARow;
290   }
291
292   public function setErrorRowFormatInARow($format)
293   {
294     $this->errorRowFormatInARow = $format;
295   }
296
297   public function getErrorRowFormatInARow()
298   {
299     return $this->errorRowFormatInARow;
300   }
301
302   public function setNamedErrorRowFormatInARow($format)
303   {
304     $this->namedErrorRowFormatInARow = $format;
305   }
306
307   public function getNamedErrorRowFormatInARow()
308   {
309     return $this->namedErrorRowFormatInARow;
310   }
311
312   public function setDecoratorFormat($format)
313   {
314     $this->decoratorFormat = $format;
315   }
316
317   public function getDecoratorFormat()
318   {
319     return $this->decoratorFormat;
320   }
321
322   public function setHelpFormat($format)
323   {
324     $this->helpFormat = $format;
325   }
326
327   public function getHelpFormat()
328   {
329     return $this->helpFormat;
330   }
331
332   /**
333    * Sets the widget schema associated with this formatter instance.
334    *
335    * @param sfWidgetFormSchema $widgetSchema A sfWidgetFormSchema instance
336    */
337   public function setWidgetSchema(sfWidgetFormSchema $widgetSchema)
338   {
339     $this->widgetSchema = $widgetSchema;
340   }
341
342   public function getWidgetSchema()
343   {
344     return $this->widgetSchema;
345   }
346 }
347
Note: See TracBrowser for help on using the browser.