PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/urlcatcher.org/htdocs/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/form/sfFormFilterDoctrine.class.php

https://github.com/bigcalm/urlcatcher
PHP | 310 lines | 181 code | 37 blank | 92 comment | 41 complexity | 53d9820be98935a2f68af28d703086e1 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. * (c) Jonathan H. Wage <jonwage@gmail.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. * sfFormFilterDoctrine is the base class for filter forms based on Doctrine objects.
  12. *
  13. * Available options:
  14. *
  15. * * query: The query object to use
  16. * * table_method: A method on the table class that will either filter the passed query object or create a new one
  17. *
  18. * @package symfony
  19. * @subpackage form
  20. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  21. * @author Jonathan H. Wage <jonwage@gmail.com>
  22. * @version SVN: $Id: sfFormFilterDoctrine.class.php 11690 2008-09-20 19:50:03Z fabien $
  23. */
  24. abstract class sfFormFilterDoctrine extends sfFormFilter
  25. {
  26. /**
  27. * Returns the current model name.
  28. *
  29. * @return string The model class name
  30. */
  31. abstract public function getModelName();
  32. /**
  33. * Returns the fields and their filter type.
  34. *
  35. * @return array An array of fields with their filter type
  36. */
  37. abstract public function getFields();
  38. /**
  39. * Get the name of the table method used to retrieve the query object for the filter
  40. *
  41. * @return string
  42. */
  43. public function getTableMethod()
  44. {
  45. return $this->getOption('table_method');
  46. }
  47. /**
  48. * Set the name of the table method used to retrieve the query object for the filter
  49. *
  50. * The specified method will be passed the query object before any changes
  51. * are made based on incoming parameters.
  52. *
  53. * @param string $tableMethod
  54. */
  55. public function setTableMethod($tableMethod)
  56. {
  57. $this->setOption('table_method', $tableMethod);
  58. }
  59. /**
  60. * Sets the query object to use.
  61. *
  62. * @param Doctrine_Query $query
  63. */
  64. public function setQuery($query)
  65. {
  66. $this->setOption('query', $query);
  67. }
  68. /**
  69. * Returns a Doctrine Query based on the current values form the form.
  70. *
  71. * @return Query A Doctrine Query object
  72. */
  73. public function getQuery()
  74. {
  75. if (!$this->isValid())
  76. {
  77. throw $this->getErrorSchema();
  78. }
  79. return $this->buildQuery($this->getValues());
  80. }
  81. /**
  82. * Processes cleaned up values with user defined methods.
  83. *
  84. * To process a value before it is used by the buildQuery() method,
  85. * you need to define an convertXXXValue() method where XXX is the PHP name
  86. * of the column.
  87. *
  88. * The method must return the processed value or false to remove the value
  89. * from the array of cleaned up values.
  90. *
  91. * @param array An array of cleaned up values to process
  92. *
  93. * @return array An array of cleaned up values processed by the user defined methods
  94. */
  95. public function processValues($values)
  96. {
  97. // see if the user has overridden some column setter
  98. $originalValues = $values;
  99. foreach ($originalValues as $field => $value)
  100. {
  101. if (method_exists($this, $method = sprintf('convert%sValue', self::camelize($field))))
  102. {
  103. if (false === $ret = $this->$method($value))
  104. {
  105. unset($values[$field]);
  106. }
  107. else
  108. {
  109. $values[$field] = $ret;
  110. }
  111. }
  112. }
  113. return $values;
  114. }
  115. /**
  116. * Builds a Doctrine Query based on the passed values.
  117. *
  118. * @param array An array of parameters to build the Query object
  119. *
  120. * @return Query A Doctrine Query object
  121. */
  122. public function buildQuery(array $values)
  123. {
  124. return $this->doBuildQuery($this->processValues($values));
  125. }
  126. /**
  127. * Builds a Doctrine query with processed values.
  128. *
  129. * Overload this method instead of {@link buildQuery()} to avoid running
  130. * {@link processValues()} multiple times.
  131. *
  132. * @param array $values
  133. *
  134. * @return Doctrine_Query
  135. */
  136. protected function doBuildQuery(array $values)
  137. {
  138. $query = isset($this->options['query']) ? clone $this->options['query'] : $this->getTable()->createQuery('r');
  139. if ($method = $this->getTableMethod())
  140. {
  141. $tmp = $this->getTable()->$method($query);
  142. // for backward compatibility
  143. if ($tmp instanceof Doctrine_Query)
  144. {
  145. $query = $tmp;
  146. }
  147. }
  148. $fields = $this->getFields();
  149. // add those fields that are not represented in getFields() with a null type
  150. $names = array_merge($fields, array_diff(array_keys($this->validatorSchema->getFields()), array_keys($fields)));
  151. $fields = array_merge($fields, array_combine($names, array_fill(0, count($names), null)));
  152. foreach ($fields as $field => $type)
  153. {
  154. if (!isset($values[$field]) || null === $values[$field] || '' === $values[$field])
  155. {
  156. continue;
  157. }
  158. if ($this->getTable()->hasField($field))
  159. {
  160. $method = sprintf('add%sColumnQuery', self::camelize($this->getFieldName($field)));
  161. }
  162. else if (!method_exists($this, $method = sprintf('add%sColumnQuery', self::camelize($field))) && null !== $type)
  163. {
  164. throw new LogicException(sprintf('You must define a "%s" method to be able to filter with the "%s" field.', $method, $field));
  165. }
  166. if (method_exists($this, $method))
  167. {
  168. $this->$method($query, $field, $values[$field]);
  169. }
  170. else if (null !== $type)
  171. {
  172. if (!method_exists($this, $method = sprintf('add%sQuery', $type)))
  173. {
  174. throw new LogicException(sprintf('Unable to filter for the "%s" type.', $type));
  175. }
  176. $this->$method($query, $field, $values[$field]);
  177. }
  178. }
  179. return $query;
  180. }
  181. protected function addForeignKeyQuery(Doctrine_Query $query, $field, $value)
  182. {
  183. $fieldName = $this->getFieldName($field);
  184. if (is_array($value))
  185. {
  186. $query->andWhereIn(sprintf('%s.%s', $query->getRootAlias(), $fieldName), $value);
  187. }
  188. else
  189. {
  190. $query->addWhere(sprintf('%s.%s = ?', $query->getRootAlias(), $fieldName), $value);
  191. }
  192. }
  193. protected function addEnumQuery(Doctrine_Query $query, $field, $value)
  194. {
  195. $fieldName = $this->getFieldName($field);
  196. $query->addWhere(sprintf('%s.%s = ?', $query->getRootAlias(), $fieldName), $value);
  197. }
  198. protected function addTextQuery(Doctrine_Query $query, $field, $values)
  199. {
  200. $fieldName = $this->getFieldName($field);
  201. if (is_array($values) && isset($values['is_empty']) && $values['is_empty'])
  202. {
  203. $query->addWhere(sprintf('%s.%s IS NULL', $query->getRootAlias(), $fieldName));
  204. }
  205. else if (is_array($values) && isset($values['text']) && '' != $values['text'])
  206. {
  207. $query->addWhere(sprintf('%s.%s LIKE ?', $query->getRootAlias(), $fieldName), '%'.$values['text'].'%');
  208. }
  209. }
  210. protected function addNumberQuery(Doctrine_Query $query, $field, $values)
  211. {
  212. $fieldName = $this->getFieldName($field);
  213. if (is_array($values) && isset($values['is_empty']) && $values['is_empty'])
  214. {
  215. $query->addWhere(sprintf('%s.%s IS NULL', $query->getRootAlias(), $fieldName));
  216. }
  217. else if (is_array($values) && isset($values['text']) && '' != $values['text'])
  218. {
  219. $query->addWhere(sprintf('%s.%s = ?', $query->getRootAlias(), $fieldName), $values['text']);
  220. }
  221. }
  222. protected function addBooleanQuery(Doctrine_Query $query, $field, $value)
  223. {
  224. $fieldName = $this->getFieldName($field);
  225. $query->addWhere(sprintf('%s.%s = ?', $query->getRootAlias(), $fieldName), $value);
  226. }
  227. protected function addDateQuery(Doctrine_Query $query, $field, $values)
  228. {
  229. $fieldName = $this->getFieldName($field);
  230. if (isset($values['is_empty']) && $values['is_empty'])
  231. {
  232. $query->addWhere(sprintf('%s.%s IS NULL', $query->getRootAlias(), $fieldName));
  233. }
  234. else
  235. {
  236. if (null !== $values['from'] && null !== $values['to'])
  237. {
  238. $query->andWhere(sprintf('%s.%s >= ?', $query->getRootAlias(), $fieldName), $values['from']);
  239. $query->andWhere(sprintf('%s.%s <= ?', $query->getRootAlias(), $fieldName), $values['to']);
  240. }
  241. else if (null !== $values['from'])
  242. {
  243. $query->andWhere(sprintf('%s.%s >= ?', $query->getRootAlias(), $fieldName), $values['from']);
  244. }
  245. else if (null !== $values['to'])
  246. {
  247. $query->andWhere(sprintf('%s.%s <= ?', $query->getRootAlias(), $fieldName), $values['to']);
  248. }
  249. }
  250. }
  251. /**
  252. * Used in generated forms when models use inheritance.
  253. */
  254. protected function setupInheritance()
  255. {
  256. }
  257. protected function getColName($field)
  258. {
  259. return $this->getTable()->getColumnName($field);
  260. }
  261. protected function getFieldName($colName)
  262. {
  263. return $this->getTable()->getFieldName($colName);
  264. }
  265. protected function camelize($text)
  266. {
  267. return sfToolkit::pregtr($text, array('#/(.?)#e' => "'::'.strtoupper('\\1')", '/(^|_|-)+(.)/e' => "strtoupper('\\2')"));
  268. }
  269. protected function getTable()
  270. {
  271. return Doctrine_Core::getTable($this->getModelName());
  272. }
  273. }