-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonForm.php
More file actions
184 lines (144 loc) · 5.84 KB
/
Copy pathJsonForm.php
File metadata and controls
184 lines (144 loc) · 5.84 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
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/site/blob/master/LICENSE
*/
namespace Quid\Site\Col;
use Quid\Base;
use Quid\Core;
use Quid\Lemur;
use Quid\Site;
// jsonForm
// class for a column containing a json form
class JsonForm extends Lemur\Col\JsonModelAlias
{
// config
protected static array $config = [
'complex'=>'json-form',
'cell'=>Site\Cell\JsonForm::class,
'formFields'=>['label','description','required','minLength','maxLength','choices'],
'typesGroup'=>[
'all'=>['inputText','textarea','select','radio','checkbox','inputFile','separator']],
'fieldsType'=>[
'label'=>['inputText','textarea','select','radio','checkbox','inputFile','separator'],
'description'=>['inputText','textarea','select','radio','checkbox','inputFile','separator'],
'required'=>['inputText','textarea','select','radio','checkbox','inputFile'],
'minLength'=>['textarea'],
'maxLength'=>['textarea'],
'choices'=>['select','radio','checkbox']]
];
// onPrepare
// arrange le tableau pour les méthode onGet et onSet
final protected function onPrepare(array $value):?array
{
$return = [];
foreach ($value as $k => $v)
{
if(is_int($k) && Base\Arr::keysExists(['type','label'],$v) && !Base\Vari::isReallyEmpty($v['label']) && !empty($v['type']))
{
$keep = true;
$v['required'] = (!empty($v['required']));
$v['minLength'] = (!empty($v['minLength']) && is_int($v['minLength']))? $v['minLength']:null;
$v['maxLength'] = (!empty($v['maxLength']) && is_int($v['maxLength']))? $v['maxLength']:null;
$choiceInput = static::isChoicesInput($v['type']);
if($choiceInput === true)
{
if(!empty($v['choices']) && is_string($v['choices']))
$v['choices'] = Base\Str::lines($v['choices']);
if(empty($v['choices']) || !is_array($v['choices']))
$keep = false;
}
else
$v['choices'] = null;
if($keep === true)
{
$v = Base\Arr::reallyEmptyToNull($v);
$return[] = Base\Arrs::trim($v);
}
}
}
return $return ?: null;
}
// onModel
// génère le formulaire du modèle
final protected function onModel($value,array $attr,?Core\Cell $cell,array $option):string
{
$r = '';
$lang = $this->db()->lang();
$val = $value['type'] ?? null;
$opt = Base\Arr::plus($option,['value'=>$val]);
$label = $lang->text(['jsonForm','type']);
$r .= static::makeModelFormElement('type','select',$label,true,static::getTypes(),$attr,$opt);
$label = $lang->text(['jsonForm','label']);
$r .= static::makeModelFormElement('label','inputText',$label,true,$value['label'] ?? null,$attr,$option);
$label = $lang->text(['jsonForm','description']);
$r .= static::makeModelFormElement('description','textarea',$label,false,$value['description'] ?? null,$attr,$option);
$val = (!empty($value['required']))? 1:0;
$opt = Base\Arr::plus($option,['value'=>$val]);
$label = $lang->text(['jsonForm','required']);
$r .= static::makeModelFormElement('required','select',$label,false,$lang->relation('bool'),$attr,$opt);
$label = $lang->text(['jsonForm','minLength']);
$r .= static::makeModelFormElement('minLength','inputDecimal',$label,false,$value['minLength'] ?? null,$attr,$option);
$label = $lang->text(['jsonForm','maxLength']);
$r .= static::makeModelFormElement('maxLength','inputDecimal',$label,false,$value['maxLength'] ?? null,$attr,$option);
$val = $value['choices'] ?? null;
$val = (is_array($val))? Base\Str::lineImplode($val):$val;
$label = $lang->text(['jsonForm','choices']);
$r .= static::makeModelFormElement('choices','textarea',$label,true,$val,$attr,$option);
return $r;
}
// getSpecificComponentAttr
// retourne les attr pour le specific component de jsonForm
public function getSpecificComponentAttr(array $return):array
{
$return = parent::getSpecificComponentAttr($return);
$return['data-form'] = static::getFormDataAttr();
return $return;
}
// getFormDataAttr
// retourne les data attr pour form
protected static function getFormDataAttr():array
{
$return = [];
foreach (static::$config['formFields'] as $v)
{
$return[$v] = static::getFieldWith($v);
}
return $return;
}
// isChoicesInput
// retourne vrai si le input en est un avec des choix
final public static function isChoicesInput($value):bool
{
return is_string($value) && in_array($value,static::getFieldWith('choices'),true);
}
// getTypes
// retourne les types pour le jsonForm
final public static function getTypes():array
{
$return = [];
$lang = static::lang();
foreach (static::$config['typesGroup']['all'] as $v)
{
$return[$v] = $lang->relation(['jsonForm',$v],null,false);
}
return $return;
}
// getFieldWith
// retourne les inputs avec
final public static function getFieldWith(string $type):array
{
return static::$config['fieldsType'][$type];
}
// isComplexTag
// retourne vrai si la tag est pour le formulaire complexe
protected static function isComplexTag(string $value):bool
{
return $value === 'json-form';
}
}
// init
JsonForm::__init();
?>