psf
 All Classes Functions Variables
table.php
1 <?php
2 
3 // Part of simple php framework (spf)
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 - 2017
16 
17 require_once (dirname(__FILE__) . "/../../functions.php");
18 require_once (dirname(__FILE__) . "/element.php");
19 
21 {
22  public $Format = NULL;
23  public $Class = NULL;
24  public $Html;
25 
26  public function __construct($_html_ = "", $_parent = NULL)
27  {
28  parent::__construct($_parent);
29  $this->Html = $_html_;
30  }
31 
32  public function ToHtml()
33  {
34  $html = "";
35  $prefix = "";
36 
37  if ($this->Format !== NULL)
38  $prefix .= " " . $this->Format;
39 
40  if ($this->Style !== NULL)
41  {
42  $style = $this->Style->ToCss();
43  if (strlen($style) > 0)
44  $prefix .= " style=\"" . $style . "\"";
45  }
46 
47  if ($this->Class !== NULL)
48  $prefix .= " class=\"" . $this->Class . "\"";
49 
50  $html = "<td" . $prefix . ">";
51  $html .= $this->Html;
52  $html .= "</td>";
53  return $html;
54  }
55 }
56 
57 class HtmlTable extends HtmlElement
58 {
59  public $Class = NULL;
60  private $mRows = 0;
61  private $mColumns = 0;
62  public $Format = NULL;
63  public $Headers = array();
64  public $BorderSize = 1;
67  public $NameAsClass = false;
68  public $Width = NULL;
70  public $Rows = array();
72  public $RepeatHeader = 0;
73 
74  public function GetFormat()
75  {
76  $f = "border=\"" . $this->BorderSize . "\"";
77  if ($this->Width !== NULL)
78  $f .= " width=\"" . $this->Width . "\"";
79  if ($this->Style !== NULL)
80  $f .= " style=\"" . $this->Style->ToCss() . "\"";
81  if ($this->ClassName !== NULL)
82  $f .= " class=\"" . $this->ClassName . "\"";
83  if ($this->Format !== NULL)
84  {
85  $f .= " $this->Format";
86  }
87  if ($this->Class !== NULL)
88  $f .= " class=\"" . $this->Class . "\"";
89  while (psf_string_startsWith($f, " "))
90  $f = substr($f, 1);
91  return $f;
92  }
93 
94  public function AppendRow(array $cells, $default_style = NULL)
95  {
96  $this->InsertRow($cells, $default_style);
97  }
98 
101  public function InsertRow(array $cells, $default_style = NULL)
102  {
103  if (count($cells) > $this->mColumns)
104  $this->mColumns = count($cells);
105  $mc = array();
106  foreach ($cells as $cell)
107  {
108  if ($default_style === NULL)
109  {
110  array_push($mc, new HtmlTable_Cell($cell));
111  } else
112  {
113  $temp = clone $default_style;
114  $temp->Html = $cell;
115  array_push($mc, $temp);
116  }
117  }
118  array_push($this->Rows, $mc);
119  }
120 
121  public function RowCount()
122  {
123  return count($this->Rows);
124  }
125 
126  private function getHeader()
127  {
128  $html = "";
129  if (count($this->Headers) > 0)
130  {
131  $html .= " <tr>\n";
132  foreach ($this->Headers as $x)
133  {
134  if ($this->NameAsClass)
135  $html .= " <th class=\"" . psf_generate_friendly_name($x) . "\">" . $x . "</th>\n";
136  else
137  $html .= " <th>" . $x . "</th>\n";
138  }
139  $html .= " </tr>\n";
140  }
141  return $html;
142  }
143 
145  public function ToCSV($separator = ";", $replace_quotas = false)
146  {
147  $txt = "";
148  if (count($this->Headers) > 0)
149  {
150  foreach ($this->Headers as $x)
151  {
152  $str = str_replace($separator, "", $x);
153  if ($replace_quotas)
154  {
155  $str = str_replace('"', "", $str);
156  $str = str_replace("'", "", $str);
157  }
158  $txt .= $str . $separator;
159  }
160  $txt .= "\n";
161  }
162  foreach ($this->Rows as $row)
163  {
164  foreach ($row as $cell)
165  {
166  $str = $cell->Html;
167  if ($replace_quotas)
168  {
169  $str = str_replace('"', "", $str);
170  $str = str_replace("'", "", $str);
171  }
172  $txt .= $str . $separator;
173  }
174  $txt .= "\n";
175  }
176  return $txt;
177  }
178 
179  public function ToHtml()
180  {
181  $prefix = "";
182  $html = "<table $prefix" . $this->GetFormat() .">\n";
183  $header = $this->getHeader();
184  $html .= $header;
185  $current_header = 0;
186  foreach ($this->Rows as $row)
187  {
188  if ($this->RepeatHeader)
189  {
190  if ($current_header >= $this->RepeatHeader)
191  {
192  $html .= $header;
193  $current_header = 0;
194  } else
195  {
196  $current_header++;
197  }
198  }
199  $html .= " <tr>\n";
200  $header_n = 0;
201  foreach ($row as $cell)
202  {
203  if ($this->NameAsClass)
204  $cell->Class = psf_generate_friendly_name($this->Headers[$header_n]);
205  $html .= " " . $cell->ToHtml() . "\n";
206  $header_n++;
207  }
208  $html .= " </tr>\n";
209  }
210  $html .= "</table>";
211  return $html;
212  }
213 }