Development

/branches/1.2/lib/plugins/sfPropelPlugin/lib/generator/sfPropelFormFilterGenerator.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/plugins/sfPropelPlugin/lib/generator/sfPropelFormFilterGenerator.class.php

Revision 16976, 8.8 kB (checked in by fabien, 6 years ago)

fixed Propel forms, filters, and admin generator when column names contain uppercase letters (closes #6151, #6187, #5030, #5610, #2855)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * Propel filter form generator.
13  *
14  * This class generates a Propel filter forms.
15  *
16  * @package    symfony
17  * @subpackage generator
18  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19  * @version    SVN: $Id$
20  */
21 class sfPropelFormFilterGenerator extends sfPropelFormGenerator
22 {
23   /**
24    * Initializes the current sfGenerator instance.
25    *
26    * @param sfGeneratorManager $generatorManager A sfGeneratorManager instance
27    */
28   public function initialize(sfGeneratorManager $generatorManager)
29   {
30     parent::initialize($generatorManager);
31
32     $this->setGeneratorClass('sfPropelFormFilter');
33   }
34
35   /**
36    * Generates classes and templates in cache.
37    *
38    * @param array $params The parameters
39    *
40    * @return string The data to put in configuration cache
41    */
42   public function generate($params = array())
43   {
44     $this->params = $params;
45
46     if (!isset($this->params['connection']))
47     {
48       throw new sfParseException('You must specify a "connection" parameter.');
49     }
50
51     if (!isset($this->params['model_dir_name']))
52     {
53       $this->params['model_dir_name'] = 'model';
54     }
55
56     if (!isset($this->params['filter_dir_name']))
57     {
58       $this->params['filter_dir_name'] = 'filter';
59     }
60
61     $this->loadBuilders();
62
63     $this->dbMap = Propel::getDatabaseMap($this->params['connection']);
64
65     // create the project base class for all forms
66     $file = sfConfig::get('sf_lib_dir').'/filter/base/BaseFormFilterPropel.class.php';
67     if (!file_exists($file))
68     {
69       if (!is_dir(sfConfig::get('sf_lib_dir').'/filter/base'))
70       {
71         mkdir(sfConfig::get('sf_lib_dir').'/filter/base', 0777, true);
72       }
73
74       file_put_contents($file, $this->evalTemplate('sfPropelFormFilterBaseTemplate.php'));
75     }
76
77     // create a form class for every Propel class
78     foreach ($this->dbMap->getTables() as $tableName => $table)
79     {
80       $this->table = $table;
81
82       // find the package to store filter forms in the same directory as the model classes
83       $packages = explode('.', constant(constant($table->getClassname().'::PEER').'::CLASS_DEFAULT'));
84       array_pop($packages);
85       if (false === $pos = array_search($this->params['model_dir_name'], $packages))
86       {
87         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')));
88       }
89       $packages[$pos] = $this->params['filter_dir_name'];
90       $baseDir = sfConfig::get('sf_root_dir').'/'.implode(DIRECTORY_SEPARATOR, $packages);
91
92       if (!is_dir($baseDir.'/base'))
93       {
94         mkdir($baseDir.'/base', 0777, true);
95       }
96
97       file_put_contents($baseDir.'/base/Base'.$table->getClassname().'FormFilter.class.php', $this->evalTemplate('sfPropelFormFilterGeneratedTemplate.php'));
98       if (!file_exists($classFile = $baseDir.'/'.$table->getClassname().'FormFilter.class.php'))
99       {
100         file_put_contents($classFile, $this->evalTemplate('sfPropelFormFilterTemplate.php'));
101       }
102     }
103   }
104
105   /**
106    * Returns a sfWidgetForm class name for a given column.
107    *
108    * @param  ColumnMap  $column A ColumnMap object
109    *
110    * @return string    The name of a subclass of sfWidgetForm
111    */
112   public function getWidgetClassForColumn(ColumnMap $column)
113   {
114     switch ($column->getType())
115     {
116       case PropelColumnTypes::BOOLEAN:
117         $name = 'Choice';
118         break;
119       case PropelColumnTypes::DATE:
120       case PropelColumnTypes::TIME:
121       case PropelColumnTypes::TIMESTAMP:
122         $name = 'FilterDate';
123         break;
124       default:
125         $name = 'FilterInput';
126     }
127
128     if ($column->isForeignKey())
129     {
130       $name = 'PropelChoice';
131     }
132
133     return sprintf('sfWidgetForm%s', $name);
134   }
135
136   /**
137    * Returns a PHP string representing options to pass to a widget for a given column.
138    *
139    * @param  ColumnMap $column  A ColumnMap object
140    *
141    * @return string    The options to pass to the widget as a PHP string
142    */
143   public function getWidgetOptionsForColumn(ColumnMap $column)
144   {
145     $options = array();
146
147     $withEmpty = sprintf('\'with_empty\' => %s', $column->isNotNull() ? 'false' : 'true');
148     switch ($column->getType())
149     {
150       case PropelColumnTypes::BOOLEAN:
151         $options[] = "'choices' => array('' => 'yes or no', 1 => 'yes', 0 => 'no')";
152         break;
153       case PropelColumnTypes::DATE:
154       case PropelColumnTypes::TIME:
155       case PropelColumnTypes::TIMESTAMP:
156         $options[] = "'from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate()";
157         $options[] = $withEmpty;
158         break;
159     }
160
161     if ($column->isForeignKey())
162     {
163       $options[] = sprintf('\'model\' => \'%s\', \'add_empty\' => true', $this->getForeignTable($column)->getClassname());
164
165       $refColumn = $this->getForeignTable($column)->getColumn($column->getRelatedColumnName());
166       if (!$refColumn->isPrimaryKey())
167       {
168         $options[] = sprintf('\'key_method\' => \'get%s\'', $refColumn->getPhpName());
169       }
170     }
171
172     return count($options) ? sprintf('array(%s)', implode(', ', $options)) : '';
173   }
174
175   /**
176    * Returns a sfValidator class name for a given column.
177    *
178    * @param  ColumnMap $column  A ColumnMap object
179    *
180    * @return string    The name of a subclass of sfValidator
181    */
182   public function getValidatorClassForColumn(ColumnMap $column)
183   {
184     switch ($column->getType())
185     {
186       case PropelColumnTypes::BOOLEAN:
187         $name = 'Choice';
188         break;
189       case PropelColumnTypes::DOUBLE:
190       case PropelColumnTypes::FLOAT:
191       case PropelColumnTypes::NUMERIC:
192       case PropelColumnTypes::DECIMAL:
193       case PropelColumnTypes::REAL:
194         $name = 'Number';
195         break;
196       case PropelColumnTypes::INTEGER:
197       case PropelColumnTypes::SMALLINT:
198       case PropelColumnTypes::TINYINT:
199       case PropelColumnTypes::BIGINT:
200         $name = 'Integer';
201         break;
202       case PropelColumnTypes::DATE:
203       case PropelColumnTypes::TIME:
204       case PropelColumnTypes::TIMESTAMP:
205         $name = 'DateRange';
206         break;
207       default:
208         $name = 'Pass';
209     }
210
211     if ($column->isPrimaryKey() || $column->isForeignKey())
212     {
213       $name = 'PropelChoice';
214     }
215
216     return sprintf('sfValidator%s', $name);
217   }
218
219   /**
220    * Returns a PHP string representing options to pass to a validator for a given column.
221    *
222    * @param  ColumnMap $column  A ColumnMap object
223    *
224    * @return string    The options to pass to the validator as a PHP string
225    */
226   public function getValidatorOptionsForColumn(ColumnMap $column)
227   {
228     $options = array('\'required\' => false');
229
230     if ($column->isForeignKey())
231     {
232       $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $this->getForeignTable($column)->getClassname(), $this->translateColumnName($column, true));
233     }
234     else if ($column->isPrimaryKey())
235     {
236       $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $column->getTable()->getClassname(), $this->translateColumnName($column));
237     }
238     else
239     {
240       switch ($column->getType())
241       {
242         case PropelColumnTypes::BOOLEAN:
243           $options[] = "'choices' => array('', 1, 0)";
244           break;
245         case PropelColumnTypes::DATE:
246         case PropelColumnTypes::TIME:
247         case PropelColumnTypes::TIMESTAMP:
248           $options[] = "'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false))";
249           break;
250       }
251     }
252
253     return count($options) ? sprintf('array(%s)', implode(', ', $options)) : '';
254   }
255
256   public function getValidatorForColumn($column)
257   {
258     $format = 'new %s(%s)';
259     if (in_array($class = $this->getValidatorClassForColumn($column), array('sfValidatorInteger', 'sfValidatorNumber')))
260     {
261       $format = 'new sfValidatorSchemaFilter(\'text\', new %s(%s))';
262     }
263
264     return sprintf($format, $class, $this->getValidatorOptionsForColumn($column));
265   }
266
267   public function getType(ColumnMap $column)
268   {
269     if ($column->isForeignKey())
270     {
271       return 'ForeignKey';
272     }
273
274     switch ($column->getType())
275     {
276       case PropelColumnTypes::BOOLEAN:
277         return 'Boolean';
278       case PropelColumnTypes::DATE:
279       case PropelColumnTypes::TIME:
280       case PropelColumnTypes::TIMESTAMP:
281         return 'Date';
282       case PropelColumnTypes::DOUBLE:
283       case PropelColumnTypes::FLOAT:
284       case PropelColumnTypes::NUMERIC:
285       case PropelColumnTypes::DECIMAL:
286       case PropelColumnTypes::REAL:
287       case PropelColumnTypes::INTEGER:
288       case PropelColumnTypes::SMALLINT:
289       case PropelColumnTypes::TINYINT:
290       case PropelColumnTypes::BIGINT:
291         return 'Number';
292       default:
293         return 'Text';
294     }
295   }
296 }
297
Note: See TracBrowser for help on using the browser.