[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 |
---|
[451] | 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. |
---|
[451] | 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. |
---|
[451] | 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 | * |
---|
[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 | |
---|
[439] | 39 | private $_columns; |
---|
| 40 | public $sort_by; |
---|
| 41 | public $order; |
---|
| 42 | public $asc_widget; |
---|
| 43 | public $desc_widget; |
---|
| 44 | public $default_sort; |
---|
| 45 | public $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 | */ |
---|
[439] | 55 | public function __construct($default_sort = '', $default_order = '') |
---|
[42] | 56 | { |
---|
[316] | 57 | $app =& App::getInstance(); |
---|
[451] | 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")); |
---|
[451] | 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 | */ |
---|
[439] | 83 | public function setColumn($name, $asc_sql, $desc_sql) |
---|
[1] | 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 | */ |
---|
[439] | 101 | public function setDefault($default_sort = '', $default_order = '') |
---|
[1] | 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 | */ |
---|
[439] | 141 | public function set($sort = null, $order = null) |
---|
[1] | 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 | |
---|
[451] | 156 | /** |
---|
| 157 | * Get the current sort and order values. |
---|
| 158 | * |
---|
| 159 | * @return array Array with keys: sort and order. These can be fed back into the SortOrder::set() method or defaults. |
---|
| 160 | */ |
---|
| 161 | function get() |
---|
| 162 | { |
---|
| 163 | return array( |
---|
| 164 | 'sort' => $this->prefs->get('sort_by'), |
---|
| 165 | 'order' => $this->prefs->get('sort_order'), |
---|
| 166 | ); |
---|
| 167 | } |
---|
[42] | 168 | |
---|
[1] | 169 | /** |
---|
| 170 | * Returns the SQL code to sort by set column and set order. |
---|
| 171 | */ |
---|
[439] | 172 | public function getSortOrderSQL() |
---|
[1] | 173 | { |
---|
[136] | 174 | $app =& App::getInstance(); |
---|
| 175 | $db =& DB::getInstance(); |
---|
| 176 | |
---|
[247] | 177 | if (!isset($this->_columns[mb_strtolower($this->sort_by)])) { |
---|
[1] | 178 | $this->sort_by = $this->default_sort; |
---|
| 179 | $this->order = $this->default_order; |
---|
| 180 | } |
---|
[247] | 181 | if (!isset($this->_columns[mb_strtolower($this->sort_by)][mb_strtolower($this->order)])) { |
---|
[1] | 182 | $this->order = 'ASC'; |
---|
| 183 | } |
---|
| 184 | |
---|
[247] | 185 | if (!empty($this->_columns[mb_strtolower($this->sort_by)][mb_strtolower($this->order)])) { |
---|
[281] | 186 | return sprintf(' ORDER BY %s ', $this->_columns[mb_strtolower($this->sort_by)][mb_strtolower($this->order)]); |
---|
[1] | 187 | } else { |
---|
[136] | 188 | $app->logMsg(sprintf('Could not find SQL to sort by %s %s.', $this->sort_by, $this->order), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 189 | return ''; |
---|
| 190 | } |
---|
[42] | 191 | } |
---|
| 192 | |
---|
| 193 | |
---|
[1] | 194 | /** |
---|
| 195 | * Prints a link for a column header with URL sort determining logic. |
---|
[321] | 196 | * Column must be defined first using setColumn(). |
---|
[1] | 197 | * |
---|
| 198 | * @param string $col The database column to sort by. |
---|
| 199 | * @param string $col_name The human-readable title of the column. |
---|
| 200 | * @param string $default_order The default order for this column (ASC, |
---|
| 201 | * for example, for an alphabetical sort) |
---|
| 202 | */ |
---|
[439] | 203 | public function printSortHeader($col, $col_name, $default_order='ASC') |
---|
[1] | 204 | { |
---|
[136] | 205 | $app =& App::getInstance(); |
---|
| 206 | |
---|
[321] | 207 | if (isset($this->_columns[$col])) { |
---|
| 208 | if ($this->sort_by == $col) { |
---|
| 209 | if (mb_strtolower($this->order) == 'desc') { |
---|
| 210 | ?><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 |
---|
| 211 | } else { |
---|
| 212 | ?><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 |
---|
| 213 | } |
---|
[1] | 214 | } else { |
---|
[321] | 215 | ?><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 |
---|
[451] | 216 | } |
---|
[1] | 217 | } else { |
---|
[321] | 218 | echo $col_name; |
---|
[1] | 219 | } |
---|
[42] | 220 | } |
---|
[1] | 221 | |
---|
| 222 | } |
---|
| 223 | |
---|