Completed
Push — master ( ad4380...e899e6 )
by Sebastian
03:55
created

BlenderFormBuilder::parts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace App\Services\Html;
4
5
use Form;
6
use HTML;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\ViewErrorBag;
9
10
class BlenderFormBuilder
11
{
12
    /** @var string */
13
    protected $module;
14
15
    /** @var \Illuminate\Database\Eloquent\Model */
16
    protected $model;
17
18
    /** @var \Illuminate\Support\ViewErrorBag */
19
    protected $errors;
20
21
    public function init(string $module, Model $model, ViewErrorBag $errors)
22
    {
23
        $this->module = $module;
24
        $this->model = $model;
25
        $this->errors = $errors;
26
    }
27
28
    public function label(string $name, bool $required = false, array $options = []) : string
29
    {
30
        return Form::label($name, fragment("back.{$this->module}.{$name}") . ($required ? '*' : ''), $options);
31
    }
32
33
    public function error(string $name) : string
34
    {
35
        return HTML::error($this->errors->first($name));
36
    }
37
38 View Code Duplication
    public function text(string $name, bool $required = false, string $locale = '') : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40
        $fieldName = $this->fieldName($name, $locale);
41
42
        return $this->group([
43
            $this->label($name, $required),
44
            Form::text($fieldName, Form::useInitialValue($this->model, $name, $locale)),
45
            $this->error($fieldName, $this->errors),
0 ignored issues
show
Unused Code introduced by
The call to BlenderFormBuilder::error() has too many arguments starting with $this->errors.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
46
        ]);
47
    }
48
49 View Code Duplication
    public function textarea(string $name, bool $required = false, string $locale = '') : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51
        $fieldName = $this->fieldName($name, $locale);
52
53
        return $this->group([
54
            $this->label($name, $required),
55
            Form::textarea($fieldName, Form::useInitialValue($this->model, $name, $locale), ['data-autosize']),
56
            $this->error($fieldName, $this->errors),
0 ignored issues
show
Unused Code introduced by
The call to BlenderFormBuilder::error() has too many arguments starting with $this->errors.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
57
        ]);
58
    }
59
60
    public function redactor(string $name, bool $required = false, string $locale = '') : string
61
    {
62
        $fieldName = $this->fieldName($name, $locale);
63
64
        $options = [
65
            'data-editor' => '',
66
            'data-editor-medialibrary-url' => action(
67
                'Back\MediaLibraryApiController@index',
68
                [
69
                    'model_name' => get_class($this->model),
70
                    'model_id' => $this->model->id,
71
                ]
72
            ),
73
        ];
74
75
        return $this->group([
76
            $this->label($name, $required),
77
            Form::textarea($fieldName, Form::useInitialValue($this->model, $name, $locale), $options),
78
            $this->error($fieldName, $this->errors),
0 ignored issues
show
Unused Code introduced by
The call to BlenderFormBuilder::error() has too many arguments starting with $this->errors.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
79
        ]);
80
    }
81
82
    public function checkbox(string $name, string $locale = '') : string
83
    {
84
        $fieldName = $this->fieldName($name, $locale);
85
86
        $contents = Form::checkbox($fieldName, 1, Form::useInitialValue($this->model, $name, $locale),
87
            ['class' => 'form-control']) . ' ' . fragment("back.{$this->module}.{$name}");
88
89
        return $this->group([el('label.-checkbox', $contents)]);
90
    }
91
92 View Code Duplication
    public function date(string $name, bool $required = false, string $locale = '') : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94
        $fieldName = $this->fieldName($name, $locale);
95
96
        return $this->group([
97
            $this->label($name, $required),
98
            Form::datePicker($fieldName, Form::useInitialValue($this->model, $name, $locale)),
99
            $this->error($fieldName, $this->errors),
0 ignored issues
show
Unused Code introduced by
The call to BlenderFormBuilder::error() has too many arguments starting with $this->errors.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
100
        ]);
101
    }
102
103 View Code Duplication
    public function select(string $name, $options, string $locale = '') : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
    {
105
        $fieldName = $this->fieldName($name, $locale);
106
107
        return $this->group([
108
            $this->label($name, true),
109
            Form::select(
110
                $fieldName,
111
                $options,
112
                Form::useInitialValue($this->model, $name, $locale),
113
                ['data-select' => 'select']
114
            ),
115
            $this->error($fieldName, $this->errors),
0 ignored issues
show
Unused Code introduced by
The call to BlenderFormBuilder::error() has too many arguments starting with $this->errors.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
116
        ]);
117
    }
