PageRenderTime 22ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/symfony/lib/form/addon/sfFormObject.class.php

https://bitbucket.org/suntiser/mfec_php_test
PHP | 291 lines | 133 code | 36 blank | 122 comment | 13 complexity | 97a9eae5f46ee669968cbbb245404255 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Base class for forms that deal with a single object.
  11. *
  12. * @package symfony
  13. * @subpackage form
  14. * @author Kris Wallsmith <kris.wallsmith@symfony-project.com>
  15. * @version SVN: $Id$
  16. */
  17. abstract class sfFormObject extends BaseForm
  18. {
  19. protected
  20. $isNew = true,
  21. $object = null;
  22. /**
  23. * Returns the current model name.
  24. *
  25. * @return string
  26. */
  27. abstract public function getModelName();
  28. /**
  29. * Returns the default connection for the current model.
  30. *
  31. * @return mixed A database connection
  32. */
  33. abstract public function getConnection();
  34. /**
  35. * Updates the values of the object with the cleaned up values.
  36. *
  37. * If you want to add some logic before updating or update other associated
  38. * objects, this is the method to override.
  39. *
  40. * @param array $values An array of values
  41. */
  42. abstract protected function doUpdateObject($values);
  43. /**
  44. * Processes cleaned up values.
  45. *
  46. * @param array $values An array of values
  47. *
  48. * @return array An array of cleaned up values
  49. */
  50. abstract public function processValues($values);
  51. /**
  52. * Returns true if the current form embeds a new object.
  53. *
  54. * @return Boolean true if the current form embeds a new object, false otherwise
  55. */
  56. public function isNew()
  57. {
  58. return $this->isNew;
  59. }
  60. /**
  61. * Returns the current object for this form.
  62. *
  63. * @return mixed The current object
  64. */
  65. public function getObject()
  66. {
  67. return $this->object;
  68. }
  69. /**
  70. * Binds the current form and saves the object to the database in one step.
  71. *
  72. * @param array $taintedValues An array of tainted values to use to bind the form
  73. * @param array $taintedFiles An array of uploaded files (in the $_FILES or $_GET format)
  74. * @param mixed $con An optional connection object
  75. *
  76. * @return Boolean true if the form is valid, false otherwise
  77. */
  78. public function bindAndSave($taintedValues, $taintedFiles = null, $con = null)
  79. {
  80. $this->bind($taintedValues, $taintedFiles);
  81. if ($this->isValid())
  82. {
  83. $this->save($con);
  84. return true;
  85. }
  86. return false;
  87. }
  88. /**
  89. * Saves the current object to the database.
  90. *
  91. * The object saving is done in a transaction and handled by the doSave() method.
  92. *
  93. * @param mixed $con An optional connection object
  94. *
  95. * @return mixed The current saved object
  96. *
  97. * @throws Exception
  98. * @throws sfValidatorErrorSchema
  99. *
  100. * @see doSave()
  101. */
  102. public function save($con = null)
  103. {
  104. if (!$this->isValid())
  105. {
  106. throw $this->getErrorSchema();
  107. }
  108. if (null === $con)
  109. {
  110. $con = $this->getConnection();
  111. }
  112. $con->beginTransaction();
  113. try
  114. {
  115. $this->doSave($con);
  116. $con->commit();
  117. }
  118. catch (Exception $e)
  119. {
  120. $con->rollBack();
  121. throw $e;
  122. }
  123. return $this->getObject();
  124. }
  125. /**
  126. * Updates and saves the current object.
  127. *
  128. * If you want to add some logic before saving or save other associated
  129. * objects, this is the method to override.
  130. *
  131. * @param mixed $con An optional connection object
  132. */
  133. protected function doSave($con = null)
  134. {
  135. $this->updateObject();
  136. $this->saveObject($con);
  137. }
  138. /**
  139. * Save form object
  140. *
  141. * @param mixed $con An optional connection object
  142. */
  143. public function saveObject($con = null)
  144. {
  145. if (null === $con)
  146. {
  147. $con = $this->getConnection();
  148. }
  149. $this->getObject()->save($con);
  150. // embedded forms
  151. $this->saveObjectEmbeddedForms($con);
  152. }
  153. /**
  154. * Updates the values of the object with the cleaned up values.
  155. *
  156. * @param array $values An array of values
  157. *
  158. * @return mixed The current updated object
  159. */
  160. public function updateObject($values = null)
  161. {
  162. if (null === $values)
  163. {
  164. $values = $this->values;
  165. }
  166. $values = $this->processValues($values);
  167. $this->doUpdateObject($values);
  168. // embedded forms
  169. $this->updateObjectEmbeddedForms($values);
  170. return $this->getObject();
  171. }
  172. /**
  173. * Updates the values of the objects in embedded forms.
  174. *
  175. * @param array $values An array of values
  176. * @param array $forms An array of forms
  177. */
  178. public function updateObjectEmbeddedForms($values, $forms = null)
  179. {
  180. if (null === $forms)
  181. {
  182. $forms = $this->embeddedForms;
  183. }
  184. foreach ($forms as $name => $form)
  185. {
  186. if (!isset($values[$name]) || !is_array($values[$name]))
  187. {
  188. continue;
  189. }
  190. if ($form instanceof sfFormObject)
  191. {
  192. $form->updateObject($values[$name]);
  193. }
  194. else
  195. {
  196. $this->updateObjectEmbeddedForms($values[$name], $form->getEmbeddedForms());
  197. }
  198. }
  199. }
  200. /**
  201. * Saves embedded form objects.
  202. *
  203. * @param mixed $con An optional connection object
  204. * @param array $forms An array of forms
  205. */
  206. public function saveObjectEmbeddedForms($con = null, $forms = null)
  207. {
  208. if (null === $con)
  209. {
  210. $con = $this->getConnection();
  211. }
  212. if (null === $forms)
  213. {
  214. $forms = $this->embeddedForms;
  215. }
  216. foreach ($forms as $form)
  217. {
  218. if ($form instanceof sfFormObject)
  219. {
  220. $form->saveObject($con);
  221. }
  222. else
  223. {
  224. $this->saveObjectEmbeddedForms($con, $form->getEmbeddedForms());
  225. }
  226. }
  227. }
  228. /**
  229. * Renders a form tag suitable for the related object.
  230. *
  231. * The method is automatically guessed based on the Doctrine object:
  232. *
  233. * * if the object is new, the method is POST
  234. * * if the object already exists, the method is PUT
  235. *
  236. * @param string $url The URL for the action
  237. * @param array $attributes An array of HTML attributes
  238. *
  239. * @return string An HTML representation of the opening form tag
  240. *
  241. * @see sfForm
  242. */
  243. public function renderFormTag($url, array $attributes = array())
  244. {
  245. if (!isset($attributes['method']))
  246. {
  247. $attributes['method'] = $this->isNew() ? 'post' : 'put';
  248. }
  249. return parent::renderFormTag($url, $attributes);
  250. }
  251. protected function camelize($text)
  252. {
  253. return strtr(ucwords(strtr($text, array('/' => ':: ', '_' => ' ', '-' => ' '))), array(' ' => ''));
  254. }
  255. }