Completed
Pull Request — master (#56)
by Sebastian
06:12
created

BlenderFormBuilder::seo()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
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\Collection;
9
use Illuminate\Support\ViewErrorBag;
10
11
class BlenderFormBuilder
12
{
13
    /** @var string */
14
    protected $module;
15
16
    /** @var \Illuminate\Database\Eloquent\Model */
17
    protected $model;
18
19
    /** @var \Illuminate\Support\ViewErrorBag */
20
    protected $errors;
21
22
    public function init(string $module, Model $model, ViewErrorBag $errors)
23
    {
24
        $this->module = $module;
25
        $this->model = $model;
26
        $this->errors = $errors;
27
    }
28
29
    public function label(string $name, bool $required = false, array $options = []): string
30
    {
31
        return Form::label($name, fragment("back.{$this->module}.{$name}").($required ? '*' : ''), $options);
32
    }
33
34
    public function error(string $name): string
35
    {
36
        return Html::error($this->errors->first($name));
37
    }
38
39 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...
40
    {
41
        $fieldName = $this->fieldName($name, $locale);
42
43
        return $this->group([
44
            $this->label($name, $required),
45
            Form::text($fieldName, Form::useInitialValue($this->model, $name, $locale)),
46
            $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...
47
        ]);
48
    }
49
50 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...
51
    {
52
        $fieldName = $this->fieldName($name, $locale);
53
54
        return $this->group([
55
            $this->label($name, $required),
56
            Form::textarea($fieldName, Form::useInitialValue($this->model, $name, $locale), ['data-autosize']),
57
            $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...
58
        ]);
59
    }
60
61
    public function redactor(string $name, bool $required = false, string $locale = ''): string
62
    {
63
        $fieldName = $this->fieldName($name, $locale);
64
65
        $options = [
66
            'data-editor' => '',
67
            'data-editor-medialibrary-url' => action(
68
                'Back\MediaLibraryApiController@index',
69
                [
70
                    'model_name' => get_class($this->model),
71
                    'model_id' => $this->model->id,
72
                ]
73
            ),
74
        ];
75
76
        return $this->group([
77
            $this->label($name, $required),
78
            Form::textarea($fieldName, Form::useInitialValue($this->model, $name, $locale), $options),
79
            $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...
80
        ]);
81
    }
82
83
    public function checkbox(string $name, string $locale = ''): string
84
    {
85
        $fieldName = $this->fieldName($name, $locale);
86
87
        $contents = Form::checkbox($fieldName, 1, Form::useInitialValue($this->model, $name, $locale),
88
            ['class' => 'form-control']).' '.fragment("back.{$this->module}.{$name}");
89
90
        return $this->group([el('label.-checkbox', $contents)]);
91
    }
92
93 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...
94
    {
95
        $fieldName = $this->fieldName($name, $locale);
96
97
        return $this->group([
98
            $this->label($name, $required),
99
            Form::datePicker($fieldName, Form::useInitialValue($this->model, $name, $locale)),
100
            $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...
101
        ]);
102
    }
103
104 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...
105
    {
106
        $fieldName = $this->fieldName($name, $locale);
107
108
        return $this->group([
109
            $this->label($name, true),
110
            Form::select(
111
                $fieldName,
112
                $options,
113
                Form::useInitialValue($this->model, $name, $locale),
114
                ['data-select' => 'select']
115
            ),
116
            $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...
117
        ]);
118
    }
119
120 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...
121
    {
122
        $fieldName = $this->fieldName($name, $locale);
123
124
        return $this->group([
125
            $this->label($name),
126
            Form::select(
127
                $fieldName,
128
                $options,
129
                Form::useInitialValue($this->model, $name, $locale),
130
                ['data-select' => 'search']
131
            ),
132
            $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...
133
        ]);
134
    }
135
136 View Code Duplication
    public function searchableMultiSelect(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...
137
    {
138
        $fieldName = $this->fieldName($name, $locale);
139
140
        return $this->group([
141
            $this->label($name),
142
            Form::select(
143
                $fieldName,
144
                $options,
145
                Form::useInitialValue($this->model, $name, $locale),
146
                ['data-select' => 'search', 'multiple' => true]
147
            ),
148
            $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...
149
        ]);
150
    }
151
152 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...
153
    {
154
        return $this->group([
155
            Form::label($type.'_tags[]', fragment("back.{$this->module}.{$type}").'*'),
156
            Form::tags($this->model, $type),
157
        ]);
158
    }
159
160 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...
161
    {
162
        return $this->group([
163
            Form::label($type.'_tags[]', fragment("back.{$this->module}.{$type}").'*'),
164
            Form::category($this->model, $type, ['data-select' => 'select']),
165
        ]);
166
    }
167
168
    public function media(string $collection, string $type, array $associated = []): string
169
    {
170
        return $this->group([
171
            $this->label($collection),
172
            Form::media($this->model, $collection, $type, $associated),
173
        ]);
174
    }
175
176
    public function map(string $name): string
177
    {
178
        $form = [];
179
180
        $form[] = el('div', ['class' => 'locationpicker_tools :align-right'], [
181
            el('input', [
182
                'type' => 'text',
183
                'class' => 'locationpicker_search',
184
                'placeholder' => fragment('back.locationpicker.search'),
185
                'data-locationpicker-search',
186
            ], ''),
187
            el('button', [
188
                'class' => 'locationpicker_button',
189
                'type' => 'button',
190
                'data-locationpicker-button',
191
            ], fragment('back.locationpicker.submit')),
192
        ]);
193
194
        $form[] = el('div', [
195
            'class' => 'locationpicker_map',
196
            'data-locationpicker-map',
197
        ], '');
198
199
        $form[] = Form::hidden(
200
            "{$name}_lat",
201
            Form::useInitialValue($this->model, "{$name}_lat"),
202
            ['data-locationpicker-lat']
203
        );
204
205
        $form[] = Form::hidden(
206
            "{$name}_lng",
207
            Form::useInitialValue($this->model, "{$name}_lng"),
208
            ['data-locationpicker-lng']
209
        );
210
211
        $form[] = Form::hidden(
212
            "{$name}_zoom",
213
            Form::useInitialValue($this->model, "{$name}_zoom"),
214
            ['data-locationpicker-zoom']
215
        );
216
217
        return $this->group([
218
            $this->label($name),
219
            el('div.locationpicker', ['data-locationpicker'], $form),
220
        ]);
221
    }
222
223
    public function translated(array $fields): string
224
    {
225
        // 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...
226
227
        $translatedFields = [];
228
229
        foreach (config('app.locales') as $locale) {
230
            $fieldset = [];
231
232
            foreach ($fields as $name => $type) {
233
                $fieldset[] = $this->$type($name, false, $locale);
234
            }
235
236
            $translatedFields[] = $this->languageFieldSet($locale, $fieldset);
237
        }
238
239
        return implode('', $translatedFields);
240
    }
241
242
    public function seo(): string
243
    {
244
        return locales()->map(function ($locale) {
245
246
            return collect($this->model->defaultSeoValues())
247
                ->keys()
248
                ->map(function ($attribute) use ($locale) {
249
250
                    $fieldName = translate_field_name("seo.{$attribute}", $locale);
251
252
                    return $this->group([
253
                        Form::label($fieldName, $this->getSeoLabel($attribute)),
254
                        Form::text(
255
                            $fieldName,
256
                            old($fieldName, $this->model->getTranslation('seo_values', $locale)[$attribute] ?? '')
257
                        ),
258
                    ]);
259
                })
260
                ->pipe(function (Collection $fields) use ($locale) {
261
                    return $this->languageFieldSet($locale, $fields->toArray());
262
                });
263
264
        })->implode('');
265
    }
266
267
    protected function getSeoLabel(string $attribute): string
268
    {
269
        if (starts_with($attribute, 'meta_')) {
270
            return "Meta: " . substr($attribute, 5);
271
        }
272
273
        return fragment("back.seo.{$attribute}");
274
    }
275
276
    public function submit(): string
277
    {
278
        return el('div.form_group.-buttons',
279
            Form::submit(fragment("back.{$this->module}.save"), ['class' => 'button -default'])
280
        );
281
    }
282
283
    protected function languageFieldSet($locale, array $elements)
284
    {
285
        return el('fieldset',
286
            array_merge([el('legend', el('div.legend_lang', $locale))], $elements)
287
        );
288
    }
289
290
    protected function group(array $elements): string
291
    {
292
        return el('div.form_group', $elements);
293
    }
294
295
    protected function parts(array $elements): string
296
    {
297
        return el('div.parts', $elements);
298
    }
299
300
    protected function fieldName(string $name, string $locale = ''): string
301
    {
302
        return $locale ? translate_field_name($name, $locale) : $name;
303
    }
304
}
305