contributions/querymodifiers/model/base/dbfiltermulticolumn.cls.php
Go to the documentation of this file.00001 <?php 00002 /** 00003 * One Item for a multicolum Filter 00004 * 00005 * @ingroup QueryModifiers 00006 * @author Gerd Riesselmann 00007 */ 00008 class DBFilterMultiColumnItem { 00009 /** 00010 * Coluimn to filter values on 00011 * 00012 * @var string 00013 */ 00014 public $column; 00015 /** 00016 * Value to filter for 00017 * 00018 * @var mixed 00019 */ 00020 public $value; 00021 /** 00022 * Operator. Use DBWhere Constants or usual operators like =, !=, > etc 00023 * 00024 * @var string 00025 */ 00026 public $operator; 00027 /** 00028 * Logical operator, AND or OR 00029 * 00030 * @since 0.5.1 00031 * 00032 * @var string 00033 */ 00034 public $logical; 00035 00036 /** 00037 * Constructor 00038 * 00039 * @param string $column Name of column to filter by 00040 * @param mixed $value 00041 * @param string $operator Operator like for DBWhere 00042 * @param string $logical Logical Operator 00043 */ 00044 public function __construct($column, $value, $operator = '=', $logical = DBWhere::LOGIC_AND) { 00045 $this->column = $column; 00046 $this->value = $value; 00047 $this->operator = $operator; 00048 $this->logical = $logical; 00049 } 00050 } 00051 00052 /** 00053 * A filter containing several columns to apply to a search result 00054 * 00055 * Construct with an array of DBFilterMultiColumnItems 00056 * 00057 * @code 00058 * public function get_filters() { 00059 * return array( 00060 * new DBFilterGroup( 00061 * '2col', 00062 * tr('Two Column Test'), 00063 * array( 00064 * 'example' => new DBFilterMultiColumn(array( 00065 * new DBFilterMultiColumnItem('field_a', 'value_a'), 00066 * new DBFilterMultiColumnItem('field_b', 'value_b', '<>') 00067 * ), tr('Example')), 00068 * 'other' => new DBFilterMultiColumn(array( 00069 * new DBFilterMultiColumnItem('field_a', 'other_value'), 00070 * new DBFilterMultiColumnItem('field_b', 'other_value_b', '<>') 00071 * ), tr('Other stuff'), 00072 * // Filters can be mixed! 00073 * 'classic' => new DBFilterColumn('field_c', 'old_stuff', tr('Classic')) 00074 * ) 00075 * ); 00076 * ) 00077 * } 00078 * @endcode 00079 * 00080 * @ingroup QueryModifiers 00081 * @author Gerd Riesselmann 00082 */ 00083 class DBFilterMultiColumn extends DBFilter { 00084 /** 00085 * Items 00086 */ 00087 private $items; 00088 00089 /** 00090 * contructor 00091 * 00092 * @param array items 00093 * @param string title 00094 */ 00095 public function __construct($items, $title) { 00096 $this->items = $items; 00097 parent::__construct($title); 00098 } 00099 00100 /** 00101 * Apply 00102 * 00103 * @param ISearchAdapter $query 00104 */ 00105 public function apply($query) { 00106 $where = new DBWhereGroup($query); 00107 foreach($this->items as $item) { 00108 /* @var $item DBFilterMultiColumnItem */ 00109 $column = trim($item->column); 00110 if (empty($column)) { 00111 return; 00112 } 00113 $where->add_where($column, $item->operator, $this->preprocess_value($item->value, $item->operator), $item->logical); 00114 } 00115 $query->add_where_object($where); 00116 } 00117 00118 /** 00119 * Return colum items 00120 * 00121 * @return array Array of DBFilterMultiColumnItem 00122 */ 00123 public function get_items() { 00124 return $this->items; 00125 } 00126 }