1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ffcms\Core\Helper\HTML\Form; |
4
|
|
|
|
5
|
|
|
use Ffcms\Core\App; |
6
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
7
|
|
|
use Ffcms\Core\Exception\SyntaxException; |
8
|
|
|
use Ffcms\Core\Helper\Type\Str; |
9
|
|
|
|
10
|
|
|
class Constructor |
11
|
|
|
{ |
12
|
|
|
const TYPE_TEXT = 'text'; |
13
|
|
|
const TYPE_PASSWORD = 'password'; |
14
|
|
|
const TYPE_EMAIL = 'email'; |
15
|
|
|
const TYPE_CHECKBOX = 'checkbox'; |
16
|
|
|
const TYPE_SELECT = 'select'; |
17
|
|
|
const TYPE_MULTISELECT = 'multiselect'; |
18
|
|
|
const TYPE_TEXTAREA = 'textarea'; |
19
|
|
|
const TYPE_MULTI_CHECKBOXES = 'checkboxes'; |
20
|
|
|
const TYPE_CAPTCHA = 'captcha'; |
21
|
|
|
const TYPE_FILE = 'file'; |
22
|
|
|
const TYPE_HIDDEN = 'hidden'; |
23
|
|
|
const TYPE_DIV_FAKE = 'div'; |
24
|
|
|
const TYPE_RADIO = 'radio'; |
25
|
|
|
|
26
|
|
|
/** @var \Ffcms\Core\Arch\Model $model */ |
27
|
|
|
private $model; |
28
|
|
|
private $formName; |
29
|
|
|
private $type; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Initialize Constructor. Pass model and type inside of current field inside. |
33
|
|
|
* @param \Ffcms\Core\Arch\Model $model |
34
|
|
|
* @param string $type |
35
|
|
|
*/ |
36
|
|
|
public function __construct($model, $formName = false, $type = 'text') |
37
|
|
|
{ |
38
|
|
|
$this->model = $model; |
39
|
|
|
$this->formName = $formName; |
40
|
|
|
$this->type = $type; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
public function makeTag($name, $value = null, $properties = null) |
45
|
|
|
{ |
46
|
|
|
// check if properties is passed well |
47
|
|
|
if ($properties !== null && !Obj::isArray($properties)) { |
48
|
|
|
throw new SyntaxException('Property must be passed as array or null! Field: ' . $name); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// add properties to autovalidation by js (properties passed by ref) |
52
|
|
|
$this->validatorProperties($name, $properties); |
53
|
|
|
// prepare properties name and id to autobuild |
54
|
|
|
$this->globalProperties($name, $value, $properties); |
55
|
|
|
|
56
|
|
|
// initialize build model depend of current type |
57
|
|
|
switch ($this->type) { |
58
|
|
|
case static::TYPE_TEXT: // for <input type="text"> |
59
|
|
|
$builder = new TextField($properties, $name); |
60
|
|
|
return $builder->make(); |
61
|
|
|
case static::TYPE_CHECKBOX: |
62
|
|
|
$builder = new CheckboxField($properties, $name, $value); |
63
|
|
|
return $builder->make(); |
64
|
|
|
case static::TYPE_PASSWORD: |
65
|
|
|
$builder = new PasswordField($properties, $name); |
66
|
|
|
return $builder->make(); |
67
|
|
|
case static::TYPE_EMAIL: |
68
|
|
|
$builder = new EmailField($properties, $name); |
69
|
|
|
return $builder->make(); |
70
|
|
|
case static::TYPE_SELECT: |
71
|
|
|
$builder = new SelectField($properties, $name, $value); |
72
|
|
|
return $builder->make(); |
73
|
|
|
case static::TYPE_TEXTAREA: |
74
|
|
|
$builder = new TextareaField($properties, $name, $value); |
75
|
|
|
return $builder->make(); |
76
|
|
|
case static::TYPE_MULTI_CHECKBOXES: |
77
|
|
|
$builder = new MultiCheckboxField($properties, $name, $value); |
78
|
|
|
return $builder->make(); |
79
|
|
|
case static::TYPE_CAPTCHA: |
80
|
|
|
$builder = new CaptchaField($properties, $name); |
81
|
|
|
return $builder->make(); |
82
|
|
|
case static::TYPE_FILE: |
83
|
|
|
$builder = new FileField($properties, $name); |
84
|
|
|
return $builder->make(); |
85
|
|
|
case static::TYPE_HIDDEN: |
86
|
|
|
$builder = new HiddenField($properties, $name, $value); |
87
|
|
|
return $builder->make(); |
88
|
|
|
case static::TYPE_DIV_FAKE: |
89
|
|
|
$builder = new DivFakeField($properties, $name, $value); |
90
|
|
|
return $builder->make(); |
91
|
|
|
case static::TYPE_MULTISELECT: |
92
|
|
|
$builder = new MultiSelectField($properties, $name, $value); |
93
|
|
|
return $builder->make(); |
94
|
|
|
case static::TYPE_RADIO: |
95
|
|
|
$builder = new RadioField($properties, $name, $value); |
96
|
|
|
return $builder->make(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// if field is unknown type add notification in debugbar |
100
|
|
View Code Duplication |
if (App::$Debug !== null) { |
|
|
|
|
101
|
|
|
App::$Debug->addMessage('Field with name [' . App::$Security->strip_tags($name) . '] have unknown type [' . $this->type . ']', 'error'); |
102
|
|
|
} |
103
|
|
|
return 'No data: ' . App::$Security->strip_tags($name); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Set validator options to current properties |
108
|
|
|
* @param string $name |
109
|
|
|
* @param array $properties |
110
|
|
|
*/ |
111
|
|
|
private function validatorProperties($name, &$properties) |
112
|
|
|
{ |
113
|
|
|
// jquery validation quick-build some rules |
114
|
|
|
$rules = $this->model->getValidationRule($name); |
115
|
|
|
if (count($rules) > 0) { |
116
|
|
|
foreach ($rules as $rule_name => $rule_value) { |
117
|
|
|
switch ($rule_name) { |
118
|
|
|
case 'required': |
119
|
|
|
$properties['required'] = null; |
120
|
|
|
break; |
121
|
|
|
case 'length_min': |
122
|
|
|
$properties['minlength'] = $rule_value; |
123
|
|
|
break; |
124
|
|
|
case 'length_max': |
125
|
|
|
$properties['maxlength'] = $rule_value; |
126
|
|
|
break; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Prepare field global properties - name, id and value |
134
|
|
|
* @param string $name |
135
|
|
|
* @param string|null $value |
136
|
|
|
* @param array $properties |
137
|
|
|
*/ |
138
|
|
|
private function globalProperties($name, $value = null, &$properties) |
139
|
|
|
{ |
140
|
|
|
// standard property data definition |
141
|
|
|
$properties['name'] = $properties['id'] = $this->formName; // form global name |
142
|
|
|
if ($value !== null && !Str::likeEmpty($value)) { |
143
|
|
|
$properties['value'] = $value; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// sounds like a array-path based obj name |
147
|
|
|
if (Str::contains('.', $name)) { |
148
|
|
|
$splitedName = explode('.', $name); |
149
|
|
|
foreach ($splitedName as $nameKey) { |
150
|
|
|
$properties['name'] .= '[' . $nameKey . ']'; |
151
|
|
|
$properties['id'] .= '-' . $nameKey; |
152
|
|
|
} |
153
|
|
|
} else { // standard property definition - add field name |
154
|
|
|
$properties['name'] .= '[' . $name . ']'; |
155
|
|
|
$properties['id'] .= '-' . $name; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.