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

https://github.com/bheneka/gitta · PHP · 311 lines · 180 code · 34 blank · 97 comment · 25 complexity · 423286584015d54281e835f46061fa53 MD5 · raw file

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