PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/servers/urlcatcher.org/htdocs/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/generator/sfDoctrineFormFilterGenerator.class.php

https://github.com/mrwabu/urlcatcher
PHP | 366 lines | 255 code | 40 blank | 71 comment | 31 complexity | cb3f2cfef664d9ca25f3605698a814fa MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Doctrine filter form generator.
  11. *
  12. * This class generates a Doctrine filter forms.
  13. *
  14. * @package symfony
  15. * @subpackage generator
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. * @version SVN: $Id: sfDoctrineFormFilterGenerator.class.php 11675 2008-09-19 15:21:38Z fabien $
  18. */
  19. class sfDoctrineFormFilterGenerator extends sfDoctrineFormGenerator
  20. {
  21. /**
  22. * Initializes the current sfGenerator instance.
  23. *
  24. * @param sfGeneratorManager $generatorManager A sfGeneratorManager instance
  25. */
  26. public function initialize(sfGeneratorManager $generatorManager)
  27. {
  28. parent::initialize($generatorManager);
  29. $this->setGeneratorClass('sfDoctrineFormFilter');
  30. }
  31. /**
  32. * Generates classes and templates in cache.
  33. *
  34. * @param array $params The parameters
  35. *
  36. * @return string The data to put in configuration cache
  37. */
  38. public function generate($params = array())
  39. {
  40. $this->params = $params;
  41. if (!isset($this->params['model_dir_name']))
  42. {
  43. $this->params['model_dir_name'] = 'model';
  44. }
  45. if (!isset($this->params['filter_dir_name']))
  46. {
  47. $this->params['filter_dir_name'] = 'filter';
  48. }
  49. $models = $this->loadModels();
  50. // create the project base class for all forms
  51. $file = sfConfig::get('sf_lib_dir').'/filter/doctrine/BaseFormFilterDoctrine.class.php';
  52. if (!file_exists($file))
  53. {
  54. if (!is_dir($directory = dirname($file)))
  55. {
  56. mkdir($directory, 0777, true);
  57. }
  58. file_put_contents($file, $this->evalTemplate('sfDoctrineFormFilterBaseTemplate.php'));
  59. }
  60. $pluginPaths = $this->generatorManager->getConfiguration()->getAllPluginPaths();
  61. // create a form class for every Doctrine class
  62. foreach ($models as $model)
  63. {
  64. $this->table = Doctrine_Core::getTable($model);
  65. $this->modelName = $model;
  66. $baseDir = sfConfig::get('sf_lib_dir') . '/filter/doctrine';
  67. $isPluginModel = $this->isPluginModel($model);
  68. if ($isPluginModel)
  69. {
  70. $pluginName = $this->getPluginNameForModel($model);
  71. $baseDir .= '/' . $pluginName;
  72. }
  73. if (!is_dir($baseDir.'/base'))
  74. {
  75. mkdir($baseDir.'/base', 0777, true);
  76. }
  77. file_put_contents($baseDir.'/base/Base'.$model.'FormFilter.class.php', $this->evalTemplate(null === $this->getParentModel() ? 'sfDoctrineFormFilterGeneratedTemplate.php' : 'sfDoctrineFormFilterGeneratedInheritanceTemplate.php'));
  78. if ($isPluginModel)
  79. {
  80. $pluginBaseDir = $pluginPaths[$pluginName].'/lib/filter/doctrine';
  81. if (!file_exists($classFile = $pluginBaseDir.'/Plugin'.$model.'FormFilter.class.php'))
  82. {
  83. if (!is_dir($pluginBaseDir))
  84. {
  85. mkdir($pluginBaseDir, 0777, true);
  86. }
  87. file_put_contents($classFile, $this->evalTemplate('sfDoctrineFormFilterPluginTemplate.php'));
  88. }
  89. }
  90. if (!file_exists($classFile = $baseDir.'/'.$model.'FormFilter.class.php'))
  91. {
  92. if ($isPluginModel)
  93. {
  94. file_put_contents($classFile, $this->evalTemplate('sfDoctrinePluginFormFilterTemplate.php'));
  95. } else {
  96. file_put_contents($classFile, $this->evalTemplate('sfDoctrineFormFilterTemplate.php'));
  97. }
  98. }
  99. }
  100. }
  101. /**
  102. * Returns a sfWidgetForm class name for a given column.
  103. *
  104. * @param sfDoctrineColumn $column
  105. * @return string The name of a subclass of sfWidgetForm
  106. */
  107. public function getWidgetClassForColumn($column)
  108. {
  109. switch ($column->getDoctrineType())
  110. {
  111. case 'boolean':
  112. $name = 'Choice';
  113. break;
  114. case 'date':
  115. case 'datetime':
  116. case 'timestamp':
  117. $name = 'FilterDate';
  118. break;
  119. case 'enum':
  120. $name = 'Choice';
  121. break;
  122. default:
  123. $name = 'FilterInput';
  124. }
  125. if ($column->isForeignKey())
  126. {
  127. $name = 'DoctrineChoice';
  128. }
  129. return sprintf('sfWidgetForm%s', $name);
  130. }
  131. /**
  132. * Returns a PHP string representing options to pass to a widget for a given column.
  133. *
  134. * @param sfDoctrineColumn $column
  135. * @return string The options to pass to the widget as a PHP string
  136. */
  137. public function getWidgetOptionsForColumn($column)
  138. {
  139. $options = array();
  140. $withEmpty = $column->isNotNull() && !$column->isForeignKey() ? array("'with_empty' => false") : array();
  141. switch ($column->getDoctrineType())
  142. {
  143. case 'boolean':
  144. $options[] = "'choices' => array('' => 'yes or no', 1 => 'yes', 0 => 'no')";
  145. break;
  146. case 'date':
  147. case 'datetime':
  148. case 'timestamp':
  149. $options[] = "'from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate()";
  150. $options = array_merge($options, $withEmpty);
  151. break;
  152. case 'enum':
  153. $values = array('' => '');
  154. $values = array_merge($values, $column['values']);
  155. $values = array_combine($values, $values);
  156. $options[] = "'choices' => ".$this->arrayExport($values);
  157. break;
  158. default:
  159. $options = array_merge($options, $withEmpty);
  160. }
  161. if ($column->isForeignKey())
  162. {
  163. $options[] = sprintf('\'model\' => \'%s\', \'add_empty\' => true', $column->getForeignTable()->getOption('name'));
  164. }
  165. return count($options) ? sprintf('array(%s)', implode(', ', $options)) : '';
  166. }
  167. /**
  168. * Returns a sfValidator class name for a given column.
  169. *
  170. * @param sfDoctrineColumn $column
  171. * @return string The name of a subclass of sfValidator
  172. */
  173. public function getValidatorClassForColumn($column)
  174. {
  175. switch ($column->getDoctrineType())
  176. {
  177. case 'boolean':
  178. $name = 'Choice';
  179. break;
  180. case 'float':
  181. case 'decimal':
  182. $name = 'Number';
  183. break;
  184. case 'integer':
  185. $name = 'Integer';
  186. break;
  187. case 'date':
  188. case 'datetime':
  189. case 'timestamp':
  190. $name = 'DateRange';
  191. break;
  192. case 'enum':
  193. $name = 'Choice';
  194. break;
  195. default:
  196. $name = 'Pass';
  197. }
  198. if ($column->isPrimarykey() || $column->isForeignKey())
  199. {
  200. $name = 'DoctrineChoice';
  201. }
  202. return sprintf('sfValidator%s', $name);
  203. }
  204. /**
  205. * Returns a PHP string representing options to pass to a validator for a given column.
  206. *
  207. * @param sfDoctrineColumn $column
  208. * @return string The options to pass to the validator as a PHP string
  209. */
  210. public function getValidatorOptionsForColumn($column)
  211. {
  212. $options = array('\'required\' => false');
  213. if ($column->isForeignKey())
  214. {
  215. $columns = $column->getForeignTable()->getColumns();
  216. foreach ($columns as $name => $col)
  217. {
  218. if (isset($col['primary']) && $col['primary'])
  219. {
  220. break;
  221. }
  222. }
  223. $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $column->getForeignTable()->getOption('name'), $column->getForeignTable()->getFieldName($name));
  224. }
  225. else if ($column->isPrimaryKey())
  226. {
  227. $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $this->table->getOption('name'), $this->table->getFieldName($columnName));
  228. }
  229. else
  230. {
  231. switch ($column->getDoctrineType())
  232. {
  233. case 'boolean':
  234. $options[] = "'choices' => array('', 1, 0)";
  235. break;
  236. case 'date':
  237. $options[] = "'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDateTime(array('required' => false))";
  238. break;
  239. case 'datetime':
  240. case 'timestamp':
  241. $options[] = "'from_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 00:00:00')), 'to_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 23:59:59'))";
  242. break;
  243. case 'enum':
  244. $values = array_combine($column['values'], $column['values']);
  245. $options[] = "'choices' => ".$this->arrayExport($values);
  246. break;
  247. }
  248. }
  249. return count($options) ? sprintf('array(%s)', implode(', ', $options)) : '';
  250. }
  251. public function getValidatorForColumn($column)
  252. {
  253. $format = 'new %s(%s)';
  254. if (in_array($class = $this->getValidatorClassForColumn($column), array('sfValidatorInteger', 'sfValidatorNumber')))
  255. {
  256. $format = 'new sfValidatorSchemaFilter(\'text\', new %s(%s))';
  257. }
  258. return sprintf($format, $class, $this->getValidatorOptionsForColumn($column));
  259. }
  260. public function getType($column)
  261. {
  262. if ($column->isForeignKey())
  263. {
  264. return 'ForeignKey';
  265. }
  266. switch ($column->getDoctrineType())
  267. {
  268. case 'enum':
  269. return 'Enum';
  270. case 'boolean':
  271. return 'Boolean';
  272. case 'date':
  273. case 'datetime':
  274. case 'timestamp':
  275. return 'Date';
  276. case 'integer':
  277. case 'decimal':
  278. case 'float':
  279. return 'Number';
  280. default:
  281. return 'Text';
  282. }
  283. }
  284. /**
  285. * Array export. Export array to formatted php code
  286. *
  287. * @param array $values
  288. * @return string $php
  289. */
  290. protected function arrayExport($values)
  291. {
  292. $php = var_export($values, true);
  293. $php = str_replace("\n", '', $php);
  294. $php = str_replace('array ( ', 'array(', $php);
  295. $php = str_replace(',)', ')', $php);
  296. $php = str_replace(' ', ' ', $php);
  297. return $php;
  298. }
  299. /**
  300. * Filter out models that have disabled generation of form classes
  301. *
  302. * @return array $models Array of models to generate forms for
  303. */
  304. protected function filterModels($models)
  305. {
  306. foreach ($models as $key => $model)
  307. {
  308. $table = Doctrine_Core::getTable($model);
  309. $symfonyOptions = $table->getOption('symfony');
  310. if (isset($symfonyOptions['filter']) && !$symfonyOptions['filter'])
  311. {
  312. unset($models[$key]);
  313. }
  314. }
  315. return $models;
  316. }
  317. /**
  318. * Get the name of the form class to extend based on the inheritance of the model
  319. *
  320. * @return string
  321. */
  322. public function getFormClassToExtend()
  323. {
  324. return null === ($model = $this->getParentModel()) ? 'BaseFormFilterDoctrine' : sprintf('%sFormFilter', $model);
  325. }
  326. }