forked from EFTEC/BladeOne
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBladeOneHtml.php
More file actions
444 lines (428 loc) · 17 KB
/
BladeOneHtml.php
File metadata and controls
444 lines (428 loc) · 17 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
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
<?php
namespace eftec\bladeone;
/**
* trait BladeOneHtml
* Copyright (c) 2016 Jorge Patricio Castro Castillo MIT License. Don't delete this comment, its part of the license.
* Extends the tags of the class BladeOne. Its optional
* It adds the next tags
* <code>
* select:
* @ select('idCountry'[,$extra])
* @ item('0','--select a country'[,$extra])
* @ items($countries,'id','name',$currentCountry[,$extra])
* @ endselect()
* input:
* @ input('iduser',$currentUser,'text'[,$extra])
* button:
* @ commandbutton('idbutton','value','text'[,$extra])
*
* </code>
* Note. The names of the tags are based in Java Server Faces (JSF)
* @package BladeOneHtml
* @version 1.8 2018-02-20 (1)
* @link https://github.com/EFTEC/BladeOne
* @author Jorge Patricio Castro Castillo <jcastro arroba eftec dot cl>
*/
trait BladeOneHtml
{
var $htmlItem=array(); // indicates the type of the current tag. such as select/selectgroup/etc.
var $htmlCurrentId=array(); //indicates the id of the current tag.
//<editor-fold desc="compile function">
protected function compileSelect($expression) {
array_push($this->htmlItem,'select');
return $this->phpTag."echo \$this->select{$expression}; ?>";
}
protected function compileListBoxes($expression) {
return $this->phpTag."echo \$this->listboxes{$expression}; ?>";
}
protected function compileLink($expression) {
return $this->phpTag."echo \$this->link{$expression}; ?>";
}
protected function compileSelectGroup($expression) {
array_push($this->htmlItem,'selectgroup');
$this->compilePush('');
return $this->phpTag."echo \$this->select{$expression}; ?>";
}
protected function compileRadio($expression) {
array_push($this->htmlItem,'radio');
return $this->phpTag."echo \$this->radio{$expression}; ?>";
}
protected function compileCheckbox($expression) {
array_push($this->htmlItem,'checkbox');
return $this->phpTag."echo \$this->checkbox{$expression}; ?>";
}
protected function compileEndSelect() {
$r=@array_pop($this->htmlItem);
if (is_null($r)) {
$this->showError("@endselect","Missing @select or so many @endselect",true);
}
return $this->phpTag."echo '</select>'; ?>";
}
protected function compileEndRadio() {
$r=@array_pop($this->htmlItem);
if (is_null($r)) {
return $this->showError("@EndRadio","Missing @Radio or so many @EndRadio",true);
}
return '';
}
protected function compileEndCheckbox() {
$r=@array_pop($this->htmlItem);
if (is_null($r)) {
return $this->showError("@EndCheckbox","Missing @Checkbox or so many @EndCheckbox",true);
}
return '';
}
protected function compileItem($expression) {
// we add a new attribute with the type of the current open tag
$r=end($this->htmlItem);
$x=trim($expression);
$x="('{$r}',".substr($x,1);
return $this->phpTag."echo \$this->item{$x}; ?>";
}
protected function compileItems($expression) {
// we add a new attribute with the type of the current open tag
$r=end($this->htmlItem);
$x=trim($expression);
$x="('{$r}',".substr($x,1);
return $this->phpTag."echo \$this->items{$x}; ?>";
}
protected function compileTrio($expression) {
// we add a new attribute with the type of the current open tag
$r=end($this->htmlItem);
$x=trim($expression);
$x="('{$r}',".substr($x,1);
return $this->phpTag."echo \$this->trio{$x}; ?>";
}
protected function compileTrios($expression) {
// we add a new attribute with the type of the current open tag
$r=end($this->htmlItem);
$x=trim($expression);
$x="('{$r}',".substr($x,1);
return $this->phpTag."echo \$this->trios{$x}; ?>";
}
protected function compileInput($expression) {
return $this->phpTag."echo \$this->input{$expression}; ?>";
}
protected function compileFile($expression) {
return $this->phpTag."echo \$this->file{$expression}; ?>";
}
protected function compileImage($expression) {
return $this->phpTag."echo \$this->image{$expression}; ?>";
}
protected function compileTextArea($expression) {
return $this->phpTag."echo \$this->textArea{$expression}; ?>";
}
protected function compileHidden($expression) {
return $this->phpTag."echo \$this->hidden{$expression}; ?>";
}
protected function compileLabel($expression) {
return $this->phpTag."// {$expression} \n echo \$this->label{$expression}; ?>";
}
protected function compileCommandButton($expression) {
return $this->phpTag."echo \$this->commandButton{$expression}; ?>";
}
protected function compileForm($expression) {
return $this->phpTag."echo \$this->form{$expression}; ?>";
}
protected function compileEndForm() {
return $this->phpTag."echo '</form>'; ?>";
}
//</editor-fold>
//<editor-fold desc="used function">
public function select($name,$extra='') {
return "<select id='".static::e($name)."' name='".static::e($name)."' {$this->convertArg($extra)}>\n";
}
public function Link($url,$label,$extra='') {
return "<a href='{$url}' {$this->convertArg($extra)}>{$label}</a>";
}
/**
* Find an element in a array of arrays
* If the element doesn't exist in the array then it returns false, otherwise returns true
* @param $find
* @param array $array array of primitives or objects
* @param array $field field to search
* @return bool
*/
private function listboxesFindArray($find,$array,$field) {
if (count($array)==0) {
return false;
}
if (!is_array($array[0])) {
return in_array($find,$array);
} else {
foreach ($array as $elem) {
if ($elem[$field] == $find) {
return true;
}
}
}
return false;
}
public function listboxes($name,$allvalues, $fieldId, $fieldText,$selectedId,$extra='') {
$html="";
$html.= "<table>\n";
$html.= " <tr>\n";
$html.= " <td>\n";
$html.= " <select id='{$name}_noselected' size='6' multiple='multiple' $extra>\n";
if (count($allvalues)==0) {
$allvalues=[];
}
$html2="";
foreach($allvalues as $v) {
if (is_object($v)) {
$v=(array) $v;
}
if (!$this->listboxesFindArray($v[$fieldId],$selectedId,$fieldId)) {
$html .= "<option value='".$v[$fieldId]."'>".$v[$fieldText]."</option>\n";
} else {
$html2.= "<option value='".$v[$fieldId]."'>".$v[$fieldText]."</option>\n";
}
}
$html.= " </select>\n";
$html.= " </td>\n";
$html.= " <td style='text-align:center;'>\n";
$html.= " <input type='button' value='>' id='{$name}_add'/><br>\n";
$html.= " <input type='button' value='>>' id='{$name}_addall'/><br>\n";
$html.= " <input type='button' value='<' id='{$name}_delete'/><br>\n";
$html.= " <input type='button' value='<<' id='{$name}_deleteall'/><br>\n";
$html.= " </td>\n";
$html.= " <td>\n";
$html.= " <select id='{$name}' name='{$name}' size='6' multiple='multiple'>\n";
$html.= $html2;
$html.= " </select>\n";
$html.= " </td>\n";
$html.= " </tr>\n";
$html.= "</table>\n";
return $html;
}
public function selectGroup($name,$extra='') {
return $this->selectGroup($name,$extra);
}
public function radio($id,$value='',$text='',$valueSelected='',$extra='') {
$num=func_num_args();
if ($num>2) {
if ($value==$valueSelected) {
if (is_array($extra)) {
$extra['checked'] = 'checked';
} else {
$extra.=' checked="checked"';
}
}
return $this->input($id, $value, 'radio', $extra) . ' ' . $text;
} else {
array_push($this->htmlCurrentId,$id);
return '';
}
}
/**
* @param $id
* @param string $value
* @param string $text
* @param string|null $valueSelected
* @param string $extra
* @return string
*/
public function checkbox($id,$value='',$text='',$valueSelected='',$extra='') {
$num=func_num_args();
if ($num>2) {
if ($value==$valueSelected ) {
if (is_array($extra)) {
$extra['checked'] = 'checked';
} else {
$extra.=' checked="checked"';
}
}
return $this->input($id, $value, 'checkbox', $extra) . ' ' . $text;
} else {
array_push($this->htmlCurrentId,$id);
return '';
}
}
/**
* @param string $type type of the current open tag
* @param array|string $valueId if is an array then the first value is used as value, the second is used as extra
* @param $valueText
* @param array|string $selectedItem Item selected (optional)
* @param string $wrapper Wrapper of the element. For example, <li>%s</li>
* @param string $extra
* @return string
* @internal param string $fieldId Field of the id
* @internal param string $fieldText Field of the value visible
*/
public function item($type,$valueId, $valueText, $selectedItem='',$wrapper='', $extra='') {
$id=@end($this->htmlCurrentId);
$wrapper=($wrapper=='')?'%s':$wrapper;
if (is_array($selectedItem)) {
$found=in_array($valueId,$selectedItem);
} else {
$found = $valueId == $selectedItem;
}
$valueHtml= (!is_array($valueId))?"value='{$valueId}'":"value='{$valueId[0]}' data='{$valueId[1]}'";
switch ($type) {
case 'select':
$selected=($found)?'selected':'';
return sprintf($wrapper,"<option $valueHtml $selected ".
$this->convertArg($extra).">{$valueText}</option>\n");
break;
case 'radio':
$selected=($found)?'checked':'';
return sprintf($wrapper,"<input type='radio' id='".static::e($id)
."' name='".static::e($id)."' $valueHtml $selected "
.$this->convertArg($extra)."> {$valueText}\n");
break;
case 'checkbox':
$selected=($found)?'checked':'';
return sprintf($wrapper,"<input type='checkbox' id='".static::e($id)
."' name='".static::e($id)."' $valueHtml $selected "
.$this->convertArg($extra)."> {$valueText}\n");
break;
default:
return '???? type undefined: [$type] on @item<br>';
}
}
/**
* @param string $type type of the current open tag
* @param array $arrValues Array of objects/arrays to show.
* @param string $fieldId Field of the id (for arrValues)
* @param string $fieldText Field of the id of selectedItem
* @param array|string $selectedItem Item selected (optional)
* @param string $selectedFieldId field of the selected item.
* @param string $wrapper Wrapper of the element. For example, <li>%s</li>
* @param string $extra (optional) is used for add additional information for the html object (such as class)
* @return string
* @version 1.1 2017
*/
public function items($type, $arrValues, $fieldId, $fieldText, $selectedItem='',$selectedFieldId='', $wrapper='', $extra='') {
if (count($arrValues)==0) {
return "";
}
if (is_object(@$arrValues[0])) {
$arrValues=(array) $arrValues;
}
if (is_array($selectedItem)) {
if (is_object(@$selectedItem[0])) {
$primitiveArray=[];
foreach($selectedItem as $v) {
$primitiveArray[]=$v->{$selectedFieldId};
}
$selectedItem=$primitiveArray;
}
}
$result='';
if (is_object($selectedItem)) {
$selectedItem=(array)$selectedItem;
}
foreach($arrValues as $v) {
if (is_object($v)) {
$v=(array)$v;
}
$result.=$this->item($type,$v[$fieldId],$v[$fieldText],$selectedItem,$wrapper,$extra);
}
return $result;
}
/**
* @param string $type type of the current open tag
* @param string $valueId value of the trio
* @param string $valueText visible value of the trio.
* @param string $value3 extra third value for select value or visual
* @param array|string $selectedItem Item selected (optional)
* @param string $wrapper Wrapper of the element. For example, <li>%s</li>
* @param string $extra
* @return string
* @internal param string $fieldId Field of the id
* @internal param string $fieldText Field of the value visible
*/
public function trio($type,$valueId, $valueText,$value3='', $selectedItem='',$wrapper='', $extra='') {
$id=@end($this->htmlCurrentId);
$wrapper=($wrapper=='')?'%s':$wrapper;
if (is_array($selectedItem)) {
$found=in_array($valueId,$selectedItem);
} else {
$found = $valueId == $selectedItem;
}
switch ($type) {
case 'selectgroup':
$selected=($found)?'selected':'';
return sprintf($wrapper,"<option value='{$valueId}' $selected ".
$this->convertArg($extra).">{$valueText}</option>\n");
break;
default:
return '???? type undefined: [$type] on @item<br>';
}
}
/**
* @param string $type type of the current open tag
* @param array $arrValues Array of objects/arrays to show.
* @param string $fieldId Field of the id
* @param string $fieldText Field of the value visible
* @param string $fieldThird
* @param array|string $selectedItem Item selected (optional)
* @param string $wrapper Wrapper of the element. For example, <li>%s</li>
* @param string $extra (optional) is used for add additional information for the html object (such as class)
* @return string
* @version 1.0
*/
public function trios($type, $arrValues, $fieldId, $fieldText,$fieldThird, $selectedItem='', $wrapper='', $extra='') {
if (count($arrValues)==0) {
return "";
}
if (is_object($arrValues[0])) {
$arrValues=(array) $arrValues;
}
$result='';
$oldV3="";
foreach($arrValues as $v) {
if (is_object($v)) {
$v=(array)$v;
}
$v3=$v[$fieldThird];
if ($type=='selectgroup') {
if ($v3!=$oldV3) {
if ($oldV3!="") {
$result.="</optgroup>";
}
$oldV3=$v3;
$result.="<optgroup label='{$v3}'>";
}
}
if ($result) {
$result .= $this->trio($type, $v[$fieldId], $v[$fieldText], $v3, $selectedItem, $wrapper, $extra);
}
}
if ($type=='selectgroup' && $oldV3!="") {
$result.="</optgroup>";
}
return $result;
}
public function input($id,$value='',$type='text',$extra='')
{
return "<input id='".static::e($id)."' name='".static::e($id)."' type='".$type."' ".$this->convertArg($extra)." value='".static::e($value)."' />\n";
}
public function file($id,$fullfilepath='',$file='',$extra='')
{
return "<a href='$fullfilepath'>$file</a>
<input id='".static::e($id)."_file' name='".static::e($id)."_file' type='hidden' value='".static::e($file)."' />
<input id='".static::e($id)."' name='".static::e($id)."' type='file' ".$this->convertArg($extra)." value='".static::e($fullfilepath)."' />\n";
}
public function textArea($id,$value='',$extra='')
{
$value=str_replace('\n',"\n",$value);
return "<textarea id='".static::e($id)."' name='".static::e($id)."' ".$this->convertArg($extra)." >$value</textarea>\n";
}
public function hidden($id,$value='',$extra='')
{
return $this->input($id,$value,'hidden',$extra);
}
public function label($id,$value='',$extra='')
{
return "<label for='{$id}' {$this->convertArg($extra)}>{$value}</label>";
}
public function commandButton($id,$value='',$text='Button',$type='submit',$extra='')
{
return "<button type='{$type}' id='".static::e($id)."' name='".static::e($id)."' value='".static::e($value)."' {$this->convertArg($extra)}>{$text}</button>\n";
}
public function form($action,$method='post',$extra='') {
return "<form $action='{$action}' method='{$method}' {$this->convertArg($extra)}>";
}
//</editor-fold>
}