source: tags/2.1.5/lib/SortOrder.inc.php

Last change on this file was 377, checked in by quinn, 14 years ago

Releasing trunk as stable version 2.1.5

File size: 8.1 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-2010 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 determing 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)
142    {
143        // Set new sort value.
144        if (isset($sort)) {
145            $this->sort_by = $sort;
146            $this->prefs->set('sort_by', $this->sort_by);
147        }
148
149        // Set new order value.
150        if (isset($order)) {
151            $this->order = $order;
152            $this->prefs->set('sort_order', $this->order);
153        }
154    }
155
156
157    /**
158     * Returns the SQL code to sort by set column and set order.
159     */
160    function getSortOrderSQL()
161    {
162        $app =& App::getInstance();
163        $db =& DB::getInstance();
164
165        if (!isset($this->_columns[mb_strtolower($this->sort_by)])) {
166            $this->sort_by = $this->default_sort;
167            $this->order = $this->default_order;
168        }
169        if (!isset($this->_columns[mb_strtolower($this->sort_by)][mb_strtolower($this->order)])) {
170            $this->order = 'ASC';
171        }
172
173        if (!empty($this->_columns[mb_strtolower($this->sort_by)][mb_strtolower($this->order)])) {
174            return sprintf(' ORDER BY %s ', $this->_columns[mb_strtolower($this->sort_by)][mb_strtolower($this->order)]);
175        } else {
176            $app->logMsg(sprintf('Could not find SQL to sort by %s %s.', $this->sort_by, $this->order), LOG_WARNING, __FILE__, __LINE__);
177            return '';
178        }
179    }
180
181
182    /**
183     * Prints a link for a column header with URL sort determining logic.
184     * Column must be defined first using setColumn().
185     *
186     * @param string $col            The database column to sort by.
187     * @param string $col_name       The human-readable title of the column.
188     * @param string $default_order  The default order for this column (ASC,
189     *                               for example, for an alphabetical sort)
190     */
191    function printSortHeader($col, $col_name, $default_order='ASC')
192    {
193        $app =& App::getInstance();
194
195        if (isset($this->_columns[$col])) {
196            if ($this->sort_by == $col) {
197                if (mb_strtolower($this->order) == 'desc') {
198                    ?><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
199                } else {
200                    ?><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
201                }
202            } else {
203                ?><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
204            }           
205        } else {
206            echo $col_name;
207        }
208    }
209
210}
211
212?>
Note: See TracBrowser for help on using the repository browser.