|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package CleverStyle Framework |
|
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
|
5
|
|
|
* @copyright Copyright (c) 2011-2017, Nazar Mokrynskyi |
|
6
|
|
|
* @license MIT License, see license.txt |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace cs\h; |
|
9
|
|
|
use |
|
10
|
|
|
nazarpc\BananaHTML, |
|
11
|
|
|
cs\Config, |
|
12
|
|
|
cs\Language, |
|
13
|
|
|
cs\Page, |
|
14
|
|
|
cs\Request, |
|
15
|
|
|
cs\Session, |
|
16
|
|
|
cs\User; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class for HTML code rendering in accordance with the standards of HTML5, and with useful syntax extensions for simpler usage |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract class Base extends BananaHTML { |
|
22
|
|
|
/** |
|
23
|
|
|
* Special processing for URLs with hash |
|
24
|
|
|
* |
|
25
|
|
|
* @static |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $url |
|
28
|
|
|
* |
|
29
|
|
|
* @return string |
|
30
|
|
|
*/ |
|
31
|
2 |
|
protected static function url_with_hash ($url) { |
|
32
|
2 |
|
return Request::instance()->uri.$url; |
|
33
|
|
|
} |
|
34
|
|
|
/** |
|
35
|
|
|
* Convert relative URL to absolute |
|
36
|
|
|
* |
|
37
|
|
|
* @static |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $url |
|
40
|
|
|
* |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
2 |
|
protected static function absolute_url ($url) { |
|
44
|
|
|
/** |
|
45
|
|
|
* If Config not initialized yet - method will return `false`, which will be interpreted as empty string |
|
46
|
|
|
*/ |
|
47
|
2 |
|
return Config::instance(true)->base_url()."/$url"; |
|
48
|
|
|
} |
|
49
|
|
|
/** |
|
50
|
|
|
* Allows to add something to inner of form, for example, hidden session input to prevent CSRF |
|
51
|
|
|
* |
|
52
|
|
|
* @static |
|
53
|
|
|
* |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
2 |
|
protected static function form_csrf () { |
|
57
|
|
|
if ( |
|
58
|
2 |
|
class_exists('\\cs\\Session', false) && |
|
59
|
2 |
|
$Session = Session::instance(true) |
|
60
|
|
|
) { |
|
61
|
2 |
|
return static::input( |
|
62
|
|
|
[ |
|
63
|
2 |
|
'value' => $Session->get_id() ?: $Session->add(User::GUEST_ID), |
|
64
|
2 |
|
'type' => 'hidden', |
|
65
|
2 |
|
'name' => 'session' |
|
66
|
|
|
] |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
2 |
|
return ''; |
|
70
|
|
|
} |
|
71
|
|
|
/** |
|
72
|
|
|
* CleverStyle Framework-specific processing of attributes |
|
73
|
|
|
* |
|
74
|
|
|
* @static |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $tag |
|
77
|
|
|
* @param array $attributes |
|
78
|
|
|
*/ |
|
79
|
36 |
|
protected static function pre_processing ($tag, &$attributes) { |
|
80
|
|
|
/** |
|
81
|
|
|
* Do not apply to custom elements, they should support this by themselves |
|
82
|
|
|
*/ |
|
83
|
|
|
if ( |
|
84
|
36 |
|
isset($attributes['tooltip']) && |
|
85
|
36 |
|
$attributes['tooltip'] !== false && |
|
86
|
36 |
|
!isset($attributes['is']) && |
|
87
|
36 |
|
strpos($tag, '-') === false |
|
88
|
|
|
) { |
|
89
|
2 |
|
$attributes['in'] = isset($attributes['in']) ? $attributes['in'].static::cs_tooltip() : static::cs_tooltip(); |
|
90
|
|
|
} |
|
91
|
36 |
|
} |
|
92
|
|
|
/** |
|
93
|
|
|
* Sometimes HTML code can be intended |
|
94
|
|
|
* |
|
95
|
|
|
* This function allows to store inner text of tags, that are sensitive to this operation (textarea, pre, code), and return some identifier. |
|
96
|
|
|
* Later, at page generation, this identifier will be replaced by original text again. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $text |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
2 |
|
protected static function indentation_protection ($text) { |
|
103
|
2 |
|
$uniqid = uniqid('html_replace_', true); |
|
104
|
2 |
|
Page::instance()->replace($uniqid, $text); |
|
105
|
2 |
|
return $uniqid; |
|
106
|
|
|
} |
|
107
|
|
|
/** |
|
108
|
|
|
* Pseudo tag for labels with tooltips, specified <i>input</i> is translation item of <b>$L</b> object, |
|
109
|
|
|
* <i>input</i>_into item of <b>$L</b> is content of tooltip |
|
110
|
|
|
* |
|
111
|
|
|
* @static |
|
112
|
|
|
* |
|
113
|
|
|
* @param array|string $in |
|
114
|
|
|
* @param array $data |
|
115
|
|
|
* |
|
116
|
|
|
* @return mixed |
|
117
|
|
|
*/ |
|
118
|
2 |
|
public static function info ($in = '', $data = []) { |
|
119
|
2 |
|
if (isset($in['insert']) || isset($data['insert'])) { |
|
120
|
2 |
|
return static::__callStatic(__FUNCTION__, func_get_args()); |
|
121
|
|
|
} |
|
122
|
2 |
|
if ($in === false) { |
|
123
|
2 |
|
return ''; |
|
124
|
2 |
|
} elseif (is_array($in)) { |
|
125
|
2 |
|
return static::__callStatic(__FUNCTION__, [$in, $data]); |
|
126
|
|
|
} |
|
127
|
2 |
|
$L = Language::instance(); |
|
128
|
2 |
|
return static::span( |
|
129
|
2 |
|
$L->$in, |
|
130
|
|
|
[ |
|
131
|
2 |
|
'tooltip' => $L->{$in.'_info'} |
|
132
|
2 |
|
] + $data |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
/** |
|
136
|
|
|
* Pseudo tag for inserting of icons |
|
137
|
|
|
* |
|
138
|
|
|
* @static |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $icon Icon name in Font Awesome |
|
141
|
|
|
* @param array $data |
|
142
|
|
|
* |
|
143
|
|
|
* @return mixed |
|
144
|
|
|
*/ |
|
145
|
6 |
|
public static function icon ($icon, $data = []) { |
|
146
|
6 |
|
if (isset($in['insert']) || isset($data['insert'])) { |
|
147
|
2 |
|
return static::__callStatic(__FUNCTION__, func_get_args()); |
|
148
|
|
|
} |
|
149
|
6 |
|
if ($icon === false) { |
|
150
|
2 |
|
return ''; |
|
151
|
|
|
} |
|
152
|
6 |
|
$data['icon'] = $icon; |
|
153
|
6 |
|
return static::cs_icon($data).' '; |
|
154
|
|
|
} |
|
155
|
|
|
/** |
|
156
|
|
|
* Rendering of input[type=checkbox] with automatic adding labels and necessary classes |
|
157
|
|
|
* |
|
158
|
|
|
* @static |
|
159
|
|
|
* |
|
160
|
|
|
* @param array $in |
|
161
|
|
|
* @param array $data |
|
162
|
|
|
* |
|
163
|
|
|
* @return string |
|
164
|
|
|
*/ |
|
165
|
2 |
|
public static function checkbox ($in = [], $data = []) { |
|
166
|
2 |
|
$pre_result = self::common_checkbox_radio_pre($in, $data, __FUNCTION__); |
|
167
|
2 |
|
if ($pre_result !== false) { |
|
168
|
2 |
|
return $pre_result; |
|
169
|
|
|
} |
|
170
|
2 |
|
if (@is_array($in['name']) || @is_array($in['id'])) { |
|
171
|
2 |
|
return static::common_checkbox_radio_reduce($in, [static::class, 'checkbox']); |
|
172
|
|
|
} |
|
173
|
2 |
|
self::common_checkbox_radio_post($in); |
|
174
|
2 |
|
return static::cs_label_switcher( |
|
175
|
2 |
|
static::label(static::u_wrap($in, 'input')), |
|
176
|
|
|
[ |
|
177
|
2 |
|
'class' => isset($item['class']) ? $item['class'] : false |
|
178
|
2 |
|
] |
|
179
|
|
|
); |
|
180
|
|
|
} |
|
181
|
|
|
/** |
|
182
|
|
|
* Rendering of input[type=radio] with automatic adding labels and necessary classes |
|
183
|
|
|
* |
|
184
|
|
|
* @static |
|
185
|
|
|
* |
|
186
|
|
|
* @param array $in |
|
187
|
|
|
* @param array $data |
|
188
|
|
|
* |
|
189
|
|
|
* @return string |
|
190
|
|
|
*/ |
|
191
|
2 |
|
public static function radio ($in = [], $data = []) { |
|
192
|
2 |
|
$pre_result = self::common_checkbox_radio_pre($in, $data, __FUNCTION__); |
|
193
|
2 |
|
if ($pre_result !== false) { |
|
194
|
2 |
|
return $pre_result; |
|
195
|
|
|
} |
|
196
|
2 |
|
if (!isset($in['checked'])) { |
|
197
|
2 |
|
$in['checked'] = $in['value'][0]; |
|
198
|
|
|
} |
|
199
|
2 |
|
return static::common_checkbox_radio_reduce( |
|
200
|
|
|
$in, |
|
201
|
|
|
function ($item) { |
|
202
|
2 |
|
self::common_checkbox_radio_post($item); |
|
203
|
2 |
|
return static::cs_label_button( |
|
204
|
2 |
|
static::label(static::u_wrap($item, 'input')), |
|
205
|
|
|
[ |
|
206
|
2 |
|
'class' => isset($item['class']) ? $item['class'] : false |
|
207
|
2 |
|
] |
|
208
|
|
|
); |
|
209
|
2 |
|
} |
|
210
|
|
|
); |
|
211
|
|
|
} |
|
212
|
|
|
/** |
|
213
|
|
|
* @static |
|
214
|
|
|
* |
|
215
|
|
|
* @param array $in |
|
216
|
|
|
* @param array $data |
|
217
|
|
|
* @param string $type |
|
218
|
|
|
* |
|
219
|
|
|
* @return bool|string |
|
220
|
|
|
*/ |
|
221
|
2 |
|
protected static function common_checkbox_radio_pre (&$in, $data, $type) { |
|
222
|
2 |
|
if (isset($in['insert']) || isset($data['insert'])) { |
|
223
|
2 |
|
return static::__callStatic($type, [$in, $data]); |
|
224
|
|
|
} |
|
225
|
2 |
|
if ($in === false) { |
|
226
|
2 |
|
return ''; |
|
227
|
|
|
} |
|
228
|
2 |
|
$in = static::input_merge($in, $data); |
|
229
|
|
|
/** @noinspection NotOptimalIfConditionsInspection */ |
|
230
|
2 |
|
if (is_array_indexed($in) && is_array($in[0])) { |
|
231
|
2 |
|
return static::__callStatic($type, [$in, $data]); |
|
232
|
|
|
} |
|
233
|
2 |
|
$in['type'] = $type; |
|
234
|
2 |
|
return false; |
|
235
|
|
|
} |
|
236
|
|
|
/** |
|
237
|
|
|
* @static |
|
238
|
|
|
* |
|
239
|
|
|
* @param array $item |
|
240
|
|
|
*/ |
|
241
|
2 |
|
protected static function common_checkbox_radio_post (&$item) { |
|
242
|
2 |
|
if (isset($item['value'], $item['checked'])) { |
|
243
|
2 |
|
$item['checked'] = $item['value'] == $item['checked']; |
|
244
|
|
|
} |
|
245
|
2 |
|
} |
|
246
|
|
|
/** |
|
247
|
|
|
* @static |
|
248
|
|
|
* |
|
249
|
|
|
* @param array $in |
|
250
|
|
|
* @param callable $callable |
|
251
|
|
|
* |
|
252
|
|
|
* @return string |
|
253
|
|
|
*/ |
|
254
|
2 |
|
protected static function common_checkbox_radio_reduce ($in, $callable) { |
|
255
|
2 |
|
return array_reduce( |
|
256
|
2 |
|
self::array_flip_3d($in) ?: [], |
|
257
|
2 |
|
function ($carry, $item) use ($callable) { |
|
258
|
2 |
|
return $carry.$callable($item); |
|
259
|
2 |
|
} |
|
260
|
|
|
); |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|