1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
15
|
|
|
* |
16
|
|
|
* This software consists of voluntary contributions made by many individuals |
17
|
|
|
* and is licensed under the MIT license. For more information, see |
18
|
|
|
* @category |
19
|
|
|
* @package sistema/Ayudantes |
20
|
|
|
* @copyright Copyright (c) 2006 - 2014 webcol.net (http://www.webcol.net/calima) |
21
|
|
|
* @license https://github.com/webcol/Calima/blob/master/LICENSE MIT |
22
|
|
|
* @version ##BETA 1.0##, ##2014 - 2015## |
23
|
|
|
* <http://www.calimaframework.com>. |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
// se agradece a ArrayZone por el aporte de esta clase |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @name Form Generator for Kernel Web |
30
|
|
|
* @version A.1.0 |
31
|
|
|
* @copyright ArrayZone 2014 |
32
|
|
|
* @license AZPL or later; see License.txt or http://arrayzone.com/license |
33
|
|
|
* @category plugin |
34
|
|
|
* |
35
|
|
|
* Description: This script write a FORM HTML automatically from array (it will be generated trhoug functions). |
36
|
|
|
* It write any type of items basics of HTML4 and some of HTML5. |
37
|
|
|
* Once user clic "submit", all information will be validated via PHP |
38
|
|
|
* Some types like "required" and "email" validated trhough PHP to avoid problems with navigator compatibilities or hacker attacks |
39
|
|
|
* |
40
|
|
|
* NEXT VERSION: If you can use JavaScript and have JQuery implemented, the form can write JavaScript code to validate all |
41
|
|
|
* Read the documentation for all information in kernel.arrayzone.com |
42
|
|
|
* or all coments of all parameters and functions. |
43
|
|
|
* |
44
|
|
|
* IMPORTANT: If you use a specific system to get POST and GET, you have to edit "private function getData()" |
45
|
|
|
* IMPORTANT 2: If you use DISABLE, control it manually when you save it |
46
|
|
|
* IMPORTANT 3: Currently aren't supported array names (name="MyField[]"), you can use: name="MyField_1" |
47
|
|
|
*/ |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
|
51
|
|
|
namespace Sistema\Ayudantes; |
52
|
|
|
|
53
|
|
|
class CFPHPFormularios |
54
|
|
|
{ |
55
|
|
|
/* |
56
|
|
|
* Form configuration. It don't need description |
57
|
|
|
*/ |
58
|
|
|
public $method = 'post'; // post / get (CAUTION!!: Case sensitive) |
59
|
|
|
public $action = ''; |
60
|
|
|
public $name = ''; |
61
|
|
|
public $on_submit = ''; |
62
|
|
|
public $id = ''; |
63
|
|
|
public $class = ''; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Logging control |
67
|
|
|
* @logErrors boolean If is true, $errors will store errors |
68
|
|
|
* @errors string Contain al errors |
69
|
|
|
*/ |
70
|
|
|
public $logErrors = true; |
71
|
|
|
public $errors = ''; |
72
|
|
|
|
73
|
|
|
/* |
74
|
|
|
* Other configurations about form |
75
|
|
|
*/ |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @show_labels boolean |
79
|
|
|
* @tutorial If is true, it show label text of some inputs before input tag |
80
|
|
|
*/ |
81
|
|
|
public $show_labels = true; |
82
|
|
|
|
83
|
|
|
/** IN DEVELOPMENT |
84
|
|
|
* @self_show boolean |
85
|
|
|
* @tutorial if is true, it show the input when function addX is called |
86
|
|
|
*/ |
87
|
|
|
//public $self_show = false; |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** IN DEVELOPMENT |
91
|
|
|
* @only_show boolean |
92
|
|
|
* @tutorial If is true, it don't save the configuration to input (so showForm() and validateData() don't work) |
93
|
|
|
*/ |
94
|
|
|
//public $only_show = false; |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/* |
98
|
|
|
* Content |
99
|
|
|
* It can be set manually or automatically trhough functions |
100
|
|
|
*/ |
101
|
|
|
public $content = array(); |
102
|
|
|
|
103
|
|
|
/* |
104
|
|
|
* Start functions |
105
|
|
|
*/ |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @name showArray |
109
|
|
|
* @tutorial This function show array result, it can be copied and you can replace all generator functions to this array |
110
|
|
|
* @example $fg->showArray(); |
111
|
|
|
*/ |
112
|
|
|
public function showArray($array = false) { |
|
|
|
|
113
|
|
|
print_r($this->content); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @name showForm |
119
|
|
|
* @tutorial This function generate the form |
120
|
|
|
* @example $fg->showForm(); |
121
|
|
|
*/ |
122
|
|
|
public function showForm() { |
123
|
|
|
// Loading all arrays |
124
|
|
|
$toShow = $this->content; |
125
|
|
|
|
126
|
|
|
// Showing form |
127
|
|
|
echo $this->showStartForm(); |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
// Itearate all inputs |
131
|
|
|
foreach ($toShow as $input) { |
132
|
|
|
// Reading data |
133
|
|
|
if (isset($input['data'])) $data = $input['data']; |
134
|
|
|
|
135
|
|
|
// New row |
136
|
|
|
echo '<div>'; |
137
|
|
|
// Showing labels |
138
|
|
|
if ($this->show_labels) { |
139
|
|
|
echo '<div>'; |
140
|
|
|
if (isset($input['label']) and $input['label'] != '') echo $input['label']; |
|
|
|
|
141
|
|
|
echo '</div>'; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// Showing content |
145
|
|
|
echo '<div>'; |
146
|
|
|
switch ($input['type']) { |
147
|
|
|
case 'input': |
148
|
|
|
echo $this->showInput($data); |
|
|
|
|
149
|
|
|
break; |
150
|
|
|
case 'radio': |
151
|
|
|
echo $this->showRadio($data, $input['values'], $input['selected']); |
152
|
|
|
break; |
153
|
|
|
case 'select'; |
|
|
|
|
154
|
|
|
echo $this->showSelect($data, $input['values'], $input['selected']); |
155
|
|
|
break; |
156
|
|
|
case 'textarea': |
157
|
|
|
echo $this->showTextArea($input['text'], $data); |
158
|
|
|
break; |
159
|
|
|
case 'separator'; |
|
|
|
|
160
|
|
|
echo '<br />'; |
161
|
|
|
break; |
162
|
|
|
} |
163
|
|
|
echo '</div>'; |
164
|
|
|
echo '</div>'.PHP_EOL; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// Closing form |
168
|
|
|
echo $this->showEndForm(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
/* |
173
|
|
|
* The following "showX" functions are autoloaded and show the different parts of form |
174
|
|
|
*/ |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @name showInput |
178
|
|
|
* @tutorial This function show <input> tag with parameters specifics |
179
|
|
|
* @param array $data All data to show in the input |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
private function showInput($data) { |
183
|
|
|
$r = ''; |
184
|
|
|
// Number start if is range |
185
|
|
View Code Duplication |
if ($data['type'] == 'range' and isset($data['min'])) $r .= $data['min'] . ' '; |
|
|
|
|
186
|
|
|
|
187
|
|
|
$r .= '<input '; |
188
|
|
|
|
189
|
|
|
// Reading value sended by user, if are any, we load it temporaly to show in form |
190
|
|
|
$data['value'] = $this->getData($data['name'], $data['value']); |
191
|
|
|
|
192
|
|
|
// Asignamos las claves conviritiendo el array |
193
|
|
|
foreach ($data as $attr=>$value) { |
194
|
|
|
$r .= $attr.'="'.$value.'" '; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$r .= '/>'; |
198
|
|
|
|
199
|
|
|
// Number end if is range |
200
|
|
View Code Duplication |
if ($data['type'] == 'range' and isset($data['max'])) $r .= $data['max'] . ' '; |
|
|
|
|
201
|
|
|
|
202
|
|
|
// Return |
203
|
|
|
return $r; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
private function showRadio($data, $values, $selected) { |
207
|
|
|
// Primero generamos un esquema |
208
|
|
|
$base = '<label><input type="radio"'; |
209
|
|
|
|
210
|
|
|
// Asignamos las claves conviritiendo el array |
211
|
|
|
foreach ($data as $attr=>$value) { |
212
|
|
|
$base .= $attr.'="'.$value.'" '; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
|
216
|
|
|
// Leemos el valor enviado por el usuario (si lo hay) y si hay, reemplazamos $select por el valor |
217
|
|
|
$selected = $this->getData($data['name'], $selected); |
218
|
|
|
|
219
|
|
|
//echo $sendValue; |
220
|
|
|
// Si no tiene m�ltiples valores (es un string), lo retornamos de golpe |
221
|
|
|
if (!is_array($values)) { |
222
|
|
|
// Comprobamos el value, es posible que el usuario este intentando crear los Radio (option) de forma separada |
223
|
|
|
if (!is_array($values) and $selected == $values) $base .= ' checked="checked" '; |
|
|
|
|
224
|
|
|
return $base . ' value="'.$values.'" />'; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
// Por el contrario, tenemos 1 o m�s value con lo que es un posible texto |
228
|
|
|
// Ahora preparamos todos los input |
229
|
|
|
$r = ''; |
230
|
|
View Code Duplication |
foreach ($values as $id=>$text) { |
231
|
|
|
$r .= $base; |
232
|
|
|
if ($selected !== null and $id == $selected) $r .= ' checked="checked" '; |
|
|
|
|
233
|
|
|
$r .= ' value="'. $id .'" />'.$text.'</label>'; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return $r; |
237
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @name showSelect |
242
|
|
|
* @tutorial This function show <select> tag with all values |
243
|
|
|
* @param array $data All data to show in the input |
244
|
|
|
* @param array $values Values to show in select |
245
|
|
|
* @param string $selected Specify selected option |
246
|
|
|
* @return string |
247
|
|
|
*/ |
248
|
|
|
private function showSelect($data, $values, $selected = null) { |
249
|
|
|
$r = '<select '; |
250
|
|
|
// Convert array to input string |
251
|
|
|
foreach ($data as $attr=>$value) { |
252
|
|
|
$r .= $attr.'="'.$value.'" '; |
253
|
|
|
} |
254
|
|
|
// Return end input |
255
|
|
|
$r .= '>'; |
256
|
|
|
|
257
|
|
|
// Leemos el valor enviado por el usuario (si lo hay) y si hay, reemplazamos $select por el valor |
258
|
|
|
$selected = $this->getData($data['name'], $selected); |
259
|
|
|
|
260
|
|
|
|
261
|
|
|
// Loading options |
262
|
|
|
// To speed up processes, we have two whiles depending if are any selected value |
263
|
|
|
if ($selected != '') { |
264
|
|
View Code Duplication |
foreach ($values as $val=>$txt) { |
265
|
|
|
$r .= '<option value="' . $val . '"'; |
266
|
|
|
if ($val == $selected) $r .= ' selected '; |
267
|
|
|
$r .= '>' . $txt . '</option>'; |
268
|
|
|
} |
269
|
|
|
} else { |
270
|
|
|
foreach ($values as $val=>$txt) { |
271
|
|
|
$r .= '<option value="' . $val . '">' . $txt . '</option>'; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return $r . '</select>'; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @name showTextArea |
280
|
|
|
* @tutorial This function show <textarea> tag with all values |
281
|
|
|
* @param array $data All data to show in the input |
282
|
|
|
* @return string |
283
|
|
|
*/ |
284
|
|
|
private function showTextArea($text, $data) { |
285
|
|
|
$r = ''; |
286
|
|
|
$r .= '<textarea '; |
287
|
|
|
|
288
|
|
|
// Asignamos las claves conviritiendo el array |
289
|
|
|
foreach ($data as $attr=>$value) { |
290
|
|
|
$r .= $attr.'="'.$value.'" '; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
$r .= '>'; |
294
|
|
|
// Reading value sended by user, if are any, we load it temporaly to show in form |
295
|
|
|
$r .= $this->getData($data['name'], $text) . '</textarea>'; |
296
|
|
|
|
297
|
|
|
// Return |
298
|
|
|
return $r; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @name showStartForm This function return the start part of the form |
303
|
|
|
* @return string |
304
|
|
|
*/ |
305
|
|
|
private function showStartForm() { |
306
|
|
|
$r = '<form '; |
307
|
|
|
if ($this->action!= '') $r .= 'action="'.$this->action.'" '; |
308
|
|
|
if ($this->method != '') $r .= 'method="'.$this->method .'" '; |
309
|
|
|
if ($this->name != '') $r .= 'name="'.$this->name .'" '; |
310
|
|
|
if ($this->on_submit != '') $r .= 'onSubmit="'.$this->on_submit .'" '; |
311
|
|
|
if ($this->id != '') $r .= 'id="'.$this->id .'" '; |
312
|
|
|
if ($this->class != '') $r .= 'class="'.$this->class .'" '; |
313
|
|
|
|
314
|
|
|
return $r . '>'.PHP_EOL; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @name showEndForm This show the end of the form |
319
|
|
|
* @return string |
320
|
|
|
*/ |
321
|
|
|
private function showEndForm() { |
322
|
|
|
return '</form>'; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
|
326
|
|
|
/* |
327
|
|
|
* VALIDATORS |
328
|
|
|
*/ |
329
|
|
|
/** |
330
|
|
|
* @name validate |
331
|
|
|
* @tutorial this function validate items sends trhough form. It DON'T FILTER |
332
|
|
|
* Basically simulate HTML5 trhoguh PHP for full compatibility |
333
|
|
|
* @param boolean $error_list If is true, it generate in $this->error_list a list with data required |
|
|
|
|
334
|
|
|
*/ |
335
|
|
|
public function validate() { |
336
|
|
|
$this->error_list = ''; // Clean error list |
|
|
|
|
337
|
|
|
$total = 0; // Total counted params |
338
|
|
|
$success = 0; // Total correct |
339
|
|
|
|
340
|
|
|
// load all inputs |
341
|
|
|
$params = $this->content; |
342
|
|
|
|
343
|
|
|
// reading all inputs |
344
|
|
|
foreach ($params as $param) { |
345
|
|
|
++$total; |
346
|
|
|
|
347
|
|
|
// Skip separators and bad generated arrays |
348
|
|
|
if (!isset($param['data'])) { |
349
|
|
|
++$success; |
350
|
|
|
continue; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
// Start validation |
354
|
|
|
$data = $param['data']; |
355
|
|
|
|
356
|
|
|
// Checking type input |
357
|
|
|
switch ($param['type']) { |
358
|
|
|
|
359
|
|
|
case 'input': |
360
|
|
|
$success += $this->validateInput($param['data']); |
361
|
|
|
break; |
362
|
|
|
case 'radio': |
363
|
|
|
$success += $this->validateRadio($param); |
364
|
|
|
break; |
365
|
|
|
case 'select': |
366
|
|
|
$success += $this->validateSelect($param); |
367
|
|
|
break; |
368
|
|
|
default: |
369
|
|
|
$success++; |
370
|
|
|
} |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
if ($success >= $total) return true; |
374
|
|
|
return false; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* @name validateInput |
379
|
|
|
* @tutorial This function test if an input (text, password, checkbox, ...) is valid depending assigned values |
380
|
|
|
* If you need other that is not invented, you can use "pattern" for example |
381
|
|
|
* It checks: |
382
|
|
|
* - required |
383
|
|
|
* - date |
384
|
|
|
* - min |
385
|
|
|
* - max |
386
|
|
|
* - number |
387
|
|
|
* @param array $data Contains all information about input |
388
|
|
|
* @return boolean If return true, its okay |
389
|
|
|
*/ |
390
|
|
|
private function validateInput($data) { |
391
|
|
|
// Obtaining value send by user |
392
|
|
|
$readValue = $this->getData($data['name']); |
393
|
|
|
|
394
|
|
|
// Empty/not send and required? |
395
|
|
|
// TODO: Add require --> file (uses $_FILE) |
396
|
|
|
|
397
|
|
View Code Duplication |
if (isset($data['required']) and ($readValue === null or $readValue == '')) { |
|
|
|
|
398
|
|
|
$this->log('is required', $data); |
399
|
|
|
return false; |
400
|
|
|
} elseif ($readValue == '') { |
401
|
|
|
return true; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
// Checking type input |
405
|
|
|
switch ($data['type']) { |
406
|
|
|
case 'text': |
407
|
|
|
// Maxlenght fail |
408
|
|
|
if (isset($data['maxlength']) and is_numeric($data['maxlength']) and $data['maxlength'] > -1 |
|
|
|
|
409
|
|
|
and strlen($readValue) > $data['maxlength']) { |
|
|
|
|
410
|
|
|
|
411
|
|
|
$this->log('is too long. Maximum' . ' ' . $data['maxlength'] . ' ' . 'characters', $data ); |
412
|
|
|
return false; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
if (isset($data['pattern']) and is_numeric($data['pattern']) and $data['maxlength'] != '' |
|
|
|
|
416
|
|
|
and preg_match($data['pattern'], $readValue) === FALSE) { |
|
|
|
|
417
|
|
|
|
418
|
|
|
$this->log('pattern error' . ' (' . $data['pattern'] . ')' , $data); |
419
|
|
|
return false; |
420
|
|
|
} |
421
|
|
|
break; |
422
|
|
|
case 'number': |
423
|
|
|
case 'range': |
424
|
|
|
// IS NUMERIC |
425
|
|
|
if ($readValue != '' and !is_numeric($readValue)) { |
|
|
|
|
426
|
|
|
$this->log('Not numeric', $data); |
427
|
|
|
return false; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
// MIN |
431
|
|
View Code Duplication |
if (isset($data['min']) and $readValue < $data['min']) { |
|
|
|
|
432
|
|
|
$this->log('The number have to be greather than' . ' ' . $data['min'].'.', $data); |
433
|
|
|
return false; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
// MAX |
437
|
|
View Code Duplication |
if (isset($data['max']) and $readValue > $data['max']) { |
|
|
|
|
438
|
|
|
$this->log('The number have to be less than' . ' ' . $data['max'].'.', $data); |
439
|
|
|
return false; |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
// STEP http://www.w3schools.com/tags/att_input_step.asp |
443
|
|
|
// Value 0 ever is valid (and if you try Divide to Zero, it will take error because the result is inifinite |
444
|
|
|
if (isset($data['step']) and $readValue != 0 and $readValue % $data['step'] !== 0) { |
|
|
|
|
445
|
|
|
$this->log('The number have to be multiple of' . ' ' . $data['step'].'.', $data); |
446
|
|
|
return false; |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
break; |
450
|
|
|
case 'date'; |
|
|
|
|
451
|
|
|
//min | max |
452
|
|
|
echo $readValue; |
453
|
|
|
if (!is_date($readValue)) { |
454
|
|
|
$this->log('The date' . ' ' .$readValue.' '. 'must have format' . ' mm/dd/yyyy '.'.', $data); |
455
|
|
|
return false; |
456
|
|
|
} |
457
|
|
|
break; |
458
|
|
|
|
459
|
|
|
case 'email': |
|
|
|
|
460
|
|
|
if (!filter_var($readValue, FILTER_VALIDATE_EMAIL)) { |
461
|
|
|
$this->log('Email invalid', $data); |
462
|
|
|
return false; |
463
|
|
|
} |
464
|
|
|
case 'file': |
465
|
|
|
//accept="image/*" (http://www.w3schools.com/tags/att_input_accept.asp) |
466
|
|
|
break; |
467
|
|
|
|
468
|
|
|
case 'url': |
469
|
|
|
if (!filter_var($readValue, FILTER_VALIDATE_URL)) { |
470
|
|
|
$this->log('Invalid url', $data); |
471
|
|
|
return false; |
472
|
|
|
} |
473
|
|
|
break; |
474
|
|
|
} |
475
|
|
|
// Validamos el resto como cierto (no podemos parametrizar el 100 % de cosas nuevas que salgan de HTML) |
476
|
|
|
return true; |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* @name validateArray |
481
|
|
|
* This functions validate an Array, is a complement to validateRadio, select... |
482
|
|
|
* @param array/string $values List of values to validate |
|
|
|
|
483
|
|
|
* @param array/string $value/s selecteds by user |
|
|
|
|
484
|
|
|
*/ |
485
|
|
|
private function validateArray($values, $value, $data = array()) { |
486
|
|
|
if (is_array($values)) { |
487
|
|
|
// Is array (serach all "$value" in "$values" |
488
|
|
|
if (!array_key_exists($value, $values)) { |
489
|
|
|
if (is_array($value)) { |
490
|
|
|
$this->log('Values don\'t match.', $data); |
491
|
|
|
} else { |
492
|
|
|
$this->log('ID ' .$value.' ' . 'don\'t match', $data); |
493
|
|
|
} |
494
|
|
|
return false; |
495
|
|
|
} |
496
|
|
|
} else { |
497
|
|
|
// Is string |
498
|
|
|
if ($readValue == $values) { |
|
|
|
|
499
|
|
|
$this->log('The value' . ' ' . $value.' ' . 'is not available', $data); |
500
|
|
|
return false; |
501
|
|
|
} |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
return true; |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
|
508
|
|
|
/** |
509
|
|
|
* @name validateRadio |
510
|
|
|
* @tutorial This function test if an radio is valid depending assigned values |
511
|
|
|
* @param array $data Contains all information about input |
|
|
|
|
512
|
|
|
* @return boolean If return true, its okay |
513
|
|
|
*/ |
514
|
|
|
private function validateRadio($params) { |
515
|
|
|
$data = $params['data']; |
516
|
|
|
// Obtaining value send by user |
517
|
|
|
$readValue = $this->getData($data['name']); |
518
|
|
|
|
519
|
|
|
// Is required? |
520
|
|
View Code Duplication |
if (isset($data['required']) and ($readValue === null or $readValue == '')) { |
|
|
|
|
521
|
|
|
$this->log('is required', $data); |
522
|
|
|
return false; |
523
|
|
|
} elseif ($readValue == '') { |
524
|
|
|
return true; |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
|
528
|
|
|
// Seleccionamos que tipo de analisis (dependiendo si se ha creado como input o como radio) |
529
|
|
|
// Esto no deberia estar aqui porque no es necesario, pero esta hecho por posibles despistes de usuarios finales |
530
|
|
|
if (isset($params['values'])) { |
531
|
|
|
return $this->validateArray($params['values'], $readValue, $data); |
532
|
|
|
} elseif ($data['value']) { |
533
|
|
|
// If user try to add radio like normal input... (into 'value' in index data) |
534
|
|
|
return $this->validateArray($params['value'], $readValue, $data); |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
return false; |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
/** |
541
|
|
|
* @name validateSelect |
542
|
|
|
* @tutorial This function test if an select is valid depending assigned values |
543
|
|
|
* @param array $data Contains all information about input |
|
|
|
|
544
|
|
|
* @return boolean If return true, its okay |
545
|
|
|
*/ |
546
|
|
|
private function validateSelect($param) { |
547
|
|
|
$data = $param['data']; |
548
|
|
|
// Obtaining value send by user |
549
|
|
|
$readValue = $this->getData($data['name']); |
550
|
|
|
|
551
|
|
|
// Is required? |
552
|
|
View Code Duplication |
if (isset($data['required']) and ($readValue === null or $readValue == '')) { |
|
|
|
|
553
|
|
|
$this->log('is required', $data); |
554
|
|
|
return false; |
555
|
|
|
} elseif ($readValue == '') { |
556
|
|
|
return true; |
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
// Seleccionamos que tipo de analisis (dependiendo si se ha creado como input o como radio) |
560
|
|
|
// Esto no deberia estar aqui porque no es necesario, pero esta hecho por posibles despistes de usuarios finales |
561
|
|
|
return $this->validateArray($param['values'], $readValue, $data); |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
|
565
|
|
|
/** |
566
|
|
|
* @name getData This function get value from POST/GET trhough ID ($key) |
567
|
|
|
* If you use other system to load GET and POST, you have to edit this |
568
|
|
|
* @param string $key Object Index |
569
|
|
|
* @param string $default Default item if not data |
570
|
|
|
* @return string/null Return the value, if don't exist, return null |
|
|
|
|
571
|
|
|
*/ |
572
|
|
|
private function getData($key, $default = null) { |
|
|
|
|
573
|
|
|
if ($this->method == "post") return isset($_POST[$key]) ? $_POST[$key] : $default; |
574
|
|
|
else return isset($_GET[$key]) ? $_GET[$key] : $default; |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
/* |
578
|
|
|
* Array constructors |
579
|
|
|
* This functions will be called to self construct array easy |
580
|
|
|
*/ |
581
|
|
|
|
582
|
|
|
/** |
583
|
|
|
* @name separator |
584
|
|
|
* @tutorial This function add separation (<hr/>) between parts |
585
|
|
|
*/ |
586
|
|
|
public function addSeparator() { |
587
|
|
|
$this->content[] = array('type' => 'separator'); |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
/** |
591
|
|
|
* @name addInput |
592
|
|
|
* @tutorial This class generate a generic INPUT |
593
|
|
|
* @param string $label If is not empty, it show a text "label" with the text |
594
|
|
|
* @param string $type Specify input type. It will be validated once use validate(); |
595
|
|
|
* Types that are current supported to validate: |
596
|
|
|
* text, password, hidden, file |
597
|
|
|
* Its supported use ALL type supported (http://www.w3schools.com/tags/att_input_type.asp), |
598
|
|
|
* Some type input you can declare directly via functions (afther this function are more) |
599
|
|
|
* @param string $name Element name |
600
|
|
|
* @param string $value Element value |
601
|
|
|
* @param string $required Element required? |
602
|
|
|
* @param array $attributes Optional atributes not listened (in array). |
603
|
|
|
* EX: You like use "date", then you can put array('min' => '1979-12-31', 'max' => '2000-01-02') |
604
|
|
|
* All parameters currently working: min, max (date and number), pattern, |
605
|
|
|
* |
606
|
|
|
* @example $fr->addInput('text', 'Username', '', true, 'Type here your username', array('class'=>'text red', id => 'text1'), array()) |
607
|
|
|
*/ |
608
|
|
|
public function addInput($label = '', $type = 'text', $name = '', $value = '', $required = false, $placeholder = '', $attributes = array()) { |
609
|
|
|
// Creating main data |
610
|
|
|
$data = array( |
611
|
|
|
'type' => $type, |
612
|
|
|
'name' => $name, |
613
|
|
|
'value' => $value, |
614
|
|
|
); |
615
|
|
|
|
616
|
|
|
|
617
|
|
|
if ($required) $data['required'] = 'required'; |
|
|
|
|
618
|
|
|
if ($placeholder != '') $data['placeholder'] = $placeholder; |
619
|
|
|
if (!empty($attributes)) array_merge($data, $attributes); |
620
|
|
|
|
621
|
|
|
|
622
|
|
|
// Saving data to object |
623
|
|
|
$content = array( |
624
|
|
|
'type' => 'input', |
625
|
|
|
'data' => $data |
626
|
|
|
); |
627
|
|
|
if ($label != '') $content['label'] = $label; |
628
|
|
|
|
629
|
|
|
$this->content[] = $content; |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
/** |
633
|
|
|
* @name addNumber This function add input "number" and "date" with min, max and step (if you like) |
634
|
|
|
* @param string $label If is not empty, it show a text "label" with the text |
635
|
|
|
* @param string $name Element name |
636
|
|
|
* @param string $value Default value |
637
|
|
|
* @param boolen $range If is true, then show "range" instead of text with number |
638
|
|
|
* @param boolen $required Is required? |
639
|
|
|
* @param number $min Minium value. Empty nothing |
640
|
|
|
* @param number $max Max value. Empty nothing |
641
|
|
|
* @param number $step Step (multiply). Empty nothing (ex: step 2: -2, 0, 2, 4 ...) |
642
|
|
|
* @param string $placeholder Default text to show if box is empty |
643
|
|
|
* @param unknown $attributes Additional attributes |
644
|
|
|
*/ |
645
|
|
View Code Duplication |
public function addNumber($label = '', $name = '', $value = '', $range = false, $required = false, $min = '', $max = '', $step = '', $placeholder = '', $attributes = array()) { |
646
|
|
|
// Creating main data |
647
|
|
|
$data = array( |
648
|
|
|
'type' => (! $range) ? 'number' : 'range', |
649
|
|
|
'name' => $name, |
650
|
|
|
'value' => $value, |
651
|
|
|
); |
652
|
|
|
|
653
|
|
|
|
654
|
|
|
if ($required) $data['required'] = 'required'; |
655
|
|
|
if ($min) $data['min'] = $min; |
656
|
|
|
if ($max) $data['max'] = $max; |
657
|
|
|
if ($step) $data['step'] = $step; |
658
|
|
|
if ($placeholder != '') $data['placeholder'] = $placeholder; |
659
|
|
|
if (!empty($attributes)) array_merge($data, $attributes); |
660
|
|
|
|
661
|
|
|
// Saving data to object |
662
|
|
|
$content = array( |
663
|
|
|
'type' => 'input', |
664
|
|
|
'data' => $data |
665
|
|
|
); |
666
|
|
|
if ($label != '') $content['label'] = $label; |
667
|
|
|
|
668
|
|
|
$this->content[] = $content; |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
/** |
672
|
|
|
* @name addDate This function add input "number" with min, max and step (if you like) |
673
|
|
|
* @param string $label If is not empty, it show a text "label" with the text |
674
|
|
|
* @param string $name Element name |
675
|
|
|
* @param string $value Default value |
676
|
|
|
* @param string $required Is required? |
677
|
|
|
* @param string $min Minium value. Empty nothing |
678
|
|
|
* @param string $max Max value. Empty nothing |
679
|
|
|
* @param string $step Step (multiply). Empty nothing (ex: step 2: -2, 0, 2, 4 ...) |
680
|
|
|
* @param string $placeholder Default text to show if box is empty |
681
|
|
|
* @param unknown $attributes Additional attributes |
682
|
|
|
*/ |
683
|
|
View Code Duplication |
public function addDate($label = '', $name = '', $value = '', $required = false, $min = '', $max = '', $step = '', $placeholder = '', $attributes = array()) { |
684
|
|
|
// Creating main data |
685
|
|
|
$data = array( |
686
|
|
|
'type' => 'date', |
687
|
|
|
'name' => $name, |
688
|
|
|
'value' => $value, |
689
|
|
|
); |
690
|
|
|
|
691
|
|
|
if ($required) $data['required'] = 'required'; |
|
|
|
|
692
|
|
|
if ($min) $data['min'] = $min; |
693
|
|
|
if ($max) $data['max'] = $max; |
694
|
|
|
if ($step) $data['step'] = $step; |
695
|
|
|
if ($placeholder != '') $data['placeholder'] = $placeholder; |
696
|
|
|
if (!empty($attributes)) array_merge($data, $attributes); |
697
|
|
|
|
698
|
|
|
// Saving data to object |
699
|
|
|
$content = array( |
700
|
|
|
'type' => 'input', |
701
|
|
|
'data' => $data |
702
|
|
|
); |
703
|
|
|
if ($label != '') $content['label'] = $label; |
704
|
|
|
|
705
|
|
|
$this->content[] = $content; |
706
|
|
|
} |
707
|
|
|
|
708
|
|
|
|
709
|
|
|
/** |
710
|
|
|
* @name addCheck |
711
|
|
|
* @tutorial Use this function to add a checkBox |
712
|
|
|
* @param string $label If is not empty, it show a text "label" with the text |
713
|
|
|
* @param string $name Check name |
714
|
|
|
* @param string $value Check value |
715
|
|
|
* @param boolean $required Specify if check is required |
716
|
|
|
* @param array $attributes Optional atributes not listened (in array). |
717
|
|
|
*/ |
718
|
|
View Code Duplication |
public function addCheck($label = '', $name = '', $value = '', $required = false, $attributes = array()) { |
719
|
|
|
$data = array( |
720
|
|
|
'type' => 'checkbox', |
721
|
|
|
'name' => $name, |
722
|
|
|
'value' => $value |
723
|
|
|
); |
724
|
|
|
if ($required) $data['required'] = 'required'; |
725
|
|
|
if (!empty($attributes)) array_merge($data, $attributes); |
726
|
|
|
|
727
|
|
|
$content = array( |
728
|
|
|
'type' => 'input', |
729
|
|
|
'data' => $data |
730
|
|
|
); |
731
|
|
|
if ($label != '') $content['label'] = $label; |
732
|
|
|
|
733
|
|
|
$this->content[] = $content; |
734
|
|
|
} |
735
|
|
|
|
736
|
|
|
|
737
|
|
|
/** |
738
|
|
|
* @name addRadio |
739
|
|
|
* @tutorial Use this function to add a radio button |
740
|
|
|
* @param string $label If is not empty, it show a text "label" with the text |
741
|
|
|
* @param string $name Radio button name |
742
|
|
|
* @param array $values Contain all radio with values and text to show |
743
|
|
|
* EX: array('val1'=>'text1', 'val2'=>'text2'); |
744
|
|
|
* @param string $selected Specify wich option (ID) will be checked by default |
745
|
|
|
* @param boolean $required Specify if radio is required |
746
|
|
|
* @param array $attributes Optional atributes not listened (in array). This will be applicated to all radioButton |
747
|
|
|
*/ |
748
|
|
|
public function addRadio($label = '', $name = '', $values = array(), $selected = null, $required = false, $attributes = array()) { |
749
|
|
|
if (!is_array($values)) die('FATAL ERROR: Trying to create "RadioButton" with string VALUE, is requried use ARRAY()'); |
|
|
|
|
750
|
|
|
$data = array( |
751
|
|
|
'name' => $name, |
752
|
|
|
); |
753
|
|
|
|
754
|
|
|
if ($required) $data['required'] = 'required'; |
755
|
|
|
if (!empty($attributes)) array_merge($data, $attributes); |
756
|
|
|
|
757
|
|
|
|
758
|
|
|
|
759
|
|
|
$content = array( |
760
|
|
|
'type' => 'radio', |
761
|
|
|
'data' => $data, |
762
|
|
|
'values' => $values, |
763
|
|
|
'selected' => $selected |
764
|
|
|
); |
765
|
|
|
if ($label != '') $content['label'] = $label; |
766
|
|
|
|
767
|
|
|
$this->content[] = $content; |
768
|
|
|
} |
769
|
|
|
|
770
|
|
|
|
771
|
|
|
/** |
772
|
|
|
* @name addSelect |
773
|
|
|
* @tutorial Use this function to add a radio button |
774
|
|
|
* @param string $label Text to show before input |
775
|
|
|
* @param string $name Select name |
776
|
|
|
* @param array $values Array('value'=>'text to show') |
777
|
|
|
* @param boolean $required Specify if select is required |
778
|
|
|
* @param string $id ID Specify ID to manipulate via javascript or CSS |
|
|
|
|
779
|
|
|
* @param array $attributes Optional atributes not listened (in array). |
780
|
|
|
*/ |
781
|
|
|
public function addSelect($label = '', $name = '', $values = '', $selected = null, $required = false, $multiple = false, $attributes = array()) { |
782
|
|
|
$data = array( |
783
|
|
|
'name' => $name, |
784
|
|
|
'required' => $required, |
785
|
|
|
); |
786
|
|
|
if ($required) $data['required'] = 'required'; |
787
|
|
|
if ($multiple) $data['multiple'] = 'multiple'; |
788
|
|
|
if (!empty($attributes)) array_merge($data, $attributes); |
789
|
|
|
|
790
|
|
|
|
791
|
|
|
// In this case, the values are saved in the main of array to speed up select loader |
792
|
|
|
$content = array( |
793
|
|
|
'type' => 'select', |
794
|
|
|
'data' => $data, |
795
|
|
|
'values' => $values, |
796
|
|
|
'selected' => $selected |
797
|
|
|
); |
798
|
|
|
if ($label != '') $content['label'] = $label; |
799
|
|
|
|
800
|
|
|
$this->content[] = $content; |
801
|
|
|
} |
802
|
|
|
|
803
|
|
|
/** |
804
|
|
|
* @name addSubmit |
805
|
|
|
* @tutorial Use this function to add a submit button |
806
|
|
|
* @param string $name Select name (optional, if leav blank it will not send in POST / GET) |
807
|
|
|
* @param string $value Text to show in button and ID |
808
|
|
|
* @param array $attributes Optional atributes not listened (in array). |
809
|
|
|
*/ |
810
|
|
View Code Duplication |
public function addSubmit($name = '', $value = '', $attributes = array()) { |
811
|
|
|
$data = array( |
812
|
|
|
'type' => 'submit', |
813
|
|
|
'name' => $name, |
814
|
|
|
'value' => $value |
815
|
|
|
); |
816
|
|
|
if (!empty($attributes)) array_merge($data, $attributes); |
817
|
|
|
|
818
|
|
|
$this->content[] = array( |
819
|
|
|
'type' => 'input', |
820
|
|
|
'data' => $data |
821
|
|
|
); |
822
|
|
|
|
823
|
|
|
|
824
|
|
|
} |
825
|
|
|
|
826
|
|
|
/** |
827
|
|
|
* @name addButton |
828
|
|
|
* @tutorial Use this function to add a button with "onclick" action (or id/class to call via jquery) |
829
|
|
|
* @param string $value Name to show in button and ID |
830
|
|
|
* @param string $onClick Action to load when button is clicked |
831
|
|
|
* @param array $attributes Optional atributes not listened (in array). |
832
|
|
|
*/ |
833
|
|
View Code Duplication |
public function addButton($value = '', $onClick = '', $attributes = array()) { |
834
|
|
|
$data = array( |
835
|
|
|
'type' => 'button', |
836
|
|
|
'value' => $value, |
837
|
|
|
'onClick' => $onClick |
838
|
|
|
); |
839
|
|
|
if (!empty($attributes)) array_merge($data, $attributes); |
840
|
|
|
|
841
|
|
|
$this->content[] = array( |
842
|
|
|
'type' => 'input', |
843
|
|
|
'data' => $data |
844
|
|
|
); |
845
|
|
|
} |
846
|
|
|
|
847
|
|
|
|
848
|
|
|
/** |
849
|
|
|
* @name addButton |
850
|
|
|
* @tutorial Use this function to add a button with "onclick" action (or id/class to call via jquery) |
851
|
|
|
* @param string $value Name to show in button and ID |
852
|
|
|
* @param string $onClick Action to load when button is clicked |
|
|
|
|
853
|
|
|
* @param array $attributes Optional atributes not listened (in array). |
854
|
|
|
*/ |
855
|
|
View Code Duplication |
public function addTextArea($label = '', $name = '', $value = '', $required = false, $attributes = array()) { |
856
|
|
|
// Creating main data |
857
|
|
|
$data = array( |
858
|
|
|
'name' => $name, |
859
|
|
|
); |
860
|
|
|
|
861
|
|
|
if ($required) $data['required'] = 'required'; |
862
|
|
|
if (!empty($attributes)) array_merge($data, $attributes); |
863
|
|
|
|
864
|
|
|
// Saving data to object |
865
|
|
|
$content = array( |
866
|
|
|
'type' => 'textarea', |
867
|
|
|
'text' => $value, |
868
|
|
|
'data' => $data |
869
|
|
|
); |
870
|
|
|
if ($label != '') $content['label'] = $label; |
871
|
|
|
|
872
|
|
|
$this->content[] = $content; |
873
|
|
|
} |
874
|
|
|
|
875
|
|
|
/* |
876
|
|
|
* OTHER |
877
|
|
|
*/ |
878
|
|
|
/** |
879
|
|
|
* @name log |
880
|
|
|
* @tutorial Save a log if is enabled logging |
881
|
|
|
* @param boolean $save If is true, save the log |
|
|
|
|
882
|
|
|
* @param string $message Message to log |
883
|
|
|
*/ |
884
|
|
|
private function log($message, $data = array()) { |
885
|
|
|
if ($this->logErrors) { |
886
|
|
|
// Try to get a name (label or name) |
887
|
|
|
if (class_exists('kw')) { |
888
|
|
View Code Duplication |
if (isset($data['label'])) $this->errors .= 'Found an error in field' . ': "' . $data['label'] . '": '; |
889
|
|
|
elseif (isset($data['name'])) $this->errors .= 'Found an error in field' . ': "' . $data['name'] . '": '; |
890
|
|
View Code Duplication |
} else { |
891
|
|
|
if (isset($data['label'])) $this->errors .= 'Found an error in field' . $data['label'] . '": '; |
892
|
|
|
elseif (isset($data['name'])) $this->errors .= 'Found an error in field' . ': "' . $data['name'] . '": '; |
893
|
|
|
} |
894
|
|
|
|
895
|
|
|
// preparing message |
896
|
|
|
$this->errors .= ' ' . $message . '.'; |
897
|
|
|
|
898
|
|
|
// Extra message (title attribute) |
899
|
|
|
if (isset($data['title']) and $data['title'] != '') $this->errors .= ' | '. 'MESSAGE' .': ' . $data['title']; |
|
|
|
|
900
|
|
|
|
901
|
|
|
$this->errors .= '<br />'; |
902
|
|
|
} |
903
|
|
|
} |
904
|
|
|
} |
905
|
|
|
|
906
|
|
|
|
907
|
|
|
/** |
908
|
|
|
* @author: http://arafatbd.net/post-item/215/php-function-to-check-valid-date/ |
909
|
|
|
* @name is_date The function is_date() validates the date and returns true or false |
910
|
|
|
* @param $str sting expected valid date format |
911
|
|
|
* @return bool returns true if the supplied parameter is a valid date |
912
|
|
|
* otherwise false |
913
|
|
|
*/ |
914
|
|
|
// More examples: |
915
|
|
|
// http://us2.php.net/manual/es/function.checkdate.php |
916
|
|
|
function is_date( $str ) { |
917
|
|
|
// Try to execute date creator |
918
|
|
|
try { |
919
|
|
|
$dt = new DateTime( trim($str) ); |
920
|
|
|
} catch( Exception $e ) { |
|
|
|
|
921
|
|
|
// If fails, it return false, date is incorrect |
922
|
|
|
return false; |
923
|
|
|
} |
924
|
|
|
|
925
|
|
|
// Checking date |
926
|
|
|
$month = $dt->format('m'); |
927
|
|
|
$day = $dt->format('d'); |
928
|
|
|
$year = $dt->format('Y'); |
929
|
|
|
|
930
|
|
|
// Date is ok |
931
|
|
|
if (checkdate($month, $day, $year)) { |
932
|
|
|
return true; |
933
|
|
|
} else { |
934
|
|
|
return false; |
935
|
|
|
} |
936
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.