[TASK] Add condition negotiation to re-display fields properly and set the copyright...
[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\Validator\InputValidator;
32 use In2code\PowermailCond\Domain\Model\Condition;
33 use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
34
35 /**
36 * Class ConditionAwareValidator
37 */
38 class ConditionAwareValidator extends InputValidator {
39
40 /**
41 * Validate a single field
42 *
43 * @param Field $field
44 * @param mixed $value
45 * @return void
46 */
47 protected function isValidField(Field $field, $value) {
48
49 /** @var FrontendUserAuthentication $feUser */
50 $feUser = $GLOBALS['TSFE']->fe_user;
51 $arguments = $feUser->getSessionData('tx_powermail_cond');
52 $fieldMarker = $field->getMarker();
53 if (!empty($arguments['todo'][$fieldMarker])) {
54 if (!$arguments['todo'][$fieldMarker] === Condition::getActionNumberMap(Condition::ACTION_HIDE)) {
55 // Mandatory Check
56 if ($field->getMandatory()) {
57 if (!$this->validateMandatory($value)) {
58 $this->setErrorAndMessage($field, 'mandatory');
59 }
60 }
61 }
62 }
63
64 // String Checks
65 if (!empty($value) && in_array($field->getType(), $this->validationFieldTypes)) {
66 switch ($field->getValidation()) {
67
68 // email
69 case 1:
70 if (!$this->validateEmail($value)) {
71 $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
72 }
73 break;
74
75 // URL
76 case 2:
77 if (!$this->validateUrl($value)) {
78 $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
79 }
80 break;
81
82 // phone
83 case 3:
84 if (!$this->validatePhone($value)) {
85 $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
86 }
87 break;
88
89 // numbers only
90 case 4:
91 if (!$this->validateNumbersOnly($value)) {
92 $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
93 }
94 break;
95
96 // letters only
97 case 5:
98 if (!$this->validateLettersOnly($value)) {
99 $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
100 }
101 break;
102
103 // min number
104 case 6:
105 if (!$this->validateMinNumber($value, $field->getValidationConfiguration())) {
106 $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
107 }
108 break;
109
110 // max number
111 case 7:
112 if (!$this->validateMaxNumber($value, $field->getValidationConfiguration())) {
113 $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
114 }
115 break;
116
117 // range
118 case 8:
119 if (!$this->validateRange($value, $field->getValidationConfiguration())) {
120 $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
121 }
122 break;
123
124 // length
125 case 9:
126 if (!$this->validateLength($value, $field->getValidationConfiguration())) {
127 $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
128 }
129 break;
130
131 // pattern
132 case 10:
133 if (!$this->validatePattern($value, $field->getValidationConfiguration())) {
134 $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
135 }
136 break;
137
138 /**
139 * E.g. Validation was extended with Page TSconfig
140 * tx_powermail.flexForm.validation.addFieldOptions.100 = New Validation
141 *
142 * Register your Class and Method with TypoScript Setup
143 * plugin.tx_powermail.settings.setup.validation.customValidation.100 =
144 * In2code\Powermailextended\Domain\Validator\ZipValidator
145 *
146 * Add method to your class
147 * validate100($value, $validationConfiguration)
148 *
149 * Define your Errormessage with TypoScript Setup
150 * plugin.tx_powermail._LOCAL_LANG.default.validationerror_validation.100 =
151 * Error happens!
152 */
153 default:
154 if ($field->getValidation()) {
155 $validation = $field->getValidation();
156 if (!empty($this->settings['validation.']['customValidation.'][$validation])) {
157 $extendedValidator = $this->objectManager->get($this->settings['validation.']['customValidation.'][$validation]);
158 if (method_exists($extendedValidator, 'validate' . ucfirst($validation))) {
159 if (!$extendedValidator->{'validate' . ucfirst($validation)}($value, $field->getValidationConfiguration())) {
160 $this->setErrorAndMessage($field, 'validation.' . $validation);
161 }
162 }
163 }
164 }
165 }
166 }
167 }
168 }