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