Development

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

You must first sign up to be able to contribute.

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

Revision 13843, 8.1 kB (checked in by FabianLange, 5 years ago)

[1.1] fixed overriding for attribute of generated labels in sfWidgetFormSchemaFormatter. fixes #5220

  • 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    * @throws InvalidArgumentException
215    */
216   public function setTranslationCatalogue($catalogue)
217   {
218     if (!is_string($catalogue))
219     {
220       throw new InvalidArgumentException('Catalogue name must be a string');
221     }
222
223     $this->translationCatalogue = $catalogue;
224   }
225
226   protected function unnestErrors($errors, $prefix = '')
227   {
228     $newErrors = array();
229
230     foreach ($errors as $name => $error)
231     {
232       if ($error instanceof ArrayAccess || is_array($error))
233       {
234         $newErrors = array_merge($newErrors, $this->unnestErrors($error, ($prefix ? $prefix.' > ' : '').$name));
235       }
236       else
237       {
238         if ($error instanceof sfValidatorError)
239         {
240           $err = $this->translate($error->getMessageFormat(), $error->getArguments());
241         }
242         else
243         {
244           $err = $this->translate($error);
245         }
246
247         if (!is_integer($name))
248         {
249           $newErrors[] = strtr($this->getNamedErrorRowFormatInARow(), array('%error%' => $err, '%name%' => ($prefix ? $prefix.' > ' : '').$name));
250         }
251         else
252         {
253           $newErrors[] = strtr($this->getErrorRowFormatInARow(), array('%error%' => $err));
254         }
255       }
256     }
257
258     return $newErrors;
259   }
260
261   public function setRowFormat($format)
262   {
263     $this->rowFormat = $format;
264   }
265
266   public function getRowFormat()
267   {
268     return $this->rowFormat;
269   }
270
271   public function setErrorRowFormat($format)
272   {
273     $this->errorRowFormat = $format;
274   }
275
276   public function getErrorRowFormat()
277   {
278     return $this->errorRowFormat;
279   }
280
281   public function setErrorListFormatInARow($format)
282   {
283     $this->errorListFormatInARow = $format;
284   }
285
286   public function getErrorListFormatInARow()
287   {
288     return $this->errorListFormatInARow;
289   }
290
291   public function setErrorRowFormatInARow($format)
292   {
293     $this->errorRowFormatInARow = $format;
294   }
295
296   public function getErrorRowFormatInARow()
297   {
298     return $this->errorRowFormatInARow;
299   }
300
301   public function setNamedErrorRowFormatInARow($format)
302   {
303     $this->namedErrorRowFormatInARow = $format;
304   }
305
306   public function getNamedErrorRowFormatInARow()
307   {
308     return $this->namedErrorRowFormatInARow;
309   }
310
311   public function setDecoratorFormat($format)
312   {
313     $this->decoratorFormat = $format;
314   }
315
316   public function getDecoratorFormat()
317   {
318     return $this->decoratorFormat;
319   }
320
321   public function setHelpFormat($format)
322   {
323     $this->helpFormat = $format;
324   }
325
326   public function getHelpFormat()
327   {
328     return $this->helpFormat;
329   }
330
331   /**
332    * Sets the widget schema associated with this formatter instance.
333    *
334    * @param sfWidgetFormSchema $widgetSchema A sfWidgetFormSchema instance
335    */
336   public function setWidgetSchema(sfWidgetFormSchema $widgetSchema)
337   {
338     $this->widgetSchema = $widgetSchema;
339   }
340
341   public function getWidgetSchema()
342   {
343     return $this->widgetSchema;
344   }
345 }
346
Note: See TracBrowser for help on using the browser.