Passed
Push — master ( 4d86c4...586e6b )
by Alex
02:58
created

Mezon/Gui/FormBuilder/FormBuilder.php (2 issues)

Severity
1
<?php
2
namespace Mezon\Gui\FormBuilder;
3
4
use Mezon\Gui\FieldsAlgorithms;
5
use Mezon\Gui\Field;
6
7
/**
8
 * Class FormBuilder
9
 *
10
 * @package Gui
11
 * @subpackage FormBuilder
12
 * @author Dodonov A.A.
13
 * @version v.1.0 (2019/08/13)
14
 * @copyright Copyright (c) 2019, aeon.org
15
 */
16
17
/**
18
 * Form builder class
19
 */
20
class FormBuilder
21
{
22
23
    /**
24
     * Fields algorithms
25
     *
26
     * @var FieldsAlgorithms
27
     */
28
    private $fieldsAlgorithms;
29
30
    /**
31
     * Session id
32
     *
33
     * @var string
34
     */
35
    private $sessionId = '';
36
37
    /**
38
     * Entity name
39
     *
40
     * @var string
41
     */
42
    private $entityName = '';
43
44
    /**
45
     * Layout
46
     *
47
     * @var array
48
     */
49
    private $layout = [];
50
51
    /**
52
     * Multiple forms
53
     */
54
    private $batch = false;
55
56
    /**
57
     * Constructor
58
     *
59
     * @param FieldsAlgorithms $fieldsAlgorithms
60
     *            fields algorithms
61
     * @param string $sessionId
62
     *            session id
63
     * @param string $entityName
64
     *            entity name
65
     * @param array $layout
66
     *            fields layout
67
     * @param bool $batch
68
     *            batch operations available
69
     */
70
    public function __construct(
71
        FieldsAlgorithms $fieldsAlgorithms,
72
        string $sessionId = '',
73
        string $entityName = 'default',
74
        array $layout = [],
75
        bool $batch = false)
76
    {
77
        $this->fieldsAlgorithms = $fieldsAlgorithms;
78
79
        $this->sessionId = $sessionId;
80
81
        $this->entityName = $entityName;
82
83
        $this->layout = $layout;
84
85
        $this->batch = $batch;
86
    }
87
88
    /**
89
     * Method compiles form without layout
90
     *
91
     * @param array $record data
92
     * @return string compiled control
93
     */
94
    protected function compileForFieldsWithNoLayout(array $record): string
0 ignored issues
show
The parameter $record is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

94
    protected function compileForFieldsWithNoLayout(/** @scrutinizer ignore-unused */ array $record): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95
    {
96
        $content = '';
97
98
        foreach ($this->fieldsAlgorithms->getFieldsNames() as $name) {
99
            $field = $this->fieldsAlgorithms->getObject($name);
100
            if ($name == 'id' || $name == 'domain_id' || $name == 'creation_date' || $name == 'modification_date' ||
101
                $field->isVisible() === false) {
102
                continue;
103
            }
104
105
            $content .= '<div class="form-group ' . $this->entityName . '">' . '<label class="control-label" >' .
106
                $field->getTitle() . ($field->isRequired() ? ' <span class="required">*</span>' : '') . '</label>' .
107
                $field->html() . '</div>';
108
        }
109
110
        return $content;
111
    }
112
113
    /**
114
     * Method compiles atoic field
115
     *
116
     * @param array $field
117
     *            field description
118
     * @param string $name
119
     *            HTML field name
120
     * @param array $record
121
     *            record
122
     * @return string Compiled field
123
     */
124
    protected function compileField(array $field, string $name, array $record): string
0 ignored issues
show
The parameter $record is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

124
    protected function compileField(array $field, string $name, /** @scrutinizer ignore-unused */ array $record): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
125
    {
126
        $control = $this->fieldsAlgorithms->getCompiledField($name);
127
128
        $fieldObject = $this->fieldsAlgorithms->getObject($name);
129
130
        if ($fieldObject->fillAllRow()) {
131
            return $control;
132
        }
133
134
        if ($fieldObject->isVisible() === false) {
135
            return '';
136
        }
137
138
        $content = '<div class="form-group ' . $this->entityName . ' col-md-' . $field['width'] . '">';
139
140
        if ($fieldObject->hasLabel()) {
141
            $content .= '<label class="control-label" style="text-align: left;">' . $fieldObject->getTitle() .
142
                ($fieldObject->isRequired() ? ' <span class="required">*</span>' : '') . '</label>';
143
        }
144
145
        return $content . $control . '</div>';
146
    }
147
148
    /**
149
     * Method compiles form with layout
150
     *
151
     * @param array $record
152
     *            record
153
     * @return string compiled fields
154
     */
155
    protected function compileForFieldsWithLayout(array $record = []): string
156
    {
157
        $content = '';
158
159
        foreach ($this->layout['rows'] as $row) {
160
            foreach ($row as $name => $field) {
161
                $content .= $this->compileField($field, $name, $record);
162
            }
163
        }
164
165
        return $content;
166
    }
167
168
    /**
169
     * Method returns amount of columns in the form
170
     *
171
     * @return string width of the column
172
     */
173
    protected function getFormWidth(): string
174
    {
175
        if (isset($_GET['form-width'])) {
176
            return $_GET['form-width'];
177
        } elseif (empty($this->layout)) {
178
            return '6';
179
        } else {
180
            return $this->layout['width'];
181
        }
182
    }
183
184
    /**
185
     * Method compiles form fields
186
     *
187
     * @param array $record
188
     *            record to be filled
189
     * @return string compiled fields
190
     */
191
    public function compileFormFields(array $record): string
192
    {
193
        if (empty($this->layout)) {
194
            return $this->compileForFieldsWithNoLayout($record);
195
        } else {
196
            return $this->compileForFieldsWithLayout($record);
197
        }
198
    }
199
200
    /**
201
     * Method compiles creation form
202
     *
203
     * @return string compiled creation form
204
     */
205
    public function creationForm(): string
206
    {
207
        if (isset($_GET['no-header'])) {
208
            $content = file_get_contents(__DIR__ . '/res/templates/creation_form_no_header.tpl');
209
        } else {
210
            $content = file_get_contents(__DIR__ . '/res/templates/creation_form_header.tpl');
211
        }
212
213
        $content .= file_get_contents(__DIR__ . '/res/templates/creation_form.tpl');
214
215
        $backLink = isset($_GET['back-link']) ? $_GET['back-link'] : '../list/';
216
217
        $content = str_replace('{fields}', $this->compileFormFields([]), $content);
218
219
        $content = str_replace('{width}', $this->getFormWidth(), $content);
220
221
        return str_replace('{back-link}', $backLink, $content);
222
    }
223
224
    /**
225
     * Method compiles updating form
226
     *
227
     * @param string $sessionId
228
     *            session id
229
     * @param array $record
230
     *            record to be updated
231
     * @return string compiled updating form
232
     */
233
    public function updatingForm(string $sessionId, array $record): string
234
    {
235
        // TODO $record must object, not array
236
        // TODO do we need $sessionId because it is setup in the __construct
237
        if (isset($_GET['no-header'])) {
238
            $content = file_get_contents(__DIR__ . '/res/templates/updating_form_no_header.tpl');
239
        } else {
240
            $content = file_get_contents(__DIR__ . '/res/templates/updating_form_header.tpl');
241
        }
242
243
        $content .= file_get_contents(__DIR__ . '/res/templates/updating_form.tpl');
244
245
        $this->sessionId = $sessionId;
246
        $this->fieldsAlgorithms->setSessionId($sessionId);
247
248
        return str_replace('{fields}', $this->compileFormFields($record), $content);
249
    }
250
}
251