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\AssetHandler\Html\DataAttributesAssetHandler; |
17
|
|
|
use Romm\Formz\Condition\Exceptions\InvalidConditionException; |
18
|
|
|
use Romm\Formz\Condition\Processor\DataObject\PhpConditionDataObject; |
19
|
|
|
use Romm\Formz\Form\Definition\FormDefinition; |
20
|
|
|
use TYPO3\CMS\Extbase\Reflection\ObjectAccess; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* This condition will match when a field has the given value. |
24
|
|
|
*/ |
25
|
|
|
class FieldHasValueCondition extends AbstractConditionItem |
26
|
|
|
{ |
27
|
|
|
const CONDITION_IDENTIFIER = 'fieldHasValue'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritdoc |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected static $javaScriptFiles = [ |
34
|
|
|
'EXT:formz/Resources/Public/JavaScript/Conditions/Formz.Condition.FieldHasValue.js' |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
* @validate NotEmpty |
40
|
|
|
*/ |
41
|
|
|
protected $fieldName; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $fieldValue; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $fieldName |
50
|
|
|
* @param string $fieldValue |
51
|
|
|
*/ |
52
|
|
|
public function __construct($fieldName, $fieldValue) |
53
|
|
|
{ |
54
|
|
|
$this->fieldName = $fieldName; |
55
|
|
|
$this->fieldValue = $fieldValue; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritdoc |
60
|
|
|
*/ |
61
|
|
|
public function getCssResult() |
62
|
|
|
{ |
63
|
|
|
if ($this->fieldValue == '') { |
64
|
|
|
return '[' . DataAttributesAssetHandler::getFieldDataValueKey($this->fieldName) . '="' . $this->fieldValue . '"]'; |
65
|
|
|
} else { |
66
|
|
|
return '[' . DataAttributesAssetHandler::getFieldDataValueKey($this->fieldName) . '~="' . $this->fieldValue . '"]'; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritdoc |
72
|
|
|
*/ |
73
|
|
|
public function getJavaScriptResult() |
74
|
|
|
{ |
75
|
|
|
return $this->getDefaultJavaScriptCall([ |
76
|
|
|
'fieldName' => $this->fieldName, |
77
|
|
|
'fieldValue' => $this->fieldValue |
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @inheritdoc |
83
|
|
|
*/ |
84
|
|
|
public function getPhpResult(PhpConditionDataObject $dataObject) |
85
|
|
|
{ |
86
|
|
|
$value = ObjectAccess::getProperty($dataObject->getForm(), $this->fieldName); |
87
|
|
|
|
88
|
|
|
return (is_array($value)) |
89
|
|
|
? (true === in_array($this->fieldValue, $value)) |
90
|
|
|
: ($value == $this->fieldValue); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Checks the condition configuration/options. |
95
|
|
|
* |
96
|
|
|
* If any syntax/configuration error is found, an exception of type |
97
|
|
|
* `InvalidConditionException` must be thrown. |
98
|
|
|
* |
99
|
|
|
* @param FormDefinition $formDefinition |
100
|
|
|
* @throws InvalidConditionException |
101
|
|
|
*/ |
102
|
|
|
protected function checkConditionConfiguration(FormDefinition $formDefinition) |
103
|
|
|
{ |
104
|
|
|
if (false === $formDefinition->hasField($this->fieldName)) { |
105
|
|
|
throw InvalidConditionException::conditionFieldHasValueFieldNotFound($this->fieldName); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|