1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Admin\Form\Factory; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Admin\Domain\Entities\AdminResource; |
8
|
|
|
use AbterPhp\Admin\Domain\Entities\UserApiKey as Entity; |
9
|
|
|
use AbterPhp\Admin\Orm\AdminResourceRepo; |
10
|
|
|
use AbterPhp\Framework\Constant\Html5; |
11
|
|
|
use AbterPhp\Framework\Constant\Session; |
12
|
|
|
use AbterPhp\Framework\Form\Component\Option; |
13
|
|
|
use AbterPhp\Framework\Form\Container\FormGroup; |
14
|
|
|
use AbterPhp\Framework\Form\Element\MultiSelect; |
15
|
|
|
use AbterPhp\Framework\Form\Element\Select; |
16
|
|
|
use AbterPhp\Framework\Form\Element\Textarea; |
17
|
|
|
use AbterPhp\Framework\Form\Factory\Base; |
18
|
|
|
use AbterPhp\Framework\Form\Factory\IFormFactory; |
19
|
|
|
use AbterPhp\Framework\Form\IForm; |
20
|
|
|
use AbterPhp\Framework\Form\Label\Label; |
21
|
|
|
use AbterPhp\Framework\I18n\ITranslator; |
22
|
|
|
use Opulence\Orm\IEntity; |
23
|
|
|
use Opulence\Sessions\ISession; |
24
|
|
|
|
25
|
|
|
class UserApiKey extends Base |
26
|
|
|
{ |
27
|
|
|
/** @var AdminResourceRepo */ |
28
|
|
|
protected $adminResourceRepo; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* UserApiKey constructor. |
32
|
|
|
* |
33
|
|
|
* @param ISession $session |
34
|
|
|
* @param ITranslator $translator |
35
|
|
|
* @param AdminResourceRepo $adminResourceRepo |
36
|
|
|
*/ |
37
|
|
|
public function __construct( |
38
|
|
|
ISession $session, |
39
|
|
|
ITranslator $translator, |
40
|
|
|
AdminResourceRepo $adminResourceRepo |
41
|
|
|
) { |
42
|
|
|
parent::__construct($session, $translator); |
43
|
|
|
|
44
|
|
|
$this->adminResourceRepo = $adminResourceRepo; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $action |
49
|
|
|
* @param string $method |
50
|
|
|
* @param string $showUrl |
51
|
|
|
* @param IEntity|null $entity |
52
|
|
|
* |
53
|
|
|
* @return IForm |
54
|
|
|
*/ |
55
|
|
|
public function create(string $action, string $method, string $showUrl, ?IEntity $entity = null): IForm |
56
|
|
|
{ |
57
|
|
|
if (!($entity instanceof Entity)) { |
58
|
|
|
throw new \InvalidArgumentException(IFormFactory::ERR_MSG_ENTITY_MISSING); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->createForm($action, $method) |
62
|
|
|
->addDefaultElements() |
63
|
|
|
->addDescription($entity) |
64
|
|
|
->addAdminResources($entity) |
65
|
|
|
->addDefaultButtons($showUrl); |
66
|
|
|
|
67
|
|
|
$form = $this->form; |
68
|
|
|
|
69
|
|
|
$this->form = null; |
70
|
|
|
|
71
|
|
|
return $form; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param Entity $entity |
76
|
|
|
* |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
|
|
protected function addDescription(Entity $entity): UserApiKey |
80
|
|
|
{ |
81
|
|
|
$input = new Textarea( |
82
|
|
|
'description', |
83
|
|
|
'description', |
84
|
|
|
$entity->getDescription() |
85
|
|
|
); |
86
|
|
|
$label = new Label('description', 'admin:userApiKeyDescription'); |
87
|
|
|
|
88
|
|
|
$this->form[] = new FormGroup($input, $label); |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param Entity $entity |
95
|
|
|
* |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
|
|
protected function addAdminResources(Entity $entity): UserApiKey |
99
|
|
|
{ |
100
|
|
|
$allUserResources = $this->getUserResources(); |
101
|
|
|
|
102
|
|
|
$existingData = []; |
103
|
|
|
foreach ($entity->getAdminResources() as $adminResource) { |
104
|
|
|
$existingData[$adminResource->getId()] = $adminResource->getIdentifier(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$options = $this->createAdminResourceOptions($allUserResources, $existingData); |
108
|
|
|
|
109
|
|
|
$this->form[] = new FormGroup( |
110
|
|
|
$this->createAdminResourceSelect($options), |
111
|
|
|
$this->createAdminResourceLabel() |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return UserGroup[] |
119
|
|
|
*/ |
120
|
|
|
protected function getUserResources(): array |
121
|
|
|
{ |
122
|
|
|
$userId = $this->session->get(Session::USER_ID); |
123
|
|
|
|
124
|
|
|
return $this->adminResourceRepo->getByUserId($userId); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param AdminResource[] $allUserResources |
129
|
|
|
* @param string[] $existingData |
130
|
|
|
* |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
|
protected function createAdminResourceOptions(array $allUserResources, array $existingData): array |
134
|
|
|
{ |
135
|
|
|
$existingIds = array_keys($existingData); |
136
|
|
|
|
137
|
|
|
$options = []; |
138
|
|
|
foreach ($allUserResources as $userResources) { |
139
|
|
|
$isSelected = in_array($userResources->getId(), $existingIds, true); |
140
|
|
|
$options[] = new Option($userResources->getId(), $userResources->getIdentifier(), $isSelected); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $options; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param Option[] $options |
148
|
|
|
* |
149
|
|
|
* @return Select |
150
|
|
|
*/ |
151
|
|
|
protected function createAdminResourceSelect(array $options): Select |
152
|
|
|
{ |
153
|
|
|
$attributes = [ |
154
|
|
|
Html5::ATTR_SIZE => $this->getMultiSelectSize( |
155
|
|
|
count($options), |
156
|
|
|
static::MULTISELECT_MIN_SIZE, |
157
|
|
|
static::MULTISELECT_MAX_SIZE |
158
|
|
|
), |
159
|
|
|
]; |
160
|
|
|
|
161
|
|
|
$select = new MultiSelect('admin_resource_ids', 'admin_resource_ids[]', [], $attributes); |
162
|
|
|
|
163
|
|
|
foreach ($options as $option) { |
164
|
|
|
$select[] = $option; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $select; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return Label |
172
|
|
|
*/ |
173
|
|
|
protected function createAdminResourceLabel(): Label |
174
|
|
|
{ |
175
|
|
|
return new Label('admin_resource_ids', 'admin:adminResources'); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|