1 <?php
2
3 /**
4 * AfDbCriteria class file.
5 *
6 * @author Keith Burton <kburton@kappasoft.net>
7 * @package advancedfilters.components
8 */
9
10 /**
11 * This class extends CDbCriteria to provide methods to easily add advanced
12 * filter conditions.
13 *
14 * As long as you aren't using an extended version of CDbCriteria, you can make
15 * use of this class. It should be instantiated using the
16 * AdvancedFilters::createCriteria() method.
17 */
18 class AfDbCriteria extends CDbCriteria
19 {
20 private $config;
21
22 /**
23 * Construct a new criteria object.
24 *
25 * @param array $data the initial property values to pass to the base
26 * CDbCriteria class.
27 * @param array $config override the application level AdvancedFilters
28 * configuration.
29 */
30 public function __construct($data=array(), $config=array())
31 {
32 parent::__construct($data);
33
34 $this->config = $config;
35 }
36
37 /**
38 * Add an advanced filter condition to the existing criteria.
39 *
40 * @param string $columnExpression the disambiguated column name (or a
41 * valid SQL expression).
42 * @param string $filterExpression the entered filter expression.
43 * @param string $operator the operator used to concatenate the new
44 * condition with the existing one. Defaults to 'AND'.
45 * @param array $config override the application and instance level
46 * AdvancedFilters configuration.
47 * @return AfDbCriteria the criteria object to allow chaining.
48 */
49 public function addAdvancedFilterCondition($columnExpression,
50 $filterExpression, $operator='AND', $config=array())
51 {
52 // Merge the existing config with the provided config
53 $config = CMap::mergeArray($this->config, $config);
54
55 // Construct a parser object with the merged criteria
56 $afParser = new AfParser($columnExpression, $filterExpression, $config);
57
58 // Merge in the criteria returned from the filter parser
59 $this->mergeWith($afParser->getCriteria(), $operator);
60
61 return $this;
62 }
63 }
64