|
1
|
|
|
<?php |
|
2
|
|
|
namespace WebServCo\Framework; |
|
3
|
|
|
|
|
4
|
|
|
abstract class AbstractForm extends \WebServCo\Framework\AbstractLibrary |
|
5
|
|
|
{ |
|
6
|
|
|
protected $errors; |
|
7
|
|
|
|
|
8
|
|
|
protected $filtered; |
|
9
|
|
|
|
|
10
|
|
|
protected $submitFields; |
|
11
|
|
|
|
|
12
|
|
|
protected $submitField; |
|
13
|
|
|
|
|
14
|
|
|
protected $valid; |
|
15
|
|
|
|
|
16
|
|
|
use \WebServCo\Framework\Traits\ExposeLibrariesTrait; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct($settings, $defaultData = [], $submitFields = []) |
|
19
|
|
|
{ |
|
20
|
|
|
parent::__construct($settings); |
|
21
|
|
|
|
|
22
|
|
|
$this->submitFields = $submitFields; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Set form data |
|
26
|
|
|
*/ |
|
27
|
|
|
foreach ($this->setting('meta', []) as $field => $title) { |
|
28
|
|
|
if ($this->isSent()) { |
|
29
|
|
|
$data = $this->request()->data($field); |
|
30
|
|
|
} else { |
|
31
|
|
|
$data = \WebServCo\Framework\Utils::arrayKey($field, $defaultData, null); |
|
32
|
|
|
} |
|
33
|
|
|
$this->setData($field, $data); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$this->errors = []; |
|
37
|
|
|
|
|
38
|
|
|
$this->filtered = $this->filter(); |
|
39
|
|
|
|
|
40
|
|
|
if ($this->isSent()) { |
|
41
|
|
|
$this->valid = $this->validate(); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
abstract protected function db(); |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return bool |
|
49
|
|
|
*/ |
|
50
|
|
|
abstract protected function filter(); |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return bool |
|
54
|
|
|
*/ |
|
55
|
|
|
abstract protected function validate(); |
|
56
|
|
|
|
|
57
|
|
|
final public function isSent() |
|
58
|
|
|
{ |
|
59
|
|
|
if (!empty($this->submitFields)) { |
|
60
|
|
|
foreach ($this->submitFields as $field) { |
|
61
|
|
|
if ($this->request()->data($field)) { |
|
62
|
|
|
$this->submitField = $field; |
|
63
|
|
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
return $this->request()->getMethod() === \WebServCo\Framework\Http::METHOD_POST; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
final public function getSubmitField() |
|
72
|
|
|
{ |
|
73
|
|
|
if (!$this->isSent() || empty($this->submitFields)) { |
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
return $this->submitField; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
final public function isValid() |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->valid; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
final public function clear() |
|
85
|
|
|
{ |
|
86
|
|
|
$this->data = []; |
|
87
|
|
|
$this->filtered = []; |
|
88
|
|
|
$this->errors = []; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
final public function toArray() |
|
92
|
|
|
{ |
|
93
|
|
|
return [ |
|
94
|
|
|
'meta' => $this->setting('meta', []), |
|
95
|
|
|
'help' => $this->setting('help', []), |
|
96
|
|
|
'required' => array_fill_keys($this->setting('required', []), true), |
|
97
|
|
|
'data' => $this->data, |
|
98
|
|
|
'errors' => $this->errors, |
|
99
|
|
|
]; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|