source: branches/2.0singleton/lib/SortOrder.inc.php @ 130

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

finished updating DB:: to $db->

File size: 6.7 KB
Line 
1<?php
2/**
3 * SortOrder.inc.php
4 * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information
5 *
6 * SortOrder can determing how to sort results of a database query for display
7 * on a listing. It can print column headers that will be links to
8 * automatically change the sort and order.
9 *
10 * @requires    This class requires Prefs.inc.php
11 *
12 * @author  Quinn Comendant <quinn@strangecode.com>
13 * @version 1.6.2
14 */
15require_once dirname(__FILE__) . '/Prefs.inc.php';
16
17class SortOrder {
18
19    var $_columns;
20    var $sort_by;
21    var $order;
22    var $asc_widget = '<img src="/admin/_widgets/sort_ascending.gif" alt="[ASC]" width="11" height="7" border="0" />';
23    var $desc_widget = '<img src="/admin/_widgets/sort_descending.gif" alt="[DESC]" width="11" height="7" border="0" />';
24    var $default_sort;
25    var $default_order;
26
27    /**
28     * Constructor. Finds the current sort by and order.
29     *
30     * @param string $default_sort   If not found elsewhere, this will be the
31     *                               current sort by.
32     * @param string $default_order  If not found elsewhere, this will be the
33     *                               current order.
34     */
35    function SortOrder($default_sort = '', $default_order = '')
36    {
37        $this->setDefault($default_sort, $default_order);
38
39        $this->default_sort = $default_sort;
40        $this->default_order = $default_order;
41    }
42
43    /**
44     * Build an array of valid sort SQL for each DB column. This SQL is reference
45     * by the name and 'asc' or 'desc'.
46     *
47     * @param string $name      Reference name for the column this SQL sorts on.
48     * @param string $asc_sql   The sort SQL if $this->order is ascending.
49     * @param string $desc_sql  The sort SQL if $this->order is descending.
50     */
51    function setColumn($name, $asc_sql, $desc_sql)
52    {
53        $this->_columns[$name] = array(
54            'asc'  => $asc_sql,
55            'desc' => $desc_sql
56        );
57    }
58
59    /**
60     * Set sort and order values. This is how you set new sort values after
61     * already declaring a SortOrder object, but expect values to come from
62     * getFormData.
63     *
64     * @param string $default_sort   If not found elsewhere, this will be the
65     *                               current sort by.
66     * @param string $default_order  If not found elsewhere, this will be the
67     *                               current order.
68     */
69    function setDefault($default_sort = '', $default_order = '')
70    {
71        // Which column to sort by?
72        // (1) By GET or POST specification, if available.
73        // (2) By saved preference, if available.
74        // (3) By default (provided at class instantiation).
75        $new_sort_by = getFormData('sort');
76        if (!empty($new_sort_by)) {
77            $this->sort_by = $new_sort_by;
78            Prefs::setValue('sort_by', $this->sort_by, $_SERVER['PHP_SELF']);
79        } else if (Prefs::exists('sort_by', $_SERVER['PHP_SELF'])) {
80            $this->sort_by = Prefs::getValue('sort_by', $_SERVER['PHP_SELF']);
81        } else {
82            $this->sort_by = $default_sort;
83        }
84
85        // Which sort order to use?
86        // (1) By GET or POST specification, if available.
87        // (2) By saved preference, if available.
88        // (3) By default (provided at class instanciation).
89        $new_order = getFormData('order');
90        if (!empty($new_order)) {
91            $this->order = $new_order;
92            Prefs::setValue('sort_order', $this->order, $_SERVER['PHP_SELF']);
93        } else if (Prefs::exists('sort_order', $_SERVER['PHP_SELF'])) {
94            $this->order = Prefs::getValue('sort_order', $_SERVER['PHP_SELF']);
95        } else {
96            $this->order = $default_order;
97        }
98    }
99
100
101    /**
102     * Forcibly set sort and order values. This is how you set new sort values after
103     * already declaring a SortOrder object. This will ignore getFormData values.
104     *
105     * @param string $sort           The sort by name.
106     * @param string $order          The order direction (ASC,
107     *                               for example, for an alphabetical sort)
108     */
109    function set($sort = null, $order = null)
110    {
111        // Set new sort value.
112        if (isset($sort)) {
113            $this->sort_by = $sort;
114            Prefs::setValue('sort_by', $this->sort_by, $_SERVER['PHP_SELF']);
115        }
116
117        // Set new order value.
118        if (isset($order)) {
119            $this->order = $order;
120            Prefs::setValue('sort_order', $this->order, $_SERVER['PHP_SELF']);
121        }
122    }
123
124
125    /**
126     * Returns the SQL code to sort by set column and set order.
127     */
128    function getSortOrderSQL()
129    {
130        $app =& App::getInstance();
131        $db =& DB::getInstance();
132
133        if (!isset($this->_columns[strtolower($this->sort_by)])) {
134            $this->sort_by = $this->default_sort;
135            $this->order = $this->default_order;
136        }
137        if (!isset($this->_columns[strtolower($this->sort_by)][strtolower($this->order)])) {
138            $this->order = 'ASC';
139        }
140
141        if (!empty($this->_columns[strtolower($this->sort_by)][strtolower($this->order)])) {
142            return sprintf(' ORDER BY %s ', $db->escapeString($this->_columns[strtolower($this->sort_by)][strtolower($this->order)]));
143        } else {
144            $app->logMsg(sprintf('Could not find SQL to sort by %s %s.', $this->sort_by, $this->order), LOG_WARNING, __FILE__, __LINE__);
145            return '';
146        }
147    }
148
149
150    /**
151     * Prints a link for a column header with URL sort determining logic.
152     *
153     * @param string $col            The database column to sort by.
154     * @param string $col_name       The human-readable title of the column.
155     * @param string $default_order  The default order for this column (ASC,
156     *                               for example, for an alphabetical sort)
157     */
158    function printSortHeader($col, $col_name, $default_order='ASC')
159    {
160        $app =& App::getInstance();
161
162        if ($this->sort_by == $col) {
163            if (strtolower($this->order) == 'desc') {
164                ?><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
165            } else {
166                ?><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
167            }
168        } else {
169            ?><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
170        }
171    }
172
173}
174
175?>
Note: See TracBrowser for help on using the repository browser.