source: trunk/lib/SortOrder.inc.php @ 457

Last change on this file since 457 was 457, checked in by anonymous, 10 years ago

Removed use of requireAccessClearance(). Adjusted sequence of sslOn() and requireLogin(). Added ACL::requireAllow() method. Added arguments to SortOrder::set(). Changed behavior of Validator::validateStrDate(). Added use of Validator::validateStrDate() to module maker templates.

File size: 8.6 KB
Line 
1<?php
2/**
3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
5 * Copyright 2001-2012 Strangecode, LLC
6 *
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.
13 *
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.
18 *
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/**
24 * SortOrder.inc.php
25 *
26 * SortOrder can determine 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
31 *
32 * @author  Quinn Comendant <quinn@strangecode.com>
33 * @version 1.6.2
34 */
35require_once dirname(__FILE__) . '/Prefs.inc.php';
36
37class SortOrder {
38
39    var $_columns;
40    var $sort_by;
41    var $order;
42    var $asc_widget;
43    var $desc_widget;
44    var $default_sort;
45    var $default_order;
46
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     */
55    function SortOrder($default_sort = '', $default_order = '')
56    {
57        $app =& App::getInstance();
58
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"));
64
65        // Setup prefs object.
66        $this->prefs = new Prefs($_SERVER['PHP_SELF']);
67        $this->prefs->setParam(array('persistent' => false));
68
69        // Setup defaults.
70        $this->setDefault($default_sort, $default_order);
71        $this->default_sort = $default_sort;
72        $this->default_order = $default_order;
73    }
74
75    /**
76     * Build an array of valid sort SQL for each DB column. This SQL is reference
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     */
83    function setColumn($name, $asc_sql, $desc_sql)
84    {
85        $this->_columns[$name] = array(
86            'asc'  => $asc_sql,
87            'desc' => $desc_sql
88        );
89    }
90
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     */
101    function setDefault($default_sort = '', $default_order = '')
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;
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');
113        } else {
114            $this->sort_by = $default_sort;
115        }
116
117        // Which sort order to use?
118        // (1) By GET or POST specification, if available.
119        // (2) By saved preference, if available.
120        // (3) By default (provided at class instantiation).
121        $new_order = getFormData('order');
122        if (!empty($new_order)) {
123            $this->order = $new_order;
124            $this->prefs->set('sort_order', $this->order);
125        } else if ($this->prefs->exists('sort_order')) {
126            $this->order = $this->prefs->get('sort_order');
127        } else {
128            $this->order = $default_order;
129        }
130    }
131
132
133    /**
134     * Forcibly set sort and order values. This is how you set new sort values after
135     * already declaring a SortOrder object. This will ignore getFormData values.
136     *
137     * @param string $sort           The sort by name.
138     * @param string $order          The order direction (ASC,
139     *                               for example, for an alphabetical sort)
140     */
141    function set($sort=null, $order=null, $save_value=true)
142    {
143        // Set new sort value.
144        if (isset($sort)) {
145            $this->sort_by = $sort;
146            if ($save_value) {
147                $this->prefs->set('sort_by', $this->sort_by);
148            }
149        }
150
151        // Set new order value.
152        if (isset($order)) {
153            $this->order = $order;
154            if ($save_value) {
155                $this->prefs->set('sort_order', $this->order);
156            }
157        }
158    }
159
160    /**
161     * Get the current sort and order values.
162     *
163     * @return array    Array with keys: sort and order. These can be fed back into the SortOrder::set() method or defaults.
164     */
165    function get()
166    {
167        return array(
168            'sort' => $this->prefs->get('sort_by'),
169            'order' => $this->prefs->get('sort_order'),
170        );
171    }
172
173    /**
174     * Returns the SQL code to sort by set column and set order.
175     */
176    function getSortOrderSQL()
177    {
178        $app =& App::getInstance();
179        $db =& DB::getInstance();
180
181        if (!isset($this->_columns[mb_strtolower($this->sort_by)])) {
182            $this->sort_by = $this->default_sort;
183            $this->order = $this->default_order;
184        }
185        if (!isset($this->_columns[mb_strtolower($this->sort_by)][mb_strtolower($this->order)])) {
186            $this->order = 'ASC';
187        }
188
189        if (!empty($this->_columns[mb_strtolower($this->sort_by)][mb_strtolower($this->order)])) {
190            return sprintf(' ORDER BY %s ', $this->_columns[mb_strtolower($this->sort_by)][mb_strtolower($this->order)]);
191        } else {
192            $app->logMsg(sprintf('Could not find SQL to sort by %s %s.', $this->sort_by, $this->order), LOG_WARNING, __FILE__, __LINE__);
193            return '';
194        }
195    }
196
197
198    /**
199     * Prints a link for a column header with URL sort determining logic.
200     * Column must be defined first using setColumn().
201     *
202     * @param string $col            The database column to sort by.
203     * @param string $col_name       The human-readable title of the column.
204     * @param string $default_order  The default order for this column (ASC,
205     *                               for example, for an alphabetical sort)
206     */
207    function printSortHeader($col, $col_name, $default_order='ASC')
208    {
209        $app =& App::getInstance();
210
211        if (isset($this->_columns[$col])) {
212            if ($this->sort_by == $col) {
213                if (mb_strtolower($this->order) == 'desc') {
214                    ?><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
215                } else {
216                    ?><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
217                }
218            } else {
219                ?><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
220            }
221        } else {
222            echo $col_name;
223        }
224    }
225
226}
227
228?>
Note: See TracBrowser for help on using the repository browser.