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\Formz\Condition\Exceptions\InvalidConditionException; |
17
|
|
|
use Romm\Formz\Condition\Parser\Node\ConditionNode; |
18
|
|
|
use Romm\Formz\Form\Definition\AbstractFormDefinition; |
19
|
|
|
use Romm\Formz\Form\Definition\Field\Activation\ActivationInterface; |
20
|
|
|
use Romm\Formz\Form\Definition\FormDefinition; |
21
|
|
|
use Romm\Formz\Form\FormObject\FormObject; |
22
|
|
|
use Romm\Formz\Service\ArrayService; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* This class must be extended by every registered condition item. When it is |
26
|
|
|
* registered, a condition can then be used in the TypoScript configuration for |
27
|
|
|
* fields/validation activation rules. |
28
|
|
|
* |
29
|
|
|
* When you want to create a new condition item, first register it by using the |
30
|
|
|
* function `ConditionFactory::registerConditionType()` inside your |
31
|
|
|
* `ext_localconf.php`. Then, you have to implement the three abstract functions |
32
|
|
|
* of this class: |
33
|
|
|
* - `getCssResult()` |
34
|
|
|
* - `getJavaScriptResult()` |
35
|
|
|
* - `getPhpResult()` |
36
|
|
|
* |
37
|
|
|
* These functions must translate the "meaning" of this condition to the three |
38
|
|
|
* context: CSS, JavaScript and PHP. |
39
|
|
|
* |
40
|
|
|
* If you need more explanation about how this class works, please refer to the |
41
|
|
|
* documentation. |
42
|
|
|
* |
43
|
|
|
* @see \Romm\Formz\Condition\ConditionFactory |
44
|
|
|
* @see \Romm\Formz\Condition\Items\FieldHasValueCondition |
45
|
|
|
* @see \Romm\Formz\Condition\Items\FieldHasErrorCondition |
46
|
|
|
* @see \Romm\Formz\Condition\Items\FieldIsValidCondition |
47
|
|
|
*/ |
48
|
|
|
abstract class AbstractConditionItem extends AbstractFormDefinition implements ConditionItemInterface |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* Contains a list of JavaScript files which will be included whenever this |
52
|
|
|
* condition is used. |
53
|
|
|
* |
54
|
|
|
* Example: |
55
|
|
|
* protected static $javaScriptFiles = [ |
56
|
|
|
* 'EXT:formz/Resources/Public/JavaScript/Conditions/Formz.Condition.FieldHasValue.js' |
57
|
|
|
* ]; |
58
|
|
|
* |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
protected static $javaScriptFiles = []; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var FormObject |
65
|
|
|
* @disableMagicMethods |
66
|
|
|
*/ |
67
|
|
|
protected $formObject; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var ActivationInterface |
71
|
|
|
* @disableMagicMethods |
72
|
|
|
*/ |
73
|
|
|
protected $activation; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var ConditionNode |
77
|
|
|
* @disableMagicMethods |
78
|
|
|
*/ |
79
|
|
|
protected $conditionNode; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var bool|InvalidConditionException |
83
|
|
|
*/ |
84
|
|
|
private $configurationValidationResult; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Will launch the condition validation: the child class must implement |
88
|
|
|
* the function `checkConditionConfiguration()` and check if the condition |
89
|
|
|
* can be considered as valid. |
90
|
|
|
* |
91
|
|
|
* @param FormDefinition $formDefinition |
92
|
|
|
* @throws InvalidConditionException |
93
|
|
|
*/ |
94
|
|
|
final public function validateConditionConfiguration(FormDefinition $formDefinition) |
95
|
|
|
{ |
96
|
|
|
if (null === $this->configurationValidationResult) { |
97
|
|
|
$this->configurationValidationResult = true; |
98
|
|
|
|
99
|
|
|
try { |
100
|
|
|
$this->checkConditionConfiguration($formDefinition); |
101
|
|
|
} catch (InvalidConditionException $exception) { |
102
|
|
|
$this->configurationValidationResult = $exception; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ($this->configurationValidationResult instanceof InvalidConditionException) { |
107
|
|
|
throw $this->configurationValidationResult; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Checks the condition configuration/options. |
113
|
|
|
* |
114
|
|
|
* If any syntax/configuration error is found, an exception of type |
115
|
|
|
* `InvalidConditionException` must be thrown. |
116
|
|
|
* |
117
|
|
|
* @param FormDefinition $formDefinition |
118
|
|
|
* @throws InvalidConditionException |
119
|
|
|
*/ |
120
|
|
|
abstract protected function checkConditionConfiguration(FormDefinition $formDefinition); |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns a generic JavaScript code which uses FormZ API to validate a |
124
|
|
|
* condition which was registered in a single JavaScript file (which is |
125
|
|
|
* filled in the `$javaScriptFiles` attribute of the PHP condition class). |
126
|
|
|
* |
127
|
|
|
* @param array $data |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
protected function getDefaultJavaScriptCall(array $data) |
131
|
|
|
{ |
132
|
|
|
$conditionName = addslashes(get_class($this)); |
133
|
|
|
$data = ArrayService::get()->arrayToJavaScriptJson($data); |
134
|
|
|
|
135
|
|
|
return <<<JS |
136
|
|
|
Fz.Condition.validateCondition('$conditionName', form, $data) |
137
|
|
|
JS; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
|
|
public function getJavaScriptFiles() |
144
|
|
|
{ |
145
|
|
|
return static::$javaScriptFiles; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param FormObject $formObject |
150
|
|
|
*/ |
151
|
|
|
public function attachFormObject(FormObject $formObject) |
152
|
|
|
{ |
153
|
|
|
$this->formObject = $formObject; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param ActivationInterface $activation |
158
|
|
|
*/ |
159
|
|
|
public function attachActivation(ActivationInterface $activation) |
160
|
|
|
{ |
161
|
|
|
$this->activation = $activation; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param ConditionNode $conditionNode |
166
|
|
|
*/ |
167
|
|
|
public function attachConditionNode(ConditionNode $conditionNode) |
168
|
|
|
{ |
169
|
|
|
$this->conditionNode = $conditionNode; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
|
|
public function __sleep() |
176
|
|
|
{ |
177
|
|
|
$properties = get_object_vars($this); |
178
|
|
|
|
179
|
|
|
unset($properties['formObject']); |
180
|
|
|
unset($properties['activation']); |
181
|
|
|
unset($properties['conditionNode']); |
182
|
|
|
|
183
|
|
|
return array_keys($properties); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|