Development

/branches/1.3/lib/plugins/sfPropelPlugin/lib/generator/sfPropelCrudGenerator.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/plugins/sfPropelPlugin/lib/generator/sfPropelCrudGenerator.class.php

Revision 22881, 13.8 kB (checked in by Kris.Wallsmith, 5 years ago)

[1.3] merged changes from sfPropel14Plugin sandbox

  • 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  * Propel CRUD generator.
13  *
14  * This class generates a basic CRUD module with propel.
15  *
16  * @package    symfony
17  * @subpackage propel
18  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19  * @version    SVN: $Id$
20  */
21 class sfPropelCrudGenerator extends sfAdminGenerator
22 {
23   protected
24     $peerClassName = '';
25
26   /**
27    * Initializes the current sfGenerator instance.
28    *
29    * @param sfGeneratorManager $generatorManager A sfGeneratorManager instance
30    */
31   public function initialize(sfGeneratorManager $generatorManager)
32   {
33     parent::initialize($generatorManager);
34
35     $this->setGeneratorClass('sfPropelCrud');
36   }
37
38   /**
39    * Sets the class name to use for scaffolding
40    *
41    * @param string $className class name
42    */
43   protected function setScaffoldingClassName($className)
44   {
45     parent::setScaffoldingClassName($className);
46
47     $this->peerClassName = constant($className.'::PEER');
48   }
49
50   /**
51    * Loads primary keys.
52    *
53    * This method is ORM dependant.
54    *
55    * @throws sfException
56    */
57   protected function loadPrimaryKeys()
58   {
59     foreach ($this->tableMap->getColumns() as $column)
60     {
61       if ($column->isPrimaryKey())
62       {
63         $this->primaryKey[] = $column;
64       }
65     }
66
67     if (!count($this->primaryKey))
68     {
69       throw new sfException(sprintf('Cannot generate a module for a model without a primary key (%s)', $this->className));
70     }
71   }
72
73   /**
74    * Loads map builder classes.
75    *
76    * This method is ORM dependant.
77    *
78    * @throws sfException
79    */
80   protected function loadMapBuilderClasses()
81   {
82     $this->tableMap = call_user_func(array($this->peerClassName, 'getTableMap'));
83     // load all related table maps,
84     // and all tables related to the related table maps (for m2m relations)
85     foreach ($this->tableMap->getRelations() as $relation)
86     {
87       $relation->getForeignTable()->getRelations();
88     }
89     $this->map = $this->tableMap;
90   }
91
92   /**
93    * Generates a PHP call to an object helper.
94    *
95    * @param string $helperName  The helper name
96    * @param string $column      The column name
97    * @param array  $params      An array of parameters
98    * @param array  $localParams An array of local parameters
99    *
100    * @return string PHP code
101    */
102   function getPHPObjectHelper($helperName, $column, $params, $localParams = array())
103   {
104     $params = $this->getObjectTagParams($params, $localParams);
105
106     return sprintf('object_%s($%s, \'%s\', %s)', $helperName, $this->getSingularName(), $this->getColumnGetter($column, false), $params);
107   }
108
109   /**
110    * Returns the getter either non-developped: 'getFoo' or developped: '$class->getFoo()'.
111    *
112    * @param string  $column     The column name
113    * @param boolean $developed  true if you want developped method names, false otherwise
114    * @param string  $prefix     The prefix value
115    *
116    * @return string PHP code
117    */
118   function getColumnGetter($column, $developed = false, $prefix = '')
119   {
120     $getter = 'get'.$column->getPhpName();
121     if ($developed)
122     {
123       $getter = sprintf('$%s%s->%s()', $prefix, $this->getSingularName(), $getter);
124     }
125
126     return $getter;
127   }
128
129   /*
130    * Gets the PHP name of the related class name.
131    *
132    * Used for foreign keys only; this method should be removed when we use sfAdminColumn instead.
133    *
134    * @param string $column  The column name
135    *
136    * @return string The PHP name of the related class name
137    */
138   function getRelatedClassName($column)
139   {
140     $relatedTable = $this->getMap()->getDatabaseMap()->getTable($column->getRelatedTableName());
141
142     return $relatedTable->getPhpName();
143   }
144
145   /**
146    * Returns HTML code for a column in edit mode.
147    *
148    * @param string  The column name
149    * @param array   The parameters
150    *
151    * @return string HTML code
152    */
153   public function getCrudColumnEditTag($column, $params = array())
154   {
155     $type = $column->getType();
156
157     if ($column->isForeignKey())
158     {
159       if (!$column->isNotNull() && !isset($params['include_blank']))
160       {
161         $params['include_blank'] = true;
162       }
163
164       return $this->getPHPObjectHelper('select_tag', $column, $params, array('related_class' => $this->getRelatedClassName($column)));
165     }
166     else if ($type == PropelColumnTypes::DATE)
167     {
168       // rich=false not yet implemented
169       return $this->getPHPObjectHelper('input_date_tag', $column, $params, array('rich' => true));
170     }
171     else if ($type == PropelColumnTypes::TIMESTAMP)
172     {
173       // rich=false not yet implemented
174       return $this->getPHPObjectHelper('input_date_tag', $column, $params, array('rich' => true, 'withtime' => true));
175     }
176     else if ($type == PropelColumnTypes::BOOLEAN)
177     {
178       return $this->getPHPObjectHelper('checkbox_tag', $column, $params);
179     }
180     else if ($type == PropelColumnTypes::CHAR || $type == PropelColumnTypes::VARCHAR)
181     {
182       $size = ($column->getSize() > 20 ? ($column->getSize() < 80 ? $column->getSize() : 80) : 20);
183       return $this->getPHPObjectHelper('input_tag', $column, $params, array('size' => $size));
184     }
185     else if ($type == PropelColumnTypes::INTEGER || $type == PropelColumnTypes::TINYINT || $type == PropelColumnTypes::SMALLINT || $type == PropelColumnTypes::BIGINT)
186     {
187       return $this->getPHPObjectHelper('input_tag', $column, $params, array('size' => 7));
188     }
189     else if ($type == PropelColumnTypes::FLOAT || $type == PropelColumnTypes::DOUBLE || $type == PropelColumnTypes::DECIMAL || $type == PropelColumnTypes::NUMERIC || $type == PropelColumnTypes::REAL)
190     {
191       return $this->getPHPObjectHelper('input_tag', $column, $params, array('size' => 7));
192     }
193     else if ($type == PropelColumnTypes::LONGVARCHAR || $type == PropelColumnTypes::CLOB)
194     {
195       return $this->getPHPObjectHelper('textarea_tag', $column, $params, array('size' => '30x3'));
196     }
197     else
198     {
199       return $this->getPHPObjectHelper('input_tag', $column, $params, array('disabled' => true));
200     }
201   }
202
203   /**
204    * Returns HTML code for a column in filter mode.
205    *
206    * @param string  The column name
207    * @param array   The parameters
208    *
209    * @return string HTML code
210    */
211   public function getColumnFilterTag($column, $params = array())
212   {
213     $user_params = $this->getParameterValue('list.fields.'.$column->getName().'.params');
214     $user_params = is_array($user_params) ? $user_params : sfToolkit::stringToArray($user_params);
215     $params      = $user_params ? array_merge($params, $user_params) : $params;
216
217     if ($column->isComponent())
218     {
219       return "get_component('".$this->getModuleName()."', '".$column->getName()."', array('type' => 'filter', 'filters' => \$filters))";
220     }
221     else if ($column->isPartial())
222     {
223       return "get_partial('".$column->getName()."', array('type' => 'filter', 'filters' => \$filters))";
224     }
225
226     $type = $column->getType();
227
228     $default_value = "isset(\$filters['".$column->getName()."']) ? \$filters['".$column->getName()."'] : null";
229     $unquotedName = 'filters['.$column->getName().']';
230     $name = "'$unquotedName'";
231
232     if ($column->isForeignKey())
233     {
234       $params = $this->getObjectTagParams($params, array('include_blank' => true, 'related_class'=>$this->getRelatedClassName($column), 'text_method'=>'__toString', 'control_name'=>$unquotedName));
235       return "object_select_tag($default_value, null, $params)";
236
237     }
238     else if ($type == PropelColumnTypes::DATE)
239     {
240       // rich=false not yet implemented
241       $params = $this->getObjectTagParams($params, array('rich' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png'));
242       return "input_date_range_tag($name, $default_value, $params)";
243     }
244     else if ($type == PropelColumnTypes::TIMESTAMP)
245     {
246       // rich=false not yet implemented
247       $params = $this->getObjectTagParams($params, array('rich' => true, 'withtime' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png'));
248       return "input_date_range_tag($name, $default_value, $params)";
249     }
250     else if ($type == PropelColumnTypes::BOOLEAN)
251     {
252       $defaultIncludeCustom = '__("yes or no")';
253
254       $option_params = $this->getObjectTagParams($params, array('include_custom' => $defaultIncludeCustom));
255       $params = $this->getObjectTagParams($params);
256
257       // little hack
258       $option_params = preg_replace("/'".preg_quote($defaultIncludeCustom)."'/", $defaultIncludeCustom, $option_params);
259
260       $options = "options_for_select(array(1 => __('yes'), 0 => __('no')), $default_value, $option_params)";
261
262       return "select_tag($name, $options, $params)";
263     }
264     else if ($type == PropelColumnTypes::CHAR || $type == PropelColumnTypes::VARCHAR || $type == PropelColumnTypes::LONGVARCHAR || $type == PropelColumnTypes::CLOB)
265     {
266       $size = ($column->getSize() < 15 ? $column->getSize() : 15);
267       $params = $this->getObjectTagParams($params, array('size' => $size));
268       return "input_tag($name, $default_value, $params)";
269     }
270     else if ($type == PropelColumnTypes::INTEGER || $type == PropelColumnTypes::TINYINT || $type == PropelColumnTypes::SMALLINT || $type == PropelColumnTypes::BIGINT)
271     {
272       $params = $this->getObjectTagParams($params, array('size' => 7));
273       return "input_tag($name, $default_value, $params)";
274     }
275     else if ($type == PropelColumnTypes::FLOAT || $type == PropelColumnTypes::DOUBLE || $type == PropelColumnTypes::DECIMAL || $type == PropelColumnTypes::NUMERIC || $type == PropelColumnTypes::REAL)
276     {
277       $params = $this->getObjectTagParams($params, array('size' => 7));
278       return "input_tag($name, $default_value, $params)";
279     }
280     else
281     {
282       $params = $this->getObjectTagParams($params, array('disabled' => true));
283       return "input_tag($name, $default_value, $params)";
284     }
285   }
286
287   /**
288    * Returns HTML code for a column in edit mode.
289    *
290    * @param string  The column name
291    * @param array   The parameters
292    *
293    * @return string HTML code
294    */
295   public function getColumnEditTag($column, $params = array())
296   {
297     // user defined parameters
298     $user_params = $this->getParameterValue('edit.fields.'.$column->getName().'.params');
299     $user_params = is_array($user_params) ? $user_params : sfToolkit::stringToArray($user_params);
300     $params      = $user_params ? array_merge($params, $user_params) : $params;
301
302     if ($column->isComponent())
303     {
304       return "get_component('".$this->getModuleName()."', '".$column->getName()."', array('type' => 'edit', '{$this->getSingularName()}' => \${$this->getSingularName()}))";
305     }
306     else if ($column->isPartial())
307     {
308       return "get_partial('".$column->getName()."', array('type' => 'edit', '{$this->getSingularName()}' => \${$this->getSingularName()}))";
309     }
310
311     // default control name
312     $params = array_merge(array('control_name' => $this->getSingularName().'['.$column->getName().']'), $params);
313
314     // default parameter values
315     $type = $column->getType();
316     if ($type == PropelColumnTypes::DATE)
317     {
318       $params = array_merge(array('rich' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png'), $params);
319     }
320     else if ($type == PropelColumnTypes::TIMESTAMP)
321     {
322       $params = array_merge(array('rich' => true, 'withtime' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png'), $params);
323     }
324
325     // user sets a specific tag to use
326     if ($inputType = $this->getParameterValue('edit.fields.'.$column->getName().'.type'))
327     {
328       if ($inputType == 'plain')
329       {
330         return $this->getColumnListTag($column, $params);
331       }
332       else
333       {
334         return $this->getPHPObjectHelper($inputType, $column, $params);
335       }
336     }
337
338     // guess the best tag to use with column type
339     return $this->getCrudColumnEditTag($column, $params);
340   }
341
342   /**
343    * Returns HTML code for a column in list mode.
344    *
345    * @param string  The column name
346    * @param array   The parameters
347    *
348    * @return string HTML code
349    */
350   public function getColumnListTag($column, $params = array())
351   {
352     $user_params = $this->getParameterValue('list.fields.'.$column->getName().'.params');
353     $user_params = is_array($user_params) ? $user_params : sfToolkit::stringToArray($user_params);
354     $params      = $user_params ? array_merge($params, $user_params) : $params;
355
356     $type = $column->getType();
357
358     $columnGetter = $this->getColumnGetter($column, true);
359
360     if ($column->isComponent())
361     {
362       return "get_component('".$this->getModuleName()."', '".$column->getName()."', array('type' => 'list', '{$this->getSingularName()}' => \${$this->getSingularName()}))";
363     }
364     else if ($column->isPartial())
365     {
366       return "get_partial('".$column->getName()."', array('type' => 'list', '{$this->getSingularName()}' => \${$this->getSingularName()}))";
367     }
368     else if ($type == PropelColumnTypes::DATE || $type == PropelColumnTypes::TIMESTAMP)
369     {
370       $format = isset($params['date_format']) ? $params['date_format'] : ($type == PropelColumnTypes::DATE ? 'D' : 'f');
371       return "($columnGetter !== null && $columnGetter !== '') ? format_date($columnGetter, \"$format\") : ''";
372     }
373     elseif ($type == PropelColumnTypes::BOOLEAN)
374     {
375       return "$columnGetter ? image_tag(sfConfig::get('sf_admin_web_dir').'/images/tick.png') : '&nbsp;'";
376     }
377     else
378     {
379       return "$columnGetter";
380     }
381   }
382
383   /**
384    * Gets the Peer class name.
385    *
386    * @return string
387    */
388   public function getPeerClassName()
389   {
390     return $this->peerClassName;
391   }
392
393   /**
394    * Returns the URL for a given action.
395    *
396    * @return string The URL related to a given action
397    */
398   public function getUrlForAction($action)
399   {
400     if (isset($this->params['route_prefix']))
401     {
402       return 'list' == $action ? $this->params['route_prefix'] : $this->params['route_prefix'].'_'.$action;
403     }
404     else
405     {
406       return $this->getModuleName().'/'.$action;
407     }
408   }
409 }
410
Note: See TracBrowser for help on using the browser.