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

Last change on this file since 44 was 44, checked in by scdev, 18 years ago

${1}

File size: 6.8 KB
Line 
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.1
18 */
19require_once dirname(__FILE__) . '/App.inc.php';
20require_once dirname(__FILE__) . '/Utilities.inc.php';
21require_once dirname(__FILE__) . '/Prefs.inc.php';
22
23class SortOrder {
24
25    var $_columns;
26    var $sort_by;
27    var $order;
28    var $asc_widget = '<img src="/admin/_widgets/sort_ascending.gif" alt="" width="11" height="7" border="0">';
29    var $desc_widget = '<img src="/admin/_widgets/sort_descending.gif" alt="" width="11" height="7" border="0">';
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        $this->setDefault($default_sort, $default_order);
44
45        $this->default_sort = $default_sort;
46        $this->default_order = $default_order;
47    }
48
49    /**
50     * Build an array of valid sort SQL for each DB column. This SQL is reference
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    }
64
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        }
90
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    }
105
106
107    /**
108     * Forcibly set sort and order values. This is how you set new sort values after
109     * already declaring a SortOrder object. This will ignore getFormData values.
110     *
111     * @param string $sort           The sort by name.
112     * @param string $order          The order direction (ASC,
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        }
122
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    }
129
130
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
144        if (!empty($this->_columns[strtolower($this->sort_by)][strtolower($this->order)])) {
145            return sprintf(' ORDER BY %s ', addslashes($this->_columns[strtolower($this->sort_by)][strtolower($this->order)]));
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        }
150    }
151
152
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        }
172    }
173
174}
175
176?>
Note: See TracBrowser for help on using the repository browser.