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

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

Initial import.

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
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;
29    var $desc_widget;
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->asc_widget = '<img src="/_widgets/sort_ascending.gif" alt="" width="11" height="7" border="0">';
44        $this->desc_widget = '<img src="/_widgets/sort_descending.gif" alt="" width="11" height="7" border="0">';
45       
46        $this->setDefault($default_sort, $default_order);
47   
48        $this->default_sort = $default_sort;
49        $this->default_order = $default_order;
50    }
51   
52    /**
53     * Build an array of valid sort SQL for each DB column. This SQL is reference
54     * by the name and 'asc' or 'desc'.
55     *
56     * @param string $name      Reference name for the column this SQL sorts on.
57     * @param string $asc_sql   The sort SQL if $this->order is ascending.
58     * @param string $desc_sql  The sort SQL if $this->order is descending.
59     */
60    function setColumn($name, $asc_sql, $desc_sql)
61    {
62        $this->_columns[$name] = array(
63            'asc'  => $asc_sql,
64            'desc' => $desc_sql
65        );
66    }
67   
68    /**
69     * Set sort and order values. This is how you set new sort values after
70     * already declaring a SortOrder object, but expect values to come from
71     * getFormData.
72     *
73     * @param string $default_sort   If not found elsewhere, this will be the
74     *                               current sort by.
75     * @param string $default_order  If not found elsewhere, this will be the
76     *                               current order.
77     */
78    function setDefault($default_sort = '', $default_order = '')
79    {
80        // Which column to sort by?
81        // (1) By GET or POST specification, if available.
82        // (2) By saved preference, if available.
83        // (3) By default (provided at class instantiation).
84        $new_sort_by = getFormData('sort');
85        if (!empty($new_sort_by)) {
86            $this->sort_by = $new_sort_by;
87            Prefs::setValue('sort_by', $this->sort_by, $_SERVER['PHP_SELF']);
88        } else if (Prefs::exists('sort_by', $_SERVER['PHP_SELF'])) {
89            $this->sort_by = Prefs::getValue('sort_by', $_SERVER['PHP_SELF']);
90        } else {
91            $this->sort_by = $default_sort;
92        }
93       
94        // Which sort order to use?
95        // (1) By GET or POST specification, if available.
96        // (2) By saved preference, if available.
97        // (3) By default (provided at class instanciation).
98        $new_order = getFormData('order');
99        if (!empty($new_order)) {
100            $this->order = $new_order;
101            Prefs::setValue('sort_order', $this->order, $_SERVER['PHP_SELF']);
102        } else if (Prefs::exists('sort_order', $_SERVER['PHP_SELF'])) {
103            $this->order = Prefs::getValue('sort_order', $_SERVER['PHP_SELF']);
104        } else {
105            $this->order = $default_order;
106        }
107    }
108   
109   
110    /**
111     * Set sort and order values. This is how you set new sort values after
112     * already declaring a SortOrder object. This will ignore getFormData values.
113     *
114     * @param string $col            The database column to sort by.
115     * @param string $col_name       The human-readable title of the column.
116     * @param string $default_order  The default order for this column (ASC,
117     *                               for example, for an alphabetical sort)
118     */
119    function set($sort = null, $order = null)
120    {
121        // Set new sort value.
122        if (isset($sort)) {
123            $this->sort_by = $sort;
124            Prefs::setValue('sort_by', $this->sort_by, $_SERVER['PHP_SELF']);
125        }
126       
127        // Set new order value.
128        if (isset($order)) {
129            $this->order = $order;
130            Prefs::setValue('sort_order', $this->order, $_SERVER['PHP_SELF']);
131        }
132    }
133   
134   
135    /**
136     * Returns the SQL code to sort by set column and set order.
137     */
138    function getSortOrderSQL()
139    {
140        if (!isset($this->_columns[strtolower($this->sort_by)])) {
141            $this->sort_by = $this->default_sort;
142            $this->order = $this->default_order;
143        }
144        if (!isset($this->_columns[strtolower($this->sort_by)][strtolower($this->order)])) {
145            $this->order = 'ASC';
146        }
147
148        if (!empty($this->_columns[strtolower($this->sort_by)][strtolower(strtolower($this->order))])) {
149            return ' ORDER BY ' . addslashes($this->_columns[strtolower($this->sort_by)][strtolower(strtolower($this->order))]);
150        } else {
151            App::logMsg(sprintf('Could not find SQL to sort by %s %s.', $this->sort_by, $this->order), LOG_WARNING, __FILE__, __LINE__);
152            return '';
153        }
154    } 
155   
156   
157    /**
158     * Prints a link for a column header with URL sort determining logic.
159     *
160     * @param string $col            The database column to sort by.
161     * @param string $col_name       The human-readable title of the column.
162     * @param string $default_order  The default order for this column (ASC,
163     *                               for example, for an alphabetical sort)
164     */
165    function printSortHeader($col, $col_name, $default_order='ASC')
166    {
167        if ($this->sort_by == $col) {
168            if (strtolower($this->order) == 'desc') {
169                ?><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
170            } else {
171                ?><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
172            }
173        } else {
174            ?><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
175        }
176    }     
177
178}
179
180?>
Note: See TracBrowser for help on using the repository browser.