[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/> |
---|
| 5 | * Copyright 2001-2009 Strangecode Internet Consultancy |
---|
| 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 | /** |
---|
[1] | 24 | * SortOrder.inc.php |
---|
[119] | 25 | * |
---|
[1] | 26 | * SortOrder can determing 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 |
---|
[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(); |
---|
| 58 | |
---|
| 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")); |
---|
| 64 | |
---|
| 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 | */ |
---|
| 141 | function set($sort = null, $order = null) |
---|
| 142 | { |
---|
| 143 | // Set new sort value. |
---|
| 144 | if (isset($sort)) { |
---|
| 145 | $this->sort_by = $sort; |
---|
[153] | 146 | $this->prefs->set('sort_by', $this->sort_by); |
---|
[1] | 147 | } |
---|
[42] | 148 | |
---|
[1] | 149 | // Set new order value. |
---|
| 150 | if (isset($order)) { |
---|
| 151 | $this->order = $order; |
---|
[153] | 152 | $this->prefs->set('sort_order', $this->order); |
---|
[1] | 153 | } |
---|
| 154 | } |
---|
[42] | 155 | |
---|
| 156 | |
---|
[1] | 157 | /** |
---|
| 158 | * Returns the SQL code to sort by set column and set order. |
---|
| 159 | */ |
---|
| 160 | function getSortOrderSQL() |
---|
| 161 | { |
---|
[136] | 162 | $app =& App::getInstance(); |
---|
| 163 | $db =& DB::getInstance(); |
---|
| 164 | |
---|
[247] | 165 | if (!isset($this->_columns[mb_strtolower($this->sort_by)])) { |
---|
[1] | 166 | $this->sort_by = $this->default_sort; |
---|
| 167 | $this->order = $this->default_order; |
---|
| 168 | } |
---|
[247] | 169 | if (!isset($this->_columns[mb_strtolower($this->sort_by)][mb_strtolower($this->order)])) { |
---|
[1] | 170 | $this->order = 'ASC'; |
---|
| 171 | } |
---|
| 172 | |
---|
[247] | 173 | if (!empty($this->_columns[mb_strtolower($this->sort_by)][mb_strtolower($this->order)])) { |
---|
[281] | 174 | return sprintf(' ORDER BY %s ', $this->_columns[mb_strtolower($this->sort_by)][mb_strtolower($this->order)]); |
---|
[1] | 175 | } else { |
---|
[136] | 176 | $app->logMsg(sprintf('Could not find SQL to sort by %s %s.', $this->sort_by, $this->order), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 177 | return ''; |
---|
| 178 | } |
---|
[42] | 179 | } |
---|
| 180 | |
---|
| 181 | |
---|
[1] | 182 | /** |
---|
| 183 | * Prints a link for a column header with URL sort determining logic. |
---|
[321] | 184 | * Column must be defined first using setColumn(). |
---|
[1] | 185 | * |
---|
| 186 | * @param string $col The database column to sort by. |
---|
| 187 | * @param string $col_name The human-readable title of the column. |
---|
| 188 | * @param string $default_order The default order for this column (ASC, |
---|
| 189 | * for example, for an alphabetical sort) |
---|
| 190 | */ |
---|
| 191 | function printSortHeader($col, $col_name, $default_order='ASC') |
---|
| 192 | { |
---|
[136] | 193 | $app =& App::getInstance(); |
---|
| 194 | |
---|
[321] | 195 | if (isset($this->_columns[$col])) { |
---|
| 196 | if ($this->sort_by == $col) { |
---|
| 197 | if (mb_strtolower($this->order) == 'desc') { |
---|
| 198 | ?><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 |
---|
| 199 | } else { |
---|
| 200 | ?><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 |
---|
| 201 | } |
---|
[1] | 202 | } else { |
---|
[321] | 203 | ?><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 |
---|
| 204 | } |
---|
[1] | 205 | } else { |
---|
[321] | 206 | echo $col_name; |
---|
[1] | 207 | } |
---|
[42] | 208 | } |
---|
[1] | 209 | |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | ?> |
---|