Development

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

You must first sign up to be able to contribute.

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

Revision 17865, 12.5 kB (checked in by FabianLange, 5 years ago)

[1.3] fixed some codeing standards (fixes #6376 - patch from gimler)

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