1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Admin\Form\Factory; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Constant\Html5; |
8
|
|
|
use AbterPhp\Framework\Form\Element\Input; |
9
|
|
|
use AbterPhp\Framework\Form\Extra\DefaultButtons; |
10
|
|
|
use AbterPhp\Framework\Form\Form; |
11
|
|
|
use AbterPhp\Framework\I18n\ITranslator; |
12
|
|
|
use Opulence\Framework\Http\CsrfTokenChecker; |
13
|
|
|
use Opulence\Http\Requests\RequestMethods; |
14
|
|
|
use Opulence\Sessions\ISession; |
15
|
|
|
|
16
|
|
|
abstract class Base implements IFormFactory |
17
|
|
|
{ |
18
|
|
|
const MULTISELECT_MIN_SIZE = 3; |
19
|
|
|
const MULTISELECT_MAX_SIZE = 20; |
20
|
|
|
|
21
|
|
|
/** @var ISession */ |
22
|
|
|
protected $session; |
23
|
|
|
|
24
|
|
|
/** @var ITranslator */ |
25
|
|
|
protected $translator; |
26
|
|
|
|
27
|
|
|
/** @var Form */ |
28
|
|
|
protected $form; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Base constructor. |
32
|
|
|
* |
33
|
|
|
* @param ISession $session |
34
|
|
|
* @param ITranslator $translator |
35
|
|
|
*/ |
36
|
|
|
public function __construct(ISession $session, ITranslator $translator) |
37
|
|
|
{ |
38
|
|
|
$this->session = $session; |
39
|
|
|
$this->translator = $translator; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $action |
44
|
|
|
* @param string $method |
45
|
|
|
* @param bool $isMultipart |
46
|
|
|
* @param string[] $intents |
47
|
|
|
* @param string[][] $attributes |
48
|
|
|
* |
49
|
|
|
* @return $this |
50
|
|
|
*/ |
51
|
|
|
public function createForm( |
52
|
|
|
string $action, |
53
|
|
|
string $method, |
54
|
|
|
bool $isMultipart = false, |
55
|
|
|
$intents = [], |
56
|
|
|
$attributes = [] |
57
|
|
|
): Base { |
58
|
|
|
if ($isMultipart) { |
59
|
|
|
$attributes[Html5::ATTR_ENCTYPE] = [Form::ENCTYPE_MULTIPART]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$formMethod = $method == RequestMethods::GET ? $method : RequestMethods::POST; |
63
|
|
|
|
64
|
|
|
$this->form = new Form($action, $formMethod, $intents, $attributes); |
65
|
|
|
|
66
|
|
|
$this->addHttpMethod($method); |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $method |
73
|
|
|
*/ |
74
|
|
|
private function addHttpMethod(string $method) |
75
|
|
|
{ |
76
|
|
|
$this->form[] = new Input( |
77
|
|
|
'', |
78
|
|
|
Input::NAME_HTTP_METHOD, |
79
|
|
|
$method, |
80
|
|
|
[], |
81
|
|
|
[Html5::ATTR_TYPE => Input::TYPE_HIDDEN] |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
|
|
protected function addDefaultElements(): Base |
89
|
|
|
{ |
90
|
|
|
$name = CsrfTokenChecker::TOKEN_INPUT_NAME; |
91
|
|
|
$value = (string)$this->session->get($name); |
92
|
|
|
|
93
|
|
|
$attributes = [Html5::ATTR_TYPE => Input::TYPE_HIDDEN]; |
94
|
|
|
|
95
|
|
|
$this->form[] = new Input($name, $name, $value, [], $attributes); |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $showUrl |
102
|
|
|
* |
103
|
|
|
* @return Base |
104
|
|
|
*/ |
105
|
|
|
protected function addDefaultButtons(string $showUrl): Base |
106
|
|
|
{ |
107
|
|
|
$buttons = new DefaultButtons(); |
108
|
|
|
|
109
|
|
|
$buttons |
110
|
|
|
->addSaveAndBack() |
111
|
|
|
->addBackToGrid($showUrl) |
112
|
|
|
->addSaveAndEdit() |
113
|
|
|
->addSaveAndCreate(); |
114
|
|
|
|
115
|
|
|
$this->form[] = $buttons; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param int $optionCount |
122
|
|
|
* @param int $minSize |
123
|
|
|
* @param int $maxSize |
124
|
|
|
* |
125
|
|
|
* @return int |
126
|
|
|
*/ |
127
|
|
|
protected function getMultiSelectSize(int $optionCount, int $minSize, int $maxSize): int |
128
|
|
|
{ |
129
|
|
|
return (int)max(min($optionCount, $maxSize), $minSize); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|