Development

/branches/1.3/lib/plugins/sfPropelPlugin/lib/form/sfFormFilterPropel.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/plugins/sfPropelPlugin/lib/form/sfFormFilterPropel.class.php

Revision 27748, 7.6 kB (checked in by Kris.Wallsmith, 5 years ago)

[1.3, 1.4] fixed form filtering by 0 on a number column (closes #8175)

  • 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  * sfFormFilterPropel is the base class for filter forms based on Propel objects.
13  *
14  * @package    symfony
15  * @subpackage form
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 abstract class sfFormFilterPropel extends sfFormFilter
20 {
21   /**
22    * Returns the current model name.
23    *
24    * @return string The model class name
25    */
26   abstract public function getModelName();
27
28   /**
29    * Returns the fields and their filter type.
30    *
31    * @return array An array of fields with their filter type
32    */
33   abstract public function getFields();
34
35   /**
36    * Returns a Propel Criteria based on the current values form the form.
37    *
38    * @return Criteria A Propel Criteria object
39    */
40   public function getCriteria()
41   {
42     if (!$this->isValid())
43     {
44       throw $this->getErrorSchema();
45     }
46
47     return $this->buildCriteria($this->getValues());
48   }
49
50   /**
51    * Processes cleaned up values with user defined methods.
52    *
53    * To process a value before it is used by the buildCriteria() method,
54    * you need to define an convertXXXValue() method where XXX is the PHP name
55    * of the column.
56    *
57    * The method must return the processed value or false to remove the value
58    * from the array of cleaned up values.
59    *
60    * @param  array An array of cleaned up values to process
61    *
62    * @return array An array of cleaned up values processed by the user defined methods
63    */
64   public function processValues($values)
65   {
66     // see if the user has overridden some column setter
67     $originalValues = $values;
68     foreach ($originalValues as $field => $value)
69     {
70       try
71       {
72         $method = sprintf('convert%sValue', call_user_func(array(constant($this->getModelName().'::PEER'), 'translateFieldName'), $field, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME));
73       }
74       catch (Exception $e)
75       {
76         // no a "real" column of this object
77         continue;
78       }
79
80       if (method_exists($this, $method))
81       {
82         if (false === $ret = $this->$method($value))
83         {
84           unset($values[$field]);
85         }
86         else
87         {
88           $values[$field] = $ret;
89         }
90       }
91     }
92
93     return $values;
94   }
95
96   /**
97    * Builds a Propel Criteria based on the passed values.
98    *
99    * @param  array    An array of parameters to build the Criteria object
100    *
101    * @return Criteria A Propel Criteria object
102    */
103   public function buildCriteria(array $values)
104   {
105     return $this->doBuildCriteria($this->processValues($values));
106   }
107
108   /**
109    * Builds a Propel Criteria with processed values.
110    *
111    * Overload this method instead of {@link buildCriteria()} to avoid running
112    * {@link processValues()} multiple times.
113    *
114    * @param  array $values
115    *
116    * @return Criteria
117    */
118   protected function doBuildCriteria(array $values)
119   {
120     $criteria = new Criteria();
121     $peer = constant($this->getModelName().'::PEER');
122
123     $fields = $this->getFields();
124
125     // add those fields that are not represented in getFields() with a null type
126     $names = array_merge($fields, array_diff(array_keys($this->validatorSchema->getFields()), array_keys($fields)));
127     $fields = array_merge($fields, array_combine($names, array_fill(0, count($names), null)));
128
129     foreach ($fields as $field => $type)
130     {
131       if (!isset($values[$field]) || null === $values[$field] || '' === $values[$field])
132       {
133         continue;
134       }
135
136       try
137       {
138         $method = sprintf('add%sColumnCriteria', call_user_func(array($peer, 'translateFieldName'), $field, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME));
139       }
140       catch (Exception $e)
141       {
142         // not a "real" column
143         if (!method_exists($this, $method = sprintf('add%sColumnCriteria', self::camelize($field))))
144         {
145           throw new LogicException(sprintf('You must define a "%s" method to be able to filter with the "%s" field.', $method, $field));
146         }
147       }
148
149       if (method_exists($this, $method))
150       {
151         $this->$method($criteria, $field, $values[$field]);
152       }
153       else
154       {
155         if (!method_exists($this, $method = sprintf('add%sCriteria', $type)))
156         {
157           throw new LogicException(sprintf('Unable to filter for the "%s" type.', $type));
158         }
159
160         $this->$method($criteria, $field, $values[$field]);
161       }
162     }
163
164     return $criteria;
165   }
166
167   protected function addForeignKeyCriteria(Criteria $criteria, $field, $value)
168   {
169     $colname = $this->getColname($field);
170
171     if (is_array($value))
172     {
173       $values = $value;
174       $value = array_pop($values);
175       $criterion = $criteria->getNewCriterion($colname, $value);
176
177       foreach ($values as $value)
178       {
179         $criterion->addOr($criteria->getNewCriterion($colname, $value));
180       }
181
182       $criteria->add($criterion);
183     }
184     else
185     {
186       $criteria->add($colname, $value);
187     }
188   }
189
190   protected function addTextCriteria(Criteria $criteria, $field, $values)
191   {
192     $colname = $this->getColname($field);
193
194     if (is_array($values) && isset($values['is_empty']) && $values['is_empty'])
195     {
196       $criterion = $criteria->getNewCriterion($colname, '');
197       $criterion->addOr($criteria->getNewCriterion($colname, null, Criteria::ISNULL));
198       $criteria->add($criterion);
199     }
200     else if (is_array($values) && isset($values['text']) && '' != $values['text'])
201     {
202       $criteria->add($colname, '%'.$values['text'].'%', Criteria::LIKE);
203     }
204   }
205
206   protected function addNumberCriteria(Criteria $criteria, $field, $values)
207   {
208     $colname = $this->getColname($field);
209
210     if (is_array($values) && isset($values['is_empty']) && $values['is_empty'])
211     {
212       $criterion = $criteria->getNewCriterion($colname, '');
213       $criterion->addOr($criteria->getNewCriterion($colname, null, Criteria::ISNULL));
214       $criteria->add($criterion);
215     }
216     else if (is_array($values) && isset($values['text']) && '' !== $values['text'])
217     {
218       $criteria->add($colname, $values['text']);
219     }
220   }
221
222   protected function addBooleanCriteria(Criteria $criteria, $field, $value)
223   {
224     $criteria->add($this->getColname($field), $value);
225   }
226
227   protected function addDateCriteria(Criteria $criteria, $field, $values)
228   {
229     $colname = $this->getColname($field);
230
231     if (isset($values['is_empty']) && $values['is_empty'])
232     {
233       $criteria->add($colname, null, Criteria::ISNULL);
234     }
235     else
236     {
237       $criterion = null;
238       if (null !== $values['from'] && null !== $values['to'])
239       {
240         $criterion = $criteria->getNewCriterion($colname, $values['from'], Criteria::GREATER_EQUAL);
241         $criterion->addAnd($criteria->getNewCriterion($colname, $values['to'], Criteria::LESS_EQUAL));
242       }
243       else if (null !== $values['from'])
244       {
245         $criterion = $criteria->getNewCriterion($colname, $values['from'], Criteria::GREATER_EQUAL);
246       }
247       else if (null !== $values['to'])
248       {
249         $criterion = $criteria->getNewCriterion($colname, $values['to'], Criteria::LESS_EQUAL);
250       }
251
252       if (null !== $criterion)
253       {
254         $criteria->add($criterion);
255       }
256     }
257   }
258
259   protected function getColName($field)
260   {
261     return call_user_func(array(constant($this->getModelName().'::PEER'), 'translateFieldName'), $field, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
262   }
263
264   protected function camelize($text)
265   {
266     return sfToolkit::pregtr($text, array('#/(.?)#e' => "'::'.strtoupper('\\1')", '/(^|_|-)+(.)/e' => "strtoupper('\\2')"));
267   }
268 }
269
Note: See TracBrowser for help on using the browser.