00001 <?php
00002
00003
00004
00005
00006
00007
00008 class Sorter implements IDBQueryModifier {
00009
00010
00011
00012 const DONT_INDEX_SORTED = 1;
00013
00014
00015
00016 const DO_INDEX_SORTED = 0;
00017
00018
00019
00020
00021
00022
00023 protected $adapter;
00024 protected $sorter_data = array();
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 public function __construct(PageData $page_data, $columns, $default_column_key = false, $adapter = false) {
00038 $this->adapter = ($adapter instanceof ISorterAdapter) ? $adapter : new SorterDefaultAdapter($page_data);
00039 if ($columns instanceof ISearchAdapter) {
00040 $default_column_key = $columns->get_sort_default_column();
00041 $columns = $columns->get_sortable_columns();
00042 }
00043 $this->sorter_data['policy'] = is_int($adapter) ? $adapter : 0;
00044 $this->sorter_data['page_data'] = $page_data;
00045 $this->sorter_data['column_objects'] = $columns;
00046 $this->sorter_data['columns_total'] = count($columns);
00047 $this->sorter_data['default_column_key'] = $default_column_key;
00048
00049
00050 $column_key = $this->adapter->get_column();
00051 if (empty($column_key)) {
00052 $column_key = $default_column_key;
00053 }
00054 $column_obj = Arr::get_item($columns, $column_key, false);
00055 if ($column_obj == false) {
00056 $column_obj = Arr::get_item($columns, 0, false);
00057 }
00058 $this->sorter_data['current_column_key'] = $column_key;
00059 $this->sorter_data['current_column_object'] = $column_obj;
00060
00061
00062 $default_order = ($column_obj) ? $column_obj->get_direction() : DBSortColumn::ORDER_FORWARD;
00063 $order = $this->adapter->get_order();
00064 if (empty($order)) {
00065 $order = $default_order;
00066 }
00067 else {
00068 if ($column_obj && $column_obj->get_is_single_direction()) {
00069
00070 $this->adapter->get_url_for_sort($column_key, '')->redirect();
00071 exit;
00072 }
00073 }
00074 $this->sorter_data['order'] = $order;
00075 $this->sorter_data['default_order'] = $default_order;
00076 $this->sorter_data['is_default'] = (($order == $default_order) && ($column_key == $default_column_key));
00077 }
00078
00079
00080
00081
00082
00083
00084 public function apply($query) {
00085 $column_obj = $this->sorter_data['current_column_object'];
00086 if ($column_obj instanceof DBSortColumn) {
00087 $column_obj->set_direction($this->sorter_data['order']);
00088 $column_obj->apply($query);
00089 }
00090 }
00091
00092
00093
00094
00095 public function prepare_view($view) {
00096 if ($this->sorter_data['columns_total'] < 2) {
00097 return;
00098 }
00099
00100 $order = $this->sorter_data['order'];
00101 $arr_colums = array();
00102 foreach($this->sorter_data['column_objects'] as $column => $data) {
00103 $arr_colums[$column] = $this->create_column_array($data, $order, $column);
00104 }
00105
00106 $current_column_obj = $this->sorter_data['current_column_object'];
00107 if ($current_column_obj) {
00108 $current_column = $this->create_column_array($current_column_obj, $order, $this->sorter_data['current_column_key']);
00109 }
00110
00111 $this->sorter_data['columns'] = $arr_colums;
00112 $this->sorter_data['current_column'] = $current_column;
00113 $view->assign('sorter_data', $this->sorter_data);
00114 }
00115
00116
00117
00118
00119
00120
00121
00122
00123 private function create_column_array($data, $order, $column_key = '') {
00124 $column = $data->get_column();
00125 if ($column_key === '') {
00126 $column_key = $column;
00127 }
00128 $order_param_value = ($data->get_is_single_direction()) ? '' : $order;
00129 $ret = array(
00130 'column' => $column,
00131 'title' => $data->get_title(),
00132 'link' => $this->get_url_for_sort($column_key, $order_param_value),
00133 'sort_title' => $data->get_order_title($order),
00134 );
00135 if (!$data->get_is_single_direction()) {
00136 $ret['other_sort_link'] = $this->get_url_for_sort($column_key, $data->get_opposite_order($order));
00137 $ret['other_sort_title'] = $data->get_order_title($data->get_opposite_order($order));
00138 }
00139 return $ret;
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149 private function get_url_for_sort($column, $order) {
00150 $url = $this->adapter->get_url_for_sort($column, $order);
00151 return $url->build(Url::RELATIVE);
00152 }
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 public static function apply_to_url($url, $column, $order) {
00165 return SorterDefaultAdapter::apply_to_url($url, $column, $order);
00166 }
00167 }
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 class SorterDefaultAdapter implements ISorterAdapter {
00178 protected $page_data;
00179 protected $param_column;
00180 protected $param_order;
00181
00182 public function __construct(PageData $page_data, $param_column = 'sc', $param_order = 'so') {
00183 $this->page_data = $page_data;
00184 $this->param_column = $param_column;
00185 $this->param_order = $param_order;
00186 }
00187
00188
00189
00190
00191
00192
00193 public function get_column() {
00194 return $this->page_data->get_get()->get_item($this->param_column, false);
00195 }
00196
00197
00198
00199
00200
00201
00202
00203
00204 public function get_order() {
00205 return $this->page_data->get_get()->get_item($this->param_order, false);
00206 }
00207
00208
00209
00210
00211
00212
00213
00214
00215 public function get_url_for_sort($column, $order) {
00216 $url = Url::current();
00217 self::apply_to_url($url, $column, $order, $this->param_column, $this->param_order);
00218 return $url;
00219 }
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229 public static function apply_to_url($url, $column, $order, $param_column = 'sc', $param_order = 'so') {
00230 $url->replace_query_parameter($param_order, $order)->replace_query_parameter($param_column, $column);
00231 }
00232 }