1 | <?php |
---|
2 | /** |
---|
3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
5 | * Copyright 2001-2012 Strangecode, LLC |
---|
6 | * |
---|
7 | * This file is part of The Strangecode Codebase. |
---|
8 | * |
---|
9 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
10 | * modify it under the terms of the GNU General Public License as published by the |
---|
11 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
12 | * any later version. |
---|
13 | * |
---|
14 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
17 | * details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License along with |
---|
20 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
21 | */ |
---|
22 | |
---|
23 | /** |
---|
24 | * SortOrder.inc.php |
---|
25 | * |
---|
26 | * SortOrder can determine how to sort results of a database query for display |
---|
27 | * on a listing. It can print column headers that will be links to |
---|
28 | * automatically change the sort and order. |
---|
29 | * |
---|
30 | * @requires This class requires Prefs.inc.php |
---|
31 | * |
---|
32 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
33 | * @version 1.6.2 |
---|
34 | */ |
---|
35 | require_once dirname(__FILE__) . '/Prefs.inc.php'; |
---|
36 | |
---|
37 | class SortOrder |
---|
38 | { |
---|
39 | |
---|
40 | protected $_columns; |
---|
41 | public $sort_by; |
---|
42 | public $order; |
---|
43 | public $asc_widget; |
---|
44 | public $desc_widget; |
---|
45 | public $default_sort; |
---|
46 | public $default_order; |
---|
47 | public $base_url; |
---|
48 | public $prefs; |
---|
49 | |
---|
50 | /** |
---|
51 | * Constructor. Finds the current sort by and order. |
---|
52 | * |
---|
53 | * @param string $default_sort If not found elsewhere, this will be the |
---|
54 | * current sort by. |
---|
55 | * @param string $default_order If not found elsewhere, this will be the |
---|
56 | * current order. |
---|
57 | */ |
---|
58 | public function __construct($default_sort = '', $default_order = '') |
---|
59 | { |
---|
60 | $app =& App::getInstance(); |
---|
61 | |
---|
62 | // Setup the HTML for printing ASC/DESC paths. |
---|
63 | // This should be converted to CSS someday. |
---|
64 | $images_path = $app->getParam('images_path') ? $app->getParam('images_path') : '/admin/_widgets'; |
---|
65 | $this->asc_widget = sprintf('<img src="%s/sort_ascending.gif" alt="%s" width="11" height="7" border="0" />', $images_path, _("Ascending")); |
---|
66 | $this->desc_widget = sprintf('<img src="%s/sort_descending.gif" alt="%s" width="11" height="7" border="0" />', $images_path, _("Descending")); |
---|
67 | |
---|
68 | // Setup prefs object. |
---|
69 | $this->prefs = new Prefs($_SERVER['PHP_SELF']); |
---|
70 | $this->prefs->setParam(array('persistent' => false)); |
---|
71 | |
---|
72 | // Setup defaults. |
---|
73 | $this->setDefault($default_sort, $default_order); |
---|
74 | $this->default_sort = $default_sort; |
---|
75 | $this->default_order = $default_order; |
---|
76 | |
---|
77 | // The base URL of sort header links. |
---|
78 | $this->base_url = $_SERVER['PHP_SELF']; |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * Build an array of valid sort SQL for each DB column. This SQL is reference |
---|
83 | * by the name and 'asc' or 'desc'. |
---|
84 | * |
---|
85 | * @param string $name Reference name for the column this SQL sorts on. |
---|
86 | * @param string $asc_sql The sort SQL if $this->order is ascending. |
---|
87 | * @param string $desc_sql The sort SQL if $this->order is descending. |
---|
88 | */ |
---|
89 | public function setColumn($name, $asc_sql, $desc_sql) |
---|
90 | { |
---|
91 | $this->_columns[$name] = array( |
---|
92 | 'asc' => $asc_sql, |
---|
93 | 'desc' => $desc_sql |
---|
94 | ); |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Set sort and order values. This is how you set new sort values after |
---|
99 | * already declaring a SortOrder object, but expect values to come from |
---|
100 | * getFormData. |
---|
101 | * |
---|
102 | * @param string $default_sort If not found elsewhere, this will be the |
---|
103 | * current sort by. |
---|
104 | * @param string $default_order If not found elsewhere, this will be the |
---|
105 | * current order. |
---|
106 | */ |
---|
107 | public function setDefault($default_sort = '', $default_order = '') |
---|
108 | { |
---|
109 | // Which column to sort by? |
---|
110 | // (1) By GET or POST specification, if available. |
---|
111 | // (2) By saved preference, if available. |
---|
112 | // (3) By default (provided at class instantiation). |
---|
113 | $new_sort_by = getFormData('sort'); |
---|
114 | if (!empty($new_sort_by)) { |
---|
115 | $this->sort_by = $new_sort_by; |
---|
116 | $this->prefs->set('sort_by', $this->sort_by); |
---|
117 | } else if ($this->prefs->exists('sort_by')) { |
---|
118 | $this->sort_by = $this->prefs->get('sort_by'); |
---|
119 | } else { |
---|
120 | $this->sort_by = $default_sort; |
---|
121 | } |
---|
122 | |
---|
123 | // Which sort order to use? |
---|
124 | // (1) By GET or POST specification, if available. |
---|
125 | // (2) By saved preference, if available. |
---|
126 | // (3) By default (provided at class instantiation). |
---|
127 | $new_order = getFormData('order'); |
---|
128 | if (!empty($new_order)) { |
---|
129 | $this->order = $new_order; |
---|
130 | $this->prefs->set('sort_order', $this->order); |
---|
131 | } else if ($this->prefs->exists('sort_order')) { |
---|
132 | $this->order = $this->prefs->get('sort_order'); |
---|
133 | } else { |
---|
134 | $this->order = $default_order; |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | /** |
---|
140 | * Forcibly set sort and order values. This is how you set new sort values after |
---|
141 | * already declaring a SortOrder object. This will ignore getFormData values. |
---|
142 | * |
---|
143 | * @param string $sort The sort by name. |
---|
144 | * @param string $order The order direction (ASC, |
---|
145 | * for example, for an alphabetical sort) |
---|
146 | */ |
---|
147 | public function set($sort=null, $order=null, $save_value=true) |
---|
148 | { |
---|
149 | // Set new sort value. |
---|
150 | if (isset($sort)) { |
---|
151 | $this->sort_by = $sort; |
---|
152 | if ($save_value) { |
---|
153 | $this->prefs->set('sort_by', $this->sort_by); |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | // Set new order value. |
---|
158 | if (isset($order)) { |
---|
159 | $this->order = $order; |
---|
160 | if ($save_value) { |
---|
161 | $this->prefs->set('sort_order', $this->order); |
---|
162 | } |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | * Get the current sort and order values. |
---|
168 | * |
---|
169 | * @return array Array with keys: sort and order. These can be fed back into the SortOrder::set() method or defaults. |
---|
170 | */ |
---|
171 | public function get() |
---|
172 | { |
---|
173 | return array( |
---|
174 | 'sort' => $this->prefs->get('sort_by'), |
---|
175 | 'order' => $this->prefs->get('sort_order'), |
---|
176 | ); |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | * Returns the SQL code to sort by set column and set order. |
---|
181 | */ |
---|
182 | public function getSortOrderSQL() |
---|
183 | { |
---|
184 | $app =& App::getInstance(); |
---|
185 | $db =& DB::getInstance(); |
---|
186 | |
---|
187 | if (!isset($this->_columns[mb_strtolower($this->sort_by)])) { |
---|
188 | $this->sort_by = $this->default_sort; |
---|
189 | $this->order = $this->default_order; |
---|
190 | } |
---|
191 | if (!isset($this->_columns[mb_strtolower($this->sort_by)][mb_strtolower($this->order)])) { |
---|
192 | $this->order = 'ASC'; |
---|
193 | } |
---|
194 | |
---|
195 | if (!empty($this->_columns[mb_strtolower($this->sort_by)][mb_strtolower($this->order)])) { |
---|
196 | return sprintf(' ORDER BY %s ', $this->_columns[mb_strtolower($this->sort_by)][mb_strtolower($this->order)]); |
---|
197 | } else { |
---|
198 | $app->logMsg(sprintf('Could not find SQL to sort by %s %s.', $this->sort_by, $this->order), LOG_WARNING, __FILE__, __LINE__); |
---|
199 | return ''; |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | |
---|
204 | /** |
---|
205 | * Prints a link for a column header with URL sort determining logic. |
---|
206 | * Column must be defined first using setColumn(). |
---|
207 | * |
---|
208 | * @param string $col The database column to sort by. |
---|
209 | * @param string $col_name The human-readable title of the column. |
---|
210 | * @param string $default_order The default order for this column (ASC, |
---|
211 | * for example, for an alphabetical sort) |
---|
212 | */ |
---|
213 | public function printSortHeader($col, $col_name, $default_order='ASC') |
---|
214 | { |
---|
215 | $app =& App::getInstance(); |
---|
216 | |
---|
217 | if (isset($this->_columns[$col])) { |
---|
218 | if ($this->sort_by == $col) { |
---|
219 | if (mb_strtolower($this->order) == 'desc') { |
---|
220 | ?><a href="<?php echo $app->oHREF($this->base_url . '?sort=' . $col . '&order=ASC'); ?>" data-tooltip title="<?php echo _("Change to ascending sort order"); ?>" class="sc-sort sc-desc"><?php echo $this->desc_widget; ?></a><?php echo $col_name; ?><?php |
---|
221 | } else { |
---|
222 | ?><a href="<?php echo $app->oHREF($this->base_url . '?sort=' . $col . '&order=DESC'); ?>" data-tooltip title="<?php echo _("Change to descending sort order"); ?>" class="sc-sort sc-asc"><?php echo $this->asc_widget; ?></a><?php echo $col_name; ?><?php |
---|
223 | } |
---|
224 | } else { |
---|
225 | ?><a href="<?php echo $app->oHREF($this->base_url . '?sort=' . $col . '&order=' . $default_order); ?>" data-tooltip title="<?php echo sprintf(_("Sort by %s"), strip_tags($col_name)); ?>" class="sc-sort"><?php echo $col_name; ?></a><?php |
---|
226 | } |
---|
227 | } else { |
---|
228 | echo $col_name; |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | /* |
---|
233 | * Print a select menu of sort options. |
---|
234 | * |
---|
235 | * @access public |
---|
236 | * @param array $cols Array of column keys (printed in <option value="âŠ">) and names (human-readable) |
---|
237 | * @return |
---|
238 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
239 | * @since 18 May 2019 15:27:21 |
---|
240 | */ |
---|
241 | public function printSortSelectMenu($cols) |
---|
242 | { |
---|
243 | $app =& App::getInstance(); |
---|
244 | |
---|
245 | ?><select name="sort" id="sort"><?php |
---|
246 | foreach ($cols as $col => $col_name) { |
---|
247 | if (!isset($this->_columns[$col])) { |
---|
248 | $app->logMsg(sprintf('Sort select option %s is not setup via setColumn', $col), LOG_NOTICE, __FILE__, __LINE__); |
---|
249 | } |
---|
250 | printf('<option value="%s"%s>%s</option>', |
---|
251 | $col, |
---|
252 | ($this->sort_by == $col ? ' selected="selected"' : ''), |
---|
253 | strip_tags($col_name) |
---|
254 | ); |
---|
255 | } |
---|
256 | ?></select><?php |
---|
257 | } |
---|
258 | |
---|
259 | } |
---|
260 | |
---|