File: Renderer.php

package info (click to toggle)
php-structures-datagrid 0.9.3-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 408 kB
  • ctags: 876
  • sloc: php: 2,576; xml: 222; makefile: 2
file content (1311 lines) | stat: -rw-r--r-- 41,602 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
<?php
/**
 * Base class of all Renderer drivers
 * 
 * PHP version 5
 *
 * LICENSE:
 * 
 * Copyright (c) 1997-2007, Andrew Nagy <asnagy@webitecture.org>,
 *                          Olivier Guilyardi <olivier@samalyse.com>,
 *                          Mark Wiesemann <wiesemann@php.net>
 *                          Sascha Grossenbacher <saschagros@bluewin.ch>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the 
 *      documentation and/or other materials provided with the distribution.
 *    * The names of the authors may not be used to endorse or promote products 
 *      derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * CSV file id: $Id$
 * 
 * @version  $Revision$
 * @package  Structures_DataGrid
 * @category Structures
 * @license  http://opensource.org/licenses/bsd-license.php New BSD License
 */

/**
 * Base class of all Renderer drivers
 *
 * SUPPORTED OPTIONS:
 *
 * - buildHeader:         (bool)   Whether to build the header.
 * - buildFooter:         (bool)   Whether to build the footer.
 * - fillWithEmptyRows:   (bool)   Ensures that all pages have the same number of 
 *                                 rows.
 * - numberAlign:         (bool)   Whether to right-align numeric values.
 * - defaultCellValue:    (string) What value to put by default into empty cells.
 * - defaultColumnValues: (array)  Per-column default cell value. This is an array
 *                                 of the form: array(fieldName => value, ...).
 * - hideColumnLinks:     (array)  By default sorting links are enabled on all
 *                                 columns. With this option it is possible to
 *                                 disable sorting links on specific columns. This
 *                                 is an array of the form: array(fieldName, ...).
 *                                 This option only affects drivers that support
 *                                 sorting.
 * - encoding:            (string) The content encoding. If the mbstring extension
 *                                 is present the default value is set from 
 *                                 mb_internal_encoding(), otherwise it is ISO-8859-1.
 * - extraVars:           (array)  Variables to be added to the generated HTTP
 *                                 queries.
 * - excludeVars:         (array)  Variables to be removed from the generated
 *                                 HTTP queries.
 * - columnAttributes:    (array)  Column cells attributes. This is an array of
 *                                 the form:
 *                                 array(fieldName => array(attribute => value, ...) ...)
 *                                 This option is only used by XML/HTML based 
 *                                 drivers.
 * - onMove:              (string) Name of a Javascript function to call on
 *                                 onclick/onsubmit events when the user is either paging
 *                                 or sorting the data. This function 
 *                                 receives a single object argument of the 
 *                                 form: { page: <page>, sort: [{field: <field>, 
 *                                 direction: <direction>}, ...], 
 *                                 data: <user_data> }. Remark: setting this 
 *                                 option doesn't remove the href attribute,
 *                                 you should return false from your handler
 *                                 function to void it (eg: for AJAX, etc..).
 * - onMoveData:          (string) User data passed in the "data" member of the
 *                                 object argument passed to onMove. No JSON
 *                                 serialization is performed, this is assigned
 *                                 as a raw string to the "data" attribute. 
 *                                 It's up to you to add quotes, slashes, etc...
 * 
 * --- DRIVER INTERFACE ---
 *
 * Methods (none required):    
 *     - Constructor
 *     - setContainer()
 *     - getContainer()
 *     - init()
 *     - defaultCellFormatter()
 *     - buildHeader()
 *     - buildBody()
 *     - buildRow()
 *     - buildEmptyRow()
 *     - buildFooter()
 *     - finalize()
 *     - flatten()
 *     - render()
 *     - getPaging()  (deprecated)
 * 
 * Properties (all read-only):    
 *     - $_columns
 *     - $_columnsNum
 *     - $_currentSort
 *     - $_firstRecord
 *     - $_lastRecord
 *     - $_multiSort 
 *     - $_options
 *     - $_page
 *     - $_pageLimit
 *     - $_pagesNum
 *     - $_records
 *     - $_recordsNum
 *     - $_requestPrefix
 *     - $_sortableFields
 *     - $_totalRecordsNum
 *     
 * Options that drivers may handle:
 *     - encoding
 *     - fillWithEmptyRows
 *     - numberAlign
 *     - extraVars
 *     - excludeVars
 * 
 * @version  $Revision$
 * @author   Olivier Guilyardi <olivier@samalyse.com>
 * @author   Mark Wiesemann <wiesemann@php.net>
 * @author   Sascha Grossenbacher <saschagros@bluewin.ch>
 * @access   public
 * @package  Structures_DataGrid
 * @category Structures
 * @abstract
 */ 
