Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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