summaryrefslogtreecommitdiff
path: root/app/Bootstrap4.php
blob: 4e101a1fcc4482fb1f2a9497903d8cf0b5275cc7 (plain)
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
<?php
/**
 * webtrees: online genealogy
 * Copyright (C) 2017 webtrees development team
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
namespace Fisharebest\Webtrees;

/**
 * Helper functions to generate markup for Bootstrap 4.
 *
 * @link https://www.getbootstrap.com
 */
class Bootstrap4 extends Html {
	/**
	 * Generate a badge containing a count of items.
	 *
	 * @param array $items
	 *
	 * @return string
	 */
	public static function badgeCount(array $items) {
		if (empty($items)) {
			return '';
		} else {
			return '<span class="badge badge-default">' . I18N::number(count($items)) . '</span>';
		}
	}

	/**
	 * Generate a breadcrumb trail.
	 *
	 * @param array  $hierarchy
	 * @param string $active
	 *
	 * @return string
	 */
	public static function breadcrumbs(array $hierarchy, $active) {
		$html = '<ol class="breadcrumb">';

		foreach ($hierarchy as $url => $label) {
			$html .= '<li class="breadcrumb-item"><a href="' . $url . '">' . $label . '</a></li>';
		}

		$html .= '<li class="breadcrumb-item active">' . $active . '</li></ol>';

		return $html;
	}

	/**
	 * Generate a checkbox.
	 *
	 * @param string   $label
	 * @param bool     $inline
	 * @param string[] $attributes
	 *
	 * @return string
	 */
	public static function checkbox($label, $inline, $attributes = []) {
		if ($inline) {
			$class = 'form-check form-check-inline';
		} else {
			$class = 'form-check';
		}

		$input_attributes = self::attributes([
				'class'   => 'form-check-input',
				'type'    => 'checkbox',
				'value'   => '1',
			] + $attributes);

		return
			'<div class="' . $class . '">' .
			'<label class="form-check-label">' .
			'<input ' . $input_attributes . '> ' . Filter::escapeHtml($label) .
			'</label>' .
			'</div>';
	}

	/**
	 * Create a set of radio buttons for a form
	 *
	 * @param string   $name The ID for the form element
	 * @param string[] $values Array of value=>display items
	 * @param string   $selected The currently selected item
	 * @param bool     $inline
	 * @param string[] $attributes
	 *
	 * @return string
	 */
	public static function radioButtons($name, $values, $selected, $inline, $attributes = []) {
		// An empty string is not the same as zero (but is the same as NULL).
		if ($selected === null) {
			$selected = '0';
		}

		if ($inline) {
			$class = 'form-check form-check-inline';
		} else {
			$class = 'form-check';
		}

		$html = '';
		foreach ($values as $value => $label) {
			$input_attributes = self::attributes([
				'class'   => 'form-check-input',
				'type'    => 'radio',
				'name'    => $name,
				'value'   => $value,
				'checked' => (string) $value === (string) $selected,
			] + $attributes);

			$html .=
				'<div class="' . $class . '">' .
				'<label class="form-check-label">' .
				'<input ' . $input_attributes . '> ' . Filter::escapeHtml($label) .
				'</label>' .
				'</div>';
		}

		return $html;
	}

	/**
	 * Create a <select> control for a form.
	 *
	 * @param string[] $options
	 * @param string   $selected
	 * @param string[] $attributes
	 *
	 * @return string
	 */
	public static function select($options, $selected, $attributes = []) {
		$html = '';
		foreach ($options as $value => $option) {
			$option_attributes = self::attributes([
				'value'    => $value,
				'selected' => (string) $value === (string) $selected,
			]);

			$html .= '<option ' . $option_attributes . '>' . Filter::escapeHtml($option) . '</option>';
		}

		if (empty($attributes['class'])) {
			$attributes['class'] = 'form-control';
		} else {
			$attributes['class'] .= ' form-control';
		}

		$select_attributes = self::attributes($attributes);

		return '<select ' . $select_attributes . '>' . $html . '</select>';
	}

	/**
	 * Create a multiple <select> control for a form.
	 *
	 * @param string[] $options
	 * @param string[] $selected
	 * @param string[] $attributes
	 *
	 * @return string
	 */
	public static function multiSelect($options, $selected, $attributes = []) {
		$html = '';
		foreach ($options as $value => $option) {
			$option_attributes = self::attributes([
				'value'    => $value,
				'selected' => in_array((string) $value, $selected),
			]);

			$html .= '<option ' . $option_attributes . '>' . Filter::escapeHtml($option) . '</option>';
		}

		if (empty($attributes['class'])) {
			$attributes['class'] = 'form-control';
		} else {
			$attributes['class'] .= ' form-control';
		}

		$select_attributes = self::attributes($attributes);

		return '<select ' . $select_attributes . ' multiple>' . $html . '</select>';
	}
}