Completed
Push — master ( 223b83...59ba0b )
by Sebastian
05:10
created

BlenderFormBuilder::translated()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 5
nop 1
dl 0
loc 23
rs 8.7972
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\Api\MediaLibraryController@index',
69
                [
70
                    'model_name' => ucfirst(get_class($this->model)),
71
                    'model_id' => $this->model->id,
72
                    'redactor=true',
73
                ]
74
            ),
75
        ];
76
77
        return $this->group([
78
            $this->label($name, $required),
79
            Form::textarea($fieldName, Form::useInitialValue($this->model, $name, $locale), $options),
80
            $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...
81
        ]);
82
    }
83
84
    public function checkbox(string $name, string $locale = ''): string
85
    {
86
        $fieldName = $this->fieldName($name, $locale);
87
88
        $contents = Form::checkbox(
89
            $fieldName,
90
            1,
91
            Form::useInitialValue($this->model, $name, $locale),
92
            ['class' => 'form-control']
93
        ).' '.fragment("back.{$this->module}.{$name}");
94
95
        return $this->group([el('label.-checkbox', $contents)]);
96
    }
97
98 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...
99
    {
100
        $fieldName = $this->fieldName($name, $locale);
101
102
        return $this->group([
103
            $this->label($name, $required),
104
            Form::datePicker($fieldName, Form::useInitialValue($this->model, $name, $locale)),
105
            $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...
106
        ]);
107
    }
108
109 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...
110
    {
111
        $fieldName = $this->fieldName($name, $locale);
112
113
        return $this->group([
114
            $this->label($name, true),
115
            Form::select(
116
                $fieldName,
117
                $options,
118
                Form::useInitialValue($this->model, $name, $locale),
119
                ['data-select' => 'select']
120
            ),
121
            $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...
122
        ]);
123
    }
124
125 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...
126
    {
127
        $fieldName = $this->fieldName($name, $locale);
128
129
        return $this->group([
130
            $this->label($name),
131
            Form::select(
132
                $fieldName,
133
                $options,
134
                Form::useInitialValue($this->model, $name, $locale),
135
                ['data-select' => 'search']
136
            ),
137
            $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...
138
        ]);
139
    }
140
141 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...
142
    {
143
        $fieldName = $this->fieldName($name, $locale);
144
145
        return $this->group([
146
            $this->label($name),
147
            Form::select(
148
                $fieldName,
149
                $options,
150
                Form::useInitialValue($this->model, $name, $locale),
151
                ['data-select' => 'search', 'multiple' => true]
152
            ),
153
            $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...
154
        ]);
155
    }
156
157 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...
158
    {
159
        return $this->group([
160
            Form::label($type.'_tags[]', fragment("back.{$this->module}.{$type}").'*'),
161
            Form::tags($this->model, $type),
162
        ]);
163
    }
164
165 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...
166
    {
167
        return $this->group([
168
            Form::label($type.'_tags[]', fragment("back.{$this->module}.{$type}").'*'),
169
            Form::category($this->model, $type, ['data-select' => 'select']),
170
        ]);
171
    }
172
173
    public function media(string $collection, string $type, array $associated = []): string
174
    {
175
        return $this->group([
176
            $this->label($collection),
177
            Form::media($this->model, $collection, $type, $associated),
178
        ]);
179
    }
180
181
    public function map(string $name): string
182
    {
183
        $form = [];
184
185
        $form[] = el('div', ['class' => 'locationpicker_tools :align-right'], [
186
            el('input', [
187
                'type' => 'text',
188
                'class' => 'locationpicker_search',
189
                'placeholder' => fragment('back.locationpicker.search'),
190
                'data-locationpicker-search',
191
            ], ''),
192
            el('button', [
193
                'class' => 'locationpicker_button',
194
                'type' => 'button',
195
                'data-locationpicker-button',
196
            ], fragment('back.locationpicker.submit')),
197
        ]);
198
199
        $form[] = el('div', [
200
            'class' => 'locationpicker_map',
201
            'data-locationpicker-map',
202
        ], '');
203
204
        $form[] = Form::hidden(
205
            "{$name}_lat",
206
            Form::useInitialValue($this->model, "{$name}_lat"),
207
            ['data-locationpicker-lat']
208
        );
209
210
        $form[] = Form::hidden(
211
            "{$name}_lng",
212
            Form::useInitialValue($this->model, "{$name}_lng"),
213
            ['data-locationpicker-lng']
214
        );
215
216
        $form[] = Form::hidden(
217
            "{$name}_zoom",
218
            Form::useInitialValue($this->model, "{$name}_zoom"),
219
            ['data-locationpicker-zoom']
220
        );
221
222
        return $this->group([
223
            $this->label($name),
224
            el('div.locationpicker', ['data-locationpicker'], $form),
225
        ]);
226
    }
227
228
    public function translated(array $fields): string
229
    {
230
        // 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...
231
232
        $translatedFields = [];
233
234
        foreach (locales() as $locale) {
235
            $fieldset = [];
236
237
            foreach ($fields as $name => $type) {
238
                $fieldset[] = $this->$type($name, false, $locale);
239
            }
240
241
            if (locales()->count() === 1) {
242
                $translatedFields[] = $this->group($fieldset);
243
                continue;
244
            }
245
246
            $translatedFields[] = $this->languageFieldSet($locale, $fieldset);
247
        }
248
249
        return implode('', $translatedFields);
250
    }
251
252
    public function seo(): string
253
    {
254
        return locales()->map(function ($locale) {
255
            return collect($this->model->defaultSeoValues())
256
                ->keys()
257
                ->map(function ($attribute) use ($locale) {
258
                    $fieldName = translate_field_name("seo.{$attribute}", $locale);
259
260
                    return $this->group([
261
                        Form::label($fieldName, $this->getSeoLabel($attribute)),
262
                        Form::text(
263
                            $fieldName,
264
                            old($fieldName, $this->model->getTranslation('seo_values', $locale)[$attribute] ?? ''),
265
                            ['placeholder' => $this->model->defaultSeoValues()[$attribute]]
266
                        ),
267
                    ]);
268
                })
269
                ->pipe(function (Collection $fields) use ($locale) {
270
                    return $this->languageFieldSet($locale, $fields->toArray());
271
                });
272
        })->implode('');
273
    }
274
275
    protected function getSeoLabel(string $attribute): string
276
    {
277
        if (starts_with($attribute, 'meta_')) {
278
            return 'Meta: '.substr($attribute, 5);
279
        }
280
281
        return fragment("back.seo.{$attribute}");
282
    }
283
284
    public function submit(): string
285
    {
286
        return el(
287
            'div.form__group.-buttons',
288
            Form::submit(fragment("back.{$this->module}.save"), ['class' => 'button -default'])
289
        );
290
    }
291
292
    protected function languageFieldSet($locale, array $elements)
293
    {
294
        return el(
295
            'fieldset',
296
            array_merge([el('legend', el('div.legend_lang', $locale))], $elements)
297
        );
298
    }
299
300
    protected function group(array $elements): string
301
    {
302
        return el('div.form__group', $elements);
303
    }
304
305
    protected function parts(array $elements): string
306
    {
307
        return el('div.parts', $elements);
308
    }
309
310
    protected function fieldName(string $name, string $locale = ''): string
311
    {
312
        return $locale ? translate_field_name($name, $locale) : $name;
313
    }
314
}
315