1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class that generates a table in HTML from array data. |
5
|
|
|
* |
6
|
|
|
* @author Alicia Fagerving <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Vesihiisi\Ctable; |
10
|
|
|
|
11
|
|
|
class Ctable |
12
|
|
|
{ |
13
|
|
|
private $headers; |
14
|
|
|
private $footers; |
15
|
|
|
private $rows; |
16
|
|
|
private $caption; |
17
|
|
|
private $class; |
18
|
|
|
private $id; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Output an html representation of a table row. |
22
|
|
|
* |
23
|
|
|
* @param array $row the values that are supposed to be in the row |
24
|
|
|
* @return string $html the html code representing the row |
25
|
|
|
*/ |
26
|
|
|
private function generateTableRow($row) |
27
|
|
|
{ |
28
|
|
|
$html = ''; |
29
|
|
|
$html .= "<tr>"; |
30
|
|
|
foreach ($row as $rowItem) { |
31
|
|
|
$html .= "<td>$rowItem</td>"; |
32
|
|
|
} |
33
|
|
|
$html .= "</tr>"; |
34
|
|
|
return $html; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Output an html representation of a table header. |
39
|
|
|
* |
40
|
|
|
* @param array $headers the values that are supposed to be in the header row |
41
|
|
|
* @return string $html the html code representing the header |
42
|
|
|
*/ |
43
|
8 |
|
private function generateTableHead($headers) |
44
|
|
|
{ |
45
|
8 |
|
$html = ''; |
46
|
8 |
|
$html .= "<thead>"; |
47
|
|
|
$html .= $this->generateTableRow($headers); |
48
|
8 |
|
$html .= "</thead>"; |
49
|
8 |
|
return $html; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Output an html representation of a table footer. |
54
|
|
|
* @param array $footers the values that are supposed to be in the footer row |
55
|
|
|
* @return string $html the html code representing the footer |
56
|
|
|
*/ |
57
|
1 |
|
private function generateTableFoot($footers) |
58
|
|
|
{ |
59
|
1 |
|
$html = ""; |
60
|
1 |
|
$html .= "<tfoot>"; |
61
|
|
|
$html .= $this->generateTableRow($footers); |
62
|
1 |
|
$html .= "</tfoot>"; |
63
|
1 |
|
return $html; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Output an html representation of a table body, |
68
|
|
|
* contained within <tbody> tags and containing a number |
69
|
|
|
* of rows. |
70
|
|
|
* @param array $rows the rows that are supposed to be contained by the table body |
71
|
|
|
* @return string $html the html code representing the table body |
72
|
|
|
*/ |
73
|
8 |
|
private function generateTableBody($rows) |
74
|
|
|
{ |
75
|
8 |
|
$html = ''; |
76
|
8 |
|
$html .= '<tbody>'; |
77
|
|
|
foreach ($rows as $row) { |
78
|
|
|
$html .= $this->generateTableRow($row); |
79
|
|
|
} |
80
|
8 |
|
$html .= '</tbody>'; |
81
|
8 |
|
return $html; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Output an html representation |
86
|
|
|
* of a table caption. |
87
|
|
|
* @param string $text the string that is supposed to be the table caption |
88
|
|
|
* @return string the html code representing the table caption |
89
|
|
|
*/ |
90
|
1 |
|
private function generateTableCaption($text) |
91
|
|
|
{ |
92
|
1 |
|
return "<caption>$text</caption>"; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Output the opening <table> tag to initate a table. |
97
|
|
|
* It can look in different ways depending on whether there are any |
98
|
|
|
* class or id attributes. |
99
|
|
|
* @return string the html code representing the opening <table> tag |
100
|
|
|
*/ |
101
|
8 |
|
private function openTable() |
102
|
|
|
{ |
103
|
8 |
|
$addons = array(); |
104
|
|
|
if (count($this->class) == 0 && empty($this->id)) { |
105
|
5 |
|
return "<table>"; |
106
|
|
|
} |
107
|
|
|
if (count($this->class) > 0) { |
108
|
|
|
$classes = implode(" ", $this->class); |
109
|
2 |
|
$addons[] = "class = '$classes'"; |
110
|
|
|
} |
111
|
1 |
|
if (!empty($this->id) && strlen($this->id) > 0) { |
112
|
2 |
|
$id = $this->id; |
113
|
2 |
|
$addons[] = "id = '$id'"; |
114
|
|
|
} |
115
|
|
|
$addonsString = implode(" ", $addons); |
116
|
3 |
|
return "<table $addonsString>"; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Output the html code that represents a table |
121
|
|
|
* consisting of a body and a header as well as, optionally, a footer and/or |
122
|
|
|
* a caption. |
123
|
|
|
* @return string $html the html representation of a table |
124
|
|
|
*/ |
125
|
8 |
|
private function generateTable() |
126
|
|
|
{ |
127
|
8 |
|
$html = ''; |
128
|
|
|
$html .= $this->openTable(); |
129
|
8 |
|
if ($this->caption != null) { |
130
|
|
|
$html .= $this->generateTableCaption($this->caption); |
131
|
|
|
} |
132
|
|
|
$html .= $this->generateTableHead($this->headers); |
133
|
|
|
$html .= $this->generateTableBody($this->rows); |
134
|
7 |
|
if (!empty($this->footers) && count($this->footers) > 0) { |
135
|
|
|
$html .= $this->generateTableFoot($this->footers); |
136
|
|
|
} |
137
|
8 |
|
$html .= "</table>"; |
138
|
8 |
|
return $html; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Initiate the object and save the data as inside parameters of the class. |
143
|
|
|
* @param array $data the array containing the data that are needed |
144
|
|
|
* to create the table |
145
|
|
|
*/ |
146
|
9 |
|
public function __construct($data) |
147
|
|
|
{ |
148
|
|
|
$this->setRows($data['rows']); |
149
|
9 |
|
if (!empty($data['headers'])) { |
150
|
|
|
$this->setHeaders($data['headers']); |
151
|
|
|
} else { |
152
|
|
|
$this->setHeaders($data['rows'][0]); |
153
|
|
|
array_shift($this->rows); |
154
|
8 |
|
} |
155
|
9 |
|
if (!empty($data['footers'])) { |
156
|
|
|
$this->setFooters($data['footers']); |
157
|
|
|
} |
158
|
9 |
|
if (!empty($data['caption'])) { |
159
|
1 |
|
$this->caption = $data['caption']; |
160
|
|
|
} |
161
|
9 |
|
if (!empty($data['class'])) { |
162
|
|
|
$this->setClass($data['class']); |
163
|
|
|
} |
164
|
9 |
|
if (!empty($data['id'])) { |
165
|
|
|
$this->setId($data['id']); |
166
|
|
|
} |
167
|
9 |
|
if (!empty($data['caption'])) { |
168
|
|
|
$this->setCaption($data['caption']); |
169
|
|
|
} |
170
|
9 |
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Save an array with rows as a property of the class. |
174
|
|
|
* @param array $array an array containing rows |
175
|
|
|
*/ |
176
|
9 |
|
public function setRows($array) |
177
|
|
|
{ |
178
|
9 |
|
$this->rows = $array; |
179
|
9 |
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Save an array with headers as a property of the class. |
183
|
|
|
* @param array $array an array containing table headers |
184
|
|
|
*/ |
185
|
9 |
|
public function setHeaders($array) |
186
|
|
|
{ |
187
|
9 |
|
$this->headers = $array; |
188
|
9 |
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Save an array with footers as a property of the class. |
192
|
|
|
* @param array $array an array containing table footers |
193
|
|
|
*/ |
194
|
1 |
|
public function setFooters($array) |
195
|
|
|
{ |
196
|
1 |
|
$this->footers = $array; |
197
|
1 |
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Save a table caption as a property of the class. |
201
|
|
|
* @param string $string a value that is supposed to be used as the table caption |
202
|
|
|
*/ |
203
|
1 |
|
public function setCaption($string) |
204
|
|
|
{ |
205
|
1 |
|
$this->caption = $string; |
206
|
1 |
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Save some CSS classes that are supposed to be assigned to the table |
210
|
|
|
* as a property of the class. |
211
|
|
|
* @param array $array an array that is a list of CSS classes |
212
|
|
|
*/ |
213
|
2 |
|
public function setClass($array) |
214
|
|
|
{ |
215
|
2 |
|
$this->class = $array; |
216
|
2 |
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Save a CSS id that is supposed to be assigned to the table |
220
|
|
|
* as a property of the class. |
221
|
|
|
* @param string $text a CSS id that is supposed to be assigned |
222
|
|
|
*/ |
223
|
2 |
|
public function setId($text) |
224
|
|
|
{ |
225
|
2 |
|
$this->id = $text; |
226
|
2 |
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Add one row of data to the existing array of |
230
|
|
|
* rows of data that is a property of the class. |
231
|
|
|
* @param array $data a row of data values |
232
|
|
|
*/ |
233
|
1 |
|
public function addRow($data) |
234
|
|
|
{ |
235
|
1 |
|
$this->rows[] = $data; |
236
|
1 |
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Tell me how many rows of data this table has. |
240
|
|
|
* @return integer a number representing how many rows of data this table has |
241
|
|
|
*/ |
242
|
|
|
public function getNumberOfRows() |
243
|
|
|
{ |
244
|
|
|
return count($this->rows); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Output the generated html table. |
249
|
|
|
* @return string complete HTML code representing a table |
250
|
|
|
*/ |
251
|
|
|
public function View() |
252
|
|
|
{ |
253
|
|
|
return $this->generateTable(); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|