Development

/branches/1.2/lib/generator/sfModelGeneratorConfiguration.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/generator/sfModelGeneratorConfiguration.class.php

Revision 23899, 14.8 kB (checked in by bschussek, 4 years ago)

[1.2, 1.3, 1.4] Default fields are created for %%variables%% in the title of the edit, list and new action, if possible. Closes #7578

  • 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  * Model generator configuration.
5  *
6  * @package    symfony
7  * @subpackage generator
8  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
9  * @version    SVN: $Id$
10  */
11 class sfModelGeneratorConfiguration
12 {
13   protected
14     $configuration = array();
15
16   /**
17    * Constructor.
18    */
19   public function __construct()
20   {
21     $this->compile();
22   }
23
24   protected function compile()
25   {
26     $config = $this->getConfig();
27
28     // inheritance rules:
29     // new|edit < form < default
30     // list < default
31     // filter < default
32     $this->configuration = array(
33       'list'   => array(
34         'fields'         => array(),
35         'layout'         => $this->getListLayout(),
36         'title'          => $this->getListTitle(),
37         'actions'        => $this->getListActions(),
38         'object_actions' => $this->getListObjectActions(),
39         'params'         => $this->getListParams(),
40       ),
41       'filter' => array(
42         'fields'  => array(),
43       ),
44       'form'   => array(
45         'fields'  => array(),
46       ),
47       'new'    => array(
48         'fields'  => array(),
49         'title'   => $this->getNewTitle(),
50         'actions' => $this->getNewActions() ? $this->getNewActions() : $this->getFormActions(),
51       ),
52       'edit'   => array(
53         'fields'  => array(),
54         'title'   => $this->getEditTitle(),
55         'actions' => $this->getEditActions() ? $this->getEditActions() : $this->getFormActions(),
56       ),
57     );
58
59     foreach (array_keys($config['default']) as $field)
60     {
61       $formConfig = array_merge($config['default'][$field], isset($config['form'][$field]) ? $config['form'][$field] : array());
62
63       $this->configuration['list']['fields'][$field]   = new sfModelGeneratorConfigurationField($field, array_merge(array('label' => sfInflector::humanize(sfInflector::underscore($field))), $config['default'][$field], isset($config['list'][$field]) ? $config['list'][$field] : array()));
64       $this->configuration['filter']['fields'][$field] = new sfModelGeneratorConfigurationField($field, array_merge($config['default'][$field], isset($config['filter'][$field]) ? $config['filter'][$field] : array()));
65       $this->configuration['new']['fields'][$field]    = new sfModelGeneratorConfigurationField($field, array_merge($formConfig, isset($config['new'][$field]) ? $config['new'][$field] : array()));
66       $this->configuration['edit']['fields'][$field]   = new sfModelGeneratorConfigurationField($field, array_merge($formConfig, isset($config['edit'][$field]) ? $config['edit'][$field] : array()));
67     }
68
69     // "virtual" fields for list
70     foreach ($this->getListDisplay() as $field)
71     {
72       list($field, $flag) = sfModelGeneratorConfigurationField::splitFieldWithFlag($field);
73
74       $this->configuration['list']['fields'][$field] = new sfModelGeneratorConfigurationField($field, array_merge(
75         array('type' => 'Text', 'label' => sfInflector::humanize(sfInflector::underscore($field))),
76         isset($config['default'][$field]) ? $config['default'][$field] : array(),
77         isset($config['list'][$field]) ? $config['list'][$field] : array(),
78         array('flag' => $flag)
79       ));
80     }
81
82     // form actions
83     foreach (array('edit', 'new') as $context)
84     {
85       foreach ($this->configuration[$context]['actions'] as $action => $parameters)
86       {
87         $this->configuration[$context]['actions'][$action] = $this->fixActionParameters($action, $parameters);
88       }
89     }
90
91     // list actions
92     foreach ($this->configuration['list']['actions'] as $action => $parameters)
93     {
94       $this->configuration['list']['actions'][$action] = $this->fixActionParameters($action, $parameters);
95     }
96
97     // list batch actions
98     $this->configuration['list']['batch_actions'] = array();
99     foreach ($this->getListBatchActions() as $action => $parameters)
100     {
101       $parameters = $this->fixActionParameters($action, $parameters);
102
103       $action = 'batch'.ucfirst(0 === strpos($action, '_') ? substr($action, 1) : $action);
104
105       $this->configuration['list']['batch_actions'][$action] = $parameters;
106     }
107
108     // list object actions
109     foreach ($this->configuration['list']['object_actions'] as $action => $parameters)
110     {
111       $this->configuration['list']['object_actions'][$action] = $this->fixActionParameters($action, $parameters);
112     }
113
114     // list field configuration
115     $this->configuration['list']['display'] = array();
116     foreach ($this->getListDisplay() as $name)
117     {
118       list($name, $flag) = sfModelGeneratorConfigurationField::splitFieldWithFlag($name);
119       if (!isset($this->configuration['list']['fields'][$name]))
120       {
121         throw new InvalidArgumentException(sprintf('The field "%s" does not exist.', $name));
122       }
123       $field = $this->configuration['list']['fields'][$name];
124       $field->setFlag($flag);
125       $this->configuration['list']['display'][$name] = $field;
126     }
127
128     // parse the %%..%% variables, remove flags and add default fields where
129     // necessary (fixes #7578)
130     $this->parseVariables('list', 'params');
131     $this->parseVariables('edit', 'title');
132     $this->parseVariables('list', 'title');
133     $this->parseVariables('new', 'title');
134
135     // action credentials
136     $this->configuration['credentials'] = array(
137       'list'   => array(),
138       'new'    => array(),
139       'create' => array(),
140       'edit'   => array(),
141       'update' => array(),
142       'delete' => array(),
143     );
144     foreach ($this->getActionsDefault() as $action => $params)
145     {
146       if (0 === strpos($action, '_'))
147       {
148         $action = substr($action, 1);
149       }
150
151       $this->configuration['credentials'][$action] = isset($params['credentials']) ? $params['credentials'] : array();
152       $this->configuration['credentials']['batch'.ucfirst($action)] = isset($params['credentials']) ? $params['credentials'] : array();
153     }
154     $this->configuration['credentials']['create'] = $this->configuration['credentials']['new'];
155     $this->configuration['credentials']['update'] = $this->configuration['credentials']['edit'];
156   }
157
158   protected function parseVariables($context, $key)
159   {
160     preg_match_all('/%%([^%]+)%%/', $this->configuration[$context][$key], $matches, PREG_PATTERN_ORDER);
161     foreach ($matches[1] as $name)
162     {
163       list($name, $flag) = sfModelGeneratorConfigurationField::splitFieldWithFlag($name);
164       if (!isset($this->configuration[$context]['fields'][$name]))
165       {
166         $this->configuration[$context]['fields'][$name] = new sfModelGeneratorConfigurationField($name, array_merge(
167           array('type' => 'Text', 'label' => sfInflector::humanize(sfInflector::underscore($name))),
168           isset($config['default'][$name]) ? $config['default'][$name] : array(),
169           isset($config[$context][$name]) ? $config[$context][$name] : array(),
170           array('flag' => $flag)
171         ));
172       }
173       else
174       {
175         $this->configuration[$context]['fields'][$name]->setFlag($flag);
176       }
177
178       $this->configuration[$context][$key] = str_replace('%%'.$flag.$name.'%%', '%%'.$name.'%%', $this->configuration[$context][$key]);
179     }
180   }
181
182   public function getContextConfiguration($context, $fields = null)
183   {
184     if (!isset($this->configuration[$context]))
185     {
186       throw new InvalidArgumentException(sprintf('The context "%s" does not exist.', $context));
187     }
188
189     if (is_null($fields))
190     {
191       return $this->configuration[$context];
192     }
193
194     $f = array();
195     foreach ($fields as $field)
196     {
197       $f[$field] = $this->configuration[$context]['fields'][$field];
198     }
199
200     return $f;
201   }
202
203   public function getFieldConfiguration($context, $field)
204   {
205     if (!isset($this->configuration[$context]))
206     {
207       throw new InvalidArgumentException(sprintf('The context "%s" does not exist.', $context));
208     }
209
210     if (!isset($this->configuration[$context]['fields'][$field]))
211     {
212       throw new InvalidArgumentException(sprintf('Field "%s" does not exist.', $field));
213     }
214
215     return $this->configuration[$context]['fields'][$field];
216   }
217
218   /**
219    * Gets the configuration for a given field.
220    *
221    * @param string  $key     The configuration key (title.list.name for example)
222    * @param mixed   $default The default value if none has been defined
223    * @param Boolean $escaped Whether to escape single quote (false by default)
224    *
225    * @return mixed The configuration value
226    */
227   public function getValue($key, $default = null, $escaped = false)
228   {
229     if (preg_match('/^(?P<context>[^\.]+)\.fields\.(?P<field>[^\.]+)\.(?P<key>.+)$/', $key, $matches))
230     {
231       $v = $this->getFieldConfiguration($matches['context'], $matches['field'])->getConfig($matches['key'], $default);
232     }
233     else if (preg_match('/^(?P<context>[^\.]+)\.(?P<key>.+)$/', $key, $matches))
234     {
235       $v = sfModelGeneratorConfiguration::getFieldConfigValue($this->getContextConfiguration($matches['context']), $matches['key'], $default);
236     }
237     else
238     {
239       throw new InvalidArgumentException(sprintf('Configuration key "%s" is invalid.', $key));
240     }
241
242     return $escaped ? str_replace("'", "\\'", $v) : $v;
243   }
244
245   /**
246    * Gets the fields that represents the filters.
247    *
248    * If no filter.display parameter is passed in the configuration,
249    * all the fields from the form are returned (dynamically).
250    *
251    * @param sfForm $form The form with the fields
252    */
253   public function getFormFilterFields(sfForm $form)
254   {
255     $config = $this->getConfig();
256
257     if ($this->getFilterDisplay())
258     {
259       $fields = array();
260       foreach ($this->getFilterDisplay() as $name)
261       {
262         list($name, $flag) = sfModelGeneratorConfigurationField::splitFieldWithFlag($name);
263         if (!isset($this->configuration['filter']['fields'][$name]))
264         {
265           $this->configuration['filter']['fields'][$name] = new sfModelGeneratorConfigurationField($name, array_merge(
266             isset($config['default'][$name]) ? $config['default'][$name] : array(),
267             isset($config['filter'][$name]) ? $config['filter'][$name] : array(),
268             array('is_real' => false, 'type' => 'Text', 'flag' => $flag)
269           ));
270         }
271         $field = $this->configuration['filter']['fields'][$name];
272         $field->setFlag($flag);
273         $fields[$name] = $field;
274       }
275
276       return $fields;
277     }
278
279     $fields = array();
280     foreach ($form->getWidgetSchema()->getPositions() as $name)
281     {
282       $fields[$name] = new sfModelGeneratorConfigurationField($name, array_merge(
283         array('type' => 'Text'),
284         isset($config['default'][$name]) ? $config['default'][$name] : array(),
285         isset($config['filter'][$name]) ? $config['filter'][$name] : array(),
286         array('is_real' => false)
287       ));
288     }
289
290     return $fields;
291   }
292
293   /**
294    * Gets the fields that represents the form.
295    *
296    * If no form.display parameter is passed in the configuration,
297    * all the fields from the form are returned (dynamically).
298    *
299    * @param sfForm $form    The form with the fields
300    * @param string $context The display context
301    */
302   public function getFormFields(sfForm $form, $context)
303   {
304     $config = $this->getConfig();
305
306     $method = sprintf('get%sDisplay', ucfirst($context));
307     if (!$fieldsets = $this->$method())
308     {
309       $fieldsets = $this->getFormDisplay();
310     }
311
312     if ($fieldsets)
313     {
314       $fields = array();
315
316       // with fieldsets?
317       if (!is_array(reset($fieldsets)))
318       {
319         $fieldsets = array('NONE' => $fieldsets);
320       }
321
322       foreach ($fieldsets as $fieldset => $names)
323       {
324         if (!$names)
325         {
326           continue;
327         }
328
329         $fields[$fieldset] = array();
330
331         foreach ($names as $name)
332         {
333           list($name, $flag) = sfModelGeneratorConfigurationField::splitFieldWithFlag($name);
334           if (!isset($this->configuration[$context]['fields'][$name]))
335           {
336             $this->configuration[$context]['fields'][$name] = new sfModelGeneratorConfigurationField($name, array_merge(
337               isset($config['default'][$name]) ? $config['default'][$name] : array(),
338               isset($config['form'][$name]) ? $config['form'][$name] : array(),
339               isset($config[$context][$name]) ? $config[$context][$name] : array(),
340               array('is_real' => false, 'type' => 'Text', 'flag' => $flag)
341             ));
342           }
343
344           $field = $this->configuration[$context]['fields'][$name];
345           $field->setFlag($flag);
346           $fields[$fieldset][$name] = $field;
347         }
348       }
349
350       return $fields;
351     }
352
353     $fields = array();
354     foreach ($form->getWidgetSchema()->getPositions() as $name)
355     {
356       $fields[$name] = new sfModelGeneratorConfigurationField($name, array_merge(
357         array('type' => 'Text'),
358         isset($config['default'][$name]) ? $config['default'][$name] : array(),
359         isset($config['form'][$name]) ? $config['form'][$name] : array(),
360         isset($config[$context][$name]) ? $config[$context][$name] : array(),
361         array('is_real' => false)
362       ));
363     }
364
365     return array('NONE' => $fields);
366   }
367
368   /**
369    * Gets the value for a given key.
370    *
371    * @param array  $config  The configuration
372    * @param string $key     The key name
373    * @param mixed  $default The default value
374    *
375    * @return mixed The key value
376    */
377   static public function getFieldConfigValue($config, $key, $default = null)
378   {
379     $ref   =& $config;
380     $parts explode('.', $key);
381     $count count($parts);
382     for ($i = 0; $i < $count; $i++)
383     {
384       $partKey = $parts[$i];
385       if (!isset($ref[$partKey]))
386       {
387         return $default;
388       }
389
390       if ($count == $i + 1)
391       {
392         return $ref[$partKey];
393       }
394       else
395       {
396         $ref =& $ref[$partKey];
397       }
398     }
399
400     return $default;
401   }
402
403   protected function mapFieldName(sfModelGeneratorConfigurationField $field)
404   {
405     return $field->getName();
406   }
407
408   protected function fixActionParameters($action, $parameters)
409   {
410     if (is_null($parameters))
411     {
412       $parameters = array();
413     }
414
415     if (!isset($parameters['params']))
416     {
417       $parameters['params'] = array();
418     }
419
420     if ('_delete' == $action && !isset($parameters['confirm']))
421     {
422       $parameters['confirm'] = 'Are you sure?';
423     }
424
425     $parameters['class_suffix'] = strtolower('_' == $action[0] ? substr($action, 1) : $action);
426
427     // merge with defaults
428     $defaults = $this->getActionsDefault();
429     if (isset($defaults[$action]))
430     {
431       $parameters = array_merge($defaults[$action], $parameters);
432     }
433
434     if (isset($parameters['label']))
435     {
436       $label = $parameters['label'];
437     }
438     else if ('_' != $action[0])
439     {
440       $label = $action;
441     }
442     else
443     {
444       $label = '_list' == $action ? 'Cancel' : substr($action, 1);
445     }
446
447     $parameters['label'] = sfInflector::humanize($label);
448
449     return $parameters;
450   }
451
452   protected function getConfig()
453   {
454     return array(
455       'default' => $this->getFieldsDefault(),
456       'list'    => $this->getFieldsList(),
457       'filter'  => $this->getFieldsFilter(),
458       'form'    => $this->getFieldsForm(),
459       'new'     => $this->getFieldsNew(),
460       'edit'    => $this->getFieldsEdit(),
461     );
462   }
463 }
464
Note: See TracBrowser for help on using the browser.