PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/generator/sfModelGeneratorConfiguration.class.php

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