Development

/branches/1.0/lib/generator/sfCrudGenerator.class.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/generator/sfCrudGenerator.class.php

Revision 8288, 11.6 kB (checked in by noel, 6 years ago)

added ignore_version_control() for every call to a finder (related to #2795)

  • 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) 2004-2006 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  * CRUD generator.
13  *
14  * This class generates a basic CRUD module.
15  *
16  * @package    symfony
17  * @subpackage generator
18  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19  * @version    SVN: $Id$
20  */
21 abstract class sfCrudGenerator extends sfGenerator
22 {
23   protected
24     $singularName  = '',
25     $pluralName    = '',
26     $peerClassName = '',
27     $map           = null,
28     $tableMap      = null,
29     $primaryKey    = array(),
30     $className     = '',
31     $params        = array();
32
33   /**
34    * Generates classes and templates in cache.
35    *
36    * @param array The parameters
37    *
38    * @return string The data to put in configuration cache
39    */
40   public function generate($params = array())
41   {
42     $this->params = $params;
43
44     $required_parameters = array('model_class', 'moduleName');
45     foreach ($required_parameters as $entry)
46     {
47       if (!isset($this->params[$entry]))
48       {
49         $error = 'You must specify a "%s"';
50         $error = sprintf($error, $entry);
51
52         throw new sfParseException($error);
53       }
54     }
55
56     $modelClass = $this->params['model_class'];
57
58     if (!class_exists($modelClass))
59     {
60       $error = 'Unable to scaffold unexistant model "%s"';
61       $error = sprintf($error, $modelClass);
62
63       throw new sfInitializationException($error);
64     }
65
66     $this->setScaffoldingClassName($modelClass);
67
68     // generated module name
69     $this->setGeneratedModuleName('auto'.ucfirst($this->params['moduleName']));
70     $this->setModuleName($this->params['moduleName']);
71
72     // get some model metadata
73     $this->loadMapBuilderClasses();
74
75     // load all primary keys
76     $this->loadPrimaryKeys();
77
78     // theme exists?
79     $theme = isset($this->params['theme']) ? $this->params['theme'] : 'default';
80     $themeDir = sfLoader::getGeneratorTemplate($this->getGeneratorClass(), $theme, '');
81     if (!is_dir($themeDir))
82     {
83       $error = 'The theme "%s" does not exist.';
84       $error = sprintf($error, $theme);
85       throw new sfConfigurationException($error);
86     }
87
88     $this->setTheme($theme);
89     $templateFiles = sfFinder::type('file')->ignore_version_control()->name('*.php')->relative()->in($themeDir.'/templates');
90     $configFiles = sfFinder::type('file')->ignore_version_control()->name('*.yml')->relative()->in($themeDir.'/config');
91
92     $this->generatePhpFiles($this->generatedModuleName, $templateFiles, $configFiles);
93
94     // require generated action class
95     $data = "require_once(sfConfig::get('sf_module_cache_dir').'/".$this->generatedModuleName."/actions/actions.class.php');\n";
96
97     return $data;
98   }
99
100   /**
101    * Returns PHP code for primary keys parameters.
102    *
103    * @param integer The indentation value
104    *
105    * @return string The PHP code
106    */
107   public function getRetrieveByPkParamsForAction($indent)
108   {
109     $params = array();
110     foreach ($this->getPrimaryKey() as $pk)
111     {
112       $params[] = "\$this->getRequestParameter('".sfInflector::underscore($pk->getPhpName())."')";
113     }
114
115     return implode(",\n".str_repeat(' ', max(0, $indent - strlen($this->singularName.$this->className))), $params);
116   }
117
118   /**
119    * Returns PHP code for getOrCreate() parameters.
120    *
121    * @return string The PHP code
122    */
123   public function getMethodParamsForGetOrCreate()
124   {
125     $method_params = array();
126     foreach ($this->getPrimaryKey() as $pk)
127     {
128       $fieldName       = sfInflector::underscore($pk->getPhpName());
129       $method_params[] = "\$$fieldName = '$fieldName'";
130     }
131
132     return implode(', ', $method_params);
133   }
134
135   /**
136    * Returns PHP code for getOrCreate() promary keys condition.
137    *
138    * @param boolean true if we pass the field name as an argument, false otherwise
139    *
140    * @return string The PHP code
141    */
142   public function getTestPksForGetOrCreate($fieldNameAsArgument = true)
143   {
144     $test_pks = array();
145     foreach ($this->getPrimaryKey() as $pk)
146     {
147       $fieldName  = sfInflector::underscore($pk->getPhpName());
148       $test_pks[] = sprintf("!\$this->getRequestParameter(%s)", $fieldNameAsArgument ? "\$$fieldName" : "'".$fieldName."'");
149     }
150
151     return implode("\n     || ", $test_pks);
152   }
153
154   /**
155    * Returns PHP code for primary keys parameters used in getOrCreate() method.
156    *
157    * @return string The PHP code
158    */
159   public function getRetrieveByPkParamsForGetOrCreate()
160   {
161     $retrieve_params = array();
162     foreach ($this->getPrimaryKey() as $pk)
163     {
164       $fieldName         = sfInflector::underscore($pk->getPhpName());
165       $retrieve_params[] = "\$this->getRequestParameter(\$$fieldName)";
166     }
167
168     return implode(",\n".str_repeat(' ', max(0, 45 - strlen($this->singularName.$this->className))), $retrieve_params);
169   }
170
171   /**
172    * Gets the table map for the current model class.
173    *
174    * @return TableMap A TableMap instance
175    */
176   public function getTableMap()
177   {
178     return $this->tableMap;
179   }
180
181   /**
182    * Sets the class name to use for scaffolding
183    *
184    * @param  string class name
185    */
186   protected function setScaffoldingClassName($className)
187   {
188     $this->singularName  = sfInflector::underscore($className);
189     $this->pluralName    = $this->singularName.'s';
190     $this->className     = $className;
191     $this->peerClassName = $className.'Peer';
192   }
193
194   /**
195    * Gets the singular name for current scaffolding class.
196    *
197    * @return string
198    */
199   public function getSingularName()
200   {
201     return $this->singularName;
202   }
203
204   /**
205    * Gets the plural name for current scaffolding class.
206    *
207    * @return string
208    */
209   public function getPluralName()
210   {
211     return $this->pluralName;
212   }
213
214   /**
215    * Gets the class name for current scaffolding class.
216    *
217    * @return string
218    */
219   public function getClassName()
220   {
221     return $this->className;
222   }
223
224   /**
225    * Gets the Peer class name.
226    *
227    * @return string
228    */
229   public function getPeerClassName()
230   {
231     return $this->peerClassName;
232   }
233
234   /**
235    * Gets the primary key name.
236    *
237    * @return string
238    */
239   public function getPrimaryKey()
240   {
241     return $this->primaryKey;
242   }
243
244   /**
245    * Gets the Map object.
246    *
247    * @return object
248    */
249   public function getMap()
250   {
251     return $this->map;
252   }
253
254   /**
255    * Returns PHP code to add to a URL for primary keys.
256    *
257    * @param string The prefix value
258    *
259    * @return string PHP code
260    */
261   public function getPrimaryKeyUrlParams($prefix = '')
262   {
263     $params = array();
264     foreach ($this->getPrimaryKey() as $pk)
265     {
266       $phpName   = $pk->getPhpName();
267       $fieldName = sfInflector::underscore($phpName);
268       $params[]  = "$fieldName='.".$this->getColumnGetter($pk, true, $prefix);
269     }
270
271     return implode(".'&", $params);
272   }
273
274   /**
275    * Gets PHP code for primary key condition.
276    *
277    * @param string The prefix value
278    *
279    * @return string PHP code
280    */
281   public function getPrimaryKeyIsSet($prefix = '')
282   {
283     $params = array();
284     foreach ($this->getPrimaryKey() as $pk)
285     {
286       $params[] = $this->getColumnGetter($pk, true, $prefix);
287     }
288
289     return implode(' && ', $params);
290   }
291
292   /**
293    * Gets object tag parameters.
294    *
295    * @param array An array of parameters
296    * @param array An array of default parameters
297    *
298    * @return string PHP code
299    */
300   protected function getObjectTagParams($params, $default_params = array())
301   {
302     return var_export(array_merge($default_params, $params), true);
303   }
304
305   /**
306    * Returns HTML code for a column in list mode.
307    *
308    * @param string  The column name
309    * @param array   The parameters
310    *
311    * @return string HTML code
312    */
313   public function getColumnListTag($column, $params = array())
314   {
315     $type = $column->getCreoleType();
316
317     $columnGetter = $this->getColumnGetter($column, true);
318
319     if ($type == CreoleTypes::TIMESTAMP)
320     {
321       return "format_date($columnGetter, 'f')";
322     }
323     elseif ($type == CreoleTypes::DATE)
324     {
325       return "format_date($columnGetter, 'D')";
326     }
327     else
328     {
329       return "$columnGetter";
330     }
331   }
332
333   /**
334    * Returns HTML code for a column in edit mode.
335    *
336    * @param string  The column name
337    * @param array   The parameters
338    *
339    * @return string HTML code
340    */
341   public function getCrudColumnEditTag($column, $params = array())
342   {
343     $type = $column->getCreoleType();
344
345     if ($column->isForeignKey())
346     {
347       if (!$column->isNotNull() && !isset($params['include_blank']))
348       {
349         $params['include_blank'] = true;
350       }
351
352       return $this->getPHPObjectHelper('select_tag', $column, $params, array('related_class' => $this->getRelatedClassName($column)));
353     }
354     else if ($type == CreoleTypes::DATE)
355     {
356       // rich=false not yet implemented
357       return $this->getPHPObjectHelper('input_date_tag', $column, $params, array('rich' => true));
358     }
359     else if ($type == CreoleTypes::TIMESTAMP)
360     {
361       // rich=false not yet implemented
362       return $this->getPHPObjectHelper('input_date_tag', $column, $params, array('rich' => true, 'withtime' => true));
363     }
364     else if ($type == CreoleTypes::BOOLEAN)
365     {
366       return $this->getPHPObjectHelper('checkbox_tag', $column, $params);
367     }
368     else if ($type == CreoleTypes::CHAR || $type == CreoleTypes::VARCHAR)
369     {
370       $size = ($column->getSize() > 20 ? ($column->getSize() < 80 ? $column->getSize() : 80) : 20);
371       return $this->getPHPObjectHelper('input_tag', $column, $params, array('size' => $size));
372     }
373     else if ($type == CreoleTypes::INTEGER || $type == CreoleTypes::TINYINT || $type == CreoleTypes::SMALLINT || $type == CreoleTypes::BIGINT)
374     {
375       return $this->getPHPObjectHelper('input_tag', $column, $params, array('size' => 7));
376     }
377     else if ($type == CreoleTypes::FLOAT || $type == CreoleTypes::DOUBLE || $type == CreoleTypes::DECIMAL || $type == CreoleTypes::NUMERIC || $type == CreoleTypes::REAL)
378     {
379       return $this->getPHPObjectHelper('input_tag', $column, $params, array('size' => 7));
380     }
381     else if ($type == CreoleTypes::TEXT || $type == CreoleTypes::LONGVARCHAR)
382     {
383       return $this->getPHPObjectHelper('textarea_tag', $column, $params, array('size' => '30x3'));
384     }
385     else
386     {
387       return $this->getPHPObjectHelper('input_tag', $column, $params, array('disabled' => true));
388     }
389   }
390
391   /**
392    * Loads primary keys.
393    *
394    * This method is ORM dependant.
395    *
396    * @throws sfException
397    */
398   abstract protected function loadPrimaryKeys();
399
400   /**
401    * Loads map builder classes.
402    *
403    * This method is ORM dependant.
404    *
405    * @throws sfException
406    */
407   abstract protected function loadMapBuilderClasses();
408
409   /**
410    * Generates a PHP call to an object helper.
411    *
412    * This method is ORM dependant.
413    *
414    * @param string The helper name
415    * @param string The column name
416    * @param array  An array of parameters
417    * @param array  An array of local parameters
418    *
419    * @return string PHP code
420    */
421   abstract function getPHPObjectHelper($helperName, $column, $params, $localParams = array());
422
423   /**
424    * Returns the getter either non-developped: 'getFoo' or developped: '$class->getFoo()'.
425    *
426    * This method is ORM dependant.
427    *
428    * @param string  The column name
429    * @param boolean true if you want developped method names, false otherwise
430    * @param string The prefix value
431    *
432    * @return string PHP code
433    */
434   abstract function getColumnGetter($column, $developed = false , $prefix = '');
435
436   /*
437    * Gets the PHP name of the related class name.
438    *
439    * Used for foreign keys only; this method should be removed when we use sfAdminColumn instead.
440    *
441    * This method is ORM dependant.
442    *
443    * @param string The column name
444    *
445    * @return string The PHP name of the related class name
446    */
447   abstract function getRelatedClassName($column);
448 }
449
Note: See TracBrowser for help on using the browser.