Development

/branches/1.1/lib/plugins/sfPropelPlugin/lib/propel/sfFormPropel.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/plugins/sfPropelPlugin/lib/propel/sfFormPropel.class.php

Revision 11363, 7.5 kB (checked in by FabianLange, 6 years ago)

1.1: sfFormPropel did pass incorrect parameters to sfForm since r7053. fixes #4194

  • 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  * @package    symfony
13  * @subpackage form
14  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
15  * @version    SVN: $Id$
16  */
17
18 /**
19  * sfFormPropel is the base class for forms based on Propel objects.
20  *
21  * @package    symfony
22  * @subpackage form
23  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
24  * @version    SVN: $Id$
25  */
26 abstract class sfFormPropel extends sfForm
27 {
28   protected
29     $isNew    = true,
30     $cultures = array(),
31     $object   = null;
32
33   /**
34    * Constructor.
35    *
36    * @param BaseObject $object      A Propel object used to initialize default values
37    * @param array      $options     An array of options
38    * @param string     $CSRFSecret  A CSRF secret (false to disable CSRF protection, null to use the global CSRF secret)
39    *
40    * @see sfForm
41    */
42   public function __construct(BaseObject $object = null, $options = array(), $CSRFSecret = null)
43   {
44     $class = $this->getModelName();
45     if (is_null($object))
46     {
47       $this->object = new $class();
48     }
49     else
50     {
51       if (!$object instanceof $class)
52       {
53         throw new sfException(sprintf('The "%s" form only accepts a "%s" object.', get_class($this), $class));
54       }
55
56       $this->object = $object;
57       $this->isNew = false;
58     }
59
60     parent::__construct(array(), $options, $CSRFSecret);
61
62     $this->updateDefaultsFromObject();
63   }
64
65   /**
66    * Returns the default connection for the current model.
67    *
68    * @return Connection A database connection
69    */
70   public function getConnection()
71   {
72     return Propel::getConnection(constant(sprintf('%s::DATABASE_NAME', get_class($this->object->getPeer()))));
73   }
74
75   /**
76    * Returns the current model name.
77    */
78   abstract public function getModelName();
79
80   /**
81    * Returns true if the current form embeds a new object.
82    *
83    * @return Boolean true if the current form embeds a new object, false otherwise
84    */
85   public function isNew()
86   {
87     return $this->isNew;
88   }
89
90   /**
91    * Embeds i18n objects into the current form.
92    *
93    * @param array   $cultures   An array of cultures
94    * @param string  $decorator  A HTML decorator for the embedded form
95    */
96   public function embedI18n($cultures, $decorator = null)
97   {
98     if (!$this->isI18n())
99     {
100       throw new sfException(sprintf('The model "%s" is not internationalized.', $this->getModelName()));
101     }
102
103     $this->cultures = $cultures;
104
105     $class = $this->getI18nFormClass();
106     $i18n = new $class();
107     foreach ($cultures as $culture)
108     {
109       $this->embedForm($culture, $i18n, $decorator);
110     }
111   }
112
113   /**
114    * Returns the current object for this form.
115    *
116    * @return BaseObject The current object.
117    */
118   public function getObject()
119   {
120     return $this->object;
121   }
122
123   /**
124    * Binds the current form and save the to the database in one step.
125    *
126    * @param  array      $taintedValues    An array of tainted values to use to bind the form
127    * @param  array      $taintedFiles     An array of uploaded files (in the $_FILES or $_GET format)
128    * @param  Connection $con              An optional Propel Connection object
129    *
130    * @return Boolean    true if the form is valid, false otherwise
131    */
132   public function bindAndSave($taintedValues, $taintedFiles = null, $con = null)
133   {
134     $this->bind($taintedValues, $taintedFiles);
135     if ($this->isValid())
136     {
137       $this->save($con);
138
139       return true;
140     }
141
142     return false;
143   }
144
145   /**
146    * Saves the current object to the database.
147    *
148    * The object saving is done in a transaction and handled by the doSave() method.
149    *
150    * If the form is not valid, it throws an sfValidatorError.
151    *
152    * @param Connection $con An optional Connection object
153    *
154    * @return BaseObject The current saved object
155    *
156    * @see doSave()
157    */
158   public function save($con = null)
159   {
160     if (!$this->isValid())
161     {
162       throw $this->getErrorSchema();
163     }
164
165     if (is_null($con))
166     {
167       $con = $this->getConnection();
168     }
169
170     try
171     {
172       $con->begin();
173
174       $this->doSave($con);
175
176       $con->commit();
177     }
178     catch (Exception $e)
179     {
180       $con->rollback();
181
182       throw $e;
183     }
184
185     return $this->object;
186   }
187
188   /**
189    * Updates the values of the object with the cleaned up values.
190    *
191    * @return BaseObject The current updated object
192    */
193   public function updateObject()
194   {
195     if (!$this->isValid())
196     {
197       throw $this->getErrorSchema();
198     }
199
200     $this->object->fromArray($this->getValues(), BasePeer::TYPE_FIELDNAME);
201
202     return $this->object;
203   }
204
205   /**
206    * Updates the associated i18n objects values.
207    *
208    * @param Connection $con An optional Connection object
209    */
210   public function updateI18nObjects()
211   {
212     if (!$this->isValid())
213     {
214       throw $this->getErrorSchema();
215     }
216
217     if (!$this->isI18n())
218     {
219       throw new sfException(sprintf('The model "%s" is not internationalized.', $this->getModelName()));
220     }
221
222     $values = $this->getValues();
223     $method = sprintf('getCurrent%s', $this->getI18nModelName());
224     foreach ($this->cultures as $culture)
225     {
226       unset($values[$culture]['id'], $values[$culture]['culture']);
227
228       $i18n = $this->object->$method($culture);
229       $i18n->fromArray($values[$culture], BasePeer::TYPE_FIELDNAME);
230     }
231   }
232
233   /**
234    * Returns true if the current form has some associated i18n objects.
235    *
236    * @return Boolean true if the current form has some associated i18n objects, false otherwise
237    */
238   public function isI18n()
239   {
240     return !is_null($this->getI18nFormClass());
241   }
242
243   /**
244    * Returns the name of the i18n model.
245    *
246    * @return string The name of the i18n model
247    */
248   public function getI18nModelName()
249   {
250     return null;
251   }
252
253   /**
254    * Returns the name of the i18n form class.
255    *
256    * @return string The name of the i18n form class
257    */
258   public function getI18nFormClass()
259   {
260     return null;
261   }
262
263   /**
264    * Updates and saves the current object.
265    *
266    * If you want to add some logic before saving or save other associated objects,
267    * this is the method to override.
268    *
269    * @param Connection $con An optional Connection object
270    */
271   protected function doSave($con = null)
272   {
273     if (is_null($con))
274     {
275       $con = $this->getConnection();
276     }
277
278     $this->updateObject();
279
280     // i18n table
281     if ($this->isI18n())
282     {
283       $this->updateI18nObjects($con);
284     }
285
286     $this->object->save($con);
287   }
288
289   /**
290    * Updates the default values of the form with the current values of the current object.
291    */
292   protected function updateDefaultsFromObject()
293   {
294     // update defaults for the main object
295     if ($this->isNew)
296     {
297       $this->setDefaults(array_merge($this->object->toArray(BasePeer::TYPE_FIELDNAME), $this->getDefaults()));
298     }
299     else
300     {
301       $this->setDefaults(array_merge($this->getDefaults(), $this->object->toArray(BasePeer::TYPE_FIELDNAME)));
302     }
303
304     // update defaults for i18n
305     if ($this->isI18n())
306     {
307       $method = sprintf('getCurrent%s', $this->getI18nModelName());
308       foreach ($this->cultures as $culture)
309       {
310         if ($this->isNew)
311         {
312           $this->setDefault($culture, array_merge($this->object->$method($culture)->toArray(BasePeer::TYPE_FIELDNAME), $this->getDefault($culture)));
313         }
314         else
315         {
316           $this->setDefault($culture, array_merge($this->getDefault($culture), $this->object->$method($culture)->toArray(BasePeer::TYPE_FIELDNAME)));
317         }
318       }
319     }
320   }
321 }
322
Note: See TracBrowser for help on using the browser.