1<?php
2/**
3 * XUL Rendering Driver
4 *
5 * PHP versions 4 and 5
6 *
7 * LICENSE:
8 *
9 * Copyright (c) 1997-2007, Andrew Nagy <asnagy@webitecture.org>,
10 *                          Olivier Guilyardi <olivier@samalyse.com>,
11 *                          Mark Wiesemann <wiesemann@php.net>
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 *
18 *    * Redistributions of source code must retain the above copyright
19 *      notice, this list of conditions and the following disclaimer.
20 *    * Redistributions in binary form must reproduce the above copyright
21 *      notice, this list of conditions and the following disclaimer in the
22 *      documentation and/or other materials provided with the distribution.
23 *    * The names of the authors may not be used to endorse or promote products
24 *      derived from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
34 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 *
38 * CSV file id: $Id: XUL.php,v 1.40 2007/12/05 12:42:32 olivierg Exp $
39 *
40 * @version  $Revision: 1.40 $
41 * @category Structures
42 * @package  Structures_DataGrid_Renderer_XUL
43 * @license  http://opensource.org/licenses/bsd-license.php New BSD License
44 */
45
46require_once 'Structures/DataGrid/Renderer.php';
47require_once 'XML/Util.php';
48
49/**
50 * XUL Rendering Driver
51 *
52 * SUPPORTED OPTIONS:
53 *
54 * - selfPath:      (string) The complete path for sorting links
55 *                           (default: $_SERVER['PHP_SELF'])
56 * - columnAttributes:   (-) IGNORED
57 *
58 * SUPPORTED OPERATION MODES:
59 *
60 * - Container Support: no
61 * - Output Buffering:  yes
62 * - Direct Rendering:  no
63 * - Streaming:         no
64 * - Object Preserving: no
65 *
66 * GENERAL NOTES:
67 *
68 * This renderer class will render a XUL listbox.
69 * For additional information on the XUL Listbox, refer to this url:
70 * http://www.xulplanet.com/references/elemref/ref_listbox.html
71 *
72 * You have to setup your XUL document, just as you would with an HTML
73 * document. This driver will only generated the <listbox> element and
74 * content.
75 *
76 * @version     $Revision: 1.40 $
77 * @example     xul.php Using the XUL renderer
78 * @author      Andrew S. Nagy <asnagy@webitecture.org>
79 * @author      Olivier Guilyardi <olivier@samalyse.com>
80 * @author      Mark Wiesemann <wiesemann@php.net>
81 * @access      public
82 * @category    Structures
83 * @package     Structures_DataGrid_Renderer_XUL
84 * @todo        Implement PEAR::XML_XUL upon maturity
85 */
86class Structures_DataGrid_Renderer_XUL extends Structures_DataGrid_Renderer
87{
88    /**
89     * The generated XUL data
90     * @var string
91     * @access protected
92     */
93    var $_xul = '';
94
95    /**
96     * Constructor
97     *
98     * Initialize default options
99     *
100     * @access public
101     */
102    function Structures_DataGrid_Renderer_XUL()
103    {
104        parent::Structures_DataGrid_Renderer();
105        $this->_addDefaultOptions(
106            array(
107                'selfPath' => htmlspecialchars($_SERVER['PHP_SELF'])
108            )
109        );
110        $this->_setFeatures(
111            array(
112                'outputBuffering' => true,
113            )
114        );
115    }
116
117    /**
118     * Initialize the XUL listbox
119     *
120     * @access protected
121     */
122    function init()
123    {
124        $this->_xul = "<listbox rows=\"" . $this->_pageLimit . "\">\n";
125    }
126
127    /**
128     * Build the <listhead> grid header
129     *
130     * @access  protected
131     * @return  void
132     */
133    function buildHeader()
134    {
135        $this->_xul .= "  <listhead>\n";
136        for ($col = 0; $col < $this->_columnsNum; $col++) {
137            $field = $this->_columns[$col]['field'];
138            $label = $this->_columns[$col]['label'];
139
140            if (in_array($field, $this->_sortableFields)) {
141                reset($this->_currentSort);
142                if (list($currentField, $direction) = each($this->_currentSort)
143                     and $currentField == $field) {
144                    if ($direction == 'ASC') {
145                        // The data is currently sorted by $column, ascending.
146                        // That means we want $dirArg set to 'DESC', for the next
147                        // click to trigger a reverse order sort, and we need
148                        // $dirCur set to 'ascending' so that a neat xul arrow
149                        // shows the current "ASC" direction.
150                        $dirArg = 'DESC';
151                        $dirCur = 'ascending';
152                    } else {
153                        // Next click will do ascending sort, and we show a reverse
154                        // arrow because we're currently descending.
155                        $dirArg = 'ASC';
156                        $dirCur = 'descending';
157                    }
158                } else {
159                    // No current sort on this column. Next click will sort by
160                    // the default direction. We show no arrow.
161                    $dirArg = $this->_defaultDirections[$field];
162                    $dirCur = 'natural';
163                }
164
165                if ($handler = $this->_buildOnMoveCall($this->_page,
166                        array($field => $dirArg))) {
167                    $onCommand = "oncommand=\"$handler\"";
168                } else {
169                    $onCommand =
170                        "oncommand=\"location.href='{$this->_options['selfPath']}?"
171                        . $this->_buildSortingHttpQuery($field, $dirArg, true)
172                        . "'\"";
173                }
174                $sortDirection = "sortDirection=\"$dirCur\"";
175            } else {
176                $onCommand = '';
177                $sortDirection = '';
178            }
179
180            $label = XML_Util::replaceEntities($label);
181            $this->_xul .= '    <listheader label="' . $label . '" ' .
182                    "$sortDirection $onCommand />\n";
183        }
184        $this->_xul .= "  </listhead>\n";
185    }
186
187    /**
188     * Handles building the body of the table
189     *
190     * @access  protected
191     * @return  void
192     */
193    function buildBody()
194    {
195        for ($row = 0; $row < $this->_recordsNum; $row++) {
196            $this->_xul .= "  <listitem>\n";
197            for ($col = 0; $col < $this->_columnsNum; $col++) {
198                $value = $this->_records[$row][$col];
199
200                // FIXME: '�' is displayed as '?' ==> encoding is required!
201                // How to use the "encoding" option here ?
202                // Is it our responsibility ?
203                $this->_xul .= '    ' .
204                        XML_Util::createTag('listcell',
205                                            array('label' => $value)) . "\n";
206            }
207
208            $this->_xul .= "  </listitem>\n";
209        }
210    }
211
212    /**
213     * Close the XUL listbox
214     *
215     * @access protected
216     */
217    function finalize()
218    {
219        $this->_xul .= "</listbox>\n";
220    }
221
222    /**
223     * Returns the XUL for the DataGrid
224     *
225     * @access  public
226     * @return  string      The XUL of the DataGrid
227     */
228    function toXUL()
229    {
230        return $this->getOutput();
231    }
232
233    /**
234     * Retrieve output from the container object
235     *
236     * @return mixed Output
237     * @access protected
238     */
239    function flatten()
240    {
241        return $this->_xul;
242    }
243}
244
245/* vim: set expandtab tabstop=4 shiftwidth=4: */
246?>
247