Passed
Push — 1.0.0-dev ( 8edc19...2b6704 )
by nguereza
03:32
created

Html::hr()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 10
rs 10
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
			if($type <= 0 || $type > 6){
136
				$type = 1;
137
			}
138
			$str = null;
139
			for ($i = 1; $i <= $nb; $i++) {
140
				$str .= '<h' . $type . attributes_to_string($attributes). '>' .$text. '</h' . $type . '>';
141
			}
142
			if($return){
143
				return $str;
144
			}
145
			echo $str;
146
		}
147
148
		/**
149
		 * Generate the html "ul" tag
150
		 * @param  array   $data the data to use for each "li" tag
151
		 * @param  array   $attributes   the "ul" properties attribute use the array index below for each tag:
152
		 *  for ul "ul", for li "li".
153
		 * @param  boolean $return whether need return the generated html or just display it directly
154
		 *
155
		 * @return string|void the generated "ul" html  if $return is true or display it if not.
156
		 */
157
		public static function ul($data = array(), $attributes = array(), $return = true){
158
			$data = (array) $data;
159
			$str = null;
160
			$ulAttributes = '';
161
			if(! empty($attributes['ul'])){
162
				$ulAttributes = ' ' . attributes_to_string($attributes['ul']);
163
			}
164
			$liAttributes = '';
165
			if(! empty($attributes['li'])){
166
				$liAttributes = ' ' . attributes_to_string($attributes['li']);
167
			}
168
			$str .= '<ul' . $ulAttributes . '>';
169
			foreach ($data as $row) {
170
				$str .= '<li' . $liAttributes .'>' .$row. '</li>';
171
			}
172
			$str .= '</ul>';
173
			if($return){
174
				return $str;
175
			}
176
			echo $str;
177
		}
178
179
		/**
180
		 * Generate the html "ol" tag
181
		 * @param  array   $data the data to use for each "li" tag
182
		 * @param  array   $attributes   the "ol" properties attribute use the array index below for each tag:
183
		 *  for ol "ol", for li "li".
184
		 * @param  boolean $return whether need return the generated html or just display it directly
185
		 * @return string|void the generated "ol" html  if $return is true or display it if not.
186
		 */
187
		public static function ol($data = array(), $attributes = array(), $return = true){
188
			$data = (array) $data;
189
			$str = null;
190
			$olAttributes = '';
191
			if(! empty($attributes['ol'])){
192
				$olAttributes = ' ' . attributes_to_string($attributes['ol']);
193
			}
194
			$liAttributes = '';
195
			if(! empty($attributes['li'])){
196
				$liAttributes = ' ' . attributes_to_string($attributes['li']);
197
			}
198
			$str .= '<ol' . $olAttributes . '>';
199
			foreach ($data as $row) {
200
				$str .= '<li' . $liAttributes .'>' .$row. '</li>';
201
			}
202
			$str .= '</ol>';
203
			if($return){
204
				return $str;
205
			}
206
			echo $str;
207
		}
208
209
		/**
210
		 * Generate the html "table" tag
211
		 * @param  array   $headers            the table headers to use between (<thead>)
212
		 * @param  array   $body the table body values between (<tbody>)
213
		 * @param  array   $attributes   the table properties attribute use the array index below for each tag:
214
		 *  for table "table", for thead "thead", for thead tr "thead_tr",
215
		 *  for thead th "thead_th", for tbody "tbody", for tbody tr "tbody_tr", for tbody td "tbody_td", for tfoot "tfoot",
216
		 *  for tfoot tr "tfoot_tr", for tfoot th "tfoot_th".
217
		 * @param boolean $use_footer whether need to generate table footer (<tfoot>) use the $headers values
218
		 * @param  boolean $return whether need return the generated html or just display it directly
219
		 * @return string|void the generated "table" html  if $return is true or display it if not.
220
		 */