class Structures_DataGrid_Renderer
{
    /**
     * Columns' fields names and labels
     * 
     * Drivers can read the content of this property but must not change it.
     * 
     * @var array Structure: 
     *            array(<columnIndex> => array(field => <fieldName>, 
     *                                         label=> <label>), ...)
     *            Where <columnIndex> is zero-based
     * @access protected
     */
    var $_columns = array();

    /**
     * Records content
     *
     * Drivers can read the content of this property but must not change it.
     * 
     * @var array Structure: 
     *            array(
     *              <rowIndex> => array(
     *                 <columnIndex> => array(<cellValue>, ...), 
     *              ...), 
     *            ...)
     *            Where <rowIndex> and <columnIndex> are zero-based
     * @access protected
     */
    var $_records = array();

    /**
     * Fields/directions the data is currently sorted by
     *
     * Drivers can read the content of this property but must not change it.
     *
     * @var     array       Structure: array(fieldName => direction, ....)
     * @access  protected
     */
    var $_currentSort = array();

    /**
     * Whether the backend support sorting by multiple fields
     *
     * Drivers can read the content of this property but must not change it.
     *
     * @var     bool
     * @access  protected
     */
    var $_multiSort = false;

    /**
     * Number of columns
     *
     * Drivers can read the content of this property but must not change it.
     * 
     * @var int
     * @access protected
     */
    var $_columnsNum;

    /**
     * Number of records in the current page
     * 
     * Drivers can read the content of this property but must not change it.
     *
     * @var int
     * @access protected
     */
    var $_recordsNum = 0;

    /**
     * Total number of records as reported by the datasource
     * 
     * Drivers can read the content of this property but must not change it.
     *
     * @var int
     * @access protected
     */
    var $_totalRecordsNum;

    /**
     * First record number (starting from 1), in the current page
     * 
     * Drivers can read the content of this property but must not change it.
     *
     * @var int
     * @access protected
     */
    var $_firstRecord;
    
    /**
     * Last record number (starting from 1), in the current page
     * 
     * Drivers can read the content of this property but must not change it.
     *
     * @var int
     * @access protected
     */
    var $_lastRecord;
    
    /**
     * Current page
     * 
     * Page number starting from 1.
     * 
     * Drivers can read the content of this property but must not change it.
     *
     * @var int
     * @access protected
     */
    var $_page = 1;

    /**
     * Number of records per page
     * 
     * Drivers can read the content of this property but must not change it.
     *
     * @var int
     * @access protected
     */
    var $_pageLimit = null;

    /**
     * Number of pages
     * 
     * Drivers can read the content of this property but must not change it.
     *
     * @var int
     * @access protected
     */
    var $_pagesNum;
    
     /**
     * GET/POST/Cookie parameters prefix
     * 
     * Drivers can read the content of this property but must not change it.
     *
     * @var string
     * @access protected
     */
    var $_requestPrefix = '';

    /**
     * Which fields the datagrid may be sorted by
     * 
     * Drivers can read the content of this property but must not change it.
     *
     * @var array Field names
     * @access protected
     */
    var $_sortableFields = array();

    /**
     * The default directions to sort by 
     *
     * Drivers can read the content of this property but must not change it.
     * 
     * @var array Structure: array(field => ASC|DESC, ...)
     * @access protected
     */
    var $_defaultDirections = array();

    /**
     * Common and driver-specific options
     * 
     * Drivers can read the content of this property but must not change it.
     *
     * @var array
     * @access protected
     * @see Structures_DataGrid_Renderer::setOption()
     * @see Structures_DataGrid_Renderer::_addDefaultOptions()
     */
    var $_options = array();

    /**
     * Special driver features
     *
     * @var array
     * @access protected
     */
    var $_features = array();

    /**
     * Columns objects 
     * 
     * Beware: this is a private property, it is not meant to be accessed
     * by drivers. Use the $_columns property instead
     * 
     * @var array
     * @access private
     * @see Structures_DataGrid_Renderer::_columns
     */
    var $_columnObjects = array();