118
119 View Code Duplication
    public function searchableSelect(string $name, $options, string $locale = '') : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
    {
121
        $fieldName = $this->fieldName($name, $locale);
122
123
        return $this->group([
124
            $this->label($name),
125
            Form::select(
126
                $fieldName,
127
                $options,
128
                Form::useInitialValue($this->model, $name, $locale),
129
                ['data-select' => 'search']
130
            ),
131
            $this->error($fieldName, $this->errors),
0 ignored issues
show
Unused Code introduced by
The call to BlenderFormBuilder::error() has too many arguments starting with $this->errors.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
132
        ]);
133
    }
134
135 View Code Duplication
    public function tags(string $type) : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
    {
137
        return $this->group([
138
            Form::label($type.'_tags[]', fragment("back.{$this->module}.{$type}") . '*'),
139
            Form::tags($this->model, $type),
140
        ]);
141
    }
142
143 View Code Duplication
    public function category(string $type) : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
    {
145
        return $this->group([
146
            Form::label($type.'_tags[]', fragment("back.{$this->module}.{$type}") . '*'),
147
            Form::category($this->model, $type, ['data-select'=>'select']),
148
        ]);
149
    }
150
151
    public function media(string $collection, string $type, array $associated = []) : string
152
    {
153
        return $this->group([
154
            $this->label($collection),
155
            Form::media($this->model, $collection, $type, $associated),
156
        ]);
157
    }
158
159
    public function map(string $name) : string
160
    {
161
        $form = [];
162
163
        $form[] = el('div', ['class' => 'locationpicker_tools :align-right'], [
164
            el('input', [
165
                'type' => 'text',
166
                'class' => 'locationpicker_search',
167
                'placeholder' => fragment('back.locationpicker.search'),
168
                'data-locationpicker-search',
169
            ], ''),
170
            el('button', [
171
                'class' => 'locationpicker_button',
172
                'type' => 'button',
173
                'data-locationpicker-button',
174
            ], fragment('back.locationpicker.submit')),
175
        ]);
176
177
        $form[] = el('div', [
178
            'class' => 'locationpicker_map',
179
            'data-locationpicker-map',
180
        ], '');
181
182
        $form[] = Form::hidden(
183
            "{$name}_lat",
184
            Form::useInitialValue($this->model, "{$name}_lat"),
185
            ['data-locationpicker-lat']
186
        );
187
188
        $form[] = Form::hidden(
189
            "{$name}_lng",
190
            Form::useInitialValue($this->model, "{$name}_lng"),
191
            ['data-locationpicker-lng']
192
        );
193
194
        $form[] = Form::hidden(
195
            "{$name}_zoom",
196
            Form::useInitialValue($this->model, "{$name}_zoom"),
197
            ['data-locationpicker-zoom']
198
        );
199
200
        return $this->group([
201
            $this->label($name),
202
            el('div.locationpicker#custom_id', $form),
203
        ]);
204
    }
205
206
    public function translated(array $fields) : string
207
    {
208
        // Ex. ['name' => 'text', 'contents' => 'redactor']
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
209
210
        $translatedFields = [];
211
212
        foreach (config('app.locales') as $locale) {
213
            $fieldset = [];
214
215
            foreach ($fields as $name => $type) {
216
                $fieldset[] = $this->$type($name, false, $locale);
217
            }
218
219
            $translatedFields[] = $this->languageFieldSet($locale, $fieldset);
220
        }
221
222
        return implode('', $translatedFields);
223
    }
224
225
    public function submit() : string
226
    {
227
        return el('div.form_group.-buttons',
228
            Form::submit(fragment("back.{$this->module}.save"), ['class' => 'button -default'])
229
        );
230
    }
231
232
    protected function languageFieldSet($locale, array $elements)
233
    {
234
        return el('fieldset',
235
            array_merge([el('legend', el('div.legend_lang', $locale))], $elements)
236
        );
237
    }
238
239
    protected function group(array $elements) : string
240
    {
241
        return el('div.form_group', $elements);
242
    }
243
244
    protected function parts(array $elements) : string
245
    {
246
        return el('div.parts', $elements);
247
    }
248
249
    protected function fieldName(string $name, string $locale = '') : string
250
    {
251
        return $locale ? translate_field_name($name, $locale) : $name;
252
    }
253
}
254