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
|
<?php ////////////////////////////////////////////////////////////////////// // table.class.php //-------------------------------------------------------------------- // // Class Table - Don't modify this file ! // //-------------------------------------------------------------------- // Revision History // $Id: LSTable.php,v 1.3 2008/07/10 04:23:27 jsgoupil Exp $ //-------------------------------------------------------------------- // Copyright (C) Jean-Sebastien Goupil // http://www.barcodephp.com //-------------------------------------------------------------------- ////////////////////////////////////////////////////////////////////// define( 'TABLE_DEFAULT_ROW_HIDDEN', FALSE ); define( 'TABLE_DEFAULT_COL_HIDDEN', FALSE ); define( 'TABLE_DEFAULT_CELL_VALUE', ' ' ); // If ' ', a cell will appear, if NULL, no cell will appear define( 'TABLE_DEFAULT_TEMPLATE', 'tpl_LS' );
include( 'Table_template.php' ); class LSTable { // PRIVATE ARGUMENTS private $template; // string private $numRows, $numCols; // int private $title; // string private $text = array(); // string private $cellsAttributes = array(), $rowsAttributes = array(); // string private $hiddenRows = array(), $hiddenCols = array(); // bool private $width; // string
private $temp_col; // int private $temp_ascending; // bool
// PUBLIC FUNCTIONS /** * @return * @param int $numRows * @param int $numCols * @param string $width * @param object $parent * @desc Constructor. You can put a $parent object. This will search the first empty place into the parent. **/ function __construct ( $numRows, $numCols, $width, &$parent ) { $this->numRows = intval( $numRows ); $this->numCols = intval( $numCols ); for( $i = 0; $i < $numRows; $i++ ) $this->_setVariable( $i ); for( $i = 0; $i < $numCols; $i++ ) $this->hiddenCols[ $i ] = TABLE_DEFAULT_COL_HIDDEN; $this->title = ''; $this->width = strval( $width );
if( defined( 'TABLE_DEFAULT_TEMPLATE' ) ) $this->setTemplate( TABLE_DEFAULT_TEMPLATE );
// We look for an empty place if $parent isn't null if( is_object( $parent ) ) { $stop = false; for( $i = 0; $i < $parent->numRows; $i++ ) { for( $j = 0; $j < $parent->numCols; $j++ ) { if( $parent->text[ $i ][ $j ] == TABLE_DEFAULT_CELL_VALUE ) { $parent->text[ $i ][ $j ] =& $this; $stop = true; } if( $stop == true ) break; } if( $stop == true ) break; } } }
// No destructor for PHP4 //function __destruct() { // //}
/** * @return void * @param int $row * @param int $col * @param mixed $string * @desc Writes Text or Object into $row and $col. **/ public function setText ( $row, $col, $string ) { if( is_int( $row ) && is_int( $col ) ) if( $row < $this->numRows && $col < $this->numCols ) $this->text[ $row ][ $col ] = $string; }
/** * @return void * @param int $row * @param int $col * @param string $attrib * @param mixed $value * @desc Adds a Cell Attribute at $row and $col. **/ public function addCellAttribute ( $row, $col, $attrib, $value ) { if( is_int( $row ) && is_int( $col ) && is_string( $attrib ) ) if( $row < $this->numRows && $col < $this->numCols ) $this->cellsAttributes[ $row ][ $col ][ strtolower( $attrib ) ] = $value; }
/** * @return void * @param int $row * @param int $col * @param string $attrib * @desc Deletes a Cell Attribute at $row and $col. **/ public function delCellAttribute ( $row, $col, $attrib ) { if( is_int( $row ) && is_int( $col ) && is_string( $attrib ) ) if( $row < $this->numRows && $col < $this->numCols ) unset( $this->cellsAttributes[ $row ][ $col ][ strtolower( $attrib ) ] ); }
/** * @return void * @param int $row * @param string $attrib * @param mixed $value * @desc Adds a Row Attribute at $row. Will be located in TR. **/ public function addRowAttribute ( $row, $attrib, $value ) { if( is_int( $row ) && is_string( $attrib ) ) if( $row < $this->numRows ) $this->rowsAttributes[ $row ][ strtolower( $attrib ) ] = $value; }
/** * @return void * @param int $row * @param string $attrib * @desc Deletes a Row Attribute at $row. **/ public function delRowAttribute ( $row, $attrib ) { if( is_int( $row ) && is_string( $attrib ) ) if( $row < $this->numRows ) unset( $this->rowsAttributes[ $row ][ strtolower( $attrib ) ] ); }
/** * @return void * @param int $row * @param string $attrib * @param mixed $value * @desc Adds a Cell Attribute in all cells located on $row row. **/ public function addAllCellsInRowAttribute ( $row, $attrib, $value ) { if( is_int( $row ) && is_string( $attrib ) ) if( $row < $this->numRows ) for( $i = 0; $i < $this->numCols; $i++ ) $this->cellsAttributes[ $row ][ $i ][ strtolower( $attrib ) ] = $value; }
/** * @return void * @param int $col * @param string $attrib * @param mixed $value * @desc Adds a Cell Attribute in all cells located on $col column. **/ public function addAllCellsInColAttribute ( $col, $attrib, $value ) { if( is_int( $col ) && is_string( $attrib ) ) if( $col < $this->numCols ) for( $i = 0; $i < $this->numRows; $i++ ) $this->cellsAttributes[ $i ][ $col ][ strtolower( $attrib ) ] = $value; }
/** * @return void * @param callback $template * @desc Sets Template (Class) **/ public function setTemplate ( $template ) { if( is_string( $template ) ) if( class_exists( $template ) ) $this->template = strval( $template ); }
/** * @return string * @desc Returns current Template **/ public function template () { return $this->template; }
/** * @return mixed * @param int $row * @param int $col * @desc Returns the text or object contained into $row and $col **/ public function text ( $row, $col ) { if( is_int( $row ) && is_int( $col ) ) if( $row < $this->numRows && $col < $this->numCols ) return $this->text[ $row ][ $col ]; return FALSE; }
/** * @return void * @param int $row * @param int $col * @desc Clears the cell located into $row and $col. Keeps the Cell. */ public function clearCell ( $row, $col ) { if( is_int( $row ) && is_int( $col ) ) { if( $row < $this->numRows && $col < $this->numCols ) { $this->text[ $row ][ $col ] = TABLE_DEFAULT_CELL_VALUE; unset( $this->cellsAttributes[ $row ][ $col ] ); $this->cellsAttributes[ $row ][ $col ] = array(); } } }
/** * @return int * @desc Returns the number of Rows **/ public function numRows () { return $this->numRows; }
/** * @return int * @desc Returns the number of Columns **/ public function numCols () { return $this->numCols; }
/** * @return void * @param int $col * @param bool $ascending * @desc Sorts table based on $col. **/ public function sortColumn ( $col, $ascending = TRUE ) { if( is_int( $col ) && is_bool( $ascending ) ) { if( $col < $this->numCols ) { $this->temp_col = $col; $this->temp_ascending = $ascending; $this->_sort_merge( $this->text ); } } }
/** * @return void * @param int $r * @desc Sets the number of rows of the table. **/ public function setNumRows ( $r ) { if( is_int( $r ) ) if( $r >= 0 ) if( $r > $this->numRows ) $this->insertRows( $this->numRows, $r - $this->numRows ); elseif( $r < $this->numRows ) for( $i = $this->numRows; $i > $r; $i-- ) $this->removeRow( $i - 1 ); }
/** * @return void * @param int $r * @desc Sets the number of columns of the table. **/ public function setNumCols ( $r ) { if( is_int( $r ) ) if( $r >= 0 ) if( $r > $this->numCols ) $this->insertColumns( $this->numCols, $r - $this->numCols ); elseif( $r < $this->numCols ) for( $i = $this->numCols; $i > $r; $i-- ) $this->removeColumn( $i - 1 ); }
/** * @return void * @param int $row * @desc Hides a $row from table **/ public function hideRow ( $row ) { if( is_int( $row ) ) if( $row < $this->numRows ) $this->hiddenRows[ $row ] = TRUE; }
/** * @return void * @param int $col * @desc Hides a $col from table **/ public function hideColumn ( $col ) { if( is_int( $col ) ) if( $col < $this->numCols ) $this->hiddenCols[ $col ] = TRUE; }
/** * @return void * @param int $row * @desc Shows a $row from table **/ public function showRow ( $row ) { if( is_int( $row ) ) if( $row < $this->numRows ) $this->hiddenRows[ $row ] = FALSE; }
/** * @return void * @param int $col * @desc Shows a $col from table **/ public function showColumn ( $col ) { if( is_int( $col ) ) if( $col < $this->numCols ) $this->hiddenCols[ $col ] = FALSE; }
/** * @return bool * @param int $row * @desc Returns TRUE if a row is hidden. **/ public function isRowHidden ( $row ) { if( is_int( $row ) ) if( $row < $this->numRows ) return $this->hiddenRows[ $row ]; return FALSE; }
/** * @return bool * @param int $col * @desc Returns TRUE if a col is hidden. **/ public function isColumnHidden ( $col ) { if( is_int( $col ) ) if( $col < $this->numCols ) return $this->hiddenCols[ $col ]; return FALSE; }
/** * @return void * @param int $row1 * @param int $row2 * @param bool $swapAttributes * @desc Swaps 2 rows. Swaps attributes if $switchAttributes is TRUE. **/ public function swapRows ( $row1, $row2, $swapAttributes = FALSE ) { if( is_int( $row1 ) && is_int( $row2 ) && is_bool( $swapAttributes ) ) { if( $row1 < $this->numRows && $row2 < $this->numRows ) { $temp =& $this->text[ $row1 ]; $this->text[ $row1 ] =& $this->text[ $row2 ]; $this->text[ $row2 ] =& $temp; if( $swapAttributes == TRUE ) { $temp2 = $this->cellsAttributes[ $row1 ]; $this->cellsAttributes[ $row1 ] = $this->cellsAttributes[ $row2 ]; $this->cellsAttributes[ $row2 ] = $temp2;
$temp2 = $this->rowsAttributes[ $row1 ]; $this->rowsAttributes[ $row1 ] = $this->rowsAttributes[ $row2 ]; $this->rowsAttributes[ $row2 ] = $temp2; } } } }
/** * @return void * @param int $col1 * @param int $col2 * @param bool $swapAttributes * @desc Swaps 2 cols. Swaps attributes if $switchAttributes is TRUE. **/ public function swapColumns ( $col1, $col2, $swapAttributes = FALSE ) { if( is_int( $col1 ) && is_int( $col2 ) && is_bool( $swapAttributes ) ) { if( $col1 < $this->numCols && $col2 < $this->numCols ) { for( $i = 0; $i < $this->numRows; $i++ ){ $temp =& $this->text[ $i ][ $col1 ]; $this->text[ $i ][ $col1 ] =& $this->text[ $i ][ $col2 ]; $this->text[ $i ][ $col2 ] =& $temp; if( $swapAttributes == TRUE ) { $temp2 = $this->cellsAttributes[ $i ][ $col1 ]; $this->cellsAttributes[ $i ][ $col1 ] = $this->cellsAttributes[ $i ][ $col2 ]; $this->cellsAttributes[ $i ][ $col2 ] = $temp2; } } } } }
/** * @return void * @param int $row1 * @param int $col1 * @param int $row2 * @param int $col2 * @param bool $swapAttributes * @desc Swaps 2 cells. Swaps attributes if $switchAttributes is TRUE. **/ public function swapCells ( $row1, $col1, $row2, $col2, $swapAttributes = FALSE ) { if( is_int( $row1 ) && is_int( $col1 ) && is_int( $row2 ) && is_int( $col2 ) && is_bool( $swapAttributes ) ) { if( $row1 < $this->numRows && $col1 < $this->numCols && $row2 < $this->numRows && $col2 < $this->numCols ) { $temp =& $this->text[ $row1 ][ $col1 ]; $this->text[ $row1 ][ $col1 ] =& $this->text[ $row2 ][ $col2 ]; $this->text[ $row2 ][ $col2 ] =& $temp; if( $swapAttributes == TRUE ) { $temp2 = $this->cellsAttributes[ $row1 ][ $col1 ]; $this->cellsAttributes[ $row1 ][ $col1 ] = $this->cellsAttributes[ $row2 ][ $col2 ]; $this->cellsAttributes[ $row2 ][ $col2 ] = $temp2; } } } }
/** * @return void * @param int $row * @param int $count * @desc Inserts $count row at $row position. **/ public function insertRows ( $row, $count = 1 ) { if( is_int( $row ) && is_int( $count ) ) { if( $row >= 0 && $row <= $this->numRows ) { for( $i = 0; $i < $count; $i++) { array_splice( $this->text, $row, 0, '' ); array_splice( $this->hiddenRows, $row, 0, FALSE ); array_splice( $this->cellsAttributes, $row, 0, FALSE ); array_splice( $this->rowsAttributes, $row, 0, FALSE ); $this->_setVariable( $row ); $this->numRows += 1; } } } }
/** * @return void * @param int $col * @param int $count * @desc Inserts $count columns at $col position. **/ public function insertColumns ( $col, $count = 1 ) { if( is_int( $col ) && is_int( $count ) ) { if( $col >= 0 && $col <= $this->numCols ) { for( $i = 0; $i < $this->numRows; $i++ ) { $temp_text = array(); $temp_attributes = array(); reset($this->text[ $i ]); for( $j = 0; $j <= $this->numCols; $j++ ) { if( $col == $j ) { for( $k = 0; $k < $count; $k++ ){ $temp_text[] = ''; $temp_attributes[] = array(); } } if( $j == $this->numCols ) break; $temp_text[] = $this->text[ $i ][ $j ]; $temp_attributes[] = $this->cellsAttributes[ $i ][ $j ]; } $this->text[ $i ] = $temp_text; $this->cellsAttributes[ $i ] = $temp_attributes; } for( $i = 0; $i < $count; $i++ ) array_splice( $this->hiddenCols, $col, 0, FALSE ); $this->numCols += $count; } } }
/** * @return void * @param int $row * @desc Removes $row. **/ public function removeRow ( $row ) { if( is_int( $row ) ) { if( $row < $this->numRows ) { array_splice( $this->text, $row, 1 ); array_splice( $this->hiddenRows, $row, 1 ); array_splice( $this->cellsAttributes, $row, 1 ); array_splice( $this->rowsAttributes, $row, 1 ); $this->numRows -= 1; } } }
/** * @return void * @param int $col * @desc Removes $col. **/ public function removeColumn ( $col ) { if( is_int( $col ) ) { if( $col < $this->numCols ) { for( $i = 0; $i < $this->numRows; $i++ ) { $temp_text = array(); $temp_attributes = array(); reset($this->text[ $i ]); while ( list( $key, ) = each( $this->text[ $i ] ) ) { if( $key != $col ) { $temp_text[] = $this->text[ $i ][ $key ]; $temp_attributes[] = $this->cellsAttributes[ $i ][ $key ]; } } $this->text[ $i ] = $temp_text; $this->cellsAttributes[ $i ] = $temp_attributes; } array_splice( $this->hiddenCols, $col, 1 ); $this->numCols -= 1; } } }
/** * @return void * @param int $width * @desc Sets Table Width. **/ public function setWidth ( $width ) { if( is_string( $width ) ) $this->width = strval( $width ); }
/** * @return string * @desc Returns Table Width. **/ public function width () { return $this->width; }
/** * @return void * @param int $title * @desc Sets Table Title. **/ public function setTitle ( $title ) { if( is_string( $title ) ) $this->title = strval( $title ); }
/** * @return string * @desc Returns Table Title. **/ public function title () { return $this->title; }
/** * @return void * @desc Displays the table with its cells. **/ public function draw () { if( $this->template != '' ) { $tpl = new $this->template; $tpl->__header( $this->width, $this->title ); $this->_check_rowcol_span(); for( $i = 0; $i < $this->numRows; $i++ ) { if( $this->hiddenRows[ $i ] == FALSE ) { $tpl->__row_start( $this->rowsAttributes[ $i ] ); for( $j = 0; $j < $this->numCols; $j++ ) { if( $this->hiddenCols[ $j ] == FALSE ) { if( $this->text[ $i ][ $j ] !== NULL ) { if( is_object( $this->text[ $i ][ $j ] ) ) { $tpl->__cell_start( '', $this->cellsAttributes[ $i ][ $j ] ); $this->text[ $i ][ $j ]->draw(); } else $tpl->__cell_start( $this->text[ $i ][ $j ], $this->cellsAttributes[ $i ][ $j ] ); $tpl->__cell_stop( $this->text[ $i ][ $j ], $this->cellsAttributes[ $i ][ $j ] ); } } } $tpl->__row_stop( $this->rowsAttributes[ $i ] ); } } $tpl->__footer( $this->width, $this->title ); } }
// PRIVATE FUNCTIONS /** * @return void * @desc Sets NULL to cells if colspan or rowspan go on the cells **/ private function _check_rowcol_span() { for( $i = 0; $i < $this->numRows; $i++ ) { for( $j = 0; $j < $this->numCols; $j++ ) { if( isset( $this->cellsAttributes[ $i ][ $j ][ 'colspan' ] ) ) if( $this->cellsAttributes[ $i ][ $j ][ 'colspan' ] > 0 ) for( $z = 1; $z < intval( $this->cellsAttributes[ $i ][ $j ][ 'colspan' ] ); $z++ ) $this->text[ $i ][ $j + $z ] = NULL; if( isset( $this->cellsAttributes[ $i ][ $j ][ 'rowspan' ] ) ) if( $this->cellsAttributes[ $i ][ $j ][ 'rowspan' ] > 0 ) for( $z = 1; $z < intval( $this->cellsAttributes[ $i ][ $j ][ 'rowspan' ] ); $z++ ) $this->text[ $i + $z ][ $j ] = NULL; } } }
/** * @return void * @param int $r * @desc Sets Default Variables for row $r. **/ private function _setVariable ( $r ) { $this->text[ $r ] = array_fill( 0, $this->numCols, TABLE_DEFAULT_CELL_VALUE ); $this->cellsAttributes[ $r ] = array_fill( 0, $this->numCols, array() ); $this->rowsAttributes[ $r ] = array(); $this->hiddenRows[ $r ] = TABLE_DEFAULT_ROW_HIDDEN; }
/** * @return void * @param int $r * @desc Unsets Default Variables for row $r. **/ private function _unsetVariable ( $r ) { unset( $this->text[ $r ] ); unset( $this->cellsAttributes[ $r ] ); unset( $this->rowsAttributes[ $r ] ); unset( $this->hiddenRows[ $r ] ); }
/** * @return void * @param string $tab * @desc Merge Function **/ private function _sort_merge ( &$tab ) { if( count( $tab ) <= 1 ) return; else { $tab1 = array(); $tab2 = array();
for( $i = 0; $i < count( $tab ); $i++) { if( $i < ( count( $tab ) ) / 2 ) $tab1[] = $tab[ $i ]; else $tab2[] = $tab[ $i ]; }
$this->_sort_merge( $tab1 ); $this->_sort_merge( $tab2 );
$this->_merge_all( $tab1, $tab2, $tab ); } }
/** * @return void * @param string $tab1 * @param string $tab2 * @param string $tab * @desc Merge Function **/ private function _merge_all ( $tab1, $tab2, &$tab ) { $i = 0; $i1 = $i2 = 0; while( $i1 < count( $tab1 ) && $i2 < count( $tab2 ) ) { if( strcmp( $tab1[ $i1 ][ $this->temp_col ], $tab2[ $i2 ][ $this->temp_col ] ) == (($this->temp_ascending==TRUE)?-1:1) ) $tab[ $i ] = $tab1[ $i1++ ]; else $tab[ $i ] = $tab2[ $i2++ ]; $i++; }
while( $i1 < count( $tab1 ) ) { $tab[ $i ] = $tab1[ $i1++ ]; $i++; } while( $i2 < count( $tab2 ) ) { $tab[ $i ] = $tab2[ $i2++ ]; $i++; } } } ?>
|