    /**
     * Whether the datagrid has been built or not
     * @var bool
     * @access private
     * @see Structures_DataGrid_Renderer::isBuilt()
     */
    var $_isBuilt = false;

    /**
     * Cache for the GET parameters that are common to all sorting http queries
     * 
     * @var array
     * @access private
     * @see Structures_DataGrid_Renderer::_buildSortingHttpQuery()
     */
    var $_sortingHttpQueryCommon = null;

    /**
     * Whether streaming is enabled or not
     * 
     * @var bool
     * @access private
     */
    var $_streamingEnabled = false;
    
    /**
     * URL mapper instance, if provided
     * 
     * @var object Net_URL_Mapper
     * @access protected
     */
    var $_urlMapper = null;
    
    /**
     * Instantiate the driver and set default options and features
     *
     * Drivers may overload this method in order to change/add default options.
     *
     * @access  public
     * @see Structures_DataGrid_Renderer::_addDefaultOptions()
     */
    function Structures_DataGrid_Renderer()
    {
        $this->_options = array(
            
            /* Options that the drivers may/should handle */    
            'encoding'              => 'ISO-8859-1',
            'fillWithEmptyRows'     => false,
            'numberAlign'           => true,
            'extraVars'             => array(),
            'excludeVars'           => array(),
            'columnAttributes'      => array(),

            /* Options that must not be accessed by drivers */
            'buildHeader'           => true, 
            'buildFooter'           => true,  
            'defaultCellValue'      => null,
            'defaultColumnValues'   => array(),
            'hideColumnLinks'       => array(), 
            'onMove'                => null,
            'onMoveData'            => '',
        );

        $this->_features = array(
                'streaming' => false, 
                'outputBuffering' => false,
                'objectPreserving' => false,
        );

        if (function_exists('mb_internal_encoding')) {
            $encoding = mb_internal_encoding();
            if ($encoding != 'pass') {
                $this->_options['encoding'] = $encoding;
            }
        }

    }

    /**
     * Adds some default options.
     *
     * This method is meant to be called by drivers. It allows adding some
     * default options. 
     *
     * @access protected
     * @param array $options An associative array of the from:
     *                       array(optionName => optionValue, ...)
     * @return void
     * @see Structures_DataGrid_Renderer::setOption()
     */
    function _addDefaultOptions($options)
    {
        $this->_options = array_merge($this->_options, $options);
    }

    /**
     * Add special driver features
     *
     * This method is meant to be called by drivers. It allows specifying 
     * the special features that are supported by the current driver.
     *
     * @access protected
     * @param array $features An associative array of the form:
     *                        array(feature => true|false, ...)
     * @return void
     */
    function _setFeatures($features)
    {
        $this->_features = array_merge($this->_features, $features);
    }

    /**
     * Set multiple options
     *
     * @param   mixed   $options    An associative array of the form:
     *                              array("option_name" => "option_value",...)
     * @access  public
     */
    function setOptions($options)
    {
        $this->_options = array_merge($this->_options, $options);
    }

    /**
     * Set a single option
     *
     * @param   string  $name       Option name
     * @param   mixed   $value      Option value
     * @access  public
     */
    function setOption($name, $value)
    {
        $this->_options[$name] = $value;
    }

    /**
     * Provide columns
     * 
     * This method is supposed to be called ONLY by the code that loads the 
     * driver. In most cases, that'll be the Structures_DataGrid class.
     * 
     * @param array $columns Array of Structures_DataGrid_Column objects
     * @access public
     */
    function setColumns($columns)
    {
        $this->_columnObjects = $columns;
    }
  
    /**
     * Specify how the datagrid is currently sorted
     *
     * 
     * This method is supposed to be called ONLY by the code that loads the 
     * driver. In most cases, that'll be the Structures_DataGrid class.
     * 
     * The multiSort capabilities is related to the multiSort DataSource
     * feature. In short : the DataGrid checks if the DataSource supports
     * multiSort and informs the Renderer about it.
     * 
     * @param array $currentSort        Structure: 
     *                                  array(fieldName => direction, ....)
     * @param bool  $multiSortCapable   Whether the backend support sorting by
     *                                  multiple fields
     * @access public
     */
    function setCurrentSorting($currentSort, $multiSortCapable = false)
    {
        $this->_currentSort = $currentSort;
        $this->_multiSort   = $multiSortCapable;
    }

