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