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