    /**
     * Specify page and row limits
     * 
     * This method is supposed to be called ONLY by the code that loads the 
     * driver. In most cases, that'll be the Structures_DataGrid class.
     * 
     * @param int $currentPage Current page number
     * @param int $rowsPerPage Maximum number of rows per page
     * @param int $totalRowNum Total number of data rows
     * @access public
     */
    function setLimit($currentPage, $rowsPerPage, $totalRowNum) {
        $this->_page            = $currentPage;
        $this->_pageLimit       = $rowsPerPage;
        $this->_totalRecordsNum = $totalRowNum;
        $this->_pagesNum        = (is_null($rowsPerPage) or $totalRowNum == 0) ?
            1 : ceil($totalRowNum / $rowsPerPage);
        $this->_firstRecord     = ($currentPage - 1) * $rowsPerPage + 1;
        $this->_lastRecord      = (is_null($rowsPerPage))
                                  ? $totalRowNum
                                  : min($this->_firstRecord + $rowsPerPage - 1,
                                        $totalRowNum);
        if ($this->_lastRecord > $totalRowNum) {
            $this->_lastRecord  = $totalRowNum;
        }
    }

    /**
     * Tell the renderer whether streaming is enabled or not
     * 
     * This method is supposed to be called ONLY by the code that loads the 
     * driver. In most cases, that'll be the Structures_DataGrid class.
     * 
     * @param int $status Whether streaming is enabled or not
     * @access public
     */
    function setStreaming($status) {
        $this->_streamingEnabled = (boolean)$status;
    }

    /**
     * Attach a container object
     *
     * Drivers that provide support for the Structures_DataGrid::fill() method
     * must implement this method.
     *
     * @abstract
     * @param  object Container of the class supported by the driver
     * @access public
     * @return mixed  True or PEAR_Error
     */
    function setContainer($container)
    {
        return $this->_noSupport(__FUNCTION__);
    }

    /**
     * Return the container used by the driver
     *
     * Drivers should implement this method when they have some kind of support
     * for rendering containers.
     * 
     * @abstract
     * @return object Container of the class supported by the driver
     *                or PEAR_Error
     * @access public
     */
    function getContainer()
    {
        return $this->_noSupport(__FUNCTION__);
    }

    /**
     * Create or/and prepare the container
     *
     * Drivers may optionally implement this method for any pre-build() 
     * operations.
     *
     * For the container support, it is responsible for creating the 
     * container if it has not already been provided by the user with 
     * the setContainer() method. It is where preliminary container 
     * setup should also be done.
     *
     * @abstract
     * @access protected
     */
    function init()
    {
    }

    /**
     * Build the header 
     *
     * Drivers may optionally implement this method.
     *
     * @abstract
     * 
     * @param   array $columns Columns' fields names and labels (This is a 
     *                         convenient reference to the $_columns protected
     *                         property)
     * @access  protected
     * @return  void or PEAR_Error
     */
    function buildHeader($columns) 
    {
    }

    /**
     * Stream a chunk of records
     *
     * @param  array    $records   2D array of records
     * @param  integer  $startRow  Starting row number
     * @param  boolean  $eof       Whether the current chunk is the last chunk
     * @access  protected
     * @return  void or PEAR_Error
     */
    function streamBody($records, $startRow, $eof = false)
    {
        $rowNum = count($records);
        for ($row = 0; $row < $rowNum; $row++) {
            $result = $this->buildRow($row + $startRow, $records[$row]);
            if (PEAR::isError($result)) {
                return $result;
            }
        }

        if ($eof && $this->_options['fillWithEmptyRows'] && !is_null($this->_pageLimit)) {
            for ($row = $this->_recordsNum; $row < $this->_pageLimit; $row++) {
                $result = $this->buildEmptyRow($row);
                if (PEAR::isError($result)) {
                    return $result;
                }
            }
        }
    }

