Development

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

You must first sign up to be able to contribute.

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

Revision 33137, 9.0 kB (checked in by fabien, 3 years ago)

[1.4] fixed multiple database support in Propel plugin (for the generator and sfPropelData, closes #8345)

  • 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     // create the project base class for all forms
64     $file = sfConfig::get('sf_lib_dir').'/filter/BaseFormFilterPropel.class.php';
65     if (!file_exists($file))
66     {
67       if (!is_dir($directory = dirname($file)))
68       {
69         mkdir($directory, 0777, true);
70       }
71
72       file_put_contents($file, $this->evalTemplate('sfPropelFormFilterBaseTemplate.php'));
73     }
74
75     // create a form class for every Propel class
76     foreach ($this->dbMap->getTables() as $tableName => $table)
77     {
78       $behaviors = $table->getBehaviors();
79       if (isset($behaviors['symfony']['filter']) && 'false' === $behaviors['symfony']['filter'])
80       {
81         continue;
82       }
83
84       $this->table = $table;
85
86       // find the package to store filter forms in the same directory as the model classes
87       $packages = explode('.', constant(constant($table->getClassname().'::PEER').'::CLASS_DEFAULT'));
88       array_pop($packages);
89       if (false === $pos = array_search($this->params['model_dir_name'], $packages))
90       {
91         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')));
92       }
93       $packages[$pos] = $this->params['filter_dir_name'];
94       $baseDir = sfConfig::get('sf_root_dir').'/'.implode(DIRECTORY_SEPARATOR, $packages);
95
96       if (!is_dir($baseDir.'/base'))
97       {
98         mkdir($baseDir.'/base', 0777, true);
99       }
100
101       file_put_contents($baseDir.'/base/Base'.$table->getClassname().'FormFilter.class.php', $this->evalTemplate('sfPropelFormFilterGeneratedTemplate.php'));
102       if (!file_exists($classFile = $baseDir.'/'.$table->getClassname().'FormFilter.class.php'))
103       {
104         file_put_contents($classFile, $this->evalTemplate('sfPropelFormFilterTemplate.php'));
105       }
106     }
107   }
108
109   /**
110    * Returns a sfWidgetForm class name for a given column.
111    *
112    * @param  ColumnMap  $column A ColumnMap object
113    *
114    * @return string    The name of a subclass of sfWidgetForm
115    */
116   public function getWidgetClassForColumn(ColumnMap $column)
117   {
118     switch ($column->getType())
119     {
120       case PropelColumnTypes::BOOLEAN:
121         $name = 'Choice';
122         break;
123       case PropelColumnTypes::DATE:
124       case PropelColumnTypes::TIME:
125       case PropelColumnTypes::TIMESTAMP:
126         $name = 'FilterDate';
127         break;
128       default:
129         $name = 'FilterInput';
130     }
131
132     if ($column->isForeignKey())
133     {
134       $name = 'PropelChoice';
135     }
136
137     return sprintf('sfWidgetForm%s', $name);
138   }
139
140   /**
141    * Returns a PHP string representing options to pass to a widget for a given column.
142    *
143    * @param  ColumnMap $column  A ColumnMap object
144    *
145    * @return string    The options to pass to the widget as a PHP string
146    */
147   public function getWidgetOptionsForColumn(ColumnMap $column)
148   {
149     $options = array();
150
151     $withEmpty = $column->isNotNull() && !$column->isForeignKey() ? array("'with_empty' => false") : array();
152     switch ($column->getType())
153     {
154       case PropelColumnTypes::BOOLEAN:
155         $options[] = "'choices' => array('' => 'yes or no', 1 => 'yes', 0 => 'no')";
156         break;
157       case PropelColumnTypes::DATE:
158       case PropelColumnTypes::TIME:
159       case PropelColumnTypes::TIMESTAMP:
160         $options[] = "'from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate()";
161         $options = array_merge($options, $withEmpty);
162         break;
163       default:
164         $options = array_merge($options, $withEmpty);
165     }
166
167     if ($column->isForeignKey())
168     {
169       $options[] = sprintf('\'model\' => \'%s\', \'add_empty\' => true', $this->getForeignTable($column)->getClassname());
170
171       $refColumn = $this->getForeignTable($column)->getColumn($column->getRelatedColumnName());
172       if (!$refColumn->isPrimaryKey())
173       {
174         $options[] = sprintf('\'key_method\' => \'get%s\'', $refColumn->getPhpName());
175       }
176     }
177
178     return count($options) ? sprintf('array(%s)', implode(', ', $options)) : '';
179   }
180
181   /**
182    * Returns a sfValidator class name for a given column.
183    *
184    * @param  ColumnMap $column  A ColumnMap object
185    *
186    * @return string    The name of a subclass of sfValidator
187    */
188   public function getValidatorClassForColumn(ColumnMap $column)
189   {
190     switch ($column->getType())
191     {
192       case PropelColumnTypes::BOOLEAN:
193         $name = 'Choice';
194         break;
195       case PropelColumnTypes::DOUBLE:
196       case PropelColumnTypes::FLOAT:
197       case PropelColumnTypes::NUMERIC:
198       case PropelColumnTypes::DECIMAL:
199       case PropelColumnTypes::REAL:
200         $name = 'Number';
201         break;
202       case PropelColumnTypes::INTEGER:
203       case PropelColumnTypes::SMALLINT:
204       case PropelColumnTypes::TINYINT:
205       case PropelColumnTypes::BIGINT:
206         $name = 'Integer';
207         break;
208       case PropelColumnTypes::DATE:
209       case PropelColumnTypes::TIME:
210       case PropelColumnTypes::TIMESTAMP:
211         $name = 'DateRange';
212         break;
213       default:
214         $name = 'Pass';
215     }
216
217     if ($column->isPrimaryKey() || $column->isForeignKey())
218     {
219       $name = 'PropelChoice';
220     }
221
222     return sprintf('sfValidator%s', $name);
223   }
224
225   /**
226    * Returns a PHP string representing options to pass to a validator for a given column.
227    *
228    * @param  ColumnMap $column  A ColumnMap object
229    *
230    * @return string    The options to pass to the validator as a PHP string
231    */
232   public function getValidatorOptionsForColumn(ColumnMap $column)
233   {
234     $options = array('\'required\' => false');
235
236     if ($column->isForeignKey())
237     {
238       $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $this->getForeignTable($column)->getClassname(), $this->translateColumnName($column, true));
239     }
240     else if ($column->isPrimaryKey())
241     {
242       $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $column->getTable()->getClassname(), $this->translateColumnName($column));
243     }
244     else
245     {
246       switch ($column->getType())
247       {
248         case PropelColumnTypes::BOOLEAN:
249           $options[] = "'choices' => array('', 1, 0)";
250           break;
251         case PropelColumnTypes::DATE:
252         case PropelColumnTypes::TIME:
253         case PropelColumnTypes::TIMESTAMP:
254           $options[] = "'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false))";
255           break;
256       }
257     }
258
259     return count($options) ? sprintf('array(%s)', implode(', ', $options)) : '';
260   }
261
262   public function getValidatorForColumn($column)
263   {
264     $format = 'new %s(%s)';
265     if (in_array($class = $this->getValidatorClassForColumn($column), array('sfValidatorInteger', 'sfValidatorNumber')))
266     {
267       $format = 'new sfValidatorSchemaFilter(\'text\', new %s(%s))';
268     }
269
270     return sprintf($format, $class, $this->getValidatorOptionsForColumn($column));
271   }
272
273   public function getType(ColumnMap $column)
274   {
275     if ($column->isForeignKey())
276     {
277       return 'ForeignKey';
278     }
279
280     switch ($column->getType())
281     {
282       case PropelColumnTypes::BOOLEAN:
283         return 'Boolean';
284       case PropelColumnTypes::DATE:
285       case PropelColumnTypes::TIME:
286       case PropelColumnTypes::TIMESTAMP:
287         return 'Date';
288       case PropelColumnTypes::DOUBLE:
289       case PropelColumnTypes::FLOAT:
290       case PropelColumnTypes::NUMERIC:
291       case PropelColumnTypes::DECIMAL:
292       case PropelColumnTypes::REAL:
293       case PropelColumnTypes::INTEGER:
294       case PropelColumnTypes::SMALLINT:
295       case PropelColumnTypes::TINYINT:
296       case PropelColumnTypes::BIGINT:
297         return 'Number';
298       default:
299         return 'Text';
300     }
301   }
302 }
303
Note: See TracBrowser for help on using the browser.