00001 <?php
00002
00003
00004
00005
00006
00007
00008 class Filter implements IDBQueryModifier {
00009 protected $filter_data = array();
00010
00011
00012
00013
00014
00015 protected $adapter;
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 public function __construct($page_data, $filtergroups, $adapter = false) {
00027 $this->adapter = ($adapter instanceof IFilterAdapter) ? $adapter : new FilterDefaultAdapter($page_data);
00028
00029 if ($filtergroups instanceof ISearchAdapter) {
00030 $filtergroups = $filtergroups->get_filters();
00031 }
00032 if (!is_array($filtergroups)) {
00033 $filtergroups = ($filtergroups) ? array($filtergroups) : array(new DBFilterGroup());
00034 }
00035
00036 foreach($filtergroups as $filtergroup) {
00037
00038 $this->prepare_filter_group($filtergroup);
00039 }
00040
00041 $this->filter_data['filter_groups'] = $filtergroups;
00042 $this->filter_data['filter_url_builder'] = $this->adapter;
00043 $this->filter_data['page_data'] = $page_data;
00044 }
00045
00046
00047
00048
00049
00050
00051 protected function prepare_filter_group($filtergroup) {
00052 if ($filtergroup->count() > 1) {
00053 $filtergroup->add_filter('all', new DBFilter(tr('Show All', 'core')));
00054 if (!$filtergroup->get_default_key()) {
00055 $filtergroup->set_default_key('all');
00056 }
00057 }
00058 $current_key = $this->adapter->get_current_key($filtergroup->get_group_id(), $filtergroup->get_default_key());
00059 $filtergroup->set_current_key($current_key);
00060 }
00061
00062
00063
00064
00065 public function apply($query) {
00066
00067 foreach($this->filter_data['filter_groups'] as $filtergroup) {
00068 $query->apply_modifier($filtergroup);
00069 }
00070 }
00071
00072 public function prepare_view($view) {
00073 $view->assign('filter_data', $this->filter_data);
00074 }
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 public static function apply_to_url($url, $filter, $group_id = '') {
00087 return FilterDefaultAdapter::apply_to_url($url, $filter, $group_id);
00088 }
00089 }
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 class FilterDefaultAdapter implements IFilterAdapter {
00100 protected $param;
00101 protected $page_data;
00102
00103 public function __construct($page_data, $param = 'fl') {
00104 $this->page_data = $page_data;
00105 $this->param = $param;
00106 }
00107
00108 public function get_current_key($group_id, $default = '') {
00109 $query_key = 'fl' . String::plain_ascii($group_id);
00110 return $this->page_data->get_get()->get_item($query_key, $default);
00111 }
00112
00113 public function get_filter_link($filter, $group_id) {
00114 $key = $filter->is_default() ? '' : $filter->get_key();
00115
00116 $url = Url::current();
00117 self::apply_to_url($url, $key, $group_id, $this->param);
00118 return $url->build(Url::RELATIVE);
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 public static function apply_to_url($url, $filter, $group_id = '', $parameter = 'fl') {
00132 $url->replace_query_parameter($parameter . String::plain_ascii($group_id), $filter);
00133 }
00134 }