Development

/plugins/sfPropelPlugin/trunk/lib/generator/sfPropelFormFilterGenerator.class.php

You must first sign up to be able to contribute.

root/plugins/sfPropelPlugin/trunk/lib/generator/sfPropelFormFilterGenerator.class.php

Revision 11675, 8.1 kB (checked in by fabien, 5 years ago)

[1.2] added the new filtering system based on the new form framework

  • 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
166     return count($options) ? sprintf('array(%s)', implode(', ', $options)) : '';
167   }
168
169   /**
170    * Returns a sfValidator class name for a given column.
171    *
172    * @param  ColumnMap $column  A ColumnMap object
173    *
174    * @return string    The name of a subclass of sfValidator
175    */
176   public function getValidatorClassForColumn(ColumnMap $column)
177   {
178     switch ($column->getType())
179     {
180       case PropelColumnTypes::BOOLEAN:
181         $name = 'Choice';
182         break;
183       case PropelColumnTypes::DOUBLE:
184       case PropelColumnTypes::FLOAT:
185       case PropelColumnTypes::NUMERIC:
186       case PropelColumnTypes::DECIMAL:
187       case PropelColumnTypes::REAL:
188         $name = 'Number';
189         break;
190       case PropelColumnTypes::INTEGER:
191       case PropelColumnTypes::SMALLINT:
192       case PropelColumnTypes::TINYINT:
193       case PropelColumnTypes::BIGINT:
194         $name = 'Integer';
195         break;
196       case PropelColumnTypes::DATE:
197       case PropelColumnTypes::TIME:
198       case PropelColumnTypes::TIMESTAMP:
199         $name = 'DateRange';
200         break;
201       default:
202         $name = 'Pass';
203     }
204
205     if ($column->isPrimaryKey() || $column->isForeignKey())
206     {
207       $name = 'PropelChoice';
208     }
209
210     return sprintf('sfValidator%s', $name);
211   }
212
213   /**
214    * Returns a PHP string representing options to pass to a validator for a given column.
215    *
216    * @param  ColumnMap $column  A ColumnMap object
217    *
218    * @return string    The options to pass to the validator as a PHP string
219    */
220   public function getValidatorOptionsForColumn(ColumnMap $column)
221   {
222     $options = array('\'required\' => false');
223
224     if ($column->isForeignKey())
225     {
226       $map = call_user_func(array(constant($this->getForeignTable($column)->getClassname().'::PEER'), 'getTableMap'));
227       foreach ($map->getColumns() as $primaryKey)
228       {
229         if ($primaryKey->isPrimaryKey())
230         {
231           break;
232         }
233       }
234
235       $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $this->getForeignTable($column)->getClassname(), strtolower($primaryKey->getColumnName()));
236     }
237     else if ($column->isPrimaryKey())
238     {
239       $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $column->getTable()->getClassname(), strtolower($column->getColumnName()));
240     }
241     else
242     {
243       switch ($column->getType())
244       {
245         case PropelColumnTypes::BOOLEAN:
246           $options[] = "'choices' => array('', 1, 0)";
247           break;
248         case PropelColumnTypes::DATE:
249         case PropelColumnTypes::TIME:
250         case PropelColumnTypes::TIMESTAMP:
251           $options[] = "'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false))";
252           break;
253       }
254     }
255
256     return count($options) ? sprintf('array(%s)', implode(', ', $options)) : '';
257   }
258
259   public function getType(ColumnMap $column)
260   {
261     if ($column->isForeignKey())
262     {
263       return 'ForeignKey';
264     }
265
266     switch ($column->getType())
267     {
268       case PropelColumnTypes::BOOLEAN:
269         return 'Boolean';
270       case PropelColumnTypes::DATE:
271       case PropelColumnTypes::TIME:
272       case PropelColumnTypes::TIMESTAMP:
273         return 'Date';
274       default:
275         return 'Text';
276     }
277   }
278 }
279
Note: See TracBrowser for help on using the browser.