221
		public static function table($headers = array(), $body = array(), $attributes = array(), $use_footer = false, $return = true){
222
			$headers = (array) $headers;
223
			$body = (array) $body;
224
			$str = null;
225
			$tableAttributes = '';
226
			if(! empty($attributes['table'])){
227
				$tableAttributes = ' ' . attributes_to_string($attributes['table']);
228
			}
229
			$str .= '<table' . $tableAttributes . '>';
230
			$str .= self::buildTableHeader($headers, $attributes);
231
			$str .= self::buildTableBody($body, $attributes);
232
233
			if($use_footer){
234
				$str .= self::buildTableFooter($headers, $attributes);
235
			}
236
			$str .= '</table>';
237
			if($return){
238
				return $str;
239
			}
240
			echo $str;
241
		}
242
243
		/**
244
		 * This method is used to build the header of the html table
245
		 * @see  Html::table 
246
		 * @return string|null
247
		 */
248
		protected static function buildTableHeader(array $headers, $attributes = array()){
249
			$str = null;
250
			if(! empty($headers)){
251
				$theadAttributes = '';
252
				if(! empty($attributes['thead'])){
253
					$theadAttributes = ' ' . attributes_to_string($attributes['thead']);
254
				}
255
				$theadtrAttributes = '';
256
				if(! empty($attributes['thead_tr'])){
257
					$theadtrAttributes = ' ' . attributes_to_string($attributes['thead_tr']);
258
				}
259
				$thAttributes = '';
260
				if(! empty($attributes['thead_th'])){
261
					$thAttributes = ' ' . attributes_to_string($attributes['thead_th']);
262
				}
263
				$str .= '<thead' . $theadAttributes .'>';
264
				$str .= '<tr' . $theadtrAttributes .'>';
265
				foreach ($headers as $value) {
266
					$str .= '<th' . $thAttributes .'>' .$value. '</th>';
267
				}
268
				$str .= '</tr>';
269
				$str .= '</thead>';
270
			}
271
			return $str;
272
		}
273
274
		/**
275
		 * This method is used to build the body of the html table
276
		 * @see  Html::table 
277
		 * @return string|null
278
		 */
279
		protected static function buildTableBody(array $body, $attributes = array()){
280
			$str = null;
281
			$tbodyAttributes = '';
282
			if(! empty($attributes['tbody'])){
283
				$tbodyAttributes = ' ' . attributes_to_string($attributes['tbody']);
284
			}
285
			$tbodytrAttributes = '';
286
			if(! empty($attributes['tbody_tr'])){
287
				$tbodytrAttributes = ' ' . attributes_to_string($attributes['tbody_tr']);
288
			}
289
			$tbodytdAttributes = '';
290
			if(! empty($attributes['tbody_td'])){
291
				$tbodytdAttributes = ' ' . attributes_to_string($attributes['tbody_td']);
292
			}
293
			$str .= '<tbody' . $tbodyAttributes .'>';
294
			foreach ($body as $row) {
295
				if(is_array($row)){
296
					$str .= '<tr' . $tbodytrAttributes .'>';
297
					foreach ($row as $value) {
298
						$str .= '<td' . $tbodytdAttributes .'>' .$value. '</td>';	
299
					}
300
					$str .= '</tr>';
301
				}
302
			}
303
			$str .= '</tbody>';
304
			return $str;
305
		}
306
307
		/**
308
		 * This method is used to build the footer of the html table
309
		 * @see  Html::table 
310
		 * @return string|null
311
		 */
312
		protected static function buildTableFooter(array $footers, $attributes = array()){
313
			$str = null;
314
			$tfootAttributes = '';
315
			if(! empty($attributes['tfoot'])){
316
				$tfootAttributes = ' ' . attributes_to_string($attributes['tfoot']);
317
			}
318
			$tfoottrAttributes = '';
319
			if(! empty($attributes['tfoot_tr'])){
320
				$tfoottrAttributes = ' ' . attributes_to_string($attributes['tfoot_tr']);
321
			}
322
			$thAttributes = '';
323
			if(! empty($attributes['tfoot_th'])){
324
				$thAttributes = ' ' . attributes_to_string($attributes['tfoot_th']);
325
			}
326
			$str .= '<tfoot' . $tfootAttributes .'>';
327
				$str .= '<tr' . $tfoottrAttributes .'>';
328
				foreach ($footers as $value) {
329
					$str .= '<th' . $thAttributes .'>' .$value. '</th>';
330
				}
331
				$str .= '</tr>';
332
				$str .= '</tfoot>';
333
			return $str;
334
		}
335
	}
336