[TASK] Experimental support for experimental IRRE-Forms and make pages hideable
[TYPO3CMS/Extensions/powermailCond.git] / Classes / Domain / Validator / ConditionAwareValidator.php
1 <?php
2 namespace In2code\PowermailCond\Domain\Validator;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2015 in2code.de
8 * Alex Kellner <alexander.kellner@in2code.de>,
9 * Oliver Eglseder <oliver.eglseder@in2code.de>
10 *
11 * All rights reserved
12 *
13 * This script is part of the TYPO3 project. The TYPO3 project is
14 * free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * The GNU General Public License can be found at
20 * http://www.gnu.org/copyleft/gpl.html.
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 use In2code\Powermail\Domain\Model\Field;
31 use In2code\Powermail\Domain\Model\Form;
32 use In2code\Powermail\Domain\Model\Page;
33 use In2code\Powermail\Domain\Validator\InputValidator;
34 use In2code\Powermail\Utility\ConfigurationUtility;
35 use In2code\PowermailCond\Domain\Model\Condition;
36 use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
37
38 /**
39 * Class ConditionAwareValidator
40 */
41 class ConditionAwareValidator extends InputValidator {
42
43 /**
44 * Validate a single field
45 *
46 * @param Field $field
47 * @param mixed $value
48 * @return void
49 */
50 protected function isValidField(Field $field, $value) {
51
52 /** @var FrontendUserAuthentication $feUser */
53 $feUser = $GLOBALS['TSFE']->fe_user;
54 $arguments = $feUser->getSessionData('tx_powermail_cond');
55 $fieldMarker = $field->getMarker();
56
57
58 if (ConfigurationUtility::isReplaceIrreWithElementBrowserActive()) {
59 $pages = $field->getPages();
60 /** @var Form $form */
61 foreach ($pages->getForms() as $form) {
62 /** @var Page $page */
63 foreach ($form->getPages() as $page) {
64 /** @var Field $field */
65 foreach ($page->getFields() as $field) {
66 if (!empty($arguments[$form->getUid()][$page->getUid()][$fieldMarker]['action'])) {
67 if ($arguments[$form->getUid()][$page->getUid()][$fieldMarker]['action'] === Condition::getActionNumberMap(Condition::ACTION_HIDE)) {
68 return;
69 }
70 }
71 }
72 }
73 }
74 } else {
75 $page = $field->getPages();
76 $form = $page->getForms()->getUid();
77 $page = $page->getUid();
78 if (!empty($arguments['todo_field'][$form][$page][$fieldMarker]['action'])) {
79 if ($arguments['todo_field'][$form][$page][$fieldMarker]['action'] === Condition::getActionNumberMap(Condition::ACTION_HIDE)) {
80 return;
81 }
82 }
83 }
84
85 // Mandatory Check
86 if ($field->getMandatory()) {
87 if (!$this->validateMandatory($value)) {
88 $this->setErrorAndMessage($field, 'mandatory');
89 }
90 }
91
92 // String Checks
93 if (!empty($value) && in_array($field->getType(), $this->validationFieldTypes)) {
94 switch ($field->getValidation()) {
95
96 // email
97 case 1:
98 if (!$this->validateEmail($value)) {
99 $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
100 }
101 break;
102
103 // URL
104 case 2:
105 if (!$this->validateUrl($value)) {
106 $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
107 }
108 break;
109
110 // phone
111 case 3:
112 if (!$this->validatePhone($value)) {
113 $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
114 }
115 break;
116
117 // numbers only
118 case 4:
119 if (!$this->validateNumbersOnly($value)) {
120 $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
121 }
122 break;
123
124 // letters only
125 case 5:
126 if (!$this->validateLettersOnly($value)) {
127 $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
128 }
129 break;
130
131 // min number
132 case 6:
133 if (!$this->validateMinNumber($value, $field->getValidationConfiguration())) {
134 $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
135 }
136 break;
137
138 // max number
139 case 7:
140 if (!$this->validateMaxNumber($value, $field->getValidationConfiguration())) {
141 $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
142 }
143 break;
144
145 // range
146 case 8:
147 if (!$this->validateRange($value, $field->getValidationConfiguration())) {
148 $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
149 }
150 break;
151
152 // length
153 case 9:
154 if (!$this->validateLength($value, $field->getValidationConfiguration())) {
155 $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
156 }
157 break;
158
159 // pattern
160 case 10:
161 if (!$this->validatePattern($value, $field->getValidationConfiguration())) {
162 $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
163 }
164 break;
165
166 /**
167 * E.g. Validation was extended with Page TSconfig
168 * tx_powermail.flexForm.validation.addFieldOptions.100 = New Validation
169 *
170 * Register your Class and Method with TypoScript Setup
171 * plugin.tx_powermail.settings.setup.validation.customValidation.100 =
172 * In2code\Powermailextended\Domain\Validator\ZipValidator
173 *
174 * Add method to your class
175 * validate100($value, $validationConfiguration)
176 *
177 * Define your Errormessage with TypoScript Setup
178 * plugin.tx_powermail._LOCAL_LANG.default.validationerror_validation.100 =
179 * Error happens!
180 */
181 default:
182 if ($field->getValidation()) {
183 $validation = $field->getValidation();
184 if (!empty($this->settings['validation.']['customValidation.'][$validation])) {
185 $extendedValidator = $this->objectManager->get($this->settings['validation.']['customValidation.'][$validation]);
186 if (method_exists($extendedValidator, 'validate' . ucfirst($validation))) {
187 if (!$extendedValidator->{'validate' . ucfirst($validation)}($value, $field->getValidationConfiguration())) {
188 $this->setErrorAndMessage($field, 'validation.' . $validation);
189 }
190 }
191 }
192 }
193 }
194 }
195 }
196 }