    /**
     * Build the body
     *
     * Drivers may overload() this method, if buildRow() and buildEmptyRow() 
     * are not flexible enough.
     *
     * @access  protected
     * @return  void or PEAR_Error
     */
    function buildBody()
    {
        for ($row = 0; $row < $this->_recordsNum; $row++) {
            $result = $this->buildRow($row, $this->_records[$row]);
            if (PEAR::isError($result)) {
                return $result;
            }
        }

        if ($this->_options['fillWithEmptyRows'] && !is_null($this->_pageLimit)) {
            for ($row = $this->_recordsNum; $row < $this->_pageLimit; $row++) {
                $result = $this->buildEmptyRow($row);
                if (PEAR::isError($result)) {
                    return $result;
                }
            }
        }
    }

    /**
     * Build a body row
     *
     * This is a very simple method for drivers to build a row.
     * For more flexibility, drivers should overload buildBody()
     *
     * @param int   $index Row index (zero-based)
     * @param array $data  Record data. 
     *                     Structure: array(0 => <value0>, 1 => <value1>, ...)
     * @return void or PEAR_Error
     * @access protected
     * @abstract
     */
    function buildRow($index, $data)
    {
    }
  
    /**
     * Build an empty row
     *
     * Drivers must overload this method if they need to do something with
     * empty rows that remain at the end of the body.
     * 
     * This method will only be called if the "fillWithEmptyRows" option is
     * enabled.
     * 
     * @param int   $index Row index (zero-based)
     * @return void or PEAR_Error
     * @access protected
     * @abstract
     */
    function buildEmptyRow($index)
    {
    }
    
    /**
     * Build the footer
     *
     * Drivers may optionally implement this method.
     *
     * @abstract
     * @access  protected
     * @return  void or PEAR_Error
     */
    function buildFooter() 
    {
    }

    /**
     * Finish building the datagrid.
     *
     * Drivers may optionally implement this method for any post-build() 
     * operations.
     *
     * @abstract
     * @access  protected
     * @return  void or PEAR_Error
     */
    function finalize()
    {
    }

    /**
     * Retrieve output from the container object 
     * 
     * Drivers may optionally implement this method.
     *
     * This method is meant to retrieve final output from the container.
     * 
     * Usually the container is an object (ex: HTMLTable instance),
     * and the final output a string. 
     *
     * The driver knows how to retrieve such final output from a given 
     * container (ex: HTMLTable::toHTML()), and this is where to do it. 
     *
     * Sometimes the container may not be an object, but the final output
     * itself. In this case, this method should simply return the container.
     * 
     * This method mustn't output anything directly to the standard output.
     *  
     * @abstract
     * @return mixed Output
     * @access protected
     */
    function flatten()
    {
        return $this->_noSupport(__FUNCTION__);
    }

    /**
     * Default formatter for all cells
     * 
     * Drivers may optionally implement this method.
     *
     * @abstract
     * @param string  Cell value 
     * @return string Formatted cell value
     * @access protected
     */
    function defaultCellFormatter($value)
    {
        return $value;
    }

