[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; |
---|
[42] | 48 | |
---|
[1] | 49 | /** |
---|
| 50 | * Constructor. Finds the current sort by and order. |
---|
| 51 | * |
---|
| 52 | * @param string $default_sort If not found elsewhere, this will be the |
---|
| 53 | * current sort by. |
---|
| 54 | * @param string $default_order If not found elsewhere, this will be the |
---|
| 55 | * current order. |
---|
| 56 | */ |
---|
[468] | 57 | public function __construct($default_sort = '', $default_order = '') |
---|
[42] | 58 | { |
---|
[316] | 59 | $app =& App::getInstance(); |
---|
[452] | 60 | |
---|
[316] | 61 | // Setup the HTML for printing ASC/DESC paths. |
---|
| 62 | // This should be converted to CSS someday. |
---|
| 63 | $images_path = $app->getParam('images_path') ? $app->getParam('images_path') : '/admin/_widgets'; |
---|
| 64 | $this->asc_widget = sprintf('<img src="%s/sort_ascending.gif" alt="%s" width="11" height="7" border="0" />', $images_path, _("Ascending")); |
---|
[635] | 65 | $this->desc_widget = sprintf('<img src="%s/sort_descending.gif" alt="%s" width="11" height="7" border="0" />', $images_path, _("Descending")); |
---|
[452] | 66 | |
---|
[316] | 67 | // Setup prefs object. |
---|
[154] | 68 | $this->prefs = new Prefs($_SERVER['PHP_SELF']); |
---|
| 69 | $this->prefs->setParam(array('persistent' => false)); |
---|
| 70 | |
---|
[316] | 71 | // Setup defaults. |
---|
[1] | 72 | $this->setDefault($default_sort, $default_order); |
---|
| 73 | $this->default_sort = $default_sort; |
---|
[316] | 74 | $this->default_order = $default_order; |
---|
[714] | 75 | |
---|
| 76 | // The base URL of sort header links. |
---|
| 77 | $this->base_url = $_SERVER['PHP_SELF']; |
---|
[1] | 78 | } |
---|
[42] | 79 | |
---|
[1] | 80 | /** |
---|
[42] | 81 | * Build an array of valid sort SQL for each DB column. This SQL is reference |
---|
[1] | 82 | * by the name and 'asc' or 'desc'. |
---|
| 83 | * |
---|
| 84 | * @param string $name Reference name for the column this SQL sorts on. |
---|
| 85 | * @param string $asc_sql The sort SQL if $this->order is ascending. |
---|
| 86 | * @param string $desc_sql The sort SQL if $this->order is descending. |
---|
| 87 | */ |
---|
[468] | 88 | public function setColumn($name, $asc_sql, $desc_sql) |
---|
[1] | 89 | { |
---|
| 90 | $this->_columns[$name] = array( |
---|
| 91 | 'asc' => $asc_sql, |
---|
| 92 | 'desc' => $desc_sql |
---|
| 93 | ); |
---|
| 94 | } |
---|
[42] | 95 | |
---|
[1] | 96 | /** |
---|
| 97 | * Set sort and order values. This is how you set new sort values after |
---|
| 98 | * already declaring a SortOrder object, but expect values to come from |
---|
| 99 | * getFormData. |
---|
| 100 | * |
---|
| 101 | * @param string $default_sort If not found elsewhere, this will be the |
---|
| 102 | * current sort by. |
---|
| 103 | * @param string $default_order If not found elsewhere, this will be the |
---|
| 104 | * current order. |
---|
| 105 | */ |
---|
[468] | 106 | public function setDefault($default_sort = '', $default_order = '') |
---|
[1] | 107 | { |
---|
| 108 | // Which column to sort by? |
---|
| 109 | // (1) By GET or POST specification, if available. |
---|
| 110 | // (2) By saved preference, if available. |
---|
| 111 | // (3) By default (provided at class instantiation). |
---|
| 112 | $new_sort_by = getFormData('sort'); |
---|
| 113 | if (!empty($new_sort_by)) { |
---|
| 114 | $this->sort_by = $new_sort_by; |
---|
[153] | 115 | $this->prefs->set('sort_by', $this->sort_by); |
---|
| 116 | } else if ($this->prefs->exists('sort_by')) { |
---|
| 117 | $this->sort_by = $this->prefs->get('sort_by'); |
---|
[1] | 118 | } else { |
---|
| 119 | $this->sort_by = $default_sort; |
---|
| 120 | } |
---|
[42] | 121 | |
---|
[1] | 122 | // Which sort order to use? |
---|
| 123 | // (1) By GET or POST specification, if available. |
---|
| 124 | // (2) By saved preference, if available. |
---|
[334] | 125 | // (3) By default (provided at class instantiation). |
---|
[1] | 126 | $new_order = getFormData('order'); |
---|
| 127 | if (!empty($new_order)) { |
---|
| 128 | $this->order = $new_order; |
---|
[153] | 129 | $this->prefs->set('sort_order', $this->order); |
---|
| 130 | } else if ($this->prefs->exists('sort_order')) { |
---|
| 131 | $this->order = $this->prefs->get('sort_order'); |
---|
[1] | 132 | } else { |
---|
| 133 | $this->order = $default_order; |
---|
| 134 | } |
---|
| 135 | } |
---|
[42] | 136 | |
---|
| 137 | |
---|
[1] | 138 | /** |
---|
[44] | 139 | * Forcibly set sort and order values. This is how you set new sort values after |
---|
[1] | 140 | * already declaring a SortOrder object. This will ignore getFormData values. |
---|
| 141 | * |
---|
[44] | 142 | * @param string $sort The sort by name. |
---|
| 143 | * @param string $order The order direction (ASC, |
---|
[1] | 144 | * for example, for an alphabetical sort) |
---|
| 145 | */ |
---|
[468] | 146 | public function set($sort=null, $order=null, $save_value=true) |
---|
[1] | 147 | { |
---|
| 148 | // Set new sort value. |
---|
| 149 | if (isset($sort)) { |
---|
| 150 | $this->sort_by = $sort; |
---|
[457] | 151 | if ($save_value) { |
---|
| 152 | $this->prefs->set('sort_by', $this->sort_by); |
---|
| 153 | } |
---|
[1] | 154 | } |
---|
[42] | 155 | |
---|
[1] | 156 | // Set new order value. |
---|
| 157 | if (isset($order)) { |
---|
| 158 | $this->order = $order; |
---|
[457] | 159 | if ($save_value) { |
---|
| 160 | $this->prefs->set('sort_order', $this->order); |
---|
| 161 | } |
---|
[1] | 162 | } |
---|
| 163 | } |
---|
[42] | 164 | |
---|
[452] | 165 | /** |
---|
| 166 | * Get the current sort and order values. |
---|
| 167 | * |
---|
| 168 | * @return array Array with keys: sort and order. These can be fed back into the SortOrder::set() method or defaults. |
---|
| 169 | */ |
---|
[468] | 170 | public function get() |
---|
[452] | 171 | { |
---|
| 172 | return array( |
---|
| 173 | 'sort' => $this->prefs->get('sort_by'), |
---|
| 174 | 'order' => $this->prefs->get('sort_order'), |
---|
| 175 | ); |
---|
| 176 | } |
---|
[42] | 177 | |
---|
[1] | 178 | /** |
---|
| 179 | * Returns the SQL code to sort by set column and set order. |
---|
| 180 | */ |
---|
[468] | 181 | public function getSortOrderSQL() |
---|
[1] | 182 | { |
---|
[479] | 183 | $app =& App::getInstance(); |
---|
| 184 | $db =& DB::getInstance(); |
---|
[136] | 185 | |
---|
[247] | 186 | if (!isset($this->_columns[mb_strtolower($this->sort_by)])) { |
---|
[1] | 187 | $this->sort_by = $this->default_sort; |
---|
| 188 | $this->order = $this->default_order; |
---|
| 189 | } |
---|
[247] | 190 | if (!isset($this->_columns[mb_strtolower($this->sort_by)][mb_strtolower($this->order)])) { |
---|
[1] | 191 | $this->order = 'ASC'; |
---|
| 192 | } |
---|
| 193 | |
---|
[247] | 194 | if (!empty($this->_columns[mb_strtolower($this->sort_by)][mb_strtolower($this->order)])) { |
---|
[281] | 195 | return sprintf(' ORDER BY %s ', $this->_columns[mb_strtolower($this->sort_by)][mb_strtolower($this->order)]); |
---|
[1] | 196 | } else { |
---|
[136] | 197 | $app->logMsg(sprintf('Could not find SQL to sort by %s %s.', $this->sort_by, $this->order), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 198 | return ''; |
---|
| 199 | } |
---|
[42] | 200 | } |
---|
| 201 | |
---|
| 202 | |
---|
[1] | 203 | /** |
---|
| 204 | * Prints a link for a column header with URL sort determining logic. |
---|
[321] | 205 | * Column must be defined first using setColumn(). |
---|
[1] | 206 | * |
---|
| 207 | * @param string $col The database column to sort by. |
---|
| 208 | * @param string $col_name The human-readable title of the column. |
---|
| 209 | * @param string $default_order The default order for this column (ASC, |
---|
| 210 | * for example, for an alphabetical sort) |
---|
| 211 | */ |
---|
[468] | 212 | public function printSortHeader($col, $col_name, $default_order='ASC') |
---|
[1] | 213 | { |
---|
[479] | 214 | $app =& App::getInstance(); |
---|
[136] | 215 | |
---|
[321] | 216 | if (isset($this->_columns[$col])) { |
---|
| 217 | if ($this->sort_by == $col) { |
---|
| 218 | if (mb_strtolower($this->order) == 'desc') { |
---|
[714] | 219 | ?><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] | 220 | } else { |
---|
[714] | 221 | ?><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] | 222 | } |
---|
[1] | 223 | } else { |
---|
[717] | 224 | ?><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] | 225 | } |
---|
[1] | 226 | } else { |
---|
[321] | 227 | echo $col_name; |
---|
[1] | 228 | } |
---|
[42] | 229 | } |
---|
[1] | 230 | |
---|
[681] | 231 | /* |
---|
| 232 | * Print a select menu of sort options. |
---|
| 233 | * |
---|
| 234 | * @access public |
---|
| 235 | * @param array $cols Array of column keys (printed in <option value="âŠ">) and names (human-readable) |
---|
| 236 | * @return |
---|
| 237 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 238 | * @since 18 May 2019 15:27:21 |
---|
| 239 | */ |
---|
| 240 | public function printSortSelectMenu($cols) |
---|
| 241 | { |
---|
| 242 | $app =& App::getInstance(); |
---|
| 243 | |
---|
| 244 | ?><select name="sort" id="sort"><?php |
---|
| 245 | foreach ($cols as $col => $col_name) { |
---|
| 246 | if (!isset($this->_columns[$col])) { |
---|
| 247 | $app->logMsg(sprintf('Sort select option %s is not setup via setColumn', $col), LOG_NOTICE, __FILE__, __LINE__); |
---|
| 248 | } |
---|
| 249 | printf('<option value="%s"%s>%s</option>', |
---|
| 250 | $col, |
---|
| 251 | ($this->sort_by == $col ? ' selected="selected"' : ''), |
---|
[717] | 252 | strip_tags($col_name) |
---|
[681] | 253 | ); |
---|
| 254 | } |
---|
| 255 | ?></select><?php |
---|
| 256 | } |
---|
| 257 | |
---|
[1] | 258 | } |
---|
| 259 | |
---|