1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle Framework |
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
5
|
|
|
* @copyright Copyright (c) 2011-2016, 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
|
34 |
|
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
|
34 |
|
isset($attributes['tooltip']) && |
85
|
34 |
|
$attributes['tooltip'] !== false && |
86
|
34 |
|
!isset($attributes['is']) && |
87
|
34 |
|
strpos($tag, '-') === false |
88
|
|
|
) { |
89
|
2 |
|
$attributes['in'] = isset($attributes['in']) ? $attributes['in'].static::cs_tooltip() : static::cs_tooltip(); |
90
|
|
|
} |
91
|
34 |
|
} |
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 |
|
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
|
2 |
|
static function icon ($icon, $data = []) { |
146
|
2 |
|
if (isset($in['insert']) || isset($data['insert'])) { |
147
|
2 |
|
return static::__callStatic(__FUNCTION__, func_get_args()); |
148
|
|
|
} |
149
|
2 |
|
if ($icon === false) { |
150
|
2 |
|
return ''; |
151
|
|
|
} |
152
|
2 |
|
$data['icon'] = $icon; |
153
|
2 |
|
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 |
|
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
|
|
|
} else { |
173
|
2 |
|
self::common_checkbox_radio_post($in); |
174
|
2 |
|
return static::label( |
175
|
2 |
|
static::u_wrap($in, 'input'), |
176
|
|
|
[ |
177
|
2 |
|
'is' => 'cs-label-switcher', |
178
|
2 |
|
'class' => isset($item['class']) ? $item['class'] : false |
179
|
|
|
] |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
/** |
184
|
|
|
* Rendering of input[type=radio] with automatic adding labels and necessary classes |
185
|
|
|
* |
186
|
|
|
* @static |
187
|
|
|
* |
188
|
|
|
* @param array $in |
189
|
|
|
* @param array $data |
190
|
|
|
* |
191
|
|
|
* @return string |
|
|
|
|
192
|
|
|
*/ |
193
|
2 |
|
static function radio ($in = [], $data = []) { |
194
|
2 |
|
$pre_result = self::common_checkbox_radio_pre($in, $data, __FUNCTION__); |
195
|
2 |
|
if ($pre_result !== false) { |
196
|
2 |
|
return $pre_result; |
197
|
|
|
} |
198
|
2 |
|
if (!isset($in['checked'])) { |
199
|
2 |
|
$in['checked'] = $in['value'][0]; |
200
|
|
|
} |
201
|
2 |
|
return static::common_checkbox_radio_reduce( |
202
|
|
|
$in, |
203
|
|
|
function ($item) { |
204
|
2 |
|
self::common_checkbox_radio_post($item); |
205
|
2 |
|
return static::label( |
206
|
2 |
|
static::u_wrap($item, 'input'), |
207
|
|
|
[ |
208
|
2 |
|
'is' => 'cs-label-button', |
209
|
2 |
|
'class' => isset($item['class']) ? $item['class'] : false |
210
|
|
|
] |
211
|
|
|
); |
212
|
2 |
|
} |
213
|
|
|
); |
214
|
|
|
} |
215
|
|
|
/** |
216
|
|
|
* @static |
217
|
|
|
* |
218
|
|
|
* @param array $in |
219
|
|
|
* @param array $data |
220
|
|
|
* @param string $type |
221
|
|
|
* |
222
|
|
|
* @return bool|string |
223
|
|
|
*/ |
224
|
2 |
|
protected static function common_checkbox_radio_pre (&$in, $data, $type) { |
225
|
2 |
|
if (isset($in['insert']) || isset($data['insert'])) { |
226
|
2 |
|
return static::__callStatic($type, [$in, $data]); |
227
|
|
|
} |
228
|
2 |
|
if ($in === false) { |
229
|
2 |
|
return ''; |
230
|
|
|
} |
231
|
2 |
|
$in = static::input_merge($in, $data); |
232
|
|
|
/** @noinspection NotOptimalIfConditionsInspection */ |
233
|
2 |
|
if (is_array_indexed($in) && is_array($in[0])) { |
234
|
2 |
|
return static::__callStatic($type, [$in, $data]); |
235
|
|
|
} |
236
|
2 |
|
$in['type'] = $type; |
237
|
2 |
|
return false; |
238
|
|
|
} |
239
|
|
|
/** |
240
|
|
|
* @static |
241
|
|
|
* |
242
|
|
|
* @param array $item |
243
|
|
|
*/ |
244
|
2 |
|
protected static function common_checkbox_radio_post (&$item) { |
245
|
2 |
|
if (isset($item['value'], $item['checked'])) { |
246
|
2 |
|
$item['checked'] = $item['value'] == $item['checked']; |
247
|
|
|
} |
248
|
2 |
|
} |
249
|
|
|
/** |
250
|
|
|
* @static |
251
|
|
|
* |
252
|
|
|
* @param array $in |
253
|
|
|
* @param callable $callable |
254
|
|
|
* |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
2 |
|
protected static function common_checkbox_radio_reduce ($in, $callable) { |
258
|
2 |
|
return array_reduce( |
259
|
2 |
|
self::array_flip_3d($in) ?: [], |
260
|
2 |
|
function ($carry, $item) use ($callable) { |
261
|
2 |
|
return $carry.$callable($item); |
262
|
2 |
|
}, |
263
|
2 |
|
'' |
264
|
|
|
); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.