1 | <?php |
||||
2 | /** |
||||
3 | * This file is part of the O2System Framework package. |
||||
4 | * |
||||
5 | * For the full copyright and license information, please view the LICENSE |
||||
6 | * file that was distributed with this source code. |
||||
7 | * |
||||
8 | * @author Steeve Andrian Salim |
||||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||||
10 | */ |
||||
11 | // ------------------------------------------------------------------------ |
||||
12 | /** |
||||
13 | * HTML Helper |
||||
14 | * |
||||
15 | * A collection of helper function to work with html. |
||||
16 | */ |
||||
17 | // ------------------------------------------------------------------------ |
||||
18 | |||||
19 | if ( ! function_exists('tag')) { |
||||
20 | /** |
||||
21 | * tag |
||||
22 | * |
||||
23 | * Generate html tag with auto closed when the content is set. |
||||
24 | * |
||||
25 | * @param string $tagName |
||||
26 | * @param string|null $contents |
||||
27 | * @param array $attributes |
||||
28 | * |
||||
29 | * @return string |
||||
30 | */ |
||||
31 | function tag($tagName, $contents = null, array $attributes = []) |
||||
32 | { |
||||
33 | if (($tag = substr($tagName, 0)) === '/') { |
||||
34 | $element = new \O2System\Html\Element($tag); |
||||
35 | |||||
36 | return $element->close(); |
||||
37 | } else { |
||||
38 | $element = new \O2System\Html\Element($tagName); |
||||
39 | if (count($attributes)) { |
||||
40 | foreach ($attributes as $name => $value) { |
||||
41 | $element->attributes->addAttribute($name, $value); |
||||
42 | } |
||||
43 | } |
||||
44 | |||||
45 | if (is_array($contents)) { |
||||
0 ignored issues
–
show
introduced
by
![]() |
|||||
46 | foreach ($contents as $content) { |
||||
47 | if ($content instanceof \O2System\Html\Element) { |
||||
48 | $element->childNodes->push($content); |
||||
49 | } elseif (is_string($content) || is_numeric($content)) { |
||||
50 | $element->textContent->push($content); |
||||
51 | } |
||||
52 | } |
||||
53 | } elseif ($contents instanceof \O2System\Html\Element) { |
||||
54 | $element->childNodes->push($contents); |
||||
55 | } elseif (is_string($contents) || is_numeric($contents)) { |
||||
56 | $element->textContent->push($contents); |
||||
57 | } |
||||
58 | |||||
59 | if ( ! is_null($contents)) { |
||||
60 | return $element->render(); |
||||
61 | } else { |
||||
62 | return $element->open(); |
||||
63 | } |
||||
64 | } |
||||
65 | } |
||||
66 | } |
||||
67 | |||||
68 | // ------------------------------------------------------------------------ |
||||
69 | |||||
70 | if ( ! function_exists('video')) { |
||||
71 | /** |
||||
72 | * video |
||||
73 | * |
||||
74 | * Generate video, such as a movie clip or other video streams. |
||||
75 | * |
||||
76 | * @param string $src |
||||
77 | * @param array $attributes |
||||
78 | * |
||||
79 | * @return string |
||||
80 | */ |
||||
81 | function video($src, array $attributes = []) |
||||
82 | { |
||||
83 | $attributes = array_merge([ |
||||
84 | 'width' => 320, |
||||
85 | 'height' => 240, |
||||
86 | 'controls' => 'controls', |
||||
87 | ], $attributes); |
||||
88 | |||||
89 | $video = new \O2System\Html\Element('video'); |
||||
90 | |||||
91 | foreach ($attributes as $name => $value) { |
||||
92 | $video->attributes->addAttribute($name, $value); |
||||
93 | } |
||||
94 | |||||
95 | $ext = strtolower(pathinfo($src, PATHINFO_EXTENSION)); |
||||
96 | |||||
97 | if (in_array($ext, ['mp4', 'ogg', 'webm'])) { |
||||
98 | $source = new \O2System\Html\Element('source'); |
||||
99 | |||||
100 | if (is_file($src)) { |
||||
101 | $src = path_to_url($src); |
||||
102 | } |
||||
103 | |||||
104 | $source->attributes->addAttribute('src', $src); |
||||
105 | $source->attributes->addAttribute('type', 'video/' . $ext); |
||||
106 | $video->childNodes->push($source); |
||||
107 | } |
||||
108 | |||||
109 | $video->textContent->push(language()->getLine('VIDEO_NOT_SUPPORTED')); |
||||
110 | |||||
111 | return $video->render(); |
||||
112 | } |
||||
113 | } |
||||
114 | |||||
115 | // ------------------------------------------------------------------------ |
||||
116 | |||||
117 | if ( ! function_exists('audio')) { |
||||
118 | /** |
||||
119 | * audio |
||||
120 | * |
||||
121 | * Generate audio, such as music or other audio streams. |
||||
122 | * |
||||
123 | * @param string $src |
||||
124 | * @param array $attributes |
||||
125 | * |
||||
126 | * @return string |
||||
127 | */ |
||||
128 | function audio($src, array $attributes = []) |
||||
129 | { |
||||
130 | $attributes = array_merge([ |
||||
131 | 'controls' => 'controls', |
||||
132 | ], $attributes); |
||||
133 | |||||
134 | $audio = new \O2System\Html\Element('audio'); |
||||
135 | |||||
136 | foreach ($attributes as $name => $value) { |
||||
137 | $audio->attributes->addAttribute($name, $value); |
||||
138 | } |
||||
139 | |||||
140 | $ext = strtolower(pathinfo($src, PATHINFO_EXTENSION)); |
||||
141 | |||||
142 | if (in_array($ext, ['mpeg', 'ogg', 'wav'])) { |
||||
143 | $source = new \O2System\Html\Element('source'); |
||||
144 | |||||
145 | if (is_file($src)) { |
||||
146 | $src = path_to_url($src); |
||||
147 | } |
||||
148 | |||||
149 | $source->attributes->addAttribute('src', $src); |
||||
150 | $source->attributes->addAttribute('type', 'audio/' . $ext); |
||||
151 | $audio->childNodes->push($source); |
||||
152 | } |
||||
153 | |||||
154 | $audio->textContent->push(language()->getLine('AUDIO_NOT_SUPPORTED')); |
||||
155 | |||||
156 | return $audio->render(); |
||||
157 | } |
||||
158 | } |
||||
159 | |||||
160 | // ------------------------------------------------------------------------ |
||||
161 | |||||
162 | if ( ! function_exists('canvas')) { |
||||
163 | /** |
||||
164 | * canvas |
||||
165 | * |
||||
166 | * Generate canvas html element. |
||||
167 | * |
||||
168 | * @param array $attributes |
||||
169 | * |
||||
170 | * @return string |
||||
171 | */ |
||||
172 | function canvas(array $attributes = []) |
||||
173 | { |
||||
174 | $canvas = new \O2System\Html\Element('canvas'); |
||||
175 | |||||
176 | if (count($attributes)) { |
||||
177 | foreach ($attributes as $name => $value) { |
||||
178 | $canvas->attributes->addAttribute($name, $value); |
||||
179 | } |
||||
180 | } |
||||
181 | |||||
182 | $canvas->textContent->push(language()->getLine('CANVAS_NOT_SUPPORTED')); |
||||
183 | |||||
184 | return $canvas->render(); |
||||
185 | } |
||||
186 | } |
||||
187 | // ------------------------------------------------------------------------ |
||||
188 | |||||
189 | if ( ! function_exists('heading')) { |
||||
190 | /** |
||||
191 | * heading |
||||
192 | * |
||||
193 | * Generates html heading tag. |
||||
194 | * |
||||
195 | * @param string $textContent |
||||
196 | * @param int $level |
||||
197 | * @param array $attributes |
||||
198 | * |
||||
199 | * @return string |
||||
200 | */ |
||||
201 | function heading($textContent = '', $level = 1, array $attributes = []) |
||||
202 | { |
||||
203 | return (new \O2System\Framework\Libraries\Ui\Contents\Heading($textContent, $level, $attributes))->render(); |
||||
204 | } |
||||
205 | } |
||||
206 | |||||
207 | // ------------------------------------------------------------------------ |
||||
208 | |||||
209 | if ( ! function_exists('ul')) { |
||||
210 | /** |
||||
211 | * ul |
||||
212 | * |
||||
213 | * Generates an HTML unordered list from an single or multi-dimensional array. |
||||
214 | * |
||||
215 | * @param array $list |
||||
216 | * @param array $attributes |
||||
217 | * |
||||
218 | * @return string |
||||
219 | */ |
||||
220 | function ul($list, array $attributes = []) |
||||
221 | { |
||||
222 | return (new \O2System\Framework\Libraries\Ui\Contents\Lists\Unordered($attributes))->createLists($list)->render(); |
||||
223 | } |
||||
224 | } |
||||
225 | |||||
226 | // ------------------------------------------------------------------------ |
||||
227 | |||||
228 | if ( ! function_exists('ol')) { |
||||
229 | /** |
||||
230 | * ol |
||||
231 | * |
||||
232 | * Generates an HTML ordered list from an single or multi-dimensional array. |
||||
233 | * |
||||
234 | * @param array $list |
||||
235 | * @param array $attributes |
||||
236 | * |
||||
237 | * @return string |
||||
238 | */ |
||||
239 | function ol($list, array $attributes = []) |
||||
240 | { |
||||
241 | return (new \O2System\Framework\Libraries\Ui\Contents\Lists\Ordered($attributes))->createLists($list)->render(); |
||||
242 | } |
||||
243 | } |
||||
244 | |||||
245 | // ------------------------------------------------------------------------ |
||||
246 | |||||
247 | if ( ! function_exists('img')) { |
||||
248 | /** |
||||
249 | * img |
||||
250 | * |
||||
251 | * @param string $src |
||||
252 | * @param array $attributes |
||||
253 | * |
||||
254 | * @return string |
||||
255 | */ |
||||
256 | function img($src = '', $alt, array $attributes = []) |
||||
257 | { |
||||
258 | $img = new \O2System\Framework\Libraries\Ui\Contents\Image($src, $alt); |
||||
259 | |||||
260 | if (count($attributes)) { |
||||
261 | foreach ($attributes as $name => $value) { |
||||
262 | $img->attributes->addAttribute($name, $value); |
||||
263 | } |
||||
264 | } |
||||
265 | } |
||||
266 | } |
||||
267 | |||||
268 | // ------------------------------------------------------------------------ |
||||
269 | |||||
270 | if ( ! function_exists('meta')) { |
||||
271 | /** |
||||
272 | * meta |
||||
273 | * |
||||
274 | * Generates meta tags from an array of key/values |
||||
275 | * |
||||
276 | * @param $meta string|array |
||||
277 | * @param $content string|null |
||||
278 | * @param $type string |
||||
279 | * |
||||
280 | * @return string |
||||
281 | */ |
||||
282 | function meta($meta = '', $content = '', $type = 'name') |
||||
0 ignored issues
–
show
The parameter
$type is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
283 | { |
||||
284 | // Since we allow the data to be passes as a string, a simple array |
||||
285 | // or a multidimensional one, we need to do a little prepping. |
||||
286 | if ( ! is_array($meta)) { |
||||
287 | $meta = [['name' => $meta, 'content' => $content, 'type']]; |
||||
288 | } elseif (isset($meta[ 'name' ])) { |
||||
289 | // Turn single array into multidimensional |
||||
290 | $meta = [$meta]; |
||||
291 | } |
||||
292 | |||||
293 | $output = []; |
||||
294 | |||||
295 | foreach ($meta as $attributes) { |
||||
296 | $element = new \O2System\Html\Element('meta'); |
||||
297 | $element->attributes->addAttribute('type', |
||||
298 | (isset($attributes[ 'type' ]) && $attributes[ 'type' ] !== 'name') ? 'http-equiv' : 'name'); |
||||
299 | $element->attributes->addAttribute('name', |
||||
300 | isset($attributes[ 'content' ]) ? $attributes[ 'content' ] : ''); |
||||
301 | $element->attributes->addAttribute('name', |
||||
302 | isset($attributes[ 'content' ]) ? $attributes[ 'content' ] : ''); |
||||
303 | |||||
304 | if (count($attributes)) { |
||||
305 | foreach ($attributes as $meta => $value) { |
||||
306 | $element->attributes->addAttribute($meta, $value); |
||||
307 | } |
||||
308 | } |
||||
309 | |||||
310 | $output[] = $element; |
||||
311 | } |
||||
312 | |||||
313 | return implode(PHP_EOL, $output); |
||||
314 | } |
||||
315 | } |
||||
316 | |||||
317 | // ------------------------------------------------------------------------ |
||||
318 | |||||
319 | if ( ! function_exists('parse_attributes')) { |
||||
320 | /** |
||||
321 | * parse_attributes |
||||
322 | * |
||||
323 | * Parse attributes from html tag string. |
||||
324 | * |
||||
325 | * @param $string |
||||
326 | * |
||||
327 | * @return array |
||||
328 | */ |
||||
329 | function parse_attributes($string) |
||||
330 | { |
||||
331 | $attributes = []; |
||||
332 | |||||
333 | if (is_string($string)) { |
||||
334 | if (is_html($string)) { |
||||
335 | $xml = simplexml_load_string(str_replace('>', '/>', $string)); |
||||
336 | } else { |
||||
337 | $xml = simplexml_load_string('<tag ' . $string . '/>'); |
||||
338 | } |
||||
339 | |||||
340 | foreach ($xml->attributes() as $key => $node) { |
||||
341 | $attributes[ $key ] = (string)$node; |
||||
342 | } |
||||
343 | } |
||||
344 | |||||
345 | return $attributes; |
||||
346 | } |
||||
347 | } |
||||
348 | |||||
349 | // ------------------------------------------------------------------------ |
||||
350 | |||||
351 | if ( ! function_exists('remove_tags')) { |
||||
352 | /** |
||||
353 | * remove_tags |
||||
354 | * |
||||
355 | * Remove the tags but keep the content. |
||||
356 | * Note this function always assumed no two tags start the same way (e.g. <tag> and <tags>) |
||||
357 | * |
||||
358 | * @param string $html HTML Source Code |
||||
359 | * @param string|array $tags Single HTML Tag | List of HTML Tag |
||||
360 | * @param bool $strip_content Whether to display the content of inside tag or erase it |
||||
361 | * |
||||
362 | * @return string |
||||
363 | */ |
||||
364 | function remove_tags($html, $tags, $strip_content = false) |
||||
365 | { |
||||
366 | $content = ''; |
||||
367 | if ( ! is_array($tags)) { |
||||
368 | $tags = (strpos($html, '>') !== false ? explode('>', str_replace('<', '', $tags)) : [$tags]); |
||||
369 | if (end($tags) == '') { |
||||
370 | array_pop($tags); |
||||
371 | } |
||||
372 | } |
||||
373 | foreach ($tags as $tag) { |
||||
374 | if ($strip_content) { |
||||
375 | $content = '(.+</' . $tag . '[^>]*>|)'; |
||||
376 | } |
||||
377 | |||||
378 | $html = preg_replace('#</?' . $tag . '[^>]*>' . $content . '#is', '', $html); |
||||
379 | } |
||||
380 | |||||
381 | return $html; |
||||
382 | } |
||||
383 | } |
||||
384 | |||||
385 | // ------------------------------------------------------------------------ |
||||
386 | |||||
387 | if ( ! function_exists('extract_tag')) { |
||||
388 | /** |
||||
389 | * extract_tag |
||||
390 | * |
||||
391 | * Extract content inside tag. |
||||
392 | * |
||||
393 | * @param string $html HTML Source Code |
||||
394 | * @param string $tag HTML Tag |
||||
395 | * |
||||
396 | * @return string |
||||
397 | */ |
||||
398 | function extract_tag($html, $tag = 'div') |
||||
399 | { |
||||
400 | $html = preg_match_all("/(\<" . $tag . ")(.*?)(" . $tag . ">)/si", $html, $matches); |
||||
0 ignored issues
–
show
|
|||||
401 | |||||
402 | $result = ''; |
||||
403 | foreach ($matches[ 0 ] as $item) { |
||||
404 | $result = preg_replace("/\<[\/]?" . $tag . "\>/", '', $item); |
||||
405 | } |
||||
406 | |||||
407 | return $result; |
||||
408 | } |
||||
409 | } |