123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- <?php
- /**
- * Model generator configuration.
- *
- * @package symfony
- * @subpackage generator
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- * @version SVN: $Id: sfModelGeneratorConfiguration.class.php 17680 2009-04-27 15:19:58Z fabien $
- */
- class sfModelGeneratorConfiguration
- {
- protected
- $configuration = array();
- /**
- * Constructor.
- */
- public function __construct()
- {
- $this->compile();
- }
- protected function compile()
- {
- $config = $this->getConfig();
- // inheritance rules:
- // new|edit < form < default
- // list < default
- // filter < default
- $this->configuration = array(
- 'list' => array(
- 'fields' => array(),
- 'layout' => $this->getListLayout(),
- 'title' => $this->getListTitle(),
- 'actions' => $this->getListActions(),
- 'object_actions' => $this->getListObjectActions(),
- ),
- 'filter' => array(
- 'fields' => array(),
- ),
- 'form' => array(
- 'fields' => array(),
- ),
- 'new' => array(
- 'fields' => array(),
- 'title' => $this->getNewTitle(),
- 'actions' => $this->getNewActions() ? $this->getNewActions() : $this->getFormActions(),
- ),
- 'edit' => array(
- 'fields' => array(),
- 'title' => $this->getEditTitle(),
- 'actions' => $this->getEditActions() ? $this->getEditActions() : $this->getFormActions(),
- ),
- );
- foreach (array_keys($config['default']) as $field)
- {
- $formConfig = array_merge($config['default'][$field], isset($config['form'][$field]) ? $config['form'][$field] : array());
- $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()));
- $this->configuration['filter']['fields'][$field] = new sfModelGeneratorConfigurationField($field, array_merge($config['default'][$field], isset($config['filter'][$field]) ? $config['filter'][$field] : array()));
- $this->configuration['new']['fields'][$field] = new sfModelGeneratorConfigurationField($field, array_merge($formConfig, isset($config['new'][$field]) ? $config['new'][$field] : array()));
- $this->configuration['edit']['fields'][$field] = new sfModelGeneratorConfigurationField($field, array_merge($formConfig, isset($config['edit'][$field]) ? $config['edit'][$field] : array()));
- }
- // "virtual" fields for list
- foreach ($this->getListDisplay() as $field)
- {
- list($field, $flag) = sfModelGeneratorConfigurationField::splitFieldWithFlag($field);
- $this->configuration['list']['fields'][$field] = new sfModelGeneratorConfigurationField($field, array_merge(
- array('type' => 'Text', 'label' => sfInflector::humanize(sfInflector::underscore($field))),
- isset($config['default'][$field]) ? $config['default'][$field] : array(),
- isset($config['list'][$field]) ? $config['list'][$field] : array(),
- array('flag' => $flag)
- ));
- }
- // form actions
- foreach (array('edit', 'new') as $context)
- {
- foreach ($this->configuration[$context]['actions'] as $action => $parameters)
- {
- $this->configuration[$context]['actions'][$action] = $this->fixActionParameters($action, $parameters);
- }
- }
- // list actions
- foreach ($this->configuration['list']['actions'] as $action => $parameters)
- {
- $this->configuration['list']['actions'][$action] = $this->fixActionParameters($action, $parameters);
- }
- // list batch actions
- $this->configuration['list']['batch_actions'] = array();
- foreach ($this->getListBatchActions() as $action => $parameters)
- {
- $parameters = $this->fixActionParameters($action, $parameters);
- $action = 'batch'.ucfirst(0 === strpos($action, '_') ? substr($action, 1) : $action);
- $this->configuration['list']['batch_actions'][$action] = $parameters;
- }
- // list object actions
- foreach ($this->configuration['list']['object_actions'] as $action => $parameters)
- {
- $this->configuration['list']['object_actions'][$action] = $this->fixActionParameters($action, $parameters);
- }
- // list field configuration
- $this->configuration['list']['display'] = array();
- foreach ($this->getListDisplay() as $name)
- {
- list($name, $flag) = sfModelGeneratorConfigurationField::splitFieldWithFlag($name);
- if (!isset($this->configuration['list']['fields'][$name]))
- {
- throw new InvalidArgumentException(sprintf('The field "%s" does not exist.', $name));
- }
- $field = $this->configuration['list']['fields'][$name];
- $field->setFlag($flag);
- $this->configuration['list']['display'][$name] = $field;
- }
- // list params configuration
- $this->configuration['list']['params'] = $this->getListParams();
- preg_match_all('/%%([^%]+)%%/', $this->getListParams(), $matches, PREG_PATTERN_ORDER);
- foreach ($matches[1] as $name)
- {
- list($name, $flag) = sfModelGeneratorConfigurationField::splitFieldWithFlag($name);
- if (!isset($this->configuration['list']['fields'][$name]))
- {
- $this->configuration['list']['fields'][$name] = new sfModelGeneratorConfigurationField($name, array_merge(
- array('type' => 'Text', 'label' => sfInflector::humanize(sfInflector::underscore($name))),
- isset($config['default'][$name]) ? $config['default'][$name] : array(),
- isset($config['list'][$name]) ? $config['list'][$name] : array(),
- array('flag' => $flag)
- ));
- }
- else
- {
- $this->configuration['list']['fields'][$name]->setFlag($flag);
- }
- $this->configuration['list']['params'] = str_replace('%%'.$flag.$name.'%%', '%%'.$name.'%%', $this->configuration['list']['params']);
- }
- // action credentials
- $this->configuration['credentials'] = array(
- 'list' => array(),
- 'new' => array(),
- 'create' => array(),
- 'edit' => array(),
- 'update' => array(),
- 'delete' => array(),
- );
- foreach ($this->getActionsDefault() as $action => $params)
- {
- if (0 === strpos($action, '_'))
- {
- $action = substr($action, 1);
- }
- $this->configuration['credentials'][$action] = isset($params['credentials']) ? $params['credentials'] : array();
- $this->configuration['credentials']['batch'.ucfirst($action)] = isset($params['credentials']) ? $params['credentials'] : array();
- }
- $this->configuration['credentials']['create'] = $this->configuration['credentials']['new'];
- $this->configuration['credentials']['update'] = $this->configuration['credentials']['edit'];
- }
- public function getContextConfiguration($context, $fields = null)
- {
- if (!isset($this->configuration[$context]))
- {
- throw new InvalidArgumentException(sprintf('The context "%s" does not exist.', $context));
- }
- if (is_null($fields))
- {
- return $this->configuration[$context];
- }
- $f = array();
- foreach ($fields as $field)
- {
- $f[$field] = $this->configuration[$context]['fields'][$field];
- }
- return $f;
- }
- public function getFieldConfiguration($context, $field)
- {
- if (!isset($this->configuration[$context]))
- {
- throw new InvalidArgumentException(sprintf('The context "%s" does not exist.', $context));
- }
- if (!isset($this->configuration[$context]['fields'][$field]))
- {
- throw new InvalidArgumentException(sprintf('Field "%s" does not exist.', $field));
- }
- return $this->configuration[$context]['fields'][$field];
- }
- /**
- * Gets the configuration for a given field.
- *
- * @param string $key The configuration key (title.list.name for example)
- * @param mixed $default The default value if none has been defined
- * @param Boolean $escaped Whether to escape single quote (false by default)
- *
- * @return mixed The configuration value
- */
- public function getValue($key, $default = null, $escaped = false)
- {
- if (preg_match('/^(?P<context>[^\.]+)\.fields\.(?P<field>[^\.]+)\.(?P<key>.+)$/', $key, $matches))
- {
- $v = $this->getFieldConfiguration($matches['context'], $matches['field'])->getConfig($matches['key'], $default);
- }
- else if (preg_match('/^(?P<context>[^\.]+)\.(?P<key>.+)$/', $key, $matches))
- {
- $v = sfModelGeneratorConfiguration::getFieldConfigValue($this->getContextConfiguration($matches['context']), $matches['key'], $default);
- }
- else
- {
- throw new InvalidArgumentException(sprintf('Configuration key "%s" is invalid.', $key));
- }
- return $escaped ? str_replace("'", "\\'", $v) : $v;
- }
- /**
- * Gets the fields that represents the filters.
- *
- * If no filter.display parameter is passed in the configuration,
- * all the fields from the form are returned (dynamically).
- *
- * @param array An array of fields
- */
- public function getFormFilterFields(sfForm $form)
- {
- $config = $this->getConfig();
- if ($this->getFilterDisplay())
- {
- $fields = array();
- foreach ($this->getFilterDisplay() as $name)
- {
- list($name, $flag) = sfModelGeneratorConfigurationField::splitFieldWithFlag($name);
- if (!isset($this->configuration['filter']['fields'][$name]))
- {
- $this->configuration['filter']['fields'][$name] = new sfModelGeneratorConfigurationField($name, array_merge(
- isset($config['default'][$name]) ? $config['default'][$name] : array(),
- isset($config['filter'][$name]) ? $config['filter'][$name] : array(),
- array('is_real' => false, 'type' => 'Text', 'flag' => $flag)
- ));
- }
- $field = $this->configuration['filter']['fields'][$name];
- $field->setFlag($flag);
- $fields[$name] = $field;
- }
- return $fields;
- }
- $fields = array();
- foreach ($form->getWidgetSchema()->getPositions() as $name)
- {
- $fields[$name] = new sfModelGeneratorConfigurationField($name, array_merge(
- isset($config['default'][$name]) ? $config['default'][$name] : array(),
- isset($config['filter'][$name]) ? $config['filter'][$name] : array(),
- array('is_real' => false, 'type' => 'Text')
- ));
- }
- return $fields;
- }
- /**
- * Gets the fields that represents the form.
- *
- * If no form.display parameter is passed in the configuration,
- * all the fields from the form are returned (dynamically).
- *
- * @param array An array of fields
- */
- public function getFormFields(sfForm $form, $context)
- {
- $config = $this->getConfig();
- $method = sprintf('get%sDisplay', ucfirst($context));
- if (!$fieldsets = $this->$method())
- {
- $fieldsets = $this->getFormDisplay();
- }
- if ($fieldsets)
- {
- $fields = array();
- // with fieldsets?
- if (!is_array(current($fieldsets)))
- {
- $fieldsets = array('NONE' => $fieldsets);
- }
- foreach ($fieldsets as $fieldset => $names)
- {
- $fields[$fieldset] = array();
- foreach ($names as $name)
- {
- list($name, $flag) = sfModelGeneratorConfigurationField::splitFieldWithFlag($name);
- if (!isset($this->configuration[$context]['fields'][$name]))
- {
- $this->configuration[$context]['fields'][$name] = new sfModelGeneratorConfigurationField($name, array_merge(
- isset($config['default'][$name]) ? $config['default'][$name] : array(),
- isset($config['form'][$name]) ? $config['form'][$name] : array(),
- isset($config[$context][$name]) ? $config[$context][$name] : array(),
- array('is_real' => false, 'type' => 'Text', 'flag' => $flag)
- ));
- }
- $field = $this->configuration[$context]['fields'][$name];
- $field->setFlag($flag);
- $fields[$fieldset][$name] = $field;
- }
- }
- return $fields;
- }
- $fields = array();
- foreach ($form->getWidgetSchema()->getPositions() as $name)
- {
- $fields[$name] = new sfModelGeneratorConfigurationField($name, array_merge(
- isset($config['default'][$name]) ? $config['default'][$name] : array(),
- isset($config['form'][$name]) ? $config['form'][$name] : array(),
- isset($config[$context][$name]) ? $config[$context][$name] : array(),
- array('is_real' => false, 'type' => 'Text')
- ));
- }
- return array('NONE' => $fields);
- }
- /**
- * Gets the value for a given key.
- *
- * @param array $config The configuration
- * @param string $key The key name
- * @param mixed $default The default value
- *
- * @return mixed The key value
- */
- static public function getFieldConfigValue($config, $key, $default = null)
- {
- $ref =& $config;
- $parts = explode('.', $key);
- $count = count($parts);
- for ($i = 0; $i < $count; $i++)
- {
- $partKey = $parts[$i];
- if (!isset($ref[$partKey]))
- {
- return $default;
- }
- if ($count == $i + 1)
- {
- return $ref[$partKey];
- }
- else
- {
- $ref =& $ref[$partKey];
- }
- }
- return $default;
- }
- /**
- * Removes visible fields not included for display.
- *
- * @param sfForm $form
- */
- protected function fixFormFields(sfForm $form)
- {
- $method = sprintf('get%sDisplay', $form->isNew() ? 'New' : 'Edit');
- if (!$display = $this->$method())
- {
- $display = $this->getFormDisplay();
- }
- if ($display)
- {
- if (is_array(current($display)))
- {
- $display = call_user_func_array('array_merge', array_values($display));
- }
- foreach ($form as $name => $field)
- {
- if (!$field->isHidden() && !in_array($name, $display))
- {
- unset($form[$name]);
- }
- }
- }
- }
- protected function fixActionParameters($action, $parameters)
- {
- if (is_null($parameters))
- {
- $parameters = array();
- }
- if (!isset($parameters['params']))
- {
- $parameters['params'] = array();
- }
- if ('_delete' == $action && !isset($parameters['confirm']))
- {
- $parameters['confirm'] = 'Are you sure?';
- }
- $parameters['class_suffix'] = strtolower('_' == $action[0] ? substr($action, 1) : $action);
- // merge with defaults
- $defaults = $this->getActionsDefault();
- if (isset($defaults[$action]))
- {
- $parameters = array_merge($defaults[$action], $parameters);
- }
- if (isset($parameters['label']))
- {
- $label = $parameters['label'];
- }
- else if ('_' != $action[0])
- {
- $label = $action;
- }
- else
- {
- $label = '_list' == $action ? 'Cancel' : substr($action, 1);
- }
- $parameters['label'] = sfInflector::humanize($label);
- return $parameters;
- }
- protected function getConfig()
- {
- return array(
- 'default' => $this->getFieldsDefault(),
- 'list' => $this->getFieldsList(),
- 'filter' => $this->getFieldsFilter(),
- 'form' => $this->getFieldsForm(),
- 'new' => $this->getFieldsNew(),
- 'edit' => $this->getFieldsEdit(),
- );
- }
- }
|