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

Last change on this file since 262 was 247, checked in by quinn, 17 years ago

Converted all string functions to multi-byte (mb_*) functions

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