psf
table.php
1 <?php
2 
3 // Part of php simple framework (psf)
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 // Copyright (c) Petr Bena <petr@bena.rocks> 2015 - 2018
16 
17 if (!defined("PSF_ENTRY_POINT"))
18  die("Not a valid psf entry point");
19 
20 require_once (dirname(__FILE__) . "/../../functions.php");
21 require_once (dirname(__FILE__) . "/element.php");
22 
24 {
25  public $Format = NULL;
26  public $Class = NULL;
27  public $Html;
28  public $PsfObj = NULL;
29 
30  public function __construct($_html_ = "", $_parent = NULL)
31  {
32  parent::__construct($_parent);
33  if ($_html_ instanceof HtmlElement)
34  $this->PsfObj = $_html_;
35  else
36  $this->Html = $_html_;
37  }
38 
39  public function ToHtml()
40  {
41  $html = "";
42  $prefix = "";
43 
44  if ($this->Format !== NULL)
45  $prefix .= " " . $this->Format;
46 
47  if ($this->Style !== NULL)
48  {
49  $style = $this->Style->ToCss();
50  if (strlen($style) > 0)
51  $prefix .= " style=\"" . $style . "\"";
52  }
53 
54  if ($this->Class !== NULL)
55  $prefix .= " class=\"" . $this->Class . "\"";
56 
57  $html = "<td" . $prefix . ">";
58  if ($this->PsfObj === NULL)
59  $html .= $this->Html;
60  else
61  $html .= $this->PsfObj->ToHtml();
62  $html .= "</td>";
63  return $html;
64  }
65 }
66 
67 class HtmlTable extends HtmlElement
68 {
69  public $Class = NULL;
70  private $mRows = 0;
71  private $mColumns = 0;
72  public $Format = NULL;
73  public $Headers = array();
74  public $BorderSize = 1;
77  public $NameAsClass = false;
78  public $Width = NULL;
80  public $Rows = array();
82  public $RepeatHeader = 0;
84  public $ColWidth = array();
85 
86  public function GetFormat()
87  {
88  $f = "";
89  if ($this->BorderSize !== NULL)
90  $f .= " border=\"" . $this->BorderSize . "\"";
91  if ($this->Width !== NULL)
92  $f .= " width=\"" . $this->Width . "\"";
93  if ($this->Style !== NULL)
94  $f .= " style=\"" . $this->Style->ToCss() . "\"";
95  if ($this->ClassName !== NULL)
96  $f .= " class=\"" . $this->ClassName . "\"";
97  if ($this->Format !== NULL)
98  $f .= " $this->Format";
99  if ($this->Class !== NULL)
100  $f .= " class=\"" . $this->Class . "\"";
101  while (psf_string_startsWith($f, " "))
102  $f = substr($f, 1);
103  return $f;
104  }
105 
106  public function AppendRow(array $cells, $default_style = NULL)
107  {
108  $this->InsertRow($cells, $default_style);
109  }
110 
111  public function SetColumnWidth($column_id, $width)
112  {
113  $this->ColWidth[$column_id] = $width;
114  }
115 
118  public function InsertRow(array $cells, $default_style = NULL)
119  {
120  if (count($cells) > $this->mColumns)
121  $this->mColumns = count($cells);
122  $mc = array();
123  foreach ($cells as $cell)
124  {
125  if ($default_style === NULL)
126  {
127  array_push($mc, new HtmlTable_Cell($cell));
128  } else
129  {
130  $temp = clone $default_style;
131  $temp->Html = $cell;
132  array_push($mc, $temp);
133  }
134  }
135  array_push($this->Rows, $mc);
136  }
137 
138  public function RowCount()
139  {
140  return count($this->Rows);
141  }
142 
143  private function getHeader()
144  {
145  $html = "";
146  if (count($this->Headers) > 0)
147  {
148  $html .= " <tr>\n";
149  $header_id = 0;
150  foreach ($this->Headers as $x)
151  {
152  $custom_style = '';
153  if (array_key_exists($header_id, $this->ColWidth))
154  $custom_style = ' style="width:' . $this->ColWidth[$header_id] . '"';
155  if ($this->NameAsClass)
156  $html .= ' <th' . $custom_style . ' class="' . psf_generate_friendly_name($x) . '">' . $x . "</th>\n";
157  else
158  $html .= ' <th' . $custom_style . '>' . $x . "</th>\n";
159 
160  $header_id += 1;
161  }
162  $html .= " </tr>\n";
163  }
164  return $html;
165  }
166 
168  public function ToCSV($separator = ";", $replace_quotas = false)
169  {
170  $txt = "";
171  if (count($this->Headers) > 0)
172  {
173  foreach ($this->Headers as $x)
174  {
175  $str = str_replace($separator, "", $x);
176  if ($replace_quotas)
177  {
178  $str = str_replace('"', "", $str);
179  $str = str_replace("'", "", $str);
180  }
181  $txt .= $str . $separator;
182  }
183  $txt .= "\n";
184  }
185  foreach ($this->Rows as $row)
186  {
187  foreach ($row as $cell)
188  {
189  $str = $cell->Html;
190  if ($replace_quotas)
191  {
192  $str = str_replace('"', "", $str);
193  $str = str_replace("'", "", $str);
194  }
195  $txt .= $str . $separator;
196  }
197  $txt .= "\n";
198  }
199  return $txt;
200  }
201 
202  public function ToHtml()
203  {
204  $prefix = "";
205  $html = "<table $prefix" . $this->GetFormat() .">\n";
206  $header = $this->getHeader();
207  $html .= $header;
208  $current_header = 0;
209  foreach ($this->Rows as $row)
210  {
211  if ($this->RepeatHeader)
212  {
213  if ($current_header >= $this->RepeatHeader)
214  {
215  $html .= $header;
216  $current_header = 0;
217  } else
218  {
219  $current_header++;
220  }
221  }
222  $html .= " <tr>\n";
223  $header_n = 0;
224  foreach ($row as $cell)
225  {
226  if ($this->NameAsClass)
227  $cell->Class = psf_generate_friendly_name($this->Headers[$header_n]);
228  $html .= " " . $cell->ToHtml() . "\n";
229  $header_n++;
230  }
231  $html .= " </tr>\n";
232  }
233  $html .= "</table>";
234  return $html;
235  }
236 }
InsertRow(array $cells, $default_style=NULL)
Insert a new row by array that consist of html blocks only if you want to directly append array of ht...
Definition: table.php:118
ToCSV($separator=";", $replace_quotas=false)
Converts the table to CSV format and return as a string.
Definition: table.php:168