[1] | 1 | <?php |
---|
| 2 | /** |
---|
[362] | 3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
[396] | 5 | * Copyright 2001-2012 Strangecode, LLC |
---|
[452] | 6 | * |
---|
[362] | 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. |
---|
[452] | 13 | * |
---|
[362] | 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. |
---|
[452] | 18 | * |
---|
[362] | 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 | /** |
---|
[1] | 24 | * SortOrder.inc.php |
---|
[119] | 25 | * |
---|
[452] | 26 | * SortOrder can determine how to sort results of a database query for display |
---|
[1] | 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 |
---|
[42] | 31 | * |
---|
[1] | 32 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
[119] | 33 | * @version 1.6.2 |
---|
[1] | 34 | */ |
---|
| 35 | require_once dirname(__FILE__) . '/Prefs.inc.php'; |
---|
| 36 | |
---|
[502] | 37 | class SortOrder |
---|
| 38 | { |
---|
[42] | 39 | |
---|
[484] | 40 | protected $_columns; |
---|
[468] | 41 | public $sort_by; |
---|
| 42 | public $order; |
---|
| 43 | public $asc_widget; |
---|
| 44 | public $desc_widget; |
---|
| 45 | public $default_sort; |
---|
| 46 | public $default_order; |
---|
[714] | 47 | public $base_url; |
---|
[812] | 48 | public $prefs; |
---|
[42] | 49 | |
---|
[1] | 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 | */ |
---|
[468] | 58 | public function __construct($default_sort = '', $default_order = '') |
---|
[42] | 59 | { |
---|
[316] | 60 | $app =& App::getInstance(); |
---|
[452] | 61 | |
---|
[316] | 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")); |
---|
[635] | 66 | $this->desc_widget = sprintf('<img src="%s/sort_descending.gif" alt="%s" width="11" height="7" border="0" />', $images_path, _("Descending")); |
---|
[452] | 67 | |
---|
[316] | 68 | // Setup prefs object. |
---|
[154] | 69 | $this->prefs = new Prefs($_SERVER['PHP_SELF']); |
---|
| 70 | $this->prefs->setParam(array('persistent' => false)); |
---|
| 71 | |
---|
[316] | 72 | // Setup defaults. |
---|
[1] | 73 | $this->setDefault($default_sort, $default_order); |
---|
| 74 | $this->default_sort = $default_sort; |
---|
[316] | 75 | $this->default_order = $default_order; |
---|
[714] | 76 | |
---|
| 77 | // The base URL of sort header links. |
---|
| 78 | $this->base_url = $_SERVER['PHP_SELF']; |
---|
[1] | 79 | } |
---|
[42] | 80 | |
---|
[1] | 81 | /** |
---|
[42] | 82 | * Build an array of valid sort SQL for each DB column. This SQL is reference |
---|
[1] | 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 | */ |
---|
[468] | 89 | public function setColumn($name, $asc_sql, $desc_sql) |
---|
[1] | 90 | { |
---|
| 91 | $this->_columns[$name] = array( |
---|
| 92 | 'asc' => $asc_sql, |
---|
| 93 | 'desc' => $desc_sql |
---|
| 94 | ); |
---|
| 95 | } |
---|
[42] | 96 | |
---|
[1] | 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 | */ |
---|
[468] | 107 | public function setDefault($default_sort = '', $default_order = '') |
---|
[1] | 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; |
---|
[153] | 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'); |
---|
[1] | 119 | } else { |
---|
| 120 | $this->sort_by = $default_sort; |
---|
| 121 | } |
---|
[42] | 122 | |
---|
[1] | 123 | // Which sort order to use? |
---|
| 124 | // (1) By GET or POST specification, if available. |
---|
| 125 | // (2) By saved preference, if available. |
---|
[334] | 126 | // (3) By default (provided at class instantiation). |
---|
[1] | 127 | $new_order = getFormData('order'); |
---|
| 128 | if (!empty($new_order)) { |
---|
| 129 | $this->order = $new_order; |
---|
[153] | 130 | $this->prefs->set('sort_order', $this->order); |
---|
| 131 | } else if ($this->prefs->exists('sort_order')) { |
---|
| 132 | $this->order = $this->prefs->get('sort_order'); |
---|
[1] | 133 | } else { |
---|
| 134 | $this->order = $default_order; |
---|
| 135 | } |
---|
| 136 | } |
---|
[42] | 137 | |
---|
| 138 | |
---|
[1] | 139 | /** |
---|
[44] | 140 | * Forcibly set sort and order values. This is how you set new sort values after |
---|
[1] | 141 | * already declaring a SortOrder object. This will ignore getFormData values. |
---|
| 142 | * |
---|
[44] | 143 | * @param string $sort The sort by name. |
---|
| 144 | * @param string $order The order direction (ASC, |
---|
[1] | 145 | * for example, for an alphabetical sort) |
---|
| 146 | */ |
---|
[468] | 147 | public function set($sort=null, $order=null, $save_value=true) |
---|
[1] | 148 | { |
---|
| 149 | // Set new sort value. |
---|
| 150 | if (isset($sort)) { |
---|
| 151 | $this->sort_by = $sort; |
---|
[457] | 152 | if ($save_value) { |
---|
| 153 | $this->prefs->set('sort_by', $this->sort_by); |
---|
| 154 | } |
---|
[1] | 155 | } |
---|
[42] | 156 | |
---|
[1] | 157 | // Set new order value. |
---|
| 158 | if (isset($order)) { |
---|
| 159 | $this->order = $order; |
---|
[457] | 160 | if ($save_value) { |
---|
| 161 | $this->prefs->set('sort_order', $this->order); |
---|
| 162 | } |
---|
[1] | 163 | } |
---|
| 164 | } |
---|
[42] | 165 | |
---|
[452] | 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 | */ |
---|
[468] | 171 | public function get() |
---|
[452] | 172 | { |
---|
| 173 | return array( |
---|
| 174 | 'sort' => $this->prefs->get('sort_by'), |
---|
| 175 | 'order' => $this->prefs->get('sort_order'), |
---|
| 176 | ); |
---|
| 177 | } |
---|
[42] | 178 | |
---|
[1] | 179 | /** |
---|
| 180 | * Returns the SQL code to sort by set column and set order. |
---|
| 181 | */ |
---|
[468] | 182 | public function getSortOrderSQL() |
---|
[1] | 183 | { |
---|
[479] | 184 | $app =& App::getInstance(); |
---|
| 185 | $db =& DB::getInstance(); |
---|
[136] | 186 | |
---|
[247] | 187 | if (!isset($this->_columns[mb_strtolower($this->sort_by)])) { |
---|
[1] | 188 | $this->sort_by = $this->default_sort; |
---|
| 189 | $this->order = $this->default_order; |
---|
| 190 | } |
---|
[247] | 191 | if (!isset($this->_columns[mb_strtolower($this->sort_by)][mb_strtolower($this->order)])) { |
---|
[1] | 192 | $this->order = 'ASC'; |
---|
| 193 | } |
---|
| 194 | |
---|
[247] | 195 | if (!empty($this->_columns[mb_strtolower($this->sort_by)][mb_strtolower($this->order)])) { |
---|
[281] | 196 | return sprintf(' ORDER BY %s ', $this->_columns[mb_strtolower($this->sort_by)][mb_strtolower($this->order)]); |
---|
[1] | 197 | } else { |
---|
[136] | 198 | $app->logMsg(sprintf('Could not find SQL to sort by %s %s.', $this->sort_by, $this->order), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 199 | return ''; |
---|
| 200 | } |
---|
[42] | 201 | } |
---|
| 202 | |
---|
| 203 | |
---|
[1] | 204 | /** |
---|
| 205 | * Prints a link for a column header with URL sort determining logic. |
---|
[321] | 206 | * Column must be defined first using setColumn(). |
---|
[1] | 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 | */ |
---|
[468] | 213 | public function printSortHeader($col, $col_name, $default_order='ASC') |
---|
[1] | 214 | { |
---|
[479] | 215 | $app =& App::getInstance(); |
---|
[136] | 216 | |
---|
[321] | 217 | if (isset($this->_columns[$col])) { |
---|
| 218 | if ($this->sort_by == $col) { |
---|
| 219 | if (mb_strtolower($this->order) == 'desc') { |
---|
[714] | 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 |
---|
[321] | 221 | } else { |
---|
[714] | 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 |
---|
[321] | 223 | } |
---|
[1] | 224 | } else { |
---|
[717] | 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 |
---|
[452] | 226 | } |
---|
[1] | 227 | } else { |
---|
[321] | 228 | echo $col_name; |
---|
[1] | 229 | } |
---|
[42] | 230 | } |
---|
[1] | 231 | |
---|
[681] | 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"' : ''), |
---|
[717] | 253 | strip_tags($col_name) |
---|
[681] | 254 | ); |
---|
| 255 | } |
---|
| 256 | ?></select><?php |
---|
| 257 | } |
---|
| 258 | |
---|
[1] | 259 | } |
---|
| 260 | |
---|