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