1: <?php
2:
3: /*
4:
5: *
6: * NOTICE OF LICENSE
7: *
8: * This source file is subject to the Open Software License (OSL 3.0)
9: * or OpenGPL v3 license (GNU Public License V3.0)
10: * that is bundled with this package in the file LICENSE.txt.
11: * It is also available through the world-wide-web at this URL:
12: * http://opensource.org/licenses/osl-3.0.php
13: * or
14: * http://www.gnu.org/licenses/gpl-3.0.txt
15: * If you did not receive a copy of the license and are unable to
16: * obtain it through the world-wide-web, please send an email
17: * to info@e-abi.ee so we can send you a copy immediately.
18: *
19: * DISCLAIMER
20: *
21: * Do not edit or add to this file if you wish to upgrade this module to newer
22: * versions in the future.
23: *
24: * @category Eabi
25: * @package Eabi_Dpd
26: * @copyright Copyright (c) 2014 Aktsiamaailm LLC (http://en.e-abi.ee/)
27: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
28: * @license http://www.gnu.org/licenses/gpl-3.0.txt GNU Public License V3.0
29: * @author Matis Halmann
30: *
31:
32: */
33: if (!defined('_PS_VERSION_')) {
34: exit;
35: }
36:
37: /**
38: * <p>Each validation function returns true or string to be translated with error message.</p>
39: * <p>Arguments:</p>
40: * <ul>
41: <li><code>$field_name</code> - field name that needs to be validated from <code>$data</code> POST array</li>
42: <li><code>$data</code> - Possibly $_POST of $_REQUEST, where data to be validated is looked up</li>
43: <li><code>$validateIfs</code> - assoc array of rules, when this validation should be triggered</li>
44: <li><code>$formFieldData</code> - information how to render the form element.</li>
45: </ul>
46: * @see eabi_dpd_parcelstore_html_helper
47: *
48: * @author Matis
49: */
50: class eabi_dpd_parcelstore_validator_helper {
51:
52:
53: /**
54: * <p>Returns true if entry is not blank</p>
55: * @param type $field_name field name, that needs to be validated
56: * @param array $data post or request data
57: * @param array $validateIfs validation if rules
58: * @param array $formFieldData supplied form field data
59: * @return string|boolean
60: * @see eabi_dpd_parcelstore_validator_helper
61: */
62: public function required_entry($field_name, array $data, array $validateIfs, array $formFieldData) {
63: if ($this->_shouldValidate($field_name, $data, $validateIfs)) {
64: $validationResult = isset($data[$field_name]) && trim($data[$field_name]) != '';
65: if (!$validationResult) {
66: return $this->l('%s required entry');
67: }
68: return true;
69: }
70: return true;
71: }
72:
73:
74: /**
75: * <p>Returns true if entry is integer of float number</p>
76: * @param type $field_name field name, that needs to be validated
77: * @param array $data post or request data
78: * @param array $validateIfs validation if rules
79: * @param array $formFieldData supplied form field data
80: * @return string|boolean
81: * @see eabi_dpd_parcelstore_validator_helper
82: */
83: public function validate_number($field_name, array $data, array $validateIfs, array $formFieldData) {
84: if ($this->_shouldValidate($field_name, $data, $validateIfs)) {
85: $validationResult = (!isset($data[$field_name]) || $data[$field_name] == '') || isset($data[$field_name]) && is_numeric($data[$field_name]);
86: if (!$validationResult) {
87: return $this->l('%s should be number');
88: }
89: return true;
90: }
91: return true;
92: }
93:
94: /**
95: * <p>Returns true if all characters are numbers between 0-9</p>
96: * @param type $field_name field name, that needs to be validated
97: * @param array $data post or request data
98: * @param array $validateIfs validation if rules
99: * @param array $formFieldData supplied form field data
100: * @return string|boolean
101: * @see eabi_dpd_parcelstore_validator_helper
102: */
103: public function validate_digit($field_name, array $data, array $validateIfs, array $formFieldData) {
104: if ($this->_shouldValidate($field_name, $data, $validateIfs)) {
105: $validationResult = (!isset($data[$field_name]) || $data[$field_name] == '') || (isset($data[$field_name]) && ctype_digit($data[$field_name]));
106: if (!$validationResult) {
107: return $this->l('%s should be number');
108: }
109: return true;
110: }
111: return true;
112: }
113:
114: /**
115: * <p>Returns true if we are dealing with select/multiselect fields and submitted value matches options</p>
116: * @param type $field_name field name, that needs to be validated
117: * @param array $data post or request data
118: * @param array $validateIfs validation if rules
119: * @param array $formFieldData supplied form field data
120: * @return string|boolean
121: * @see eabi_dpd_parcelstore_validator_helper
122: */
123: public function validate_select($field_name, array $data, array $validateIfs, array $formFieldData) {
124: if ($this->_shouldValidate($field_name, $data, $validateIfs)) {
125: if (!in_array($formFieldData['type'], array('select', 'multiselect'))) {
126: //perform validation only on select and multiselect fields
127: return true;
128: }
129: //if possible options are not set, then validation failed.
130: if (!isset($formFieldData['options'])) {
131: return $this->l('%s has no possible options defined');
132: }
133: $valuesToValidate = isset($data[$field_name])?$data[$field_name]:array();
134: if (!is_array($valuesToValidate) && $formFieldData['type'] != 'multiselect') {
135: //convert single select to array if required
136: $valuesToValidate = array($valuesToValidate);
137: }
138:
139:
140: if ($formFieldData['type'] == 'select') {
141: if (count($valuesToValidate) > 1) {
142: //regular select menu can only have one option
143: return $this->l('%s can only be one of possible options');
144: }
145: }
146: foreach ($valuesToValidate as $valueToValidate) {
147: $validationResult = in_array($valueToValidate, array_keys($formFieldData['options']));
148: if (!$validationResult) {
149: //return error if any of the selected options does not exist in array
150: return $this->l('%s can be only be the options from the select');
151: }
152: }
153: return true;
154: }
155: return true;
156:
157: }
158:
159: /**
160: * <p>Returns true if input is not empty and validates value as e-mail</p>
161: * @param type $field_name field name, that needs to be validated
162: * @param array $data post or request data
163: * @param array $validateIfs validation if rules
164: * @param array $formFieldData supplied form field data
165: * @return string|boolean
166: * @see eabi_dpd_parcelstore_validator_helper
167: */
168: public function validate_email($field_name, array $data, array $validateIfs, array $formFieldData) {
169: if ($this->_shouldValidate($field_name, $data, $validateIfs)) {
170: $validationResult = (!isset($data[$field_name]) || $data[$field_name] == '') || (isset($data[$field_name]) && filter_var($data[$field_name], FILTER_VALIDATE_EMAIL));
171: if (!$validationResult) {
172: return $this->l('%s should be e-mail');
173: }
174: return true;
175: }
176: return true;
177: }
178:
179: /**
180: * <p>Returns true if all the fields in price-definitions-based-on-country validate correctly</p>
181: * @param type $field_name field name, that needs to be validated
182: * @param array $data post or request data
183: * @param array $validateIfs validation if rules
184: * @param array $formFieldData supplied form field data
185: * @return string|boolean
186: * @see eabi_dpd_parcelstore_validator_helper
187: */
188: public function validate_handling_fee_country($field_name, array $data, array $validateIfs, array $formFieldData) {
189: if ($this->_shouldValidate($field_name, $data, $validateIfs)) {
190: $initialValidationResult = (!isset($data[$field_name]) || $data[$field_name] == '');
191: if ($initialValidationResult) {
192: //empty field, exit validation routine
193: return true;
194: }
195: if (is_array($data[$field_name])) {
196: $subValidationRoutine = array(
197: 'base_price' => array('required_entry', 'validate_number'),
198: 'kg_price' => array('required_entry', 'validate_number'),
199: 'free_shipping_from' => array('validate_number'),
200: );
201: foreach ($data[$field_name] as $validationDataSet) {
202: //country_id
203: //base_price
204: //kg_price
205: //free_shipping_from
206: foreach ($subValidationRoutine as $subFieldName => $subValidations) {
207: foreach ($subValidations as $subValidation) {
208: $subValidationResult = $this->validate($subValidation, $subFieldName, $validationDataSet, array(), $formFieldData);
209: if (is_string($subValidationResult)) {
210: return $subValidationResult;
211: }
212: }
213: }
214: }
215: }
216: //all passed, return true
217: return true;
218: }
219: return true;
220: }
221:
222:
223: /**
224: * <p>Wrapper class for calling out validation methods by it's name.</p>
225: * @param string $function validation function to call.
226: * @param string $field_name field name, that needs to be validated
227: * @param array $data post or request data
228: * @param array $validateIfs validation if rules
229: * @param array $formFieldData supplied form field data
230: * @return string|boolean
231: * @see eabi_dpd_parcelstore_validator_helper
232: */
233: public function validate($function, $field_name, array $data, array $validateIfs, array $formFieldData) {
234: return $this->$function($field_name, $data, $validateIfs, $formFieldData);
235: }
236:
237: /**
238: * <p>Determines if current field needs to be validated based on the <code>validate-if</code> rules</p>
239: * @param string $field_name form field name
240: * @param array $data form submitted data
241: * @param array $validateIfs array of validation condition rules
242: * @return boolean
243: */
244: private function _shouldValidate($field_name, array $data, array $validateIfs) {
245: $shouldValidate = false;
246: if (count($validateIfs)) {
247: foreach ($validateIfs as $fieldToCheck => $valueToMatch) {
248: $fieldToCheck = strtoupper($fieldToCheck);
249: if (isset($data[$fieldToCheck]) && $data[$fieldToCheck] === $valueToMatch) {
250: $shouldValidate = true;
251: break;
252: }
253: }
254:
255: } else {
256: $shouldValidate = true;
257: }
258: return $shouldValidate;
259: }
260:
261: /**
262: * <p>Required for the PrestaShop module to recognize translations</p>
263: * @param string $i
264: * @return string
265: */
266: protected function l($i) {
267: return Module::getInstanceByName(eabi_dpd_parcelstore::NAME)->l($i);
268: }
269:
270: }
271: