Development

/branches/1.4/lib/plugins/sfPropelPlugin/lib/form/sfFormPropel.class.php

You must first sign up to be able to contribute.

root/branches/1.4/lib/plugins/sfPropelPlugin/lib/form/sfFormPropel.class.php

Revision 27915, 8.4 kB (checked in by Kris.Wallsmith, 4 years ago)

[1.3, 1.4] fixed use of array union operator (closes #8238)

  • 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  * sfFormPropel is the base class for forms based on Propel objects.
13  *
14  * This class extends BaseForm, a class generated automatically with each new project.
15  *
16  * @package    symfony
17  * @subpackage form
18  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19  * @version    SVN: $Id$
20  */
21 abstract class sfFormPropel extends sfFormObject
22 {
23   /**
24    * Constructor.
25    *
26    * @param mixed  A object used to initialize default values
27    * @param array  An array of options
28    * @param string A CSRF secret (false to disable CSRF protection, null to use the global CSRF secret)
29    *
30    * @see sfForm
31    */
32   public function __construct($object = null, $options = array(), $CSRFSecret = null)
33   {
34     $class = $this->getModelName();
35     if (!$object)
36     {
37       $this->object = new $class();
38     }
39     else
40     {
41       if (!$object instanceof $class)
42       {
43         throw new sfException(sprintf('The "%s" form only accepts a "%s" object.', get_class($this), $class));
44       }
45
46       $this->object = $object;
47       $this->isNew = $this->getObject()->isNew();
48     }
49
50     parent::__construct(array(), $options, $CSRFSecret);
51
52     $this->updateDefaultsFromObject();
53   }
54
55   /**
56    * @return PropelPDO
57    * @see sfFormObject
58    */
59   public function getConnection()
60   {
61     return Propel::getConnection(constant(constant(get_class($this->getObject()).'::PEER').'::DATABASE_NAME'));
62   }
63
64   /**
65    * Embeds i18n objects into the current form.
66    *
67    * @param array   $cultures   An array of cultures
68    * @param string  $decorator  A HTML decorator for the embedded form
69    */
70   public function embedI18n($cultures, $decorator = null)
71   {
72     if (!$this->isI18n())
73     {
74       throw new sfException(sprintf('The model "%s" is not internationalized.', $this->getModelName()));
75     }
76
77     $class = $this->getI18nFormClass();
78     foreach ($cultures as $culture)
79     {
80       $method = sprintf('getCurrent%s', $this->getI18nModelName($culture));
81       $i18nObject = $this->getObject()->$method($culture);
82       $i18n = new $class($i18nObject);
83       
84       if ($i18nObject->isNew())
85       {
86         unset($i18n['id'], $i18n['culture']);
87       }
88
89       $this->embedForm($culture, $i18n, $decorator);
90     }
91   }
92
93   /**
94    * @see sfFormObject
95    */
96   protected function doUpdateObject($values)
97   {
98     $this->getObject()->fromArray($values, BasePeer::TYPE_FIELDNAME);
99   }
100
101   /**
102    * Processes cleaned up values with user defined methods.
103    *
104    * To process a value before it is used by the updateObject() method,
105    * you need to define an updateXXXColumn() method where XXX is the PHP name
106    * of the column.
107    *
108    * The method must return the processed value or false to remove the value
109    * from the array of cleaned up values.
110    *
111    * @see sfFormObject
112    */
113   public function processValues($values)
114   {
115     // see if the user has overridden some column setter
116     $valuesToProcess = $values;
117     foreach ($valuesToProcess as $field => $value)
118     {
119       try
120       {
121         $method = sprintf('update%sColumn', call_user_func(array(constant(get_class($this->getObject()).'::PEER'), 'translateFieldName'), $field, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME));
122       }
123       catch (Exception $e)
124       {
125         // not a "real" column of this object
126         if (!method_exists($this, $method = sprintf('update%sColumn', self::camelize($field))))
127         {
128           continue;
129         }
130       }
131
132       if (method_exists($this, $method))
133       {
134         if (false === $ret = $this->$method($value))
135         {
136           unset($values[$field]);
137         }
138         else
139         {
140           $values[$field] = $ret;
141         }
142       }
143       else
144       {
145         // save files
146         if ($this->validatorSchema[$field] instanceof sfValidatorFile)
147         {
148           $values[$field] = $this->processUploadedFile($field, null, $valuesToProcess);
149         }
150       }
151     }
152
153     return $values;
154   }
155
156   /**
157    * Returns true if the current form has some associated i18n objects.
158    *
159    * @return Boolean true if the current form has some associated i18n objects, false otherwise
160    */
161   public function isI18n()
162   {
163     return null !== $this->getI18nFormClass();
164   }
165
166   /**
167    * Returns the name of the i18n model.
168    *
169    * @return string The name of the i18n model
170    */
171   public function getI18nModelName()
172   {
173     return null;
174   }
175
176   /**
177    * Returns the name of the i18n form class.
178    *
179    * @return string The name of the i18n form class
180    */
181   public function getI18nFormClass()
182   {
183     return null;
184   }
185
186   /**
187    * Updates the default values of the form with the current values of the current object.
188    */
189   protected function updateDefaultsFromObject()
190   {
191     // update defaults for the main object
192     if ($this->isNew())
193     {
194       $this->setDefaults($this->getDefaults() + $this->getObject()->toArray(BasePeer::TYPE_FIELDNAME));
195     }
196     else
197     {
198       $this->setDefaults($this->getObject()->toArray(BasePeer::TYPE_FIELDNAME) + $this->getDefaults());
199     }
200   }
201
202   /**
203    * Saves the uploaded file for the given field.
204    *
205    * @param  string $field The field name
206    * @param  string $filename The file name of the file to save
207    * @param  array  $values An array of values
208    *
209    * @return string The filename used to save the file
210    */
211   protected function processUploadedFile($field, $filename = null, $values = null)
212   {
213     if (!$this->validatorSchema[$field] instanceof sfValidatorFile)
214     {
215       throw new LogicException(sprintf('You cannot save the current file for field "%s" as the field is not a file.', $field));
216     }
217
218     if (null === $values)
219     {
220       $values = $this->values;
221     }
222
223     if (isset($values[$field.'_delete']) && $values[$field.'_delete'])
224     {
225       $this->removeFile($field);
226
227       return '';
228     }
229
230     if (!$values[$field])
231     {
232       $column = call_user_func(array(constant(get_class($this->getObject()).'::PEER'), 'translateFieldName'), $field, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME);
233       $getter = 'get'.$column;
234
235       return $this->getObject()->$getter();
236     }
237
238     // we need the base directory
239     if (!$this->validatorSchema[$field]->getOption('path'))
240     {
241       return $values[$field];
242     }
243
244     $this->removeFile($field);
245
246     return $this->saveFile($field, $filename, $values[$field]);
247   }
248
249   /**
250    * Removes the current file for the field.
251    *
252    * @param string $field The field name
253    */
254   protected function removeFile($field)
255   {
256     if (!$this->validatorSchema[$field] instanceof sfValidatorFile)
257     {
258       throw new LogicException(sprintf('You cannot remove the current file for field "%s" as the field is not a file.', $field));
259     }
260
261     $column = call_user_func(array(constant(get_class($this->getObject()).'::PEER'), 'translateFieldName'), $field, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME);
262     $getter = 'get'.$column;
263
264     if (($directory = $this->validatorSchema[$field]->getOption('path')) && is_file($directory.DIRECTORY_SEPARATOR.$this->getObject()->$getter()))
265     {
266       unlink($directory.DIRECTORY_SEPARATOR.$this->getObject()->$getter());
267     }
268   }
269
270   /**
271    * Saves the current file for the field.
272    *
273    * @param  string          $field    The field name
274    * @param  string          $filename The file name of the file to save
275    * @param  sfValidatedFile $file     The validated file to save
276    *
277    * @return string The filename used to save the file
278    */
279   protected function saveFile($field, $filename = null, sfValidatedFile $file = null)
280   {
281     if (!$this->validatorSchema[$field] instanceof sfValidatorFile)
282     {
283       throw new LogicException(sprintf('You cannot save the current file for field "%s" as the field is not a file.', $field));
284     }
285
286     if (null === $file)
287     {
288       $file = $this->getValue($field);
289     }
290
291     $column = call_user_func(array(constant(get_class($this->getObject()).'::PEER'), 'translateFieldName'), $field, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME);
292     $method = sprintf('generate%sFilename', $column);
293
294     if (null !== $filename)
295     {
296       return $file->save($filename);
297     }
298     else if (method_exists($this, $method))
299     {
300       return $file->save($this->$method($file));
301     }
302     else if (method_exists($this->getObject(), $method))
303     {
304       return $file->save($this->getObject()->$method($file));
305     }
306     else
307     {
308       return $file->save();
309     }
310   }
311 }
312
Note: See TracBrowser for help on using the browser.