    /**
     * Build the grid
     *
     * Drivers must not overload this method. Pre and post-build operations
     * can be performed in init() and finalize()
     * 
     * @param  array    $chunk     2D array of records
     * @param  integer  $startRow  Starting row number of current chunk
     * @param  boolean  $eof       Whether the current chunk is the last chunk
     * @access public
     * @return void
     */
    function build($chunk, $startRow, $eof = false)
    {
        // on first call of build(): initialize the columns and prepare the header
        if (empty($this->_columns)) {
            $this->_columns = array();
            foreach ($this->_columnObjects as $index => $column) {
                if (!is_null($column->orderBy)) {
                    $field = $column->orderBy;
                    if (!in_array($field,$this->_sortableFields) and 
                        !in_array($field, $this->_options['hideColumnLinks'])
                       ) {
                        $this->_sortableFields[] = $field;
                    }
                } else if (!is_null($column->fieldName)) {
                    $field = $column->fieldName;
                } else {
                    $field = $column->columnName;
                }

                $label = $column->columnName;

                if (isset($this->_options['defaultColumnValues'][$field])) {
                    $column->setAutoFillValue($this->_options['defaultColumnValues'][$field]);
                } else if (!is_null($this->_options['defaultCellValue'])) {
                    $column->setAutoFillValue($this->_options['defaultCellValue']);
                }

                if (is_array($column->attribs)) {
                    if (!array_key_exists($field, $this->_options['columnAttributes'])) {
                        $this->_options['columnAttributes'][$field] = array();
                    }
                    $this->_options['columnAttributes'][$field] =
                        array_merge($this->_options['columnAttributes'][$field],
                                    $column->attribs);
                }

                $this->_defaultDirections[$field] = $column->defaultDirection;

                $this->_columns[$index] = compact('field','label');
            }

            $this->_columnsNum = count($this->_columns);

            $result = $this->init();
            if (PEAR::isError($result)) {
                return $result;
            }

            if ($this->_options['buildHeader']) {
                $result = $this->buildHeader($this->_columns);
                if (PEAR::isError($result)) {
                    return $result;
                }
            }
        }

        $chunkSize = count($chunk);
        $this->_recordsNum += $chunkSize;

        $row = 0;
        for ($rec = 0; $rec < $chunkSize; $rec++) {
            // Currently, no formatting is performed on object records.
            // These are not converted to indexed arrays, so that some
            // renderer drivers might fail to process them.
            if (is_array($chunk[$rec]) or !$this->hasFeature('objectPreserving')) {
                $content = array();
                $col = 0;
                foreach ($this->_columnObjects as $column) {
                    $content[] = $this->recordToCell($column, $chunk[$rec],
                                                     $row + $startRow, $col);
                    $col++;
                }
                $chunk[$rec] = $content;
            }
            $row++;
        }

        if (!$this->hasFeature('streaming')) {
            $this->_records = array_merge($this->_records, $chunk);
        } else {
            $result = $this->streamBody($chunk, $startRow, $eof);
            if (PEAR::isError($result)) {
                return $result;
            }
        }

        // if this is the last chunk, do some final operations
        if ($eof) {
            if (is_null($this->_pageLimit)) {
                $result = $this->_pageLimit = $this->_recordsNum;
                if (PEAR::isError($result)) {
                    return $result;
                }
            }

            if (!$this->hasFeature('streaming')) {
                $result = $this->buildBody();
                if (PEAR::isError($result)) {
                    return $result;
                }
            }

            if ($this->_options['buildFooter']) {
                $result = $this->buildFooter();
                if (PEAR::isError($result)) {
                    return $result;
                }
            }

            $result = $this->finalize();
            if (PEAR::isError($result)) {
                return $result;
            }

            $this->_isBuilt = true;
        }
    }

    /**
     * Returns the output from the renderer (e.g. HTML table, XLS object, ...)
     *
     * Drivers must not overload this method. Output generation has to be 
     * implemented in flatten().
     * 
     * @access  public
     * @return  mixed    The output from the renderer
     */
    function getOutput()
    {
        if ($this->_streamingEnabled) {
            return PEAR::raiseError('getOutput() cannot be used together with ' .
                                    'streaming.');
        }

        if ($this->hasFeature('outputBuffering')) {
            return $this->flatten();
        } else {
            return $this->_noSupport(__FUNCTION__);
        }
    }

    /**
     * Render to the standard output
     *
     * This method may be overloaded by renderer drivers in order to prepare
     * writing to the standard output (like calling header(), etc...).
     * 
     * @access  public
     * @return  void or object PEAR_Error
     */
    function render()
    {
        if ($this->hasFeature('outputBuffering')) {
            echo $this->flatten();
        } else {
            $result = $this->build(array(), 0);
            if (PEAR::isError($result)) {
                return $result;
            }
        }
    }

    /**
     * Return an error related to an unsupported public method
     *
     * When a given public method is not implemented/supported by the driver
     * it must return a PEAR_Error object with code DATAGRID_ERROR_UNSUPPORTED.
     * This is a helper method for generating such PEAR_Error objects. 
     *
     * Example:
     * 
     * <code>
     * function anUnsupportedMethod()
     * {
     *     return $this->_noSupport(__FUNCTION__);
     * }
     * </code>
     *
     * @param string $method The name of the unsupported method
     * @return object PEAR_Error with code DATAGRID_ERROR_UNSUPPORTED
     * @access protected
     */
    function _noSupport($method)
    {
        return PEAR::raiseError("The renderer driver class \"" .get_class($this). 
                                "\" does not support the $method() method",
                                DATAGRID_ERROR_UNSUPPORTED);
    }
    
    /**
     * Sets the rendered status.  This can be used to "flush the cache" in case
     * you need to render the datagrid twice with the second time having changes
     *
     * This is quite an obsolete method...
     * 
     * @access  public
     * @param   bool        $status     The rendered status of the DataGrid
     */
    function setRendered($status = false)
    {
        if (!$status) {
            $this->_isBuilt = false;
        }
        /* What are we supposed to do with $status = true ? */
    }   

