239de460afa3180b09b53d79a9183bd099230d64
[PHP-P10.git] / Tools / Table.class.php
1 <?php
2 /******************************* PHP-P10 v2 *****************************
3  * Copyright (C) 2011-2012  Philipp Kreil (pk910)                       *
4  *                                                                      *
5  * This program is free software: you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation, either version 3 of the License, or    *
8  * (at your option) any later version.                                  *
9  *                                                                      * 
10  * This program is distributed in the hope that it will be useful,      *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
13  * GNU General Public License for more details.                         *
14  *                                                                      *
15  * You should have received a copy of the GNU General Public License    *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  *                                                                      *
18  ************************************************************************
19  * 
20  * Tools/Table.class.php
21  *
22  *  IRC Table Class
23  */
24
25 class Table {
26         private $table;
27         
28         public function Table($colums) {
29                 $this->table = array();
30                 $this->table['set'] = array();
31                 $this->table['data'] = array();
32                 $this->table['set']['col'] = $colums;
33                 $this->table['set']['bold'] = array();
34                 for($i = 0; $i < $this->table['set']['col']; $i++) {
35                         $this->table['set']['max'.$i] = 0;
36                         $this->table['set']['bold'][$i] = false;
37                 }
38         }
39         
40         public function setBold($colum) {
41                 $this->table['set']['bold'][$colum] = true;
42         }
43         
44         public function add() {
45                 $args = func_get_args();
46                 $row = array();
47                 for($i = 0; $i < $this->table['set']['col']; $i++) {
48                         if(count($args) <= $i) $args[$i]= "";
49                         $row[] = $args[$i];
50                         if(count($args) >= $i)
51                                 if(strlen($args[$i]) > $this->table['set']['max'.$i]) $this->table['set']['max'.$i] = strlen($args[$i]);
52                 }
53                 $this->table['data'][] = $row;
54                 return true;
55         }
56         
57         public function end() {
58                 $space = "                                                                                       ";
59                 $output = array();
60                 for($row = 0; $row < count($this->table['data']); $row++) {
61                         $out = "";
62                         for($i = 0; $i < $this->table['set']['col']; $i++) {
63                                 if($i < $this->table['set']['col'] - 1)
64                                         $this->table['data'][$row][$i] .= substr($space,0,$this->table['set']['max'.$i] - strlen($this->table['data'][$row][$i]) + 1);
65                                 $bold = $this->table['set']['bold'][$i];
66                                 $out .= ($bold ? "\002" : "").$this->table['data'][$row][$i].($bold ? "\002" : "");
67                         }
68                         $output[] = $out;
69                 }
70                 return $output;
71         }
72         
73 }
74
75 ?>