-
Notifications
You must be signed in to change notification settings - Fork 481
/
display.lib.php
2695 lines (2439 loc) · 88.5 KB
/
display.lib.php
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
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* For licensing terms, see /license.txt */
use Chamilo\CoreBundle\Component\Utils\ActionIcon;
use Chamilo\CoreBundle\Component\Utils\ObjectIcon;
use Chamilo\CoreBundle\Component\Utils\StateIcon;
use Chamilo\CoreBundle\Component\Utils\ToolIcon;
use Chamilo\CoreBundle\Entity\ExtraField;
use Chamilo\CoreBundle\Entity\ExtraFieldValues;
use Chamilo\CoreBundle\Framework\Container;
use Chamilo\CoreBundle\ServiceHelper\ThemeHelper;
use ChamiloSession as Session;
use Symfony\Component\HttpFoundation\Response;
/**
* Class Display
* Contains several public functions dealing with the display of
* table data, messages, help topics, ...
*
* Include/require it in your code to use its public functionality.
* There are also several display public functions in the main api library.
*
* All public functions static public functions inside a class called Display,
* so you use them like this: e.g.
* Display::return_message($message)
*/
class Display
{
/** @var Template */
public static $global_template;
public static $preview_style = null;
public static $legacyTemplate;
public function __construct()
{
}
/**
* @return array
*/
public static function toolList()
{
return [
'group',
'work',
'glossary',
'forum',
'course_description',
'gradebook',
'attendance',
'course_progress',
'notebook',
];
}
/**
* Displays the page header.
*
* @param string The name of the page (will be showed in the page title)
* @param string Optional help file name
* @param string $page_header
*/
public static function display_header(
$tool_name = '',
$help = null,
$page_header = null
) {
global $interbreadcrumb;
$interbreadcrumb[] = ['url' => '#', 'name' => $tool_name];
ob_start();
return true;
}
/**
* Displays the reduced page header (without banner).
*/
public static function display_reduced_header()
{
ob_start();
self::$legacyTemplate = '@ChamiloCore/Layout/no_layout.html.twig';
return true;
}
/**
* Display no header.
*/
public static function display_no_header()
{
global $tool_name, $show_learnpath;
self::$global_template = new Template(
$tool_name,
false,
false,
$show_learnpath
);
}
/**
* Display the page footer.
*/
public static function display_footer()
{
$contents = ob_get_contents();
if (ob_get_length()) {
ob_end_clean();
}
$tpl = '@ChamiloCore/Layout/layout_one_col.html.twig';
if (!empty(self::$legacyTemplate)) {
$tpl = self::$legacyTemplate;
}
$response = new Response();
$params['content'] = $contents;
global $interbreadcrumb, $htmlHeadXtra;
$courseInfo = api_get_course_info();
if (!empty($courseInfo)) {
$url = $courseInfo['course_public_url'];
$sessionId = api_get_session_id();
if (!empty($sessionId)) {
$url .= '?sid='.$sessionId;
}
if (!empty($interbreadcrumb)) {
array_unshift(
$interbreadcrumb,
['name' => $courseInfo['title'], 'url' => $url]
);
}
}
if (empty($interbreadcrumb)) {
$interbreadcrumb = [];
} else {
$interbreadcrumb = array_filter(
$interbreadcrumb,
function ($item) {
return isset($item['name']) && !empty($item['name']);
}
);
}
$params['legacy_javascript'] = $htmlHeadXtra;
$params['legacy_breadcrumb'] = json_encode(array_values($interbreadcrumb));
Template::setVueParams($params);
$content = Container::getTwig()->render($tpl, $params);
$response->setContent($content);
$response->send();
exit;
}
/**
* Display the page footer.
*/
public static function display_reduced_footer()
{
return self::display_footer();
}
/**
* Displays the tool introduction of a tool.
*
* @author Patrick Cool <[email protected]>, Ghent University
*
* @param string $tool these are the constants that are used for indicating the tools
* @param array $editor_config Optional configuration settings for the online editor.
* return: $tool return a string array list with the "define" in main_api.lib
*
* @return string html code for adding an introduction
*/
public static function display_introduction_section(
$tool,
$editor_config = null
) {
// @todo replace introduction section with a vue page.
return;
}
/**
* @param string $tool
* @param array $editor_config
*/
public static function return_introduction_section(
$tool,
$editor_config = null
) {
}
/**
* Displays a table.
*
* @param array $header Titles for the table header
* each item in this array can contain 3 values
* - 1st element: the column title
* - 2nd element: true or false (column sortable?)
* - 3th element: additional attributes for
* th-tag (eg for column-width)
* - 4the element: additional attributes for the td-tags
* @param array $content 2D-array with the tables content
* @param array $sorting_options Keys are:
* 'column' = The column to use as sort-key
* 'direction' = SORT_ASC or SORT_DESC
* @param array $paging_options Keys are:
* 'per_page_default' = items per page when switching from
* full- list to per-page-view
* 'per_page' = number of items to show per page
* 'page_nr' = The page to display
* @param array $query_vars Additional variables to add in the query-string
* @param array $form_actions
* @param string $style The style that the table will show. You can set 'table' or 'grid'
* @param string $tableName
* @param string $tableId
*
* @author [email protected]
*/
public static function display_sortable_table(
$header,
$content,
$sorting_options = [],
$paging_options = [],
$query_vars = null,
$form_actions = [],
$style = 'table',
$tableName = 'tablename',
$tableId = ''
) {
$column = $sorting_options['column'] ?? 0;
$default_items_per_page = $paging_options['per_page'] ?? 20;
$table = new SortableTableFromArray($content, $column, $default_items_per_page, $tableName, null, $tableId);
if (is_array($query_vars)) {
$table->set_additional_parameters($query_vars);
}
if ('table' === $style) {
if (is_array($header) && count($header) > 0) {
foreach ($header as $index => $header_item) {
$table->set_header(
$index,
isset($header_item[0]) ? $header_item[0] : null,
isset($header_item[1]) ? $header_item[1] : null,
isset($header_item[2]) ? $header_item[2] : null,
isset($header_item[3]) ? $header_item[3] : null
);
}
}
$table->set_form_actions($form_actions);
$table->display();
} else {
$table->display_grid();
}
}
/**
* Returns an HTML table with sortable column (through complete page refresh).
*
* @param array $header
* @param array $content Array of row arrays
* @param array $sorting_options
* @param array $paging_options
* @param array $query_vars
* @param array $form_actions
* @param string $style
*
* @return string HTML string for array
*/
public static function return_sortable_table(
$header,
$content,
$sorting_options = [],
$paging_options = [],
$query_vars = null,
$form_actions = [],
$style = 'table'
) {
ob_start();
self::display_sortable_table(
$header,
$content,
$sorting_options,
$paging_options,
$query_vars,
$form_actions,
$style
);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
/**
* Shows a nice grid.
*
* @param string grid name (important to create css)
* @param array header content
* @param array array with the information to show
* @param array $paging_options Keys are:
* 'per_page_default' = items per page when switching from
* full- list to per-page-view
* 'per_page' = number of items to show per page
* 'page_nr' = The page to display
* 'hide_navigation' = true to hide the navigation
* @param array $query_vars Additional variables to add in the query-string
* @param mixed An array with bool values to know which columns show.
* i.e: $visibility_options= array(true, false) we will only show the first column
* Can be also only a bool value. TRUE: show all columns, FALSE: show nothing
*/
public static function display_sortable_grid(
$name,
$header,
$content,
$paging_options = [],
$query_vars = null,
$form_actions = [],
$visibility_options = true,
$sort_data = true,
$grid_class = []
) {
echo self::return_sortable_grid(
$name,
$header,
$content,
$paging_options,
$query_vars,
$form_actions,
$visibility_options,
$sort_data,
$grid_class
);
}
/**
* Gets a nice grid in html string.
*
* @param string grid name (important to create css)
* @param array header content
* @param array array with the information to show
* @param array $paging_options Keys are:
* 'per_page_default' = items per page when switching from
* full- list to per-page-view
* 'per_page' = number of items to show per page
* 'page_nr' = The page to display
* 'hide_navigation' = true to hide the navigation
* @param array $query_vars Additional variables to add in the query-string
* @param mixed An array with bool values to know which columns show. i.e:
* $visibility_options= array(true, false) we will only show the first column
* Can be also only a bool value. TRUE: show all columns, FALSE: show nothing
* @param bool true for sorting data or false otherwise
* @param array grid classes
*
* @return string html grid
*/
public static function return_sortable_grid(
$name,
$header,
$content,
$paging_options = [],
$query_vars = null,
$form_actions = [],
$visibility_options = true,
$sort_data = true,
$grid_class = [],
$elementCount = 0
) {
$column = 0;
$default_items_per_page = $paging_options['per_page'] ?? 20;
$table = new SortableTableFromArray($content, $column, $default_items_per_page, $name);
$table->total_number_of_items = (int) $elementCount;
if (is_array($query_vars)) {
$table->set_additional_parameters($query_vars);
}
return $table->display_simple_grid(
$visibility_options,
$paging_options['hide_navigation'],
$default_items_per_page,
$sort_data,
$grid_class
);
}
/**
* Displays a table with a special configuration.
*
* @param array $header Titles for the table header
* each item in this array can contain 3 values
* - 1st element: the column title
* - 2nd element: true or false (column sortable?)
* - 3th element: additional attributes for th-tag (eg for column-width)
* - 4the element: additional attributes for the td-tags
* @param array $content 2D-array with the tables content
* @param array $sorting_options Keys are:
* 'column' = The column to use as sort-key
* 'direction' = SORT_ASC or SORT_DESC
* @param array $paging_options Keys are:
* 'per_page_default' = items per page when switching from full list to per-page-view
* 'per_page' = number of items to show per page
* 'page_nr' = The page to display
* @param array $query_vars Additional variables to add in the query-string
* @param array $column_show Array of binaries 1= show columns 0. hide a column
* @param array $column_order An array of integers that let us decide how the columns are going to be sort.
* i.e: $column_order=array('1''4','3','4'); The 2nd column will be order like the 4th column
* @param array $form_actions Set optional forms actions
*
* @author Julio Montoya
*/
public static function display_sortable_config_table(
$table_name,
$header,
$content,
$sorting_options = [],
$paging_options = [],
$query_vars = null,
$column_show = [],
$column_order = [],
$form_actions = []
) {
$column = isset($sorting_options['column']) ? $sorting_options['column'] : 0;
$default_items_per_page = isset($paging_options['per_page']) ? $paging_options['per_page'] : 20;
$table = new SortableTableFromArrayConfig(
$content,
$column,
$default_items_per_page,
$table_name,
$column_show,
$column_order
);
if (is_array($query_vars)) {
$table->set_additional_parameters($query_vars);
}
// Show or hide the columns header
if (is_array($column_show)) {
for ($i = 0; $i < count($column_show); $i++) {
if (!empty($column_show[$i])) {
$val0 = isset($header[$i][0]) ? $header[$i][0] : null;
$val1 = isset($header[$i][1]) ? $header[$i][1] : null;
$val2 = isset($header[$i][2]) ? $header[$i][2] : null;
$val3 = isset($header[$i][3]) ? $header[$i][3] : null;
$table->set_header($i, $val0, $val1, $val2, $val3);
}
}
}
$table->set_form_actions($form_actions);
$table->display();
}
/**
* Returns a div html string with.
*
* @param string $message
* @param string $type Example: confirm, normal, warning, error
* @param bool $filter Whether to XSS-filter or not
*
* @return string Message wrapped into an HTML div
*/
public static function return_message(string $message, string $type = 'normal', bool $filter = true): string
{
if (empty($message)) {
return '';
}
if ($filter) {
$message = api_htmlentities(
$message,
ENT_QUOTES
);
}
$class = '';
switch ($type) {
case 'warning':
$class .= 'alert alert-warning';
break;
case 'error':
$class .= 'alert alert-danger';
break;
case 'confirmation':
case 'confirm':
case 'success':
$class .= 'alert alert-success';
break;
case 'normal':
case 'info':
default:
$class .= 'alert alert-info';
}
return self::div($message, ['class' => $class]);
}
/**
* Returns an encrypted mailto hyperlink.
*
* @param string e-mail
* @param string clickable text
* @param string optional, class from stylesheet
* @param bool $addExtraContent
*
* @return string encrypted mailto hyperlink
*/
public static function encrypted_mailto_link(
$email,
$clickable_text = null,
$style_class = '',
$addExtraContent = false
) {
if (is_null($clickable_text)) {
$clickable_text = $email;
}
// "mailto:" already present?
if ('mailto:' !== substr($email, 0, 7)) {
$email = 'mailto:'.$email;
}
// Class (stylesheet) defined?
if ('' !== $style_class) {
$style_class = ' class="'.$style_class.'"';
}
// Encrypt email
$hmail = '';
for ($i = 0; $i < strlen($email); $i++) {
$hmail .= '&#'.ord($email[$i]).';';
}
$value = ('true' === api_get_setting('profile.add_user_course_information_in_mailto'));
if ($value) {
if ('false' === api_get_setting('allow_email_editor')) {
$hmail .= '?';
}
if (!api_is_anonymous()) {
$hmail .= '&subject='.Security::remove_XSS(api_get_setting('siteName'));
}
if ($addExtraContent) {
$content = '';
if (!api_is_anonymous()) {
$userInfo = api_get_user_info();
$content .= get_lang('User').': '.$userInfo['complete_name']."\n";
$courseInfo = api_get_course_info();
if (!empty($courseInfo)) {
$content .= get_lang('Course').': ';
$content .= $courseInfo['name'];
$sessionInfo = api_get_session_info(api_get_session_id());
if (!empty($sessionInfo)) {
$content .= ' '.$sessionInfo['name'].' <br />';
}
}
}
$hmail .= '&body='.rawurlencode($content);
}
}
$hclickable_text = '';
// Encrypt clickable text if @ is present
if (strpos($clickable_text, '@')) {
for ($i = 0; $i < strlen($clickable_text); $i++) {
$hclickable_text .= '&#'.ord($clickable_text[$i]).';';
}
} else {
$hclickable_text = @htmlspecialchars(
$clickable_text,
ENT_QUOTES,
api_get_system_encoding()
);
}
// Return encrypted mailto hyperlink
return '<a href="'.$hmail.'"'.$style_class.' class="clickable_email_link">'.$hclickable_text.'</a>';
}
/**
* Prints an <option>-list with all letters (A-Z).
*
* @todo This is English language specific implementation.
* It should be adapted for the other languages.
*
* @return string
*/
public static function get_alphabet_options($selectedLetter = '')
{
$result = '';
for ($i = 65; $i <= 90; $i++) {
$letter = chr($i);
$result .= '<option value="'.$letter.'"';
if ($selectedLetter == $letter) {
$result .= ' selected="selected"';
}
$result .= '>'.$letter.'</option>';
}
return $result;
}
/**
* Get the options withing a select box within the given values.
*
* @param int Min value
* @param int Max value
* @param int Default value
*
* @return string HTML select options
*/
public static function get_numeric_options($min, $max, $selected_num = 0)
{
$result = '';
for ($i = $min; $i <= $max; $i++) {
$result .= '<option value="'.$i.'"';
if (is_int($selected_num)) {
if ($selected_num == $i) {
$result .= ' selected="selected"';
}
}
$result .= '>'.$i.'</option>';
}
return $result;
}
/**
* Gets the path of an icon.
*
* @param string $icon
* @param int $size
*
* @return string
*/
public static function returnIconPath($icon, $size = ICON_SIZE_SMALL)
{
return self::return_icon($icon, null, null, $size, null, true, false);
}
/**
* This public function returns the htmlcode for an icon.
*
* @param string The filename of the file (in the main/img/ folder
* @param string The alt text (probably a language variable)
* @param array Additional attributes (for instance height, width, onclick, ...)
* @param int The wanted width of the icon (to be looked for in the corresponding img/icons/ folder)
*
* @return string An HTML string of the right <img> tag
*
* @author Patrick Cool <[email protected]>, Ghent University 2006
* @author Julio Montoya 2010 Function improved, adding image constants
* @author Yannick Warnier 2011 Added size handler
*
* @version Feb 2011
*/
public static function return_icon(
string $image,
?string $alt_text = '',
?array $additional_attributes = [],
?int $size = ICON_SIZE_SMALL,
?bool $show_text = true,
?bool $return_only_path = false,
?bool $loadThemeIcon = true
) {
$code_path = api_get_path(SYS_PUBLIC_PATH);
$w_code_path = api_get_path(WEB_PUBLIC_PATH);
// The following path is checked to see if the file exist. It's
// important to use the public path (i.e. web/css/) rather than the
// internal path (/app/Resource/public/css/) because the path used
// in the end must be the public path
$alternateCssPath = api_get_path(SYS_PUBLIC_PATH).'css/';
$alternateWebCssPath = api_get_path(WEB_PUBLIC_PATH).'css/';
// Avoid issues with illegal string offset for legacy calls to this
// method with an empty string rather than null or an empty array
if (empty($additional_attributes)) {
$additional_attributes = [];
}
$image = trim($image);
if (isset($size)) {
$size = (int) $size;
} else {
$size = ICON_SIZE_SMALL;
}
$size_extra = $size.'/';
$icon = $w_code_path.'img/'.$image;
$theme = 'themes/chamilo/icons/';
if ($loadThemeIcon) {
// @todo with chamilo 2 code
$theme = 'themes/'.api_get_visual_theme().'/icons/';
if (is_file($alternateCssPath.$theme.$image)) {
$icon = $alternateWebCssPath.$theme.$image;
}
// Checking the theme icons folder example: var/themes/chamilo/icons/XXX
if (is_file($alternateCssPath.$theme.$size_extra.$image)) {
$icon = $alternateWebCssPath.$theme.$size_extra.$image;
} elseif (is_file($code_path.'img/icons/'.$size_extra.$image)) {
//Checking the main/img/icons/XXX/ folder
$icon = $w_code_path.'img/icons/'.$size_extra.$image;
}
} else {
if (is_file($code_path.'img/icons/'.$size_extra.$image)) {
// Checking the main/img/icons/XXX/ folder
$icon = $w_code_path.'img/icons/'.$size_extra.$image;
}
}
// Special code to enable SVG - refs #7359 - Needs more work
// The code below does something else to "test out" SVG: for each icon,
// it checks if there is an SVG version. If so, it uses it.
// When moving this to production, the return_icon() calls should
// ask for the SVG version directly
$svgIcons = api_get_setting('icons_mode_svg');
if ('true' == $svgIcons && false == $return_only_path) {
$svgImage = substr($image, 0, -3).'svg';
if (is_file($code_path.$theme.'svg/'.$svgImage)) {
$icon = $w_code_path.$theme.'svg/'.$svgImage;
} elseif (is_file($code_path.'img/icons/svg/'.$svgImage)) {
$icon = $w_code_path.'img/icons/svg/'.$svgImage;
}
if (empty($additional_attributes['height'])) {
$additional_attributes['height'] = $size;
}
if (empty($additional_attributes['width'])) {
$additional_attributes['width'] = $size;
}
}
if ($return_only_path) {
return $icon;
}
$img = self::img($icon, $alt_text, $additional_attributes);
if (SHOW_TEXT_NEAR_ICONS == true && !empty($alt_text)) {
if ($show_text) {
$img = "$img $alt_text";
}
}
return $img;
}
/**
* Returns the htmlcode for an image.
*
* @param string $image_path the filename of the file (in the main/img/ folder
* @param string $alt_text the alt text (probably a language variable)
* @param array $additional_attributes (for instance height, width, onclick, ...)
* @param bool $filterPath Optional. Whether filter the image path. Default is true
*
* @return string
*
* @author Julio Montoya 2010
*/
public static function img(
$image_path,
$alt_text = '',
$additional_attributes = null,
$filterPath = true
) {
if (empty($image_path)) {
return '';
}
// Sanitizing the parameter $image_path
if ($filterPath) {
$image_path = Security::filter_img_path($image_path);
}
// alt text = the image name if there is none provided (for XHTML compliance)
if ('' == $alt_text) {
$alt_text = basename($image_path);
}
if (empty($additional_attributes)) {
$additional_attributes = [];
}
$additional_attributes['src'] = $image_path;
if (empty($additional_attributes['alt'])) {
$additional_attributes['alt'] = $alt_text;
}
if (empty($additional_attributes['title'])) {
$additional_attributes['title'] = $alt_text;
}
return self::tag('img', '', $additional_attributes);
}
/**
* Returns the htmlcode for a tag (h3, h1, div, a, button), etc.
*
* @param string $tag the tag name
* @param string $content the tag's content
* @param array $additional_attributes (for instance height, width, onclick, ...)
*
* @return string
*
* @author Julio Montoya 2010
*/
public static function tag($tag, $content, $additional_attributes = [])
{
$attribute_list = '';
// Managing the additional attributes
if (!empty($additional_attributes) && is_array($additional_attributes)) {
$attribute_list = '';
foreach ($additional_attributes as $key => &$value) {
$attribute_list .= $key.'="'.$value.'" ';
}
}
//some tags don't have this </XXX>
if (in_array($tag, ['img', 'input', 'br'])) {
$return_value = '<'.$tag.' '.$attribute_list.' />';
} else {
$return_value = '<'.$tag.' '.$attribute_list.' >'.$content.'</'.$tag.'>';
}
return $return_value;
}
/**
* Creates a URL anchor.
*
* @param string $name
* @param string $url
* @param array $attributes
*
* @return string
*/
public static function url($name, $url, $attributes = [])
{
if (!empty($url)) {
$url = preg_replace('#&#', '&', $url);
$url = htmlspecialchars($url, ENT_QUOTES, 'UTF-8');
$attributes['href'] = $url;
}
return self::tag('a', $name, $attributes);
}
/**
* Creates a div tag.
*
* @param string $content
* @param array $attributes
*
* @return string
*/
public static function div($content, $attributes = [])
{
return self::tag('div', $content, $attributes);
}
/**
* Creates a span tag.
*/
public static function span($content, $attributes = [])
{
return self::tag('span', $content, $attributes);
}
/**
* Displays an HTML input tag.
*/
public static function input($type, $name, $value, $attributes = [])
{
if (isset($type)) {
$attributes['type'] = $type;
}
if (isset($name)) {
$attributes['name'] = $name;
}
if (isset($value)) {
$attributes['value'] = $value;
}
return self::tag('input', '', $attributes);
}
/**
* Displays an HTML select tag.
*/
public static function select(
string $name,
array $values,
mixed $default = -1,
array $extra_attributes = [],
bool $show_blank_item = true,
string $blank_item_text = ''
): string {
$html = '';
$extra = '';
$default_id = 'id="'.$name.'" ';
$extra_attributes = array_merge(
['class' => 'p-dropdown p-component p-inputwrapper p-inputwrapper-filled'],
$extra_attributes
);
foreach ($extra_attributes as $key => $parameter) {
if ('id' == $key) {
$default_id = '';
}
$extra .= $key.'="'.$parameter.'" ';
}
$html .= '<select name="'.$name.'" '.$default_id.' '.$extra.'>';
if ($show_blank_item) {
if (empty($blank_item_text)) {
$blank_item_text = get_lang('Select');
} else {
$blank_item_text = Security::remove_XSS($blank_item_text);
}
$html .= self::tag(
'option',
'-- '.$blank_item_text.' --',
['value' => '-1']
);
}
if ($values) {
foreach ($values as $key => $value) {
if (is_array($value) && isset($value['name'])) {
$value = $value['name'];
}
$html .= '<option value="'.$key.'"';
if (is_array($default)) {
foreach ($default as $item) {
if ($item == $key) {
$html .= ' selected="selected"';
break;
}
}
} else {
if ($default == $key) {
$html .= ' selected="selected"';
}
}
$html .= '>'.$value.'</option>';
}
}
$html .= '</select>';
return $html;
}
/**
* @param $name
* @param $value
* @param array $attributes
*
* @return string
*/
public static function button($name, $value, $attributes = [])
{
if (!empty($name)) {
$attributes['name'] = $name;
}
return self::tag('button', $value, $attributes);
}
/**
* Creates a tab menu
* Requirements: declare the jquery, jquery-ui libraries + the jquery-ui.css
* in the $htmlHeadXtra variable before the display_header
* Add this script.
*
* @param array $headers list of the tab titles
* @param array $items
* @param string $id id of the container of the tab in the example "tabs"
* @param array $attributes for the ul
* @param array $ul_attributes
* @param string $selected
*
* @return string
*/
public static function tabs(
$headers,
$items,
$id = 'tabs',
$attributes = [],
$ul_attributes = [],
$selected = ''
) {
if (empty($headers) || 0 === count($headers)) {
return '';
}
$lis = '';
$i = 1;
foreach ($headers as $item) {
$active = '';
if (1 == $i) {
$active = ' active';
}