Form::_construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * File: Form.php
4
 *
5
 * @author      Maciej Sławik <[email protected]>
6
 * Github:      https://github.com/maciejslawik
7
 */
8
9
namespace MSlwk\ICatalogue\Block\Adminhtml\Catalogue\Edit;
10
11
use Magento\Backend\Block\Template\Context;
12
use Magento\Backend\Block\Widget\Form\Generic;
13
use Magento\Cms\Model\Wysiwyg\Config;
14
use Magento\Framework\Data\Form as DataForm;
15
use Magento\Framework\Data\FormFactory;
16
use Magento\Framework\Registry;
17
use Magento\Store\Model\System\Store;
18
use MSlwk\ICatalogue\Model\Catalogue;
19
use MSlwk\ICatalogue\Api\CatalogueInterface;
20
21
/**
22
 * Class Form
23
 *
24
 * @package MSlwk\ICatalogue\Block\Adminhtml\Catalogue\Edit
25
 */
26
class Form extends Generic
27
{
28
    /**
29
     * @var Store
30
     */
31
    protected $systemStore;
32
33
    /**
34
     * Form constructor.
35
     *
36
     * @param Store $systemStore
37
     * @param Context $context
38
     * @param Registry $registry
39
     * @param FormFactory $formFactory
40
     * @param array $data
41
     */
42
    public function __construct(
43
        Store $systemStore,
44
        Context $context,
45
        Registry $registry,
46
        FormFactory $formFactory,
47
        array $data = []
48
    ) {
49
        $this->systemStore = $systemStore;
50
        parent::__construct($context, $registry, $formFactory, $data);
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    protected function _construct()
57
    {
58
        parent::_construct();
59
        $this->setId('mslwk_icatalogue_catalogue_form');
60
        $this->setTitle(__('iCatalogue'));
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    protected function _prepareForm()
67
    {
68
        /** @var Catalogue $catalogue */
69
        $catalogue = $this->_coreRegistry->registry('mslwk_icatalogue_catalogue');
70
71
        /** @var DataForm $form */
72
        $form = $this->_formFactory->create(
73
            [
74
                'data' => [
75
                    'id' => 'edit_form',
76
                    'action' => $this->getData('action'),
77
                    'method' => 'post',
78
                    'enctype' => 'multipart/form-data'
79
                ]
80
            ]
81
        );
82
83
        $form->setHtmlIdPrefix('catalogue_');
84
85
        /**
86
         * General fieldset
87
         */
88
        $fieldsetGeneral = $form->addFieldset(
89
            'general_fieldset',
90
            [
91
                'legend' => __('General Information'),
92
                'class' => 'fieldset-wide'
93
            ]
94
        );
95
96
        if ($catalogue->getId()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $catalogue->getId() of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
97
            $url = '/icatalogue/catalogue/view/id/' . $catalogue->getId();
98
            $catalogue->setUrl($url);
99
            $fieldsetGeneral->addField(
100
                'url',
101
                'text',
102
                [
103
                    'name' => 'url',
104
                    'readonly' => true,
105
                    'label' => __('URL'),
106
                    'title' => __('URL'),
107
                    'after_element_html' => '<a href=' . $url . '>'. __('View').'</a>'
108
                ]
109
            );
110
        }
111
112
        $fieldsetGeneral->addField(
113
            CatalogueInterface::TITLE,
114
            'text',
115
            [
116
                'name' => CatalogueInterface::TITLE,
117
                'label' => __('Title'),
118
                'title' => __('Title'),
119
                'required' => true
120
            ]
121
        );
122
        $fieldsetGeneral->addField(
123
            'stores',
124
            'multiselect',
125
            [
126
                'name' => 'stores[]',
127
                'label' => __('Store Views'),
128
                'title' => __('Store Views'),
129
                'required' => true,
130
                'values' => $this->systemStore->getStoreValuesForForm(false, true),
131
            ]
132
        );
133
134
        /**
135
         * Images fieldset
136
         */
137
        $fieldsetImages = $form->addFieldset(
138
            'images_fieldset',
139
            [
140
                'legend' => __('Images'),
141
                'class' => 'fieldset-wide'
142
            ]
143
        );
144
        $fieldsetImages->addField(
145
            'images',
146
            'MSlwk\ICatalogue\Form\Element\Gallery',
147
            [
148
                'name' => 'images',
149
                'label' => __('Image'),
150
                'title' => __('Image')
151
            ]
152
        );
153
154
        /** Catalogue ID */
155
        if ($catalogue->getId()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $catalogue->getId() of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
156
            $fieldsetGeneral->addField(
157
                CatalogueInterface::CATALOGUE_ID,
158
                'hidden',
159
                [
160
                    'name' => CatalogueInterface::CATALOGUE_ID
161
                ]
162
            );
163
        }
164
165
        $form->setValues($catalogue->getData());
166
        $form->setUseContainer(true);
167
        $this->setForm($form);
168
169
        return parent::_prepareForm();
170
    }
171
}
172