Issues (106)

src/Form/Factory/Block.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\Form\Factory;
6
7
use AbterPhp\Admin\Form\Factory\Base;
8
use AbterPhp\Framework\Constant\Html5;
9
use AbterPhp\Framework\Constant\Session;
10
use AbterPhp\Framework\Form\Component\Option;
11
use AbterPhp\Framework\Form\Container\FormGroup;
12
use AbterPhp\Framework\Form\Element\Input;
13
use AbterPhp\Framework\Form\Element\Select;
14
use AbterPhp\Framework\Form\Element\Textarea;
15
use AbterPhp\Framework\Form\Extra\Help;
16
use AbterPhp\Framework\Form\IForm;
17
use AbterPhp\Framework\Form\Label\Countable;
18
use AbterPhp\Framework\Form\Label\Label;
19
use AbterPhp\Framework\I18n\ITranslator;
20
use AbterPhp\Website\Constant\Authorization;
21
use AbterPhp\Website\Domain\Entities\Block as Entity;
22
use AbterPhp\Website\Domain\Entities\BlockLayout;
0 ignored issues
show
This use statement conflicts with another class in this namespace, AbterPhp\Website\Form\Factory\BlockLayout. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
23
use AbterPhp\Website\Orm\BlockLayoutRepo;
24
use Casbin\Enforcer;
25
use Opulence\Orm\IEntity;
26
use Opulence\Sessions\ISession;
27
28
class Block extends Base
29
{
30
    /** @var BlockLayoutRepo */
31
    protected $layoutRepo;
32
33
    /** @var Enforcer */
34
    protected $enforcer;
35
36
    /**
37
     * Block constructor.
38
     *
39
     * @param ISession        $session
40
     * @param ITranslator     $translator
41
     * @param BlockLayoutRepo $layoutRepo
42
     * @param Enforcer        $enforcer
43
     */
44
    public function __construct(
45
        ISession $session,
46
        ITranslator $translator,
47
        BlockLayoutRepo $layoutRepo,
48
        Enforcer $enforcer
49
    ) {
50
        parent::__construct($session, $translator);
51
52
        $this->layoutRepo = $layoutRepo;
53
        $this->enforcer   = $enforcer;
54
    }
55
56
    /**
57
     * @param string       $action
58
     * @param string       $method
59
     * @param string       $showUrl
60
     * @param IEntity|null $entity
61
     *
62
     * @return IForm
63
     * @throws \Casbin\Exceptions\CasbinException
64
     */
65
    public function create(string $action, string $method, string $showUrl, ?IEntity $entity = null): IForm
66
    {
67
        assert($entity instanceof Entity, new \InvalidArgumentException());
68
69
        $username        = $this->session->get(Session::USERNAME);
70
        $advancedAllowed = $this->enforcer->enforce(
71
            $username,
72
            Authorization::RESOURCE_BLOCKS,
73
            Authorization::ROLE_ADVANCED_WRITE
74
        );
75
76
        $this->createForm($action, $method)
77
            ->addDefaultElements()
78
            ->addTitle($entity)
79
            ->addIdentifier($entity)
80
            ->addBody($entity)
81
            ->addLayoutId($entity, $advancedAllowed)
82
            ->addLayout($entity, $advancedAllowed)
83
            ->addDefaultButtons($showUrl);
84
85
        $form = $this->form;
86
87
        $this->form = null;
88
89
        return $form;
90
    }
91
92
    /**
93
     * @param Entity $entity
94
     *
95
     * @return $this
96
     */
97
    protected function addTitle(Entity $entity): Block
98
    {
99
        $input = new Input('title', 'title', $entity->getTitle());
100
        $label = new Label('title', 'website:blockTitle');
101
102
        $this->form[] = new FormGroup($input, $label, null, [], [Html5::ATTR_CLASS => FormGroup::CLASS_REQUIRED]);
103
104
        return $this;
105
    }
106
107
    /**
108
     * @param Entity $entity
109
     *
110
     * @return $this
111
     */
112
    protected function addIdentifier(Entity $entity): Block
113
    {
114
        $input = new Input(
115
            'identifier',
116
            'identifier',
117
            $entity->getIdentifier(),
118
            [],
119
            [Html5::ATTR_CLASS => 'semi-auto']
120
        );
121
        $label = new Label('identifier', 'website:blockIdentifier');
122
        $help  = new Help('website:blockIdentifierHelp');
123
124
        $this->form[] = new FormGroup($input, $label, $help);
125
126
        return $this;
127
    }
128
129
    /**
130
     * @param Entity $entity
131
     *
132
     * @return $this
133
     */
134
    protected function addBody(Entity $entity): Block
135
    {
136
        $attribs = [Html5::ATTR_CLASS => Textarea::CLASS_WYSIWYG, Html5::ATTR_ROWS => '15'];
137
        $input   = new Textarea('body', 'body', $entity->getBody(), [], $attribs);
138
        $label   = new Label('body', 'website:blockBody');
139
140
        $this->form[] = new FormGroup($input, $label);
141
142
        return $this;
143
    }
144
145
    /**
146
     * @param Entity $entity
147
     * @param bool   $advancedAllowed
148
     *
149
     * @return $this
150
     */
151
    protected function addLayoutId(Entity $entity, bool $advancedAllowed): Block
152
    {
153
        if (!$advancedAllowed && $entity->getId() && !$entity->getLayoutId()) {
154
            return $this;
155
        }
156
157
        $allLayouts = $this->getAllLayouts();
158
        $layoutId   = $entity->getLayoutId();
159
160
        $options = $this->createLayoutIdOptions($allLayouts, $layoutId, $advancedAllowed);
161
162
        $this->form[] = new FormGroup(
163
            $this->createLayoutIdSelect($options),
164
            $this->createLayoutIdLabel()
165
        );
166
167
        return $this;
168
    }
169
170
    /**
171
     * @return BlockLayout[]
172
     */
173
    protected function getAllLayouts(): array
174
    {
175
        return $this->layoutRepo->getAll();
176
    }
177
178
    /**
179
     * @param BlockLayout[] $allLayouts
180
     * @param string|null   $layoutId
181
     * @param bool          $advancedAllowed
182
     *
183
     * @return Option[]
184
     */
185
    protected function createLayoutIdOptions(array $allLayouts, ?string $layoutId, bool $advancedAllowed): array
186
    {
187
        $options = [];
188
        if ($advancedAllowed) {
189
            $options[] = new Option('', 'framework:none', false);
190
        }
191
        foreach ($allLayouts as $layout) {
192
            $isSelected = $layout->getId() === $layoutId;
193
            $options[]  = new Option($layout->getId(), $layout->getName(), $isSelected);
194
        }
195
196
        return $options;
197
    }
198
199
    /**
200
     * @param Option[] $options
201
     *
202
     * @return Select
203
     */
204
    protected function createLayoutIdSelect(array $options): Select
205
    {
206
        $select = new Select('layout_id', 'layout_id');
207
208
        foreach ($options as $option) {
209
            $select[] = $option;
210
        }
211
212
        return $select;
213
    }
214
215
    /**
216
     * @return Label
217
     */
218
    protected function createLayoutIdLabel(): Label
219
    {
220
        return new Label('layout_id', 'website:blockLayoutIdLabel');
221
    }
222
223
    /**
224
     * @param Entity $entity
225
     * @param bool   $advancedAllowed
226
     *
227
     * @return $this
228
     */
229
    protected function addLayout(Entity $entity, bool $advancedAllowed): Block
230
    {
231
        if (!$advancedAllowed) {
232
            return $this;
233
        }
234
235
        return $this->addLayoutTextarea($entity);
236
    }
237
238
    /**
239
     * @param Entity $entity
240
     *
241
     * @return $this
242
     */
243
    protected function addLayoutTextarea(Entity $entity): Block
244
    {
245
        $input   = new Textarea('layout', 'layout', $entity->getLayout(), [], [Html5::ATTR_ROWS => '15']);
246
        $label   = new Countable('description', 'website:blockLayoutLabel', Countable::DEFAULT_SIZE);
247
        $attribs = [Html5::ATTR_ID => 'layout-div', Html5::ATTR_CLASS => FormGroup::CLASS_COUNTABLE];
248
249
        $this->form[] = new FormGroup($input, $label, null, [], $attribs);
250
251
        return $this;
252
    }
253
}
254