[1] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * SortOrder.inc.php |
---|
| 4 | * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information |
---|
[119] | 5 | * |
---|
[1] | 6 | * SortOrder can determing how to sort results of a database query for display |
---|
| 7 | * on a listing. It can print column headers that will be links to |
---|
| 8 | * automatically change the sort and order. |
---|
| 9 | * |
---|
| 10 | * @requires This class requires Prefs.inc.php |
---|
[42] | 11 | * |
---|
[1] | 12 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
[119] | 13 | * @version 1.6.2 |
---|
[1] | 14 | */ |
---|
| 15 | require_once dirname(__FILE__) . '/Prefs.inc.php'; |
---|
| 16 | |
---|
| 17 | class SortOrder { |
---|
[42] | 18 | |
---|
[1] | 19 | var $_columns; |
---|
| 20 | var $sort_by; |
---|
| 21 | var $order; |
---|
[46] | 22 | var $asc_widget = '<img src="/admin/_widgets/sort_ascending.gif" alt="[ASC]" width="11" height="7" border="0" />'; |
---|
| 23 | var $desc_widget = '<img src="/admin/_widgets/sort_descending.gif" alt="[DESC]" width="11" height="7" border="0" />'; |
---|
[1] | 24 | var $default_sort; |
---|
| 25 | var $default_order; |
---|
[42] | 26 | |
---|
[1] | 27 | /** |
---|
| 28 | * Constructor. Finds the current sort by and order. |
---|
| 29 | * |
---|
| 30 | * @param string $default_sort If not found elsewhere, this will be the |
---|
| 31 | * current sort by. |
---|
| 32 | * @param string $default_order If not found elsewhere, this will be the |
---|
| 33 | * current order. |
---|
| 34 | */ |
---|
| 35 | function SortOrder($default_sort = '', $default_order = '') |
---|
[42] | 36 | { |
---|
[1] | 37 | $this->setDefault($default_sort, $default_order); |
---|
[42] | 38 | |
---|
[1] | 39 | $this->default_sort = $default_sort; |
---|
| 40 | $this->default_order = $default_order; |
---|
| 41 | } |
---|
[42] | 42 | |
---|
[1] | 43 | /** |
---|
[42] | 44 | * Build an array of valid sort SQL for each DB column. This SQL is reference |
---|
[1] | 45 | * by the name and 'asc' or 'desc'. |
---|
| 46 | * |
---|
| 47 | * @param string $name Reference name for the column this SQL sorts on. |
---|
| 48 | * @param string $asc_sql The sort SQL if $this->order is ascending. |
---|
| 49 | * @param string $desc_sql The sort SQL if $this->order is descending. |
---|
| 50 | */ |
---|
| 51 | function setColumn($name, $asc_sql, $desc_sql) |
---|
| 52 | { |
---|
| 53 | $this->_columns[$name] = array( |
---|
| 54 | 'asc' => $asc_sql, |
---|
| 55 | 'desc' => $desc_sql |
---|
| 56 | ); |
---|
| 57 | } |
---|
[42] | 58 | |
---|
[1] | 59 | /** |
---|
| 60 | * Set sort and order values. This is how you set new sort values after |
---|
| 61 | * already declaring a SortOrder object, but expect values to come from |
---|
| 62 | * getFormData. |
---|
| 63 | * |
---|
| 64 | * @param string $default_sort If not found elsewhere, this will be the |
---|
| 65 | * current sort by. |
---|
| 66 | * @param string $default_order If not found elsewhere, this will be the |
---|
| 67 | * current order. |
---|
| 68 | */ |
---|
| 69 | function setDefault($default_sort = '', $default_order = '') |
---|
| 70 | { |
---|
[132] | 71 | $prefs =& Prefs::getInstance(); |
---|
| 72 | |
---|
[1] | 73 | // Which column to sort by? |
---|
| 74 | // (1) By GET or POST specification, if available. |
---|
| 75 | // (2) By saved preference, if available. |
---|
| 76 | // (3) By default (provided at class instantiation). |
---|
| 77 | $new_sort_by = getFormData('sort'); |
---|
| 78 | if (!empty($new_sort_by)) { |
---|
| 79 | $this->sort_by = $new_sort_by; |
---|
[132] | 80 | $prefs->setValue('sort_by', $this->sort_by, $_SERVER['PHP_SELF']); |
---|
| 81 | } else if ($prefs->exists('sort_by', $_SERVER['PHP_SELF'])) { |
---|
| 82 | $this->sort_by = $prefs->getValue('sort_by', $_SERVER['PHP_SELF']); |
---|
[1] | 83 | } else { |
---|
| 84 | $this->sort_by = $default_sort; |
---|
| 85 | } |
---|
[42] | 86 | |
---|
[1] | 87 | // Which sort order to use? |
---|
| 88 | // (1) By GET or POST specification, if available. |
---|
| 89 | // (2) By saved preference, if available. |
---|
| 90 | // (3) By default (provided at class instanciation). |
---|
| 91 | $new_order = getFormData('order'); |
---|
| 92 | if (!empty($new_order)) { |
---|
| 93 | $this->order = $new_order; |
---|
[132] | 94 | $prefs->setValue('sort_order', $this->order, $_SERVER['PHP_SELF']); |
---|
| 95 | } else if ($prefs->exists('sort_order', $_SERVER['PHP_SELF'])) { |
---|
| 96 | $this->order = $prefs->getValue('sort_order', $_SERVER['PHP_SELF']); |
---|
[1] | 97 | } else { |
---|
| 98 | $this->order = $default_order; |
---|
| 99 | } |
---|
| 100 | } |
---|
[42] | 101 | |
---|
| 102 | |
---|
[1] | 103 | /** |
---|
[44] | 104 | * Forcibly set sort and order values. This is how you set new sort values after |
---|
[1] | 105 | * already declaring a SortOrder object. This will ignore getFormData values. |
---|
| 106 | * |
---|
[44] | 107 | * @param string $sort The sort by name. |
---|
| 108 | * @param string $order The order direction (ASC, |
---|
[1] | 109 | * for example, for an alphabetical sort) |
---|
| 110 | */ |
---|
| 111 | function set($sort = null, $order = null) |
---|
| 112 | { |
---|
[132] | 113 | $prefs =& Prefs::getInstance(); |
---|
| 114 | |
---|
[1] | 115 | // Set new sort value. |
---|
| 116 | if (isset($sort)) { |
---|
| 117 | $this->sort_by = $sort; |
---|
[132] | 118 | $prefs->setValue('sort_by', $this->sort_by, $_SERVER['PHP_SELF']); |
---|
[1] | 119 | } |
---|
[42] | 120 | |
---|
[1] | 121 | // Set new order value. |
---|
| 122 | if (isset($order)) { |
---|
| 123 | $this->order = $order; |
---|
[132] | 124 | $prefs->setValue('sort_order', $this->order, $_SERVER['PHP_SELF']); |
---|
[1] | 125 | } |
---|
| 126 | } |
---|
[42] | 127 | |
---|
| 128 | |
---|
[1] | 129 | /** |
---|
| 130 | * Returns the SQL code to sort by set column and set order. |
---|
| 131 | */ |
---|
| 132 | function getSortOrderSQL() |
---|
| 133 | { |
---|
[128] | 134 | $app =& App::getInstance(); |
---|
[130] | 135 | $db =& DB::getInstance(); |
---|
[128] | 136 | |
---|
[1] | 137 | if (!isset($this->_columns[strtolower($this->sort_by)])) { |
---|
| 138 | $this->sort_by = $this->default_sort; |
---|
| 139 | $this->order = $this->default_order; |
---|
| 140 | } |
---|
| 141 | if (!isset($this->_columns[strtolower($this->sort_by)][strtolower($this->order)])) { |
---|
| 142 | $this->order = 'ASC'; |
---|
| 143 | } |
---|
| 144 | |
---|
[20] | 145 | if (!empty($this->_columns[strtolower($this->sort_by)][strtolower($this->order)])) { |
---|
[130] | 146 | return sprintf(' ORDER BY %s ', $db->escapeString($this->_columns[strtolower($this->sort_by)][strtolower($this->order)])); |
---|
[1] | 147 | } else { |
---|
[128] | 148 | $app->logMsg(sprintf('Could not find SQL to sort by %s %s.', $this->sort_by, $this->order), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 149 | return ''; |
---|
| 150 | } |
---|
[42] | 151 | } |
---|
| 152 | |
---|
| 153 | |
---|
[1] | 154 | /** |
---|
| 155 | * Prints a link for a column header with URL sort determining logic. |
---|
| 156 | * |
---|
| 157 | * @param string $col The database column to sort by. |
---|
| 158 | * @param string $col_name The human-readable title of the column. |
---|
| 159 | * @param string $default_order The default order for this column (ASC, |
---|
| 160 | * for example, for an alphabetical sort) |
---|
| 161 | */ |
---|
| 162 | function printSortHeader($col, $col_name, $default_order='ASC') |
---|
| 163 | { |
---|
[128] | 164 | $app =& App::getInstance(); |
---|
| 165 | |
---|
[1] | 166 | if ($this->sort_by == $col) { |
---|
| 167 | if (strtolower($this->order) == 'desc') { |
---|
[128] | 168 | ?><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 |
---|
[1] | 169 | } else { |
---|
[128] | 170 | ?><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 |
---|
[1] | 171 | } |
---|
| 172 | } else { |
---|
[128] | 173 | ?><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 |
---|
[1] | 174 | } |
---|
[42] | 175 | } |
---|
[1] | 176 | |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | ?> |
---|