1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ffcms\Core\Helper\HTML\Form; |
4
|
|
|
|
5
|
|
|
use Ffcms\Core\App; |
6
|
|
|
use Ffcms\Core\Arch\Model; |
7
|
|
|
use Ffcms\Core\Exception\SyntaxException; |
8
|
|
|
use Ffcms\Core\Helper\Type\Any; |
9
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
10
|
|
|
use Ffcms\Core\Helper\Type\Str; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Constructor. Form field construction manager. |
14
|
|
|
* @package Ffcms\Core\Helper\HTML\Form |
15
|
|
|
*/ |
16
|
|
|
class Constructor |
17
|
|
|
{ |
18
|
|
|
const TYPE_TEXT = 'text'; |
19
|
|
|
const TYPE_PASSWORD = 'password'; |
20
|
|
|
const TYPE_EMAIL = 'email'; |
21
|
|
|
const TYPE_CHECKBOX = 'checkbox'; |
22
|
|
|
const TYPE_SELECT = 'select'; |
23
|
|
|
const TYPE_MULTISELECT = 'multiselect'; |
24
|
|
|
const TYPE_TEXTAREA = 'textarea'; |
25
|
|
|
const TYPE_MULTI_CHECKBOXES = 'checkboxes'; |
26
|
|
|
const TYPE_CAPTCHA = 'captcha'; |
27
|
|
|
const TYPE_FILE = 'file'; |
28
|
|
|
const TYPE_HIDDEN = 'hidden'; |
29
|
|
|
const TYPE_DIV_FAKE = 'div'; |
30
|
|
|
const TYPE_RADIO = 'radio'; |
31
|
|
|
|
32
|
|
|
/** @var \Ffcms\Core\Arch\Model $model */ |
33
|
|
|
private $model; |
34
|
|
|
private $formName; |
35
|
|
|
private $type; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Initialize Constructor. Pass model and type inside of current field inside. |
39
|
|
|
* @param \Ffcms\Core\Arch\Model $model |
40
|
|
|
* @param string|null $formName |
41
|
|
|
* @param string $type |
42
|
|
|
*/ |
43
|
|
|
public function __construct(Model $model, ?string $formName = null, ?string $type = 'text') |
44
|
|
|
{ |
45
|
|
|
$this->model = $model; |
46
|
|
|
$this->formName = $formName; |
47
|
|
|
$this->type = $type; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string|array $name |
52
|
|
|
* @param string|array|null $value |
53
|
|
|
* @param array|null $properties |
54
|
|
|
* @return null|string |
55
|
|
|
*/ |
56
|
|
|
public function makeTag($name, $value = null, ?array $properties = null): ?string |
57
|
|
|
{ |
58
|
|
|
// check if properties is passed well |
59
|
|
|
if ($properties !== null && !Any::isArray($properties)) { |
60
|
|
|
return null; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// add properties to autovalidation by js (properties passed by ref) |
64
|
|
|
$this->addValidationProperties($name, $properties); |
|
|
|
|
65
|
|
|
// set default form data as properties: name="", value="", id="" based tag info |
66
|
|
|
$this->setDefaultProperties($name, $value, $properties); |
|
|
|
|
67
|
|
|
|
68
|
|
|
// initialize build model depend of current type |
69
|
|
|
switch ($this->type) { |
70
|
|
|
case static::TYPE_TEXT: // for <input type="text"> |
71
|
|
|
$builder = new TextField($properties, $name); |
|
|
|
|
72
|
|
|
break; |
73
|
|
|
case static::TYPE_CHECKBOX: |
74
|
|
|
$builder = new CheckboxField($properties, $name, $value); |
|
|
|
|
75
|
|
|
break; |
76
|
|
|
case static::TYPE_PASSWORD: |
77
|
|
|
$builder = new PasswordField($properties, $name); |
|
|
|
|
78
|
|
|
break; |
79
|
|
|
case static::TYPE_EMAIL: |
80
|
|
|
$builder = new EmailField($properties, $name); |
|
|
|
|
81
|
|
|
break; |
82
|
|
|
case static::TYPE_SELECT: |
83
|
|
|
$builder = new SelectField($properties, $name, $value); |
|
|
|
|
84
|
|
|
break; |
85
|
|
|
case static::TYPE_TEXTAREA: |
86
|
|
|
$builder = new TextareaField($properties, $name, $value); |
|
|
|
|
87
|
|
|
break; |
88
|
|
|
case static::TYPE_MULTI_CHECKBOXES: |
89
|
|
|
$builder = new MultiCheckboxField($properties, $name, $value); |
|
|
|
|
90
|
|
|
break; |
91
|
|
|
case static::TYPE_CAPTCHA: |
92
|
|
|
$builder = new CaptchaField($properties, $name); |
|
|
|
|
93
|
|
|
break; |
94
|
|
|
case static::TYPE_FILE: |
95
|
|
|
$builder = new FileField($properties, $name); |
|
|
|
|
96
|
|
|
break; |
97
|
|
|
case static::TYPE_HIDDEN: |
98
|
|
|
$builder = new HiddenField($properties, $name, $value); |
|
|
|
|
99
|
|
|
break; |
100
|
|
|
case static::TYPE_DIV_FAKE: |
101
|
|
|
$builder = new DivFakeField($properties, $name, $value); |
|
|
|
|
102
|
|
|
break; |
103
|
|
|
case static::TYPE_MULTISELECT: |
104
|
|
|
$builder = new MultiSelectField($properties, $name, $value); |
|
|
|
|
105
|
|
|
break; |
106
|
|
|
case static::TYPE_RADIO: |
107
|
|
|
$builder = new RadioField($properties, $name, $value); |
|
|
|
|
108
|
|
|
break; |
109
|
|
|
default: |
110
|
|
|
if (App::$Debug) { |
111
|
|
|
App::$Debug->addMessage('Field has unknown type: ' . App::$Security->strip_tags($name)); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $builder->make(); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set validator options to current properties |
120
|
|
|
* @param string $name |
121
|
|
|
* @param array|null $properties |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
|
|
private function addValidationProperties(string $name, ?array &$properties = null): void |
125
|
|
|
{ |
126
|
|
|
// jquery validation quick-build some rules |
127
|
|
|
$rules = $this->model->getValidationRule($name); |
128
|
|
|
if (count($rules) > 0) { |
129
|
|
|
foreach ($rules as $type => $value) { |
130
|
|
|
switch ($type) { |
131
|
|
|
case 'required': |
132
|
|
|
$properties['required'] = null; |
133
|
|
|
break; |
134
|
|
|
case 'length_min': |
135
|
|
|
$properties['minlength'] = $value; |
136
|
|
|
break; |
137
|
|
|
case 'length_max': |
138
|
|
|
$properties['maxlength'] = $value; |
139
|
|
|
break; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Prepare field global properties - name, id and value |
147
|
|
|
* @param string $name |
148
|
|
|
* @param string|null $value |
149
|
|
|
* @param array|null $properties |
150
|
|
|
*/ |
151
|
|
|
private function setDefaultProperties(string $name, $value = null, array &$properties = null): void |
152
|
|
|
{ |
153
|
|
|
// standard property data definition |
154
|
|
|
$properties['name'] = $properties['id'] = $this->formName; // form global name |
155
|
|
|
if ($value !== null && !Any::isEmpty($value)) { |
156
|
|
|
$properties['value'] = $value; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// sounds like a array-path based obj name |
160
|
|
|
if (Str::contains('.', $name)) { |
161
|
|
|
$splitedName = explode('.', $name); |
162
|
|
|
foreach ($splitedName as $nameKey) { |
163
|
|
|
$properties['name'] .= '[' . $nameKey . ']'; |
164
|
|
|
$properties['id'] .= '-' . $nameKey; |
165
|
|
|
} |
166
|
|
|
} else { // standard property definition - add field name |
167
|
|
|
$properties['name'] .= '[' . $name . ']'; |
168
|
|
|
$properties['id'] .= '-' . $name; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|