PageRenderTime 76ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/generator/sfPropelFormFilterGenerator.class.php

https://github.com/zdanozdan/sfPropelORMPlugin
PHP | 321 lines | 226 code | 35 blank | 60 comment | 26 complexity | 11b252174cba0237c3ec7849c5a9bb6c 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. * Propel filter form generator.
  11. *
  12. * This class generates a Propel filter forms.
  13. *
  14. * @package symfony
  15. * @subpackage generator
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. * @version SVN: $Id: sfPropelFormFilterGenerator.class.php 24392 2009-11-25 18:35:39Z FabianLange $
  18. */
  19. class sfPropelFormFilterGenerator extends sfPropelFormGenerator
  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('sfPropelFormFilter');
  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['connection']))
  42. {
  43. throw new sfParseException('You must specify a "connection" parameter.');
  44. }
  45. if (!isset($this->params['model_dir_name']))
  46. {
  47. $this->params['model_dir_name'] = 'model';
  48. }
  49. if (!isset($this->params['filter_dir_name']))
  50. {
  51. $this->params['filter_dir_name'] = 'filter';
  52. }
  53. $this->loadBuilders();
  54. $this->dbMap = Propel::getDatabaseMap($this->params['connection']);
  55. // create the project base class for all forms
  56. $file = sfConfig::get('sf_lib_dir').'/filter/BaseFormFilterPropel.class.php';
  57. if (!file_exists($file))
  58. {
  59. if (!is_dir($directory = dirname($file)))
  60. {
  61. mkdir($directory, 0777, true);
  62. }
  63. file_put_contents($file, $this->evalTemplate('sfPropelFormFilterBaseTemplate.php'));
  64. }
  65. // create a form class for every Propel class
  66. foreach ($this->dbMap->getTables() as $tableName => $table)
  67. {
  68. $behaviors = $table->getBehaviors();
  69. if (isset($behaviors['symfony']['filter']) && 'false' === $behaviors['symfony']['filter'])
  70. {
  71. continue;
  72. }
  73. $this->table = $table;
  74. // find the package to store filter forms in the same directory as the model classes
  75. $packages = explode('.', constant(constant($table->getClassname().'::PEER').'::CLASS_DEFAULT'));
  76. array_pop($packages);
  77. if (false === $pos = array_search($this->params['model_dir_name'], $packages))
  78. {
  79. throw new InvalidArgumentException(sprintf('Unable to find the model dir name (%s) in the package %s.', $this->params['model_dir_name'], constant(constant($table->getClassname().'::PEER').'::CLASS_DEFAULT')));
  80. }
  81. $packages[$pos] = $this->params['filter_dir_name'];
  82. $baseDir = sfConfig::get('sf_root_dir').'/'.implode(DIRECTORY_SEPARATOR, $packages);
  83. if (!is_dir($baseDir.'/base'))
  84. {
  85. mkdir($baseDir.'/base', 0777, true);
  86. }
  87. file_put_contents($baseDir.'/base/Base'.$table->getClassname().'FormFilter.class.php', $this->evalTemplate('sfPropelFormFilterGeneratedTemplate.php'));
  88. if (!file_exists($classFile = $baseDir.'/'.$table->getClassname().'FormFilter.class.php'))
  89. {
  90. file_put_contents($classFile, $this->evalTemplate('sfPropelFormFilterTemplate.php'));
  91. }
  92. }
  93. }
  94. /**
  95. * Returns a sfWidgetForm class name for a given column.
  96. *
  97. * @param ColumnMap $column A ColumnMap object
  98. *
  99. * @return string The name of a subclass of sfWidgetForm
  100. */
  101. public function getWidgetClassForColumn(ColumnMap $column)
  102. {
  103. switch ($column->getType())
  104. {
  105. case PropelColumnTypes::BOOLEAN:
  106. case PropelColumnTypes::BOOLEAN_EMU:
  107. case PropelColumnTypes::ENUM:
  108. $name = 'Choice';
  109. break;
  110. case PropelColumnTypes::DATE:
  111. case PropelColumnTypes::TIME:
  112. case PropelColumnTypes::TIMESTAMP:
  113. $name = 'FilterDate';
  114. break;
  115. default:
  116. $name = 'FilterInput';
  117. }
  118. if ($column->isForeignKey())
  119. {
  120. $name = 'PropelChoice';
  121. }
  122. return sprintf('sfWidgetForm%s', $name);
  123. }
  124. /**
  125. * Returns a PHP string representing options to pass to a widget for a given column.
  126. *
  127. * @param ColumnMap $column A ColumnMap object
  128. *
  129. * @return string The options to pass to the widget as a PHP string
  130. */
  131. public function getWidgetOptionsForColumn(ColumnMap $column)
  132. {
  133. $options = array();
  134. $withEmpty = $column->isNotNull() && !$column->isForeignKey() ? array("'with_empty' => false") : array();
  135. switch ($column->getType())
  136. {
  137. case PropelColumnTypes::BOOLEAN:
  138. case PropelColumnTypes::BOOLEAN_EMU:
  139. $options[] = "'choices' => array('' => 'yes or no', 1 => 'yes', 0 => 'no')";
  140. break;
  141. case PropelColumnTypes::DATE:
  142. case PropelColumnTypes::TIME:
  143. case PropelColumnTypes::TIMESTAMP:
  144. $options[] = "'from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate()";
  145. $options = array_merge($options, $withEmpty);
  146. break;
  147. case PropelColumnTypes::ENUM:
  148. $valueSet = $column->getValueSet();
  149. $choices = array_merge(array(''=>'all'), $valueSet);
  150. $options[] = sprintf("'choices' => %s", preg_replace('/\s+/', '', var_export($choices, true)));
  151. break;
  152. default:
  153. $options = array_merge($options, $withEmpty);
  154. }
  155. if ($column->isForeignKey())
  156. {
  157. $options[] = sprintf('\'model\' => \'%s\', \'add_empty\' => true', $this->getForeignTable($column)->getClassname());
  158. $refColumn = $this->getForeignTable($column)->getColumn($column->getRelatedColumnName());
  159. if (!$refColumn->isPrimaryKey())
  160. {
  161. $options[] = sprintf('\'key_method\' => \'get%s\'', $refColumn->getPhpName());
  162. }
  163. }
  164. return count($options) ? sprintf('array(%s)', implode(', ', $options)) : '';
  165. }
  166. /**
  167. * Returns a sfValidator class name for a given column.
  168. *
  169. * @param ColumnMap $column A ColumnMap object
  170. *
  171. * @return string The name of a subclass of sfValidator
  172. */
  173. public function getValidatorClassForColumn(ColumnMap $column)
  174. {
  175. switch ($column->getType())
  176. {
  177. case PropelColumnTypes::BOOLEAN:
  178. case PropelColumnTypes::BOOLEAN_EMU:
  179. case PropelColumnTypes::ENUM:
  180. $name = 'Choice';
  181. break;
  182. case PropelColumnTypes::DOUBLE:
  183. case PropelColumnTypes::FLOAT:
  184. case PropelColumnTypes::NUMERIC:
  185. case PropelColumnTypes::DECIMAL:
  186. case PropelColumnTypes::REAL:
  187. $name = 'Number';
  188. break;
  189. case PropelColumnTypes::INTEGER:
  190. case PropelColumnTypes::SMALLINT:
  191. case PropelColumnTypes::TINYINT:
  192. case PropelColumnTypes::BIGINT:
  193. $name = 'Integer';
  194. break;
  195. case PropelColumnTypes::DATE:
  196. case PropelColumnTypes::TIME:
  197. case PropelColumnTypes::TIMESTAMP:
  198. $name = 'DateRange';
  199. break;
  200. default:
  201. $name = 'Pass';
  202. }
  203. if ($column->isPrimaryKey() || $column->isForeignKey())
  204. {
  205. $name = 'PropelChoice';
  206. }
  207. return sprintf('sfValidator%s', $name);
  208. }
  209. /**
  210. * Returns a PHP string representing options to pass to a validator for a given column.
  211. *
  212. * @param ColumnMap $column A ColumnMap object
  213. *
  214. * @return string The options to pass to the validator as a PHP string
  215. */
  216. public function getValidatorOptionsForColumn(ColumnMap $column)
  217. {
  218. $options = array('\'required\' => false');
  219. if ($column->isForeignKey())
  220. {
  221. $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $this->getForeignTable($column)->getClassname(), $this->translateColumnName($column, true));
  222. }
  223. else if ($column->isPrimaryKey())
  224. {
  225. $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $column->getTable()->getClassname(), $this->translateColumnName($column));
  226. }
  227. else
  228. {
  229. switch ($column->getType())
  230. {
  231. case PropelColumnTypes::BOOLEAN:
  232. case PropelColumnTypes::BOOLEAN_EMU:
  233. $options[] = "'choices' => array('', 1, 0)";
  234. break;
  235. case PropelColumnTypes::DATE:
  236. case PropelColumnTypes::TIME:
  237. case PropelColumnTypes::TIMESTAMP:
  238. $options[] = "'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false))";
  239. break;
  240. case PropelColumnTypes::ENUM:
  241. $valueSet = $column->getValueSet();
  242. $options[] = sprintf("'choices' => %s", preg_replace('/\s+/', '', var_export(array_keys($valueSet), true)));
  243. break;
  244. }
  245. }
  246. return count($options) ? sprintf('array(%s)', implode(', ', $options)) : '';
  247. }
  248. public function getValidatorForColumn($column)
  249. {
  250. $format = 'new %s(%s)';
  251. if (in_array($class = $this->getValidatorClassForColumn($column), array('sfValidatorInteger', 'sfValidatorNumber')))
  252. {
  253. $format = 'new sfValidatorSchemaFilter(\'text\', new %s(%s))';
  254. }
  255. return sprintf($format, $class, $this->getValidatorOptionsForColumn($column));
  256. }
  257. public function getType(ColumnMap $column)
  258. {
  259. if ($column->isForeignKey())
  260. {
  261. return 'ForeignKey';
  262. }
  263. switch ($column->getType())
  264. {
  265. case PropelColumnTypes::BOOLEAN:
  266. case PropelColumnTypes::BOOLEAN_EMU:
  267. return 'Boolean';
  268. case PropelColumnTypes::DATE:
  269. case PropelColumnTypes::TIME:
  270. case PropelColumnTypes::TIMESTAMP:
  271. return 'Date';
  272. case PropelColumnTypes::DOUBLE:
  273. case PropelColumnTypes::FLOAT:
  274. case PropelColumnTypes::NUMERIC:
  275. case PropelColumnTypes::DECIMAL:
  276. case PropelColumnTypes::REAL:
  277. case PropelColumnTypes::INTEGER:
  278. case PropelColumnTypes::SMALLINT:
  279. case PropelColumnTypes::TINYINT:
  280. case PropelColumnTypes::BIGINT:
  281. return 'Number';
  282. default:
  283. return 'Text';
  284. }
  285. }
  286. }