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

Last change on this file since 20 was 20, checked in by scdev, 19 years ago

Tons of little updates and bugfixes. CSS updates to templates and core css files. File upload ability to module_maker. Remade Upload interface to use setParam/getParam.

File size: 7.0 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     * 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 $col            The database column to sort by.
112     * @param string $col_name       The human-readable title of the column.
113     * @param string $default_order  The default order for this column (ASC,
114     *                               for example, for an alphabetical sort)
115     */
116    function set($sort = null, $order = null)
117    {
118        // Set new sort value.
119        if (isset($sort)) {
120            $this->sort_by = $sort;
121            Prefs::setValue('sort_by', $this->sort_by, $_SERVER['PHP_SELF']);
122        }
123       
124        // Set new order value.
125        if (isset($order)) {
126            $this->order = $order;
127            Prefs::setValue('sort_order', $this->order, $_SERVER['PHP_SELF']);
128        }
129    }
130   
131   
132    /**
133     * Returns the SQL code to sort by set column and set order.
134     */
135    function getSortOrderSQL()
136    {
137        if (!isset($this->_columns[strtolower($this->sort_by)])) {
138            $this->sort_by = $this->default_sort;
139            $this->order = $this->default_order;
140        }
141        if (!isset($this->_columns[strtolower($this->sort_by)][strtolower($this->order)])) {
142            $this->order = 'ASC';
143        }
144
145        if (!empty($this->_columns[strtolower($this->sort_by)][strtolower($this->order)])) {
146            return ' ORDER BY ' . addslashes($this->_columns[strtolower($this->sort_by)][strtolower($this->order)]);
147        } else {
148            App::logMsg(sprintf('Could not find SQL to sort by %s %s.', $this->sort_by, $this->order), LOG_WARNING, __FILE__, __LINE__);
149            return '';
150        }
151    } 
152   
153   
154    /**
155     * Prints a link for a column header with URL sort determining logic.
156     *
157     * @param string $col            The database column to sort by.
158     * @param string $col_name       The human-readable title of the column.
159     * @param string $default_order  The default order for this column (ASC,
160     *                               for example, for an alphabetical sort)
161     */
162    function printSortHeader($col, $col_name, $default_order='ASC')
163    {
164        if ($this->sort_by == $col) {
165            if (strtolower($this->order) == 'desc') {
166                ?><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
167            } else {
168                ?><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
169            }
170        } else {
171            ?><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
172        }
173    }     
174
175}
176
177?>
Note: See TracBrowser for help on using the repository browser.