FileCategory::addName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Files\Form\Factory;
6
7
use AbterPhp\Admin\Domain\Entities\UserGroup;
8
use AbterPhp\Admin\Form\Factory\Base;
9
use AbterPhp\Admin\Orm\UserGroupRepo;
10
use AbterPhp\Files\Domain\Entities\FileCategory as Entity;
11
use AbterPhp\Framework\Constant\Html5;
12
use AbterPhp\Framework\Form\Component\Option;
13
use AbterPhp\Framework\Form\Container\CheckboxGroup;
14
use AbterPhp\Framework\Form\Container\FormGroup;
15
use AbterPhp\Framework\Form\Element\Input;
16
use AbterPhp\Framework\Form\Element\MultiSelect;
17
use AbterPhp\Framework\Form\Element\Select;
18
use AbterPhp\Framework\Form\Extra\Help;
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 FileCategory extends Base
26
{
27
    /** @var UserGroupRepo */
28
    protected $userGroupRepo;
29
30
    /**
31
     * FileCategory constructor.
32
     *
33
     * @param ISession      $session
34
     * @param ITranslator   $translator
35
     * @param UserGroupRepo $userGroupRepo
36
     */
37
    public function __construct(ISession $session, ITranslator $translator, UserGroupRepo $userGroupRepo)
38
    {
39
        parent::__construct($session, $translator);
40
41
        $this->userGroupRepo = $userGroupRepo;
42
    }
43
44
    /**
45
     * @param string       $action
46
     * @param string       $method
47
     * @param string       $showUrl
48
     * @param IEntity|null $entity
49
     *
50
     * @return IForm
51
     */
52
    public function create(string $action, string $method, string $showUrl, ?IEntity $entity = null): IForm
53
    {
54
        assert($entity instanceof Entity, new \InvalidArgumentException());
55
56
        $this->createForm($action, $method)
57
            ->addDefaultElements()
58
            ->addName($entity)
59
            ->addIdentifier($entity)
60
            ->addIsPublic($entity)
61
            ->addUserGroups($entity)
62
            ->addDefaultButtons($showUrl);
63
64
        $form = $this->form;
65
66
        $this->form = null;
67
68
        return $form;
69
    }
70
71
    /**
72
     * @param Entity $entity
73
     *
74
     * @return $this
75
     */
76
    protected function addName(Entity $entity): FileCategory
77
    {
78
        $input = new Input('name', 'name', $entity->getName());
79
        $label = new Label('name', 'files:fileCategoryName');
80
81
        $this->form[] = new FormGroup($input, $label, null, [], [Html5::ATTR_CLASS => FormGroup::CLASS_REQUIRED]);
82
83
        return $this;
84
    }
85
86
    /**
87
     * @param Entity $entity
88
     *
89
     * @return $this
90
     */
91
    protected function addIdentifier(Entity $entity): FileCategory
92
    {
93
        $input = new Input(
94
            'identifier',
95
            'identifier',
96
            $entity->getIdentifier(),
97
            [],
98
            [Html5::ATTR_CLASS => 'semi-auto']
99
        );
100
        $label = new Label('identifier', 'files:fileCategoryIdentifier');
101
        $help  = new Help('files:fileCategoryIdentifierHelp');
102
103
        $this->form[] = new FormGroup($input, $label, $help);
104
105
        return $this;
106
    }
107
108
    /**
109
     * @param Entity $entity
110
     *
111
     * @return $this
112
     */
113
    protected function addIsPublic(Entity $entity): FileCategory
114
    {
115
        $attributes = [Html5::ATTR_TYPE => [Input::TYPE_CHECKBOX]];
116
        if ($entity->isPublic()) {
117
            $attributes[Html5::ATTR_CHECKED] = null;
118
        }
119
        $input = new Input(
120
            'is_public',
121
            'is_public',
122
            '1',
123
            [],
124
            $attributes
125
        );
126
        $label = new Label(
127
            'is_public',
128
            'files:fileCategoryIsPublic'
129
        );
130
        $help  = new Help('files:fileCategoryIsPublicHelp');
131
132
        $this->form[] = new CheckboxGroup($input, $label, $help);
133
134
        return $this;
135
    }
136
137
    /**
138
     * @param Entity $entity
139
     *
140
     * @return $this
141
     */
142
    protected function addUserGroups(Entity $entity): FileCategory
143
    {
144
        $allUserGroups = $this->userGroupRepo->getAll();
145
        $userGroupIds  = $this->getUserGroupIds($entity);
146
147
        $options = $this->createUserGroupOptions($allUserGroups, $userGroupIds);
148
149
        $this->form[] = new FormGroup(
150
            $this->createUserGroupSelect($options),
151
            $this->createUserGroupLabel()
152
        );
153
154
        return $this;
155
    }
156
157
    /**
158
     * @param Entity $entity
159
     *
160
     * @return int[]
161
     */
162
    protected function getUserGroupIds(Entity $entity): array
163
    {
164
        $userGroupIds = [];
165
        foreach ($entity->getUserGroups() as $userGroup) {
166
            $userGroupIds[] = $userGroup->getId();
167
        }
168
169
        return $userGroupIds;
170
    }
171
172
    /**
173
     * @param UserGroup[] $allUserGroups
174
     * @param int[]       $userGroupIds
175
     *
176
     * @return array
177
     */
178
    protected function createUserGroupOptions(array $allUserGroups, array $userGroupIds): array
179
    {
180
        $options = [];
181
        foreach ($allUserGroups as $userGroup) {
182
            $isSelected = in_array($userGroup->getId(), $userGroupIds, true);
183
            $options[]  = new Option((string)$userGroup->getId(), $userGroup->getName(), $isSelected);
184
        }
185
186
        return $options;
187
    }
188
189
    /**
190
     * @param Option[] $options
191
     *
192
     * @return Select
193
     */
194
    protected function createUserGroupSelect(array $options): Select
195
    {
196
        $attributes = [
197
            Html5::ATTR_SIZE => $this->getMultiSelectSize(
198
                count($options),
199
                static::MULTISELECT_MIN_SIZE,
200
                static::MULTISELECT_MAX_SIZE
201
            ),
202
        ];
203
204
        $select = new MultiSelect('user_group_ids', 'user_group_ids[]', [], $attributes);
205
206
        foreach ($options as $option) {
207
            $select[] = $option;
208
        }
209
210
        return $select;
211
    }
212
213
    /**
214
     * @return Label
215
     */
216
    protected function createUserGroupLabel(): Label
217
    {
218
        return new Label('user_group_ids', 'files:fileCategoryUserGroups');
219
    }
220
221
    /**
222
     * @param int $optionCount
223
     * @param int $minSize
224
     * @param int $maxSize
225
     *
226
     * @return int
227
     */
228
    protected function getMultiSelectSize(int $optionCount, int $minSize, int $maxSize): int
229
    {
230
        return (int)max(min($optionCount, $maxSize), $minSize);
231
    }
232
}
233