-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap_helper.php
More file actions
130 lines (103 loc) · 5.41 KB
/
bootstrap_helper.php
File metadata and controls
130 lines (103 loc) · 5.41 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
<?php
// app/Helpers/bootstrap_helper.php
use CodeIgniter\CodeIgniter;
function bs_nav_item(string $page_url, string $nav_item_text, array $options = null): string {
$extenal_link = $options['external-link'] ?? false;
$after_a_tag = $options['after-a-tag'] ?? '';
$disabled = $options['disabled'] ?? false;
$is_current_page = ($page_url == uri_string());
$href = ($extenal_link == false) ? base_url($page_url) : $page_url;
// Handle 'nav-link' classes and attributes
$a_additional_classes = $options['a_classes'] ?? '';
$a_extra_attributes = $options['a_attributes'] ?? [];
$a_classes = 'nav-link' . ($is_current_page ? ' active' : '') . ($disabled ? ' disabled' : '') . ' ' . $a_additional_classes;
$a_attributes = array_merge(['class' => trim($a_classes), 'href' => $href], $a_extra_attributes);
// Build 'a' tag
$a_tag = bs_html_builder('a', $nav_item_text, $a_attributes);
// Handle 'nav-item' classes and attributes
$li_additional_classes = $options['li_classes'] ?? '';
$li_extra_attributes = $options['li_attributes'] ?? [];
$li_classes = 'nav-item ' . $li_additional_classes;
$li_attributes = array_merge(['class' => trim($li_classes)], $li_extra_attributes);
// Build 'li' tag
$li_tag = bs_html_builder('li', $a_tag . $after_a_tag , $li_attributes);
return $li_tag;
}
// ==================================================
function bs_nav_item_basic_menu(string $page_url, string $nav_item_text, array $menu_items = [], array $options = null): string {
$options['after-a-tag'] = '<ul class="ps-4 list-unstyled">';
foreach($menu_items as $menu_item){
$menu_item_options = $menu_item['options'] ?? null;
$options['after-a-tag'] .= bs_nav_item($menu_item['url'], $menu_item['text'], $menu_item_options);
}
$options['after-a-tag'] .= '</ul>';
return bs_nav_item($page_url, $nav_item_text, $options);
}
// ==================================================
function bs_nav_item_basic_menu_uri_toggle(string $page_url, string $nav_item_text, array $menu_items = [], array $options = null): string {
$extenal_link = $options['external-link'] ?? false;
if($extenal_link == false){
$page_url_array = explode('/', $page_url);
$page_url_first = reset($page_url_array);
$uri_array = explode('/', uri_string());
$uri_first = reset($uri_array);
$menu_array = (strcasecmp($page_url_first, $uri_first) === 0) ? $menu_items : [];
return bs_nav_item_basic_menu($page_url, $nav_item_text, $menu_array, $options);
}else{
throw new \Exception('Cannot use the function with external links.');
}
}
// ==================================================
function bs_nav_dropdown(string $dropdown_url, string $dropdown_text, array $dropdown_items, array $options = null): string {
$disabled = $options['disabled'] ?? false;
$is_current_page = ($dropdown_url == uri_string());
$href = base_url($dropdown_url);
//Build 'dropdown-item' items
$dropdown_menu_items = "";
foreach($dropdown_items as $dropdown_item){
if(isset($dropdown_item['type'])){
if($dropdown_item['type'] == 'item'){
$dropdown_menu_items .= bs_html_builder('li', bs_html_builder('a', $dropdown_item['text'], ['class' => 'dropdown-item', 'href' => $dropdown_item['href']]));
}else if($dropdown_item['type'] == 'divider'){
$dropdown_menu_items .= bs_html_builder('li', bs_html_builder('hr', '', ['class' => 'dropdown-divider']));
}
}else{
$dropdown_menu_items .= $dropdown_item;
}
}
// Build 'ul.dropdown-menu' tag
$dropdown_menu = bs_html_builder('ul', $dropdown_menu_items, ['class' => 'dropdown-menu']);
//Build 'a.nav-link' tag
$nav_link = bs_html_builder('a', $dropdown_text, ['class' => 'nav-link dropdown-toggle', 'href' => '#', 'role' => 'button', 'data-bs-toggle' => 'dropdown', 'aria-expanded' => 'false']);
//Build 'li.nav-item' tag
$nav_item = bs_html_builder('li', $nav_link . $dropdown_menu, ['class' => 'nav-item dropdown']);
return $nav_item;
}
// ==================================================
function bs_icon(string $class, array $options = null): string{
$options['tag'] = $options['tag'] ?? "i";
$options['additional_classes'] = isset($options['additional_classes']) ? " " . $options['additional_classes'] : "";
return "<{$options['tag']} class=\"bi bi-{$class}{$options['additional_classes']}\"></{$options['tag']}>";
}
// ==================================================
function bs_html_builder(string $tag, string $content = '', array $attributes = [], bool $requires_closing_tag = true): string {
// Initialize the attribute string
$attrString = '';
// Build the attribute string
foreach ($attributes as $key => $value) {
if (is_bool($value)) {
if ($value) {
$attrString .= " {$key}";
}
} else {
$attrString .= " {$key}=\"" . htmlspecialchars($value, ENT_QUOTES) . "\"";
}
}
// Generate the HTML based on whether a closing tag is required
if ($requires_closing_tag) {
return "<{$tag}{$attrString}>{$content}</{$tag}>";
} else {
return "<{$tag}{$attrString} />";
}
}
?>