PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/sbweb/sbweb_logica/lib/symfony/plugins/sfPropelPlugin/lib/generator/sfPropelFormFilterGenerator.class.php

http://opac-sbweb.googlecode.com/
PHP | 296 lines | 203 code | 33 blank | 60 comment | 23 complexity | 1f98da1d429f7a8bd85dcaba93c5da2f MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  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 15134 2009-01-31 19:30:35Z Kris.Wallsmith $
  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/base/BaseFormFilterPropel.class.php';
  57. if (!file_exists($file))
  58. {
  59. if (!is_dir(sfConfig::get('sf_lib_dir').'/filter/base'))
  60. {
  61. mkdir(sfConfig::get('sf_lib_dir').'/filter/base', 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. $this->table = $table;
  69. // find the package to store filter forms in the same directory as the model classes
  70. $packages = explode('.', constant(constant($table->getClassname().'::PEER').'::CLASS_DEFAULT'));
  71. array_pop($packages);
  72. if (false === $pos = array_search($this->params['model_dir_name'], $packages))
  73. {
  74. 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')));
  75. }
  76. $packages[$pos] = $this->params['filter_dir_name'];
  77. $baseDir = sfConfig::get('sf_root_dir').'/'.implode(DIRECTORY_SEPARATOR, $packages);
  78. if (!is_dir($baseDir.'/base'))
  79. {
  80. mkdir($baseDir.'/base', 0777, true);
  81. }
  82. file_put_contents($baseDir.'/base/Base'.$table->getClassname().'FormFilter.class.php', $this->evalTemplate('sfPropelFormFilterGeneratedTemplate.php'));
  83. if (!file_exists($classFile = $baseDir.'/'.$table->getClassname().'FormFilter.class.php'))
  84. {
  85. file_put_contents($classFile, $this->evalTemplate('sfPropelFormFilterTemplate.php'));
  86. }
  87. }
  88. }
  89. /**
  90. * Returns a sfWidgetForm class name for a given column.
  91. *
  92. * @param ColumnMap $column A ColumnMap object
  93. *
  94. * @return string The name of a subclass of sfWidgetForm
  95. */
  96. public function getWidgetClassForColumn(ColumnMap $column)
  97. {
  98. switch ($column->getType())
  99. {
  100. case PropelColumnTypes::BOOLEAN:
  101. $name = 'Choice';
  102. break;
  103. case PropelColumnTypes::DATE:
  104. case PropelColumnTypes::TIME:
  105. case PropelColumnTypes::TIMESTAMP:
  106. $name = 'FilterDate';
  107. break;
  108. default:
  109. $name = 'FilterInput';
  110. }
  111. if ($column->isForeignKey())
  112. {
  113. $name = 'PropelChoice';
  114. }
  115. return sprintf('sfWidgetForm%s', $name);
  116. }
  117. /**
  118. * Returns a PHP string representing options to pass to a widget for a given column.
  119. *
  120. * @param ColumnMap $column A ColumnMap object
  121. *
  122. * @return string The options to pass to the widget as a PHP string
  123. */
  124. public function getWidgetOptionsForColumn(ColumnMap $column)
  125. {
  126. $options = array();
  127. $withEmpty = sprintf('\'with_empty\' => %s', $column->isNotNull() ? 'false' : 'true');
  128. switch ($column->getType())
  129. {
  130. case PropelColumnTypes::BOOLEAN:
  131. $options[] = "'choices' => array('' => 'yes or no', 1 => 'yes', 0 => 'no')";
  132. break;
  133. case PropelColumnTypes::DATE:
  134. case PropelColumnTypes::TIME:
  135. case PropelColumnTypes::TIMESTAMP:
  136. $options[] = "'from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate()";
  137. $options[] = $withEmpty;
  138. break;
  139. }
  140. if ($column->isForeignKey())
  141. {
  142. $options[] = sprintf('\'model\' => \'%s\', \'add_empty\' => true', $this->getForeignTable($column)->getClassname());
  143. $refColumn = $this->getForeignTable($column)->getColumn($column->getRelatedColumnName());
  144. if (!$refColumn->isPrimaryKey())
  145. {
  146. $options[] = sprintf('\'key_method\' => \'get%s\'', $refColumn->getPhpName());
  147. }
  148. }
  149. return count($options) ? sprintf('array(%s)', implode(', ', $options)) : '';
  150. }
  151. /**
  152. * Returns a sfValidator class name for a given column.
  153. *
  154. * @param ColumnMap $column A ColumnMap object
  155. *
  156. * @return string The name of a subclass of sfValidator
  157. */
  158. public function getValidatorClassForColumn(ColumnMap $column)
  159. {
  160. switch ($column->getType())
  161. {
  162. case PropelColumnTypes::BOOLEAN:
  163. $name = 'Choice';
  164. break;
  165. case PropelColumnTypes::DOUBLE:
  166. case PropelColumnTypes::FLOAT:
  167. case PropelColumnTypes::NUMERIC:
  168. case PropelColumnTypes::DECIMAL:
  169. case PropelColumnTypes::REAL:
  170. $name = 'Number';
  171. break;
  172. case PropelColumnTypes::INTEGER:
  173. case PropelColumnTypes::SMALLINT:
  174. case PropelColumnTypes::TINYINT:
  175. case PropelColumnTypes::BIGINT:
  176. $name = 'Integer';
  177. break;
  178. case PropelColumnTypes::DATE:
  179. case PropelColumnTypes::TIME:
  180. case PropelColumnTypes::TIMESTAMP:
  181. $name = 'DateRange';
  182. break;
  183. default:
  184. $name = 'Pass';
  185. }
  186. if ($column->isPrimaryKey() || $column->isForeignKey())
  187. {
  188. $name = 'PropelChoice';
  189. }
  190. return sprintf('sfValidator%s', $name);
  191. }
  192. /**
  193. * Returns a PHP string representing options to pass to a validator for a given column.
  194. *
  195. * @param ColumnMap $column A ColumnMap object
  196. *
  197. * @return string The options to pass to the validator as a PHP string
  198. */
  199. public function getValidatorOptionsForColumn(ColumnMap $column)
  200. {
  201. $options = array('\'required\' => false');
  202. if ($column->isForeignKey())
  203. {
  204. $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $this->getForeignTable($column)->getClassname(), strtolower($column->getRelatedColumnName()));
  205. }
  206. else if ($column->isPrimaryKey())
  207. {
  208. $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $column->getTable()->getClassname(), strtolower($column->getColumnName()));
  209. }
  210. else
  211. {
  212. switch ($column->getType())
  213. {
  214. case PropelColumnTypes::BOOLEAN:
  215. $options[] = "'choices' => array('', 1, 0)";
  216. break;
  217. case PropelColumnTypes::DATE:
  218. case PropelColumnTypes::TIME:
  219. case PropelColumnTypes::TIMESTAMP:
  220. $options[] = "'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false))";
  221. break;
  222. }
  223. }
  224. return count($options) ? sprintf('array(%s)', implode(', ', $options)) : '';
  225. }
  226. public function getValidatorForColumn($column)
  227. {
  228. $format = 'new %s(%s)';
  229. if (in_array($class = $this->getValidatorClassForColumn($column), array('sfValidatorInteger', 'sfValidatorNumber')))
  230. {
  231. $format = 'new sfValidatorSchemaFilter(\'text\', new %s(%s))';
  232. }
  233. return sprintf($format, $class, $this->getValidatorOptionsForColumn($column));
  234. }
  235. public function getType(ColumnMap $column)
  236. {
  237. if ($column->isForeignKey())
  238. {
  239. return 'ForeignKey';
  240. }
  241. switch ($column->getType())
  242. {
  243. case PropelColumnTypes::BOOLEAN:
  244. return 'Boolean';
  245. case PropelColumnTypes::DATE:
  246. case PropelColumnTypes::TIME:
  247. case PropelColumnTypes::TIMESTAMP:
  248. return 'Date';
  249. case PropelColumnTypes::DOUBLE:
  250. case PropelColumnTypes::FLOAT:
  251. case PropelColumnTypes::NUMERIC:
  252. case PropelColumnTypes::DECIMAL:
  253. case PropelColumnTypes::REAL:
  254. case PropelColumnTypes::INTEGER:
  255. case PropelColumnTypes::SMALLINT:
  256. case PropelColumnTypes::TINYINT:
  257. case PropelColumnTypes::BIGINT:
  258. return 'Number';
  259. default:
  260. return 'Text';
  261. }
  262. }
  263. }