forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_helper.php
More file actions
executable file
·845 lines (757 loc) · 17.4 KB
/
Copy pathhtml_helper.php
File metadata and controls
executable file
·845 lines (757 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
<?php
/**
* CodeIgniter
*
* An open source application development framework for PHP
*
* This content is released under the MIT License (MIT)
*
* Copyright (c) 2014 - 2016, British Columbia Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @package CodeIgniter
* @author CodeIgniter Dev Team
* @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
* @license http://opensource.org/licenses/MIT MIT License
* @link http://codeigniter.com
* @since Version 3.0.0
* @filesource
*/
// --------------------------------------------------------------------
/**
* CodeIgniter HTML Helper
*
* @package CodeIgniter
* @subpackage Helpers
* @category Helpers
* @author CodeIgniter Dev Team
* @link http://codeigniter.com/user_guide/helpers/html_helper.html
*/
if ( ! function_exists('ul'))
{
/**
* Unordered List
*
* Generates an HTML unordered list from an single or
* multi-dimensional array.
*
* @param array $list
* @param string $attributes HTML attributes
* @return string
*/
function ul(array $list, string $attributes = ''): string
{
return _list('ul', $list, $attributes);
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('ol'))
{
/**
* Ordered List
*
* Generates an HTML ordered list from an single or multi-dimensional array.
*
* @param array $list
* @param string $attributes HTML attributes
* @return string
*/
function ol(array $list, string $attributes = ''): string
{
return _list('ol', $list, $attributes);
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('_list'))
{
/**
* Generates the list
*
* Generates an HTML ordered list from an single or multi-dimensional array.
*
* @param string $type
* @param array $list
* @param string $attributes
* @param int $depth
* @return string
*/
function _list
(
string $type = 'ul',
array $list = [],
string $attributes = '',
int $depth = 0
): string
{
// If an array wasn't submitted there's nothing to do...
if ( ! is_array($list))
{
return $list;
}
// Set the indentation based on the depth
$out = str_repeat(' ', $depth)
// Write the opening list tag
. '<' . $type . stringify_attributes($attributes) . ">\n";
// Cycle through the list elements. If an array is
// encountered we will recursively call _list()
static $_last_list_item = '';
foreach ($list as $key => $val)
{
$_last_list_item = $key;
$out .= str_repeat(' ', $depth + 2) . '<li>';
if ( ! is_array($val))
{
$out .= $val;
}
else
{
$out .= $_last_list_item
. "\n"
. _list($type, $val, '', $depth + 4)
. str_repeat(' ', $depth + 2);
}
$out .= "</li>\n";
}
// Set the indentation for the closing tag and apply it
return $out . str_repeat(' ', $depth) . '</' . $type . ">\n";
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('img'))
{
/**
* Image
*
* Generates an image element
*
* @param mixed $src
* @param bool $indexPage
* @param string $attributes
* @return string
*/
function img
(
$src = '',
bool $indexPage = false,
string $attributes = ''
): string
{
if ( ! is_array($src) )
{
$src = ['src' => $src];
}
//If there is no alt attribute defined, set it to an empty string.
if ( ! isset($src['alt']))
{
$src['alt'] = '';
}
$img = '<img';
foreach ($src as $k => $v)
{
//Include a protocol if nothing is explicitely defined.
if ($k === 'src' && ! preg_match('#^([a-z]+:)?//#i', $v))
{
if ($indexPage === true)
{
$img .= ' src="' . site_url($v) . '"';
}
else
{
$img .= ' src="' . slash_item('baseURL') . $v . '"';
}
}
else
{
$img .= ' ' . $k . '="' . $v . '"';
}
}
return $img . stringify_attributes($attributes) . ' />';
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('doctype'))
{
/**
* Doctype
*
* Generates a page document type declaration
*
* Examples of valid options: html5, xhtml-11, xhtml-strict, xhtml-trans,
* xhtml-frame, html4-strict, html4-trans, and html4-frame.
* All values are saved in the doctypes config file.
*
* @param mixed $type The doctype to be generated
* @return string
*/
function doctype($type = 'html5'): string
{
$doctypes = null;
$env = ENVIRONMENT;
$doctypes = Config\DocTypes::$list;
$customDocTypesPath = APPPATH . "Config/{$env}/DocTypes.php";
if (file_exists($customDocTypesPath))
{
$customDocTypesNs = "Config\{$env}\DocTypes";
$doctypes = $customDocTypesNs::$list;
}
return isset($doctypes[$type]) ? $doctypes[$type] : false;
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('script_tag'))
{
/**
* Script
*
* Generates link to a JS file
*
* @param mixed $src Script source or an array
* @param bool $indexPage Should indexPage be added to the JS path
* @return string
*/
function script_tag
(
$src = '',
bool $indexPage = false
): string
{
$script = '<script ';
if (is_array($src))
{
foreach ($src as $k => $v)
{
if ($k === 'src' && ! preg_match('#^([a-z]+:)?//#i', $v))
{
if ($indexPage === true)
{
$script .= 'src="' . site_url($v) . '" ';
}
else
{
$script .= 'src="' . slash_item('baseURL') . $v . '" ';
}
}
else
{
$script .= $k . '="' . $v . '" ';
}
}
}
else
{
if (preg_match('#^([a-z]+:)?//#i', $src))
{
$script .= 'src="' . $src . '" ';
}
elseif ($indexPage === true)
{
$script .= 'src="' . site_url($src) . '" ';
}
else
{
$script .= 'src="' . slash_item('baseURL') . $src . '" ';
}
}
return $script . 'type="text/javascript"' . "></script>";
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('link_tag'))
{
/**
* Link
*
* Generates link to a CSS file
*
* @param mixed $href Stylesheet hrefs or an array
* @param string $rel
* @param string $type
* @param string $title
* @param string $media
* @param bool $indexPage should indexPage be added to the CSS path.
* @return string
*/
function link_tag
(
$href = '',
string $rel = 'stylesheet',
string $type = 'text/css',
string $title = '',
string $media = '',
bool $indexPage = false
): string
{
$link = '<link ';
if (is_array($href))
{
foreach ($href as $k => $v)
{
if ($k === 'href' && ! preg_match('#^([a-z]+:)?//#i', $v))
{
if ($indexPage === true)
{
$link .= 'href="' . site_url($v) . '" ';
}
else
{
$link .= 'href="' . slash_item('baseURL') . $v . '" ';
}
}
else
{
$link .= $k . '="' . $v . '" ';
}
}
}
else
{
if (preg_match('#^([a-z]+:)?//#i', $href))
{
$link .= 'href="' . $href . '" ';
}
elseif ($indexPage === true)
{
$link .= 'href="' . site_url($href) . '" ';
}
else
{
$link .= 'href="' . slash_item('baseURL') . $href . '" ';
}
$link .= 'rel="' . $rel . '" type="' . $type . '" ';
if ($media !== '')
{
$link .= 'media="' . $media . '" ';
}
if ($title !== '')
{
$link .= 'title="' . $title . '" ';
}
}
return $link . "/>";
}
// ------------------------------------------------------------------------
if ( ! function_exists('video'))
{
/**
* Video
*
* Geneartes a video element to embed videos. The video element can
* contain one or more video sources
*
* @param mixed $src Either a source string or
* an array of sources
* @param string $unsupportedMessage The message to display
* if the media tag is not supported by the browser
* @param string $attributes HTML attributes
* @param array $tracks
* @param bool $indexPage
* @return string
*
*/
function video
(
$src,
string $unsupportedMessage = '',
string $attributes = '',
array $tracks = [],
bool $indexPage = false
): string
{
if(is_array($src))
{
return _media
(
'video',
$src,
$unsupportedMessage,
$attributes,
$tracks
);
}
else
{
$video = '<video';
if(_has_protocol($src))
{
$video .= ' src="' . $src . '"';
}
elseif ($indexPage === true)
{
$video .= ' src="' . site_url($src) . '"';
}
else
{
$video .= ' src="' . slash_item('baseURL') . $src . '"';
}
if ($attributes !== '')
{
$video .= ' ' . $attributes;
}
$video .= ">\n";
if( ! empty($tracks))
{
foreach ($tracks as $track)
{
$video .= _space_indent() . $track . "\n";
}
}
if( ! empty($unsupportedMessage))
{
$video .= _space_indent()
. $unsupportedMessage
. "\n";
}
$video .= "</video>\n";
return $video;
}
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('audio'))
{
/**
* Audio
*
* Generates an audio element to embed sounds
*
* @param mixed $src Either a source string or
* an array of sources
* @param string $unsupportedMessage The message to display
* if the media tag is not supported by the browser.
* @param string $attributes HTML attributes
* @return string
*
*/
function audio
(
$src,
string $unsupportedMessage = '',
string $attributes = '',
array $tracks = [],
bool $indexPage = false
): string
{
if(is_array($src))
{
return _media
(
'audio',
$src,
$unsupportedMessage,
$attributes,
$tracks
);
}
else
{
$audio = '<audio';
if(_has_protocol($src))
{
$audio .= ' src="' . $src . '"';
}
elseif ($indexPage === true)
{
$audio .= ' src="' . site_url($src) . '"';
}
else
{
$audio .= ' src="' . slash_item('baseURL') . $src . '"';
}
if ($attributes !== '')
{
$audio .= ' ' . $attributes;
}
$audio .= '>';
if( ! empty($tracks))
{
foreach ($tracks as $track)
{
$audio .= $track;
}
}
if( ! empty($unsupportedMessage))
{
$video .= _space_indent()
. $unsupportedMessage
. "\n";
}
$audio .= "</audio>\n";
return $audio;
}
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('_media'))
{
function _media
(
string $name,
array $types,
string $unsupportedMessage = '',
string $attributes = '',
array $tracks = []
): string
{
$media = '<' . $name;
if(empty($attributes))
{
$media .= '>';
}
else
{
$media .= ' ' . $attributes . '>';
}
$media .= "\n";
foreach($types as $option)
{
$media .= _space_indent() .$option . "\n";
}
if(empty($tracks))
{
$media .= "/>";
}
else
{
foreach ($tracks as $track)
{
$media .= _space_indent() . $track . "\n";
}
}
if(! empty($unsupportedMessage))
{
$media .= _space_indent() . $unsupportedMessage . "\n";
}
$media .= '</' . $name . ">\n";
return $media;
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('source'))
{
/**
* Source
*
* Generates a source element that specifies multiple media resources
* for either audio or video element
*
* @param string $src The path of the media resource
* @param string $type The MIME-type of the resource with
* optional codecs parameters
* @param string $attributes HTML attributes
* @param bool $indexPage
* @return string
*/
function source
(
string $src,
string $type,
string $attributes = '',
bool $indexPage = false
): string
{
if(_has_protocol($src))
{
//Do nothing.
}
elseif ($indexPage === true)
{
$src = site_url($src);
}
else
{
$src = slash_item('baseURL') . $src;
}
$source = '<source src="' . $src
. '" type="' . $type . '"';
if( ! empty($attributes))
{
$source .= ' ' . $attributes;
}
$source .= ' />';
return $source;
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('track'))
{
/**
* Track
*
* Generates a track element to specify timed tracks. The tracks are
* formatted in WebVTT format.
*
* @param string $src The path of the .VTT file
* @param string $kind
* @param string $srcLanguage
* @param string $label
* @return string
*/
function track
(
string $src,
string $kind,
string $srcLanguage,
string $label
): string
{
return '<track src="' . $src
. '" kind="' . $kind
. '" srclang="' . $srcLanguage
. '" label="' . $label
. '" />';
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('object'))
{
/**
* Object
*
* Generates an object element that represents the media
* as either image or a resource plugin such as audio, video,
* Java applets, ActiveX, PDF and Flash
*
* @param string $data A resource URL
* @param string $type Content-type of the resource
* @param string $attributes HTML attributes
* @param array $params
* @return string
*/
function object
(
string $data,
string $type,
string $attributes = '',
array $params = [],
bool $indexPage = false
): string
{
if(_has_protocol($data))
{
//Do nothing.
}
elseif ($indexPage === true)
{
$data = site_url($data);
}
else
{
$data = slash_item('baseURL') . $data;
}
$object = '<object data="' . $data . '" '
. $attributes . '>';
if( ! empty($params))
{
$object .= "\n";
}
foreach ($params as $param)
{
$object .= _space_indent() . $param . "\n";
}
$object .= "</object>\n";
return $object;
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('param'))
{
/**
* Param
*
* Generates a param element that defines parameters
* for the object element.
*
* @param string $name The name of the parameter
* @param string $value The value of the parameter
* @param string $type The MIME-type
* @param string $attributes HTML attributes
* @return string
*/
function param
(
string $name,
string $value,
string $type = 'ref',
string $attributes = ''
): string
{
return '<param name="' . $name
. '" type="' . $type
. '" value="' . $value
. '" ' . $attributes . ' />';
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('embed'))
{
/**
* Embed
*
* Generates an embed element
*
* @param string $src The path of the resource to embed
* @param string $type MIME-type
* @param string $attributes HTML attributes
* @param bool $indexPage
* @return string
*/
function embed
(
string $src,
string $type,
string $attributes = '',
bool $indexPage = false
): string
{
if(_has_protocol($src))
{
//Do nothing.
}
elseif ($indexPage === true)
{
$src = site_url($src);
}
else
{
$src = slash_item('baseURL') . $src;
}
return '<embed src="' . $src
. '" type="' . $type . '" '
. $attributes . " />\n";
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('_has_protocol'))
{
function _has_protocol($url)
{
return preg_match('#^([a-z]+:)?//#i', $url);
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('_space_indent'))
{
function _space_indent($depth = 2)
{
return str_repeat(' ', $depth);
}
}
}
// ------------------------------------------------------------------------