|
1
|
|
|
<?php |
|
2
|
|
|
defined('ROOT_PATH') || exit('Access denied'); |
|
3
|
|
|
/** |
|
4
|
|
|
* TNH Framework |
|
5
|
|
|
* |
|
6
|
|
|
* A simple PHP framework using HMVC architecture |
|
7
|
|
|
* |
|
8
|
|
|
* This content is released under the GNU GPL License (GPL) |
|
9
|
|
|
* |
|
10
|
|
|
* Copyright (C) 2017 Tony NGUEREZA |
|
11
|
|
|
* |
|
12
|
|
|
* This program is free software; you can redistribute it and/or |
|
13
|
|
|
* modify it under the terms of the GNU General Public License |
|
14
|
|
|
* as published by the Free Software Foundation; either version 3 |
|
15
|
|
|
* of the License, or (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* This program is distributed in the hope that it will be useful, |
|
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20
|
|
|
* GNU General Public License for more details. |
|
21
|
|
|
* |
|
22
|
|
|
* You should have received a copy of the GNU General Public License |
|
23
|
|
|
* along with this program; if not, write to the Free Software |
|
24
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
class Html{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Generate the html anchor link |
|
31
|
|
|
* @param string $link the href attribute value |
|
32
|
|
|
* @param string $anchor the displayed anchor |
|
33
|
|
|
* @param array $attributes the additional attributes to be added |
|
34
|
|
|
* @param boolean $return whether need return the generated html or just display it directly |
|
35
|
|
|
* |
|
36
|
|
|
* @return string|void the anchor link generated html if $return is true or display it if not |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function a($link = '', $anchor = null, array $attributes = array(), $return = true){ |
|
39
|
|
|
$link = Url::site_url($link); |
|
40
|
|
|
if(! $anchor){ |
|
41
|
|
|
$anchor = $link; |
|
42
|
|
|
} |
|
43
|
|
|
$str = null; |
|
44
|
|
|
$str .= '<a href = "'.$link.'"'; |
|
45
|
|
|
$str .= attributes_to_string($attributes); |
|
46
|
|
|
$str .= '>'; |
|
47
|
|
|
$str .= $anchor; |
|
48
|
|
|
$str .= '</a>'; |
|
49
|
|
|
|
|
50
|
|
|
if($return){ |
|
51
|
|
|
return $str; |
|
52
|
|
|
} |
|
53
|
|
|
echo $str; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Generate an mailto anchor link |
|
58
|
|
|
* @param string $link the email address |
|
59
|
|
|
* @param string $anchor the displayed value of the link |
|
60
|
|
|
* @param array $attributes the additional attributes to be added |
|
61
|
|
|
* @param boolean $return whether need return the generated html or just display it directly |
|
62
|
|
|
* |
|
63
|
|
|
* @return string|void the generated html for mailto link if $return is true or display it if not |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function mailto($link, $anchor = null, array $attributes = array(), $return = true){ |
|
66
|
|
|
if(! $anchor){ |
|
67
|
|
|
$anchor = $link; |
|
68
|
|
|
} |
|
69
|
|
|
$str = null; |
|
70
|
|
|
$str .= '<a href = "mailto:'.$link.'"'; |
|
71
|
|
|
$str .= attributes_to_string($attributes); |
|
72
|
|
|
$str .= '>'; |
|
73
|
|
|
$str .= $anchor; |
|
74
|
|
|
$str .= '</a>'; |
|
75
|
|
|
|
|
76
|
|
|
if($return){ |
|
77
|
|
|
return $str; |
|
78
|
|
|
} |
|
79
|
|
|
echo $str; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Generate the html "br" tag |
|
84
|
|
|
* @param integer $nb the number of generated "<br />" tag |
|
85
|
|
|
* @param boolean $return whether need return the generated html or just display it directly |
|
86
|
|
|
* |
|
87
|
|
|
* @return string|void the generated "br" html if $return is true or display it if not |
|
88
|
|
|
*/ |
|
89
|
|
|
public static function br($nb = 1, $return = true){ |
|
90
|
|
|
$nb = (int) $nb; |
|
91
|
|
|
$str = null; |
|
92
|
|
|
for ($i = 1; $i <= $nb; $i++) { |
|
93
|
|
|
$str .= '<br />'; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if($return){ |
|
97
|
|
|
return $str; |
|
98
|
|
|
} |
|
99
|
|
|
echo $str; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Generate the html content for tag "hr" |
|
104
|
|
|
* @param integer $nb the number of generated "<hr />" tag |
|
105
|
|
|
* @param array $attributes the tag attributes |
|
106
|
|
|
* @param boolean $return whether need return the generated html or just display it directly |
|
107
|
|
|
* |
|
108
|
|
|
* @return string|void the generated "hr" html if $return is true or display it if not. |
|
109
|
|
|
*/ |
|
110
|
|
|
public static function hr($nb = 1, array $attributes = array(), $return = true){ |
|
111
|
|
|
$nb = (int) $nb; |
|
112
|
|
|
$str = null; |
|
113
|
|
|
for ($i = 1; $i <= $nb; $i++) { |
|
114
|
|
|
$str .= '<hr' .attributes_to_string($attributes). ' />'; |
|
115
|
|
|
} |
|
116
|
|
|
if($return){ |
|
117
|
|
|
return $str; |
|
118
|
|
|
} |
|
119
|
|
|
echo $str; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Generate the html content for tag like h1, h2, h3, h4, h5 and h6 |
|
124
|
|
|
* @param integer $type the tag type 1 mean h1, 2 h2, etc, |
|
125
|
|
|
* @param string $text the display text |
|
126
|
|
|
* @param integer $nb the number of generated "<h{1,2,3,4,5,6}>" |
|
127
|
|
|
* @param array $attributes the tag attributes |
|
128
|
|
|
* @param boolean $return whether need return the generated html or just display it directly |
|
129
|
|
|
* |
|
130
|
|
|
* @return string|void the generated header html if $return is true or display it if not. |
|
131
|
|
|
*/ |
|
132
|
|
|
public static function head($type = 1, $text = null, $nb = 1, array $attributes = array(), $return = true){ |
|
133
|
|
|
$nb = (int) $nb; |
|
134
|
|
|
$type = (int) $type; |
|
135
|
|
|
$str = null; |
|
136
|
|
|
for ($i = 1; $i <= $nb; $i++) { |
|
137
|
|
|
$str .= '<h' . $type . attributes_to_string($attributes). '>' .$text. '</h' . $type . '>'; |
|
138
|
|
|
} |
|
139
|
|
|
if($return){ |
|
140
|
|
|
return $str; |
|
141
|
|
|
} |
|
142
|
|
|
echo $str; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Generate the html "ul" tag |
|
147
|
|
|
* @param array $data the data to use for each "li" tag |
|
148
|
|
|
* @param array $attributes the "ul" properties attribute use the array index below for each tag: |
|
149
|
|
|
* for ul "ul", for li "li". |
|
150
|
|
|
* @param boolean $return whether need return the generated html or just display it directly |
|
151
|
|
|
* |
|
152
|
|
|
* @return string|void the generated "ul" html if $return is true or display it if not. |
|
153
|
|
|
*/ |
|
154
|
|
|
public static function ul($data = array(), $attributes = array(), $return = true){ |
|
155
|
|
|
if($return){ |
|
156
|
|
|
return self::buildUlOl($data, $attributes, true, 'ul'); |
|
157
|
|
|
} |
|
158
|
|
|
self::buildUlOl($data, $attributes, false, 'ul'); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Generate the html "ol" tag |
|
163
|
|
|
* @param array $data the data to use for each "li" tag |
|
164
|
|
|
* @param array $attributes the "ol" properties attribute use the array index below for each tag: |
|
165
|
|
|
* for ol "ol", for li "li". |
|
166
|
|
|
* @param boolean $return whether need return the generated html or just display it directly |
|
167
|
|
|
* @return string|void the generated "ol" html if $return is true or display it if not. |
|
168
|
|
|
*/ |
|
169
|
|
|
public static function ol($data = array(), $attributes = array(), $return = true){ |
|
170
|
|
|
if($return){ |
|
171
|
|
|
return self::buildUlOl($data, $attributes, true, 'ol'); |
|
172
|
|
|
} |
|
173
|
|
|
self::buildUlOl($data, $attributes, false, 'ol'); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Generate the html "table" tag |
|
179
|
|
|
* @param array $headers the table headers to use between (<thead>) |
|
180
|
|
|
* @param array $body the table body values between (<tbody>) |
|
181
|
|
|
* @param array $attributes the table properties attribute use the array index below for each tag: |
|
182
|
|
|
* for table "table", for thead "thead", for thead tr "thead_tr", |
|
183
|
|
|
* for thead th "thead_th", for tbody "tbody", for tbody tr "tbody_tr", for tbody td "tbody_td", for tfoot "tfoot", |
|
184
|
|
|
* for tfoot tr "tfoot_tr", for tfoot th "tfoot_th". |
|
185
|
|
|
* @param boolean $use_footer whether need to generate table footer (<tfoot>) use the $headers values |
|
186
|
|
|
* @param boolean $return whether need return the generated html or just display it directly |
|
187
|
|
|
* @return string|void the generated "table" html if $return is true or display it if not. |
|
188
|
|
|
*/ |
|
189
|
|
|
public static function table($headers = array(), $body = array(), $attributes = array(), $use_footer = false, $return = true){ |
|
190
|
|
|
$headers = (array) $headers; |
|
191
|
|
|
$body = (array) $body; |
|
192
|
|
|
$str = null; |
|
193
|
|
|
$tableAttributes = ''; |
|
194
|
|
|
if(! empty($attributes['table'])){ |
|
195
|
|
|
$tableAttributes = ' ' . attributes_to_string($attributes['table']); |
|
196
|
|
|
} |
|
197
|
|
|
$str .= '<table' . $tableAttributes . '>'; |
|
198
|
|
|
$str .= self::buildTableHeader($headers, $attributes); |
|
199
|
|
|
$str .= self::buildTableBody($body, $attributes); |
|
200
|
|
|
|
|
201
|
|
|
if($use_footer){ |
|
202
|
|
|
$str .= self::buildTableFooter($headers, $attributes); |
|
203
|
|
|
} |
|
204
|
|
|
$str .= '</table>'; |
|
205
|
|
|
if($return){ |
|
206
|
|
|
return $str; |
|
207
|
|
|
} |
|
208
|
|
|
echo $str; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* This method is used to build the header of the html table |
|
213
|
|
|
* @see Html::table |
|
214
|
|
|
* @return string|null |
|
215
|
|
|
*/ |
|
216
|
|
|
protected static function buildTableHeader(array $headers, $attributes = array()){ |
|
217
|
|
|
$str = null; |
|
218
|
|
|
$theadAttributes = ''; |
|
219
|
|
|
if(! empty($attributes['thead'])){ |
|
220
|
|
|
$theadAttributes = ' ' . attributes_to_string($attributes['thead']); |
|
221
|
|
|
} |
|
222
|
|
|
$theadtrAttributes = ''; |
|
223
|
|
|
if(! empty($attributes['thead_tr'])){ |
|
224
|
|
|
$theadtrAttributes = ' ' . attributes_to_string($attributes['thead_tr']); |
|
225
|
|
|
} |
|
226
|
|
|
$thAttributes = ''; |
|
227
|
|
|
if(! empty($attributes['thead_th'])){ |
|
228
|
|
|
$thAttributes = ' ' . attributes_to_string($attributes['thead_th']); |
|
229
|
|
|
} |
|
230
|
|
|
$str .= '<thead' . $theadAttributes .'>'; |
|
231
|
|
|
$str .= '<tr' . $theadtrAttributes .'>'; |
|
232
|
|
|
foreach ($headers as $value) { |
|
233
|
|
|
$str .= '<th' . $thAttributes .'>' .$value. '</th>'; |
|
234
|
|
|
} |
|
235
|
|
|
$str .= '</tr>'; |
|
236
|
|
|
$str .= '</thead>'; |
|
237
|
|
|
return $str; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* This method is used to build the body of the html table |
|
242
|
|
|
* @see Html::table |
|
243
|
|
|
* @return string|null |
|
244
|
|
|
*/ |
|
245
|
|
|
protected static function buildTableBody(array $body, $attributes = array()){ |
|
246
|
|
|
$str = null; |
|
247
|
|
|
$tbodyAttributes = ''; |
|
248
|
|
|
if(! empty($attributes['tbody'])){ |
|
249
|
|
|
$tbodyAttributes = ' ' . attributes_to_string($attributes['tbody']); |
|
250
|
|
|
} |
|
251
|
|
|
$tbodytrAttributes = ''; |
|
252
|
|
|
if(! empty($attributes['tbody_tr'])){ |
|
253
|
|
|
$tbodytrAttributes = ' ' . attributes_to_string($attributes['tbody_tr']); |
|
254
|
|
|
} |
|
255
|
|
|
$tbodytdAttributes = ''; |
|
256
|
|
|
if(! empty($attributes['tbody_td'])){ |
|
257
|
|
|
$tbodytdAttributes = ' ' . attributes_to_string($attributes['tbody_td']); |
|
258
|
|
|
} |
|
259
|
|
|
$str .= '<tbody' . $tbodyAttributes .'>'; |
|
260
|
|
|
$str .= self::buildTableBodyContent($body, $tbodytrAttributes, $tbodytdAttributes); |
|
261
|
|
|
$str .= '</tbody>'; |
|
262
|
|
|
return $str; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* This method is used to build the body content of the html table |
|
267
|
|
|
* @param array $body the table body data |
|
268
|
|
|
* @param string $tbodytrAttributes the html attributes for each tr in tbody |
|
269
|
|
|
* @param string $tbodytdAttributes the html attributes for each td in tbody |
|
270
|
|
|
* @return string |
|
271
|
|
|
*/ |
|
272
|
|
|
protected static function buildTableBodyContent(array $body, $tbodytrAttributes, $tbodytdAttributes){ |
|
273
|
|
|
$str = null; |
|
274
|
|
|
foreach ($body as $row) { |
|
275
|
|
|
if(is_array($row)){ |
|
276
|
|
|
$str .= '<tr' . $tbodytrAttributes .'>'; |
|
277
|
|
|
foreach ($row as $value) { |
|
278
|
|
|
$str .= '<td' . $tbodytdAttributes .'>' .$value. '</td>'; |
|
279
|
|
|
} |
|
280
|
|
|
$str .= '</tr>'; |
|
281
|
|
|
} |
|
282
|
|
|
} |
|
283
|
|
|
return $str; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* This method is used to build the footer of the html table |
|
288
|
|
|
* @see Html::table |
|
289
|
|
|
* @return string|null |
|
290
|
|
|
*/ |
|
291
|
|
|
protected static function buildTableFooter(array $footers, $attributes = array()){ |
|
292
|
|
|
$str = null; |
|
293
|
|
|
$tfootAttributes = ''; |
|
294
|
|
|
if(! empty($attributes['tfoot'])){ |
|
295
|
|
|
$tfootAttributes = ' ' . attributes_to_string($attributes['tfoot']); |
|
296
|
|
|
} |
|
297
|
|
|
$tfoottrAttributes = ''; |
|
298
|
|
|
if(! empty($attributes['tfoot_tr'])){ |
|
299
|
|
|
$tfoottrAttributes = ' ' . attributes_to_string($attributes['tfoot_tr']); |
|
300
|
|
|
} |
|
301
|
|
|
$thAttributes = ''; |
|
302
|
|
|
if(! empty($attributes['tfoot_th'])){ |
|
303
|
|
|
$thAttributes = ' ' . attributes_to_string($attributes['tfoot_th']); |
|
304
|
|
|
} |
|
305
|
|
|
$str .= '<tfoot' . $tfootAttributes .'>'; |
|
306
|
|
|
$str .= '<tr' . $tfoottrAttributes .'>'; |
|
307
|
|
|
foreach ($footers as $value) { |
|
308
|
|
|
$str .= '<th' . $thAttributes .'>' .$value. '</th>'; |
|
309
|
|
|
} |
|
310
|
|
|
$str .= '</tr>'; |
|
311
|
|
|
$str .= '</tfoot>'; |
|
312
|
|
|
return $str; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* Return the HTML content for ol or ul tags |
|
317
|
|
|
* @see Html::ol |
|
318
|
|
|
* @see Html::ul |
|
319
|
|
|
* @param string $olul the type 'ol' or 'ul' |
|
320
|
|
|
* @return void|string |
|
321
|
|
|
*/ |
|
322
|
|
|
protected static function buildUlOl($data = array(), $attributes = array(), $return = true, $olul = 'ul'){ |
|
323
|
|
|
$data = (array) $data; |
|
324
|
|
|
$str = null; |
|
325
|
|
|
$olulAttributes = ''; |
|
326
|
|
|
if(! empty($attributes[$olul])){ |
|
327
|
|
|
$olulAttributes = ' ' . attributes_to_string($attributes[$olul]); |
|
328
|
|
|
} |
|
329
|
|
|
$liAttributes = ''; |
|
330
|
|
|
if(! empty($attributes['li'])){ |
|
331
|
|
|
$liAttributes = ' ' . attributes_to_string($attributes['li']); |
|
332
|
|
|
} |
|
333
|
|
|
$str .= '<' . $olul . $olulAttributes . '>'; |
|
334
|
|
|
foreach ($data as $row) { |
|
335
|
|
|
$str .= '<li' . $liAttributes .'>' .$row. '</li>'; |
|
336
|
|
|
} |
|
337
|
|
|
$str .= '</' . $olul . '>'; |
|
338
|
|
|
if($return){ |
|
339
|
|
|
return $str; |
|
340
|
|
|
} |
|
341
|
|
|
echo $str; |
|
342
|
|
|
} |
|
343
|
|
|
} |
|
344
|
|
|
|