|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* 2017 Romain CANON <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the TYPO3 Formz project. |
|
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
|
7
|
|
|
* under the terms of the GNU General Public License, either |
|
8
|
|
|
* version 3 of the License, or any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, see: |
|
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Romm\Formz\Condition\Items; |
|
15
|
|
|
|
|
16
|
|
|
use Romm\ConfigurationObject\Traits\ConfigurationObject\MagicMethodsTrait; |
|
17
|
|
|
use Romm\Formz\Condition\Exceptions\InvalidConditionException; |
|
18
|
|
|
use Romm\Formz\Condition\Parser\Node\ConditionNode; |
|
19
|
|
|
use Romm\Formz\Configuration\Form\Condition\Activation\ActivationInterface; |
|
20
|
|
|
use Romm\Formz\Configuration\Form\Field\Field; |
|
21
|
|
|
use Romm\Formz\Configuration\Form\Field\Validation\Validation; |
|
22
|
|
|
use Romm\Formz\Form\FormObject; |
|
23
|
|
|
use Romm\Formz\Service\ArrayService; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* This class must be extended by every registered condition item. When it is |
|
27
|
|
|
* registered, a condition can then be used in the TypoScript configuration for |
|
28
|
|
|
* fields/validation activation rules. |
|
29
|
|
|
* |
|
30
|
|
|
* When you want to create a new condition item, first register it by using the |
|
31
|
|
|
* function `ConditionFactory::registerConditionType()` inside your |
|
32
|
|
|
* `ext_localconf.php`. Then, you have to implement the three abstract functions |
|
33
|
|
|
* of this class: |
|
34
|
|
|
* - `getCssResult()` |
|
35
|
|
|
* - `getJavaScriptResult()` |
|
36
|
|
|
* - `getPhpResult()` |
|
37
|
|
|
* |
|
38
|
|
|
* These functions must translate the "meaning" of this condition to the three |
|
39
|
|
|
* context: CSS, JavaScript and PHP. |
|
40
|
|
|
* |
|
41
|
|
|
* If you need more explanation about how this class works, please refer to the |
|
42
|
|
|
* documentation. |
|
43
|
|
|
* |
|
44
|
|
|
* @see \Romm\Formz\Condition\ConditionFactory |
|
45
|
|
|
* @see \Romm\Formz\Condition\Items\FieldHasValueCondition |
|
46
|
|
|
* @see \Romm\Formz\Condition\Items\FieldHasErrorCondition |
|
47
|
|
|
* @see \Romm\Formz\Condition\Items\FieldIsValidCondition |
|
48
|
|
|
*/ |
|
49
|
|
|
abstract class AbstractConditionItem implements ConditionItemInterface |
|
50
|
|
|
{ |
|
51
|
|
|
use MagicMethodsTrait; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Contains a list of JavaScript files which will be included whenever this |
|
55
|
|
|
* condition is used. |
|
56
|
|
|
* |
|
57
|
|
|
* Example: |
|
58
|
|
|
* protected static $javaScriptFiles = [ |
|
59
|
|
|
* 'EXT:formz/Resources/Public/JavaScript/Conditions/Formz.Condition.FieldHasValue.js' |
|
60
|
|
|
* ]; |
|
61
|
|
|
* |
|
62
|
|
|
* @var array |
|
63
|
|
|
*/ |
|
64
|
|
|
protected static $javaScriptFiles = []; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var FormObject |
|
68
|
|
|
* @disableMagicMethods |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $formObject; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @var ActivationInterface |
|
74
|
|
|
* @disableMagicMethods |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $activation; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @var ConditionNode |
|
80
|
|
|
* @disableMagicMethods |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $conditionNode; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param FormObject $formObject |
|
86
|
|
|
*/ |
|
87
|
|
|
public function attachFormObject(FormObject $formObject) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->formObject = $formObject; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param ActivationInterface $activation |
|
94
|
|
|
*/ |
|
95
|
|
|
public function attachActivation(ActivationInterface $activation) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->activation = $activation; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param ConditionNode $conditionNode |
|
102
|
|
|
*/ |
|
103
|
|
|
public function attachConditionNode(ConditionNode $conditionNode) |
|
104
|
|
|
{ |
|
105
|
|
|
$this->conditionNode = $conditionNode; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Returns a generic JavaScript code which uses Formz API to validate a |
|
110
|
|
|
* condition which was registered in a single JavaScript file (which is |
|
111
|
|
|
* filled in the `$javaScriptFiles` attribute of the PHP condition class). |
|
112
|
|
|
* |
|
113
|
|
|
* @param array $data |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function getDefaultJavaScriptCall(array $data) |
|
117
|
|
|
{ |
|
118
|
|
|
$conditionName = addslashes(get_class($this)); |
|
119
|
|
|
$data = ArrayService::get()->arrayToJavaScriptJson($data); |
|
120
|
|
|
|
|
121
|
|
|
return <<<JS |
|
122
|
|
|
Formz.Condition.validateCondition('$conditionName', form, $data) |
|
123
|
|
|
JS; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return array |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getJavaScriptFiles() |
|
130
|
|
|
{ |
|
131
|
|
|
return static::$javaScriptFiles; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Will launch the condition validation: the child class should implement |
|
136
|
|
|
* the function `checkConditionConfiguration()` and check if the condition |
|
137
|
|
|
* can be considered as valid. |
|
138
|
|
|
* |
|
139
|
|
|
* If any syntax/configuration error is found, an exception of type |
|
140
|
|
|
* `InvalidConditionException` must be thrown. |
|
141
|
|
|
* |
|
142
|
|
|
* @throws InvalidConditionException |
|
143
|
|
|
* @return bool |
|
144
|
|
|
*/ |
|
145
|
|
|
final public function validateConditionConfiguration() |
|
146
|
|
|
{ |
|
147
|
|
|
try { |
|
148
|
|
|
$this->checkConditionConfiguration(); |
|
149
|
|
|
} catch (InvalidConditionException $exception) { |
|
150
|
|
|
/** @var Field|Validation $rootObject */ |
|
151
|
|
|
$rootObject = $this->activation->getRootObject(); |
|
152
|
|
|
|
|
153
|
|
|
throw new InvalidConditionException( |
|
154
|
|
|
vsprintf( |
|
155
|
|
|
'Invalid configuration for the condition "%s", used on %s of the form "%s"; error message is « %s ».', |
|
156
|
|
|
[ |
|
157
|
|
|
$this->conditionNode->getConditionName(), |
|
158
|
|
|
($rootObject instanceof Field) |
|
159
|
|
|
? 'the field "' . $rootObject->getFieldName() . '"' |
|
160
|
|
|
: 'the validation "' . $rootObject->getValidationName() . '" of the field "' . $rootObject->getParentField()->getFieldName() . '"', |
|
161
|
|
|
$this->formObject->getClassName(), |
|
162
|
|
|
$exception->getMessage() |
|
163
|
|
|
] |
|
164
|
|
|
), |
|
165
|
|
|
$exception->getCode() |
|
166
|
|
|
); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return true; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @see validateConditionConfiguration() |
|
174
|
|
|
* @throws InvalidConditionException |
|
175
|
|
|
* @return bool |
|
176
|
|
|
*/ |
|
177
|
|
|
abstract protected function checkConditionConfiguration(); |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @return array |
|
181
|
|
|
*/ |
|
182
|
|
|
public function __sleep() |
|
183
|
|
|
{ |
|
184
|
|
|
$properties = get_object_vars($this); |
|
185
|
|
|
unset($properties['formObject']); |
|
186
|
|
|
unset($properties['activation']); |
|
187
|
|
|
unset($properties['conditionNode']); |
|
188
|
|
|
|
|
189
|
|
|
return array_keys($properties); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|