PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/sbweb/sbweb_logica/lib/symfony/generator/sfModelGeneratorConfiguration.class.php

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