     /**
     * Set the HTTP Request prefix
     * 
     * @param string $prefix The prefix string
     * @return void
     * @access public
     */
    function setRequestPrefix($prefix) 
    {
        $this->_requestPrefix = $prefix;
    }

    /**
     * Perform record/column to cell intersection and formatting
     * 
     * @param  object $column The column object
     * @param  array  $record Array of record values
     * @param  int    $row    The row number of the cell
     * @param  int    $col    The column number of the cell
     * @return string Formatted cell value
     * @access private
     */
    function recordToCell($column, $record, $row = null, $col = null)
    {
        $value = '';
        if (isset($column->formatter) and !empty($column->formatter)) {
            $value = $column->formatter($record, $row, $col);
        } else if (isset($column->fieldName)) { 
            $record = (array) $record; // record might be an object
            if (isset($record[$column->fieldName])) {
                $value = $this->defaultCellFormatter($record[$column->fieldName]);
            }
        }

        if (empty($value) and !is_null($column->autoFillValue)) {
            $value = $column->autoFillValue; 
        }

        return $value;
    }

    /**
     * Query the grid build status 
     * 
     * @return bool Whether the grid has already been built or not
     * @access public
     */
    function isBuilt()
    {
        return $this->_isBuilt;
    }
    
    /**
     * Build an HTTP query for sorting a given column
     * 
     * This is a handy method that most drivers can use in order to build 
     * the HTTP queries that are used to sort columns.
     *
     * It takes the global "extraVars", "excludeVars" options as well as the 
     * $_requestPrefix property into account and can also convert the ampersand 
     * to XML/HTML entities according to the "encoding" option.
     *
     * @param string $field            Sort field name
     * @param string $direction        Sort direction
     * @param bool   $convertAmpersand Whether to convert ampersands to 
     *                                 XML/HTML compliant entities
     * @param array  $extraParameters  Optional extra HTTP parameters
     * @return string Query string of the
     * @access protected
     *             
     */
    function _buildSortingHttpQuery($field, $direction, $convertAmpersand = false, 
                                    $extraParameters = array())
    {
        $prefix = $this->_requestPrefix;

        if (is_null($this->_sortingHttpQueryCommon)) {
            // Build and cache the list of common get parameters
            $this->_sortingHttpQueryCommon = $this->_options['extraVars'];
            $ignore   = $this->_options['excludeVars'];
            $ignore[] = $prefix . 'orderBy';
            $ignore[] = $prefix . 'direction';
            foreach ($extraParameters as $var => $value) {
                $ignore[] = $prefix . $var;
            }
            foreach ($_GET as $key => $val) {
                if (!in_array($key, $ignore)) {
                    $this->_sortingHttpQueryCommon[$key] = $val;
                }
            }
        }

        // Build list of GET variables
        $get = array();
        $get[$prefix . 'orderBy'] = $field;
        $get[$prefix . 'direction'] = $direction;
        foreach ($extraParameters as $var => $value) {
            $get[$prefix . $var] = $value;
        }

        // Merge common and column-specific GET variables
        $get = array_merge($this->_sortingHttpQueryCommon, $get);

        // Build query
        if ($convertAmpersand and ini_get('arg_separator.output') == '&') {
            $query = htmlentities(http_build_query($get),ENT_QUOTES,
                                  $this->_options['encoding']);
        } else {
            $query = http_build_query($get);
        }

        return $query;
    }
    
    /**
     * Builds a HTTP URL for sorting and paging.
     * 
     * It uses NUM and optionally adds a query string with extraVars/GET
     *
     * @param string $field     Sort field name
     * @param string $direction Sort direction
     * @param int    $page      Pager index
     * 
     * @return string generated HTTP URL
     */
    function _buildMapperURL($field, $direction, $page = 1) 
    {
        if (!empty($direction)) {
            $direction = strtolower($direction);
        }

        $params = array('page' => $page,
                        'orderBy' => $field,
                        'direction' => $direction);
        
        if (is_null($this->_sortingHttpQueryCommon)) {
            // Build and cache the list of common get parameters
            $prefix = $this->_requestPrefix;
            $this->_sortingHttpQueryCommon = $this->_options['extraVars'];
            $ignore   = $this->_options['excludeVars'];
            $ignore[] = $prefix . 'page';
            $ignore[] = $prefix . 'orderBy';
            $ignore[] = $prefix . 'direction';
            foreach ($_GET as $key => $val) {
                if (!in_array($key, $ignore)) {
                    $this->_sortingHttpQueryCommon[$key] = $val;
                }
            }
        }
            
        return $this->_urlMapper->generate($params, $this->_sortingHttpQueryCommon);
    }

    /**
     * Build a Javascript handler call for a given page and sorting spec
     *
     * @param  string   $page     Page number (can also be "%d" for replacement
     *                            by Pager, etc...)
     * @param  mixed    $sortSpec Array of fields and directions, or raw 
     *                            javascript string
     * @return string             JS function string, semi-colon included
     * @access protected
     */
    function _buildOnMoveCall($page, $sortSpec)
    {
        $handler = '';
        if ($this->_options['onMove']) {
            if (is_array($sortSpec)) {
                $sort = array();
                foreach ($sortSpec as $field => $direction) {
                    $sort[] = "{field: '" . addslashes($field) . "', " .
                              "direction:'$direction'}";
                }
                $sort = "[" . join(',', $sort) . "]";
            } else {
                $sort = $sortSpec;
            }
            $data = $this->_options['onMoveData'] or $data = "''";
            $handler = $this->_options['onMove'] .
                "({ page: $page, sort: $sort, data: $data });";
        }
        return $handler;
    }

    /**
     * List special driver features
     *
     * @return array Of the form: array(feature => true|false, etc...)
     * @access public
     */
    function getFeatures()
    {
        return $this->_features;
    }
   
    /**
     * Tell if the driver as a specific feature
     *
     * @param  string $name Feature name
     * @return bool 
     * @access public
     */
    function hasFeature($name)
    {
        return $this->_features[$name];
    }

    /**
     * Set the URL mapper
     *
     * @param object $instance Net_URL_Mapper instance
     * @return void
     * @access public
     */
    function setUrlMapper($instance)
    {
        $this->_urlMapper = $instance;
    }

    /**
     * Return the URL mapper
     *
     * @return object Net_URL_Mapper instance or null
     * @access public
     */
    function getUrlMapper()
    {
        return $this->_urlMapper;
    }

}

// This function is here because we can't depend on PHP_Compat
if (!function_exists('http_build_query')) {
    function http_build_query($formdata, $numeric_prefix = null)
    {
        // If $formdata is an object, convert it to an array
        if (is_object($formdata)) {
            $formdata = get_object_vars($formdata);
        }

        // Check we have an array to work with
        if (!is_array($formdata)) {
            user_error('http_build_query() Parameter 1 expected to be Array or Object. Incorrect value given.',
                E_USER_WARNING);
            return false;
        }

        // If the array is empty, return null
        if (empty($formdata)) {
            return;
        }

        // Argument seperator
        $separator = ini_get('arg_separator.output');
        if (strlen($separator) == 0) {
            $separator = '&';
        }

        // Start building the query
        $tmp = array ();
        foreach ($formdata as $key => $val) {
            if (is_null($val)) {
                continue;
            }

            if (is_integer($key) && $numeric_prefix != null) {
                $key = $numeric_prefix . $key;
            }

            if (is_scalar($val)) {
                array_push($tmp, urlencode($key) . '=' . urlencode($val));
                continue;
            }

            // If the value is an array, recursively parse it
            if (is_array($val) || is_object($val)) {
                array_push($tmp, http_build_query_helper($val, urlencode($key)));
                continue;
            }

            // The value is a resource
            return null;
        }

        return implode($separator, $tmp);
    }

    // Helper function
    function http_build_query_helper($array, $name)
    {
        $tmp = array ();
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                array_push($tmp, http_build_query_helper($value, sprintf('%s[%s]', $name, $key)));
            } elseif (is_scalar($value)) {
                array_push($tmp, sprintf('%s[%s]=%s', $name, urlencode($key), urlencode($value)));
            } elseif (is_object($value)) {
                array_push($tmp, http_build_query_helper(get_object_vars($value), sprintf('%s[%s]', $name, $key)));
            }
        }

        // Argument seperator
        $separator = ini_get('arg_separator.output');
        if (strlen($separator) == 0) {
            $separator = '&';
        }

        return implode($separator, $tmp);
    }
}

/* vim: set expandtab tabstop=4 shiftwidth=4: */
?>