processFormStepsAndFields()   F
last analyzed

Complexity

Conditions 15
Paths 576

Size

Total Lines 78
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 45
c 1
b 0
f 0
dl 0
loc 78
rs 2.3388
cc 15
nc 576
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace MedianetDev\BackpackForm\Http\Controllers\Admin;
4
5
use App\Traits\CrudPermissions;
0 ignored issues
show
Bug introduced by
The type App\Traits\CrudPermissions was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use App\Traits\Field;
0 ignored issues
show
Bug introduced by
The type App\Traits\Field was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Backpack\CRUD\app\Library\Widget;
8
use MedianetDev\BackpackForm\Models\Formbuilder;
9
use Backpack\CRUD\app\Http\Controllers\CrudController;
10
use MedianetDev\BackpackForm\Http\Requests\FormbuilderRequest;
11
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
12
use Illuminate\Support\Facades\App;
13
use MedianetDev\BackpackForm\Models\FormbuilderTranslation;
14
use MedianetDev\BackpackForm\Models\FormField;
15
use MedianetDev\BackpackForm\Models\FormFieldCondition;
16
use MedianetDev\BackpackForm\Models\FormFieldTranslation;
17
use MedianetDev\BackpackForm\Models\FormStep;
18
use MedianetDev\BackpackForm\Models\FormStepTranslation;
19
20
/**
21
 * Class FormbuilderCrudController
22
 * @package MedianetDev\BackpackForm\Http\Controllers\Admin
23
 * @property-read \Backpack\CRUD\app\Library\CrudPanel\CrudPanel $crud
24
 */
25
class FormbuilderCrudController extends CrudController
26
{
27
    use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
28
    use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation {
29
        store as traitStore;
30
    }
31
    use \Backpack\CRUD\app\Http\Controllers\Operations\InlineCreateOperation;
32
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation {
33
        update as traitUpdate;
34
    }
35
    use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation {
36
        destroy as traitDestroy;
37
    }
38
    use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;
39
    use Field;
40
    use CrudPermissions;
41
42
43
    public function setup()
44
    {
45
        CRUD::setModel(Formbuilder::class);
46
        CRUD::setRoute(config('backpack.base.route_prefix') . '/formbuilder');
47
        CRUD::setEntityNameStrings(
48
            __('medianet-dev.backpack-form::formbuilder.labels.entity_form'),
0 ignored issues
show
Bug introduced by
It seems like __('medianet-dev.backpac...er.labels.entity_form') can also be of type array and array; however, parameter $singular of Backpack\CRUD\app\Librar...:setEntityNameStrings() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

48
            /** @scrutinizer ignore-type */ __('medianet-dev.backpack-form::formbuilder.labels.entity_form'),
Loading history...
49
            __('medianet-dev.backpack-form::formbuilder.labels.entities_form')
0 ignored issues
show
Bug introduced by
It seems like __('medianet-dev.backpac....labels.entities_form') can also be of type array and array; however, parameter $plural of Backpack\CRUD\app\Librar...:setEntityNameStrings() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

49
            /** @scrutinizer ignore-type */ __('medianet-dev.backpack-form::formbuilder.labels.entities_form')
Loading history...
50
        );
51
        $this->crud->addButtonFromModelFunction('line', 'entries_list', 'entriesList', 'beginning');
52
    }
53
54
    protected function setupListOperation()
55
    {
56
        $this->crud->addColumns([
57
            [
58
                'name' => 'uniq_id',
59
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.uuid')),
0 ignored issues
show
Bug introduced by
It seems like __('medianet-dev.backpac...rmbuilder.labels.uuid') can also be of type array and array; however, parameter $string of ucfirst() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

59
                'label' => ucfirst(/** @scrutinizer ignore-type */ __('medianet-dev.backpack-form::formbuilder.labels.uuid')),
Loading history...
60
                'type' => 'text',
61
            ],
62
            [
63
                'name' => 'translation.title',
64
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.title')),
65
                'type' => 'text',
66
            ],
67
            [
68
                'name' => 'translation.header',
69
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.header')),
70
                'type' => 'text',
71
            ],
72
            [
73
                'name' => 'translation.footer',
74
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.footer')),
75
                'type' => 'text',
76
            ],
77
            [
78
                'name' => 'in_database',
79
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.in_database')),
80
                'type' => 'boolean', // Changed from 'check' to 'boolean' for better clarity
81
            ],
82
            [
83
                'name' => 'by_mail',
84
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.by_mail')),
85
                'type' => 'boolean', // Changed from 'check' to 'boolean' for better clarity
86
            ],
87
            [
88
                'name' => 'updated_at',
89
                'type' => 'datetime',
90
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.updated_at'))
91
            ],
92
            [
93
                'name' => 'created_at',
94
                'type' => 'datetime',
95
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.created_at'))
96
            ]
97
        ]);
98
    }
99
100
    protected function setupShowOperation()
101
    {
102
        $this->crud->set('show.setFromDb', false);
103
        $this->crud->addColumns([
104
            [
105
                'name' => 'uniq_id',
106
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.uuid')),
0 ignored issues
show
Bug introduced by
It seems like __('medianet-dev.backpac...rmbuilder.labels.uuid') can also be of type array and array; however, parameter $string of ucfirst() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

106
                'label' => ucfirst(/** @scrutinizer ignore-type */ __('medianet-dev.backpack-form::formbuilder.labels.uuid')),
Loading history...
107
                'type' => 'text',
108
            ],
109
            [
110
                'name' => 'translation.title',
111
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.title')),
112
                'type' => 'text',
113
            ],
114
            [
115
                'name' => 'translation.description',
116
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.description')),
117
                'type' => 'html',
118
            ],
119
            [
120
                'name' => 'translation.slug',
121
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.slug')),
122
                'type' => 'text',
123
            ],
124
            [
125
                'name' => 'translation.header',
126
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.header')),
127
                'type' => 'text',
128
            ],
129
            [
130
                'name' => 'translation.footer',
131
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.footer')),
132
                'type' => 'text',
133
            ],
134
            [
135
                'name' => 'text_button',
136
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.text_button')),
137
                'type' => 'text',
138
            ],
139
            [
140
                'name' => 'in_database',
141
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.in_database')),
142
                'type' => 'boolean',
143
            ],
144
            [
145
                'name' => 'display_title',
146
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.display_title')),
147
                'type' => 'boolean',
148
            ],
149
            [
150
                'name' => 'multiple_steps',
151
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.multiple_steps')),
152
                'type' => 'boolean',
153
            ],
154
            [
155
                'name' => 'display_header',
156
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.display_header')),
157
                'type' => 'boolean',
158
            ],
159
            [
160
                'name' => 'display_footer',
161
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.display_footer')),
162
                'type' => 'boolean',
163
            ],
164
            [
165
                'name' => 'display_intro',
166
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.display_intro')),
167
                'type' => 'boolean',
168
            ],
169
            [
170
                'name' => 'display_captcha',
171
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.display_captcha')),
172
                'type' => 'boolean',
173
            ],
174
            [
175
                'name' => 'by_mail',
176
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.by_mail')),
177
                'type' => 'boolean',
178
            ],
179
            [
180
                'name' => 'mail_to',
181
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.mail_to')),
182
                'type' => 'text',
183
            ],
184
            [
185
                'name' => 'include_data',
186
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.include_data')),
187
                'type' => 'boolean',
188
            ],
189
            [
190
                'name' => 'subject_admin',
191
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.subject_admin')),
192
                'type' => 'text',
193
            ],
194
            [
195
                'name' => 'message_admin',
196
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.message_admin')),
197
                'type' => 'html',
198
            ],
199
            [
200
                'name' => 'copy_user',
201
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.copy_user')),
202
                'type' => 'boolean',
203
            ],
204
            [
205
                'name' => 'field_mail_name',
206
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.field_mail_name')),
207
                'type' => 'text',
208
            ],
209
            [
210
                'name' => 'subject_user',
211
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.subject_user')),
212
                'type' => 'text',
213
            ],
214
            [
215
                'name' => 'message_user',
216
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.message_user')),
217
                'type' => 'html',
218
            ],
219
            [
220
                'name' => 'form',
221
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.form')),
222
                'type' => 'form_builder_preview',
223
                'view_namespace' => 'medianet.backpack-form::columns',
224
            ],
225
            [
226
                'name' => 'created_at',
227
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.created_at')),
228
                'type' => 'datetime',
229
            ],
230
            [
231
                'name' => 'updated_at',
232
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.updated_at')),
233
                'type' => 'datetime',
234
            ],
235
        ]);
236
    }
237
238
    protected function setupCreateOperation()
239
    {
240
241
        CRUD::setValidation(FormbuilderRequest::class);
242
243
        $translations = $this->getTranslations(
244
            $this->crud->getOperation(),
245
            $this->crud->getModel(),
246
            $this->crud->getCurrentEntry()
247
        );
248
        $languages = languages();
0 ignored issues
show
Bug introduced by
The function languages was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

248
        $languages = /** @scrutinizer ignore-call */ languages();
Loading history...
249
250
        // Language-specific fields
251
        foreach ($languages as $key => $value) {
252
            $this->crud->addFields([
253
                [
254
                    'name' => "{$key}[title]",
255
                    'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.title')),
0 ignored issues
show
Bug introduced by
It seems like __('medianet-dev.backpac...mbuilder.labels.title') can also be of type array and array; however, parameter $string of ucfirst() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

255
                    'label' => ucfirst(/** @scrutinizer ignore-type */ __('medianet-dev.backpack-form::formbuilder.labels.title')),
Loading history...
256
                    'type' => 'text',
257
                    'default' => ($translations && isset($translations->translations[$key]))
258
                        ? $translations->translations[$key]->title
259
                        : '',
260
                    'tab' => __($value),
261
                    'slug_class' => "slug_{$key}"
262
                ],
263
                [
264
                    'name' => "{$key}[slug]",
265
                    'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.slug')),
266
                    'type' => 'text',
267
                    'slug' => true,
268
                    'default' => ($translations && isset($translations->translations[$key]))
269
                        ? $translations->translations[$key]->slug ?? null
270
                        : '',
271
                    'tab' => __($value),
272
                    'attributes' => ['class' => "slug_{$key} form-control"]
273
                ],
274
                [
275
                    'name' => "{$key}[description]",
276
                    'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.description')),
277
                    'type' => 'tinymce',
278
                    'default' => ($translations && isset($translations->translations[$key]))
279
                        ? $translations->translations[$key]->description
280
                        : '',
281
                    'tab' => __($value),
282
                    'options' => $this->tinyMceOption()
283
                ],
284
                [
285
                    'name' => "{$key}[text_button]",
286
                    'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.text_button')),
287
                    'type' => 'text',
288
                    'default' => __('medianet-dev.backpack-form::formbuilder.labels.default_text_button'),
289
                    'tab' => __($value)
290
                ],
291
                [
292
                    'name' => "{$key}[header]",
293
                    'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.header')),
294
                    'type' => 'tinymce',
295
                    'options' => $this->tinyMceOption(),
296
                    'default' => ($translations && isset($translations->translations[$key]))
297
                        ? $translations->translations[$key]->header
298
                        : '',
299
                    'tab' => __($value)
300
                ],
301
                [
302
                    'name' => "{$key}[footer]",
303
                    'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.footer')),
304
                    'type' => 'tinymce',
305
                    'options' => $this->tinyMceOption(),
306
                    'default' => ($translations && isset($translations->translations[$key]))
307
                        ? $translations->translations[$key]->footer
308
                        : '',
309
                    'tab' => __($value)
310
                ]
311
            ]);
312
        }
313
        // Create an instance of FormStepCrudController
314
315
        // $crud = new FormStepCrudController();
316
        // $dataCrud =  $crud->getCrudData();
317
318
        // Call the setupListOperation method
319
320
        // Main form fields
321
        $this->crud->addFields([
322
            // Form.io Component Builder (JSON)
323
            [
324
                'name' => 'formio_builder',
325
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.formio_builder')),
326
                'type' => 'form_builder_custom_html',
327
                'value' => '<div id="formio-builder-container" class="mb-4"> <div id="formio-builder"></div></div>',
328
                'tab' => __('medianet-dev.backpack-form::formbuilder.tabs.form_io_builder')
329
            ],
330
331
            // Hidden field to store Form.io component JSON
332
            [
333
                'name' => 'formio_component',
334
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.formio_component')),
335
                'type' => 'hidden',
336
                'attributes' => ['id' => 'formio-component-json'],
337
                'tab' => __('medianet-dev.backpack-form::formbuilder.tabs.form_io_builder')
338
            ],
339
340
            // Validation rules (JSON)
341
            [
342
                'name' => 'validation_rules',
343
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.validation_rules')),
344
                'type' => 'textarea',
345
                'attributes' => ['rows' => 5, 'id' => 'validation-rules-json'],
346
                'tab' => __('medianet-dev.backpack-form::formbuilder.tabs.validation'),
347
                'hint' => 'Enter validation rules in JSON format'
348
            ],
349
350
            // Config tab fields
351
352
            [
353
                'name' => 'in_database',
354
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.in_database')),
355
                'type' => 'select_from_array',
356
                'options' => __('medianet-dev.backpack-form::formbuilder.labels.bool'),
357
                'wrapper' => ['class' => 'form-group col-md-6'],
358
                'tab' => __('medianet-dev.backpack-form::formbuilder.labels.config_tab')
359
            ],
360
            [
361
                'name' => 'display_title',
362
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.display_title')),
363
                'type' => 'select_from_array',
364
                'options' => __('medianet-dev.backpack-form::formbuilder.labels.bool'),
365
                'wrapper' => ['class' => 'form-group col-md-6'],
366
                'default' => 1,
367
                'tab' => __('medianet-dev.backpack-form::formbuilder.labels.config_tab')
368
            ],
369
            [
370
                'name' => 'display_intro',
371
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.display_intro')),
372
                'type' => 'select_from_array',
373
                'options' => __('medianet-dev.backpack-form::formbuilder.labels.bool'),
374
                'wrapper' => ['class' => 'form-group col-md-6'],
375
                'default' => 1,
376
                'tab' => __('medianet-dev.backpack-form::formbuilder.labels.config_tab')
377
            ],
378
            [
379
                'name' => 'display_captcha',
380
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.display_captcha')),
381
                'type' => 'select_from_array',
382
                'options' => __('medianet-dev.backpack-form::formbuilder.labels.bool'),
383
                'wrapper' => ['class' => 'form-group col-md-6'],
384
                'default' => 1,
385
                'tab' => __('medianet-dev.backpack-form::formbuilder.labels.config_tab')
386
            ],
387
            [
388
                'name' => 'display_title',
389
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.display_title')),
390
                'type' => 'select_from_array',
391
                'options' => __('medianet-dev.backpack-form::formbuilder.labels.bool'),
392
                'wrapper' => ['class' => 'form-group col-md-6'],
393
                'default' => 1,
394
                'tab' => __('medianet-dev.backpack-form::formbuilder.labels.config_tab')
395
            ],
396
            [
397
                'name' => 'display_header',
398
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.display_header')),
399
                'type' => 'select_from_array',
400
                'options' => __('medianet-dev.backpack-form::formbuilder.labels.bool'),
401
                'wrapper' => ['class' => 'form-group col-md-6'],
402
                'default' => 1,
403
                'tab' => __('medianet-dev.backpack-form::formbuilder.labels.config_tab')
404
            ],
405
406
            [
407
                'name' => 'display_footer',
408
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.display_footer')),
409
                'type' => 'select_from_array',
410
                'options' => __('medianet-dev.backpack-form::formbuilder.labels.bool'),
411
                'wrapper' => ['class' => 'form-group col-md-6'],
412
                'default' => 1,
413
                'tab' => __('medianet-dev.backpack-form::formbuilder.labels.config_tab')
414
            ],
415
            [
416
                'name' => 'multiple_steps',
417
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.multiple_steps')),
418
                'type' => 'select_from_array',
419
                'options' => __('medianet-dev.backpack-form::formbuilder.labels.bool'),
420
                'wrapper' => ['class' => 'form-group col-md-6'],
421
                'default' => 1,
422
                'tab' => __('medianet-dev.backpack-form::formbuilder.labels.config_tab')
423
            ],
424
425
            // Notifications tab fields
426
            [
427
                'name' => 'separator_email_admin',
428
                'type' => 'custom_html',
429
                'value' => __('medianet-dev.backpack-form::formbuilder.labels.notification_admin'),
430
                'tab' => __('medianet-dev.backpack-form::formbuilder.labels.notifications_tab')
431
            ],
432
            [
433
                'name' => 'by_mail',
434
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.by_mail')),
435
                'type' => 'select_from_array',
436
                'options' => __('medianet-dev.backpack-form::formbuilder.labels.bool'),
437
                'wrapper' => ['class' => 'form-group col-md-6'],
438
                'tab' => __('medianet-dev.backpack-form::formbuilder.labels.notifications_tab')
439
            ],
440
            [
441
                'name' => 'mail_to',
442
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.mail_to')),
443
                'hint' => ucfirst(__('medianet-dev.backpack-form::formbuilder.hints.mail_to')),
444
                'type' => 'text',
445
                'wrapper' => ['class' => 'form-group col-md-6'],
446
                'attributes' => ['placeholder' => config('backpack-form.email.to')],
447
                'tab' => __('medianet-dev.backpack-form::formbuilder.labels.notifications_tab')
448
            ],
449
            [
450
                'name' => 'include_data',
451
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.include_data')),
452
                'hint' => ucfirst(__('medianet-dev.backpack-form::formbuilder.hints.include_data')),
453
                'type' => 'select_from_array',
454
                'options' => __('medianet-dev.backpack-form::formbuilder.labels.bool'),
455
                'wrapper' => ['class' => 'form-group col-md-6'],
456
                'tab' => __('medianet-dev.backpack-form::formbuilder.labels.notifications_tab')
457
            ],
458
            [
459
                'name' => 'subject_admin',
460
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.subject_admin')),
461
                'type' => 'text',
462
                'wrapper' => ['class' => 'form-group col-md-6'],
463
                'attributes' => [
464
                    'placeholder' => __('medianet-dev.backpack-form::formbuilder.emails.default_subject', [
465
                        'app_name' => config('app.name')
466
                    ])
467
                ],
468
                'tab' => __('medianet-dev.backpack-form::formbuilder.labels.notifications_tab')
469
            ],
470
            [
471
                'name' => 'message_admin',
472
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.message_admin')),
473
                'hint' => ucfirst(__('medianet-dev.backpack-form::formbuilder.hints.message_admin')),
474
                'type' => 'summernote',
475
                'options' => [
476
                    'toolbar' => [
477
                        ['style', ['bold', 'italic', 'underline', 'clear']],
478
                        ['insert', ['link', 'hr']],
479
                        ['fontsize', ['fontsize']],
480
                        ['color', ['color']],
481
                        ['para', ['ul', 'ol', 'paragraph']],
482
                        ['height', ['height']]
483
                    ],
484
                    'placeholder' => __('medianet-dev.backpack-form::formbuilder.emails.message_admin', ['form_title' => ''])
485
                ],
486
                'tab' => __('medianet-dev.backpack-form::formbuilder.labels.notifications_tab')
487
            ],
488
            [
489
                'name' => 'separator_email_user',
490
                'type' => 'custom_html',
491
                'value' => __('medianet-dev.backpack-form::formbuilder.labels.notification_user'),
492
                'tab' => __('medianet-dev.backpack-form::formbuilder.labels.notifications_tab')
493
            ],
494
            [
495
                'name' => 'copy_user',
496
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.copy_user')),
497
                'type' => 'select_from_array',
498
                'options' => __('medianet-dev.backpack-form::formbuilder.labels.bool'),
499
                'wrapper' => ['class' => 'form-group col-md-6'],
500
                'tab' => __('medianet-dev.backpack-form::formbuilder.labels.notifications_tab')
501
            ],
502
            [
503
                'name' => 'field_mail_name',
504
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.field_mail_name')),
505
                'hint' => ucfirst(__('medianet-dev.backpack-form::formbuilder.hints.field_mail_name')),
506
                'type' => 'text',
507
                'wrapper' => ['class' => 'form-group col-md-6'],
508
                'tab' => __('medianet-dev.backpack-form::formbuilder.labels.notifications_tab')
509
            ],
510
            [
511
                'name' => 'subject_user',
512
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.subject_user')),
513
                'type' => 'text',
514
                'wrapper' => ['class' => 'form-group col-md-6'],
515
                'attributes' => [
516
                    'placeholder' => __('medianet-dev.backpack-form::formbuilder.emails.default_subject', [
517
                        'app_name' => config('app.name')
518
                    ])
519
                ],
520
                'tab' => __('medianet-dev.backpack-form::formbuilder.labels.notifications_tab')
521
            ],
522
            [
523
                'name' => 'message_user',
524
                'label' => ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.message_user')),
525
                'hint' => ucfirst(__('medianet-dev.backpack-form::formbuilder.hints.message_user')),
526
                'type' => 'summernote',
527
                'options' => [
528
                    'toolbar' => [
529
                        ['style', ['bold', 'italic', 'underline', 'clear']],
530
                        ['insert', ['link', 'hr']],
531
                        ['fontsize', ['fontsize']],
532
                        ['color', ['color']],
533
                        ['para', ['ul', 'ol', 'paragraph']],
534
                        ['height', ['height']]
535
                    ],
536
                    'placeholder' => __('medianet-dev.backpack-form::formbuilder.emails.message_user', [
537
                        'app_name' => config('app.name')
538
                    ])
539
                ],
540
                'tab' => __('medianet-dev.backpack-form::formbuilder.labels.notifications_tab')
541
            ]
542
        ]);
543
544
        if (
545
            empty(config('backpack-form.captcha_v3_site_key')) ||
546
            empty(config('backpack-form.captcha_v3_secret_key'))
547
        ) {
548
            $this->crud->field('display_captcha')
549
                ->hint(__('medianet-dev.backpack-form::formbuilder.hints.captcha_config_error'))
0 ignored issues
show
Bug introduced by
It seems like __('medianet-dev.backpac....captcha_config_error') can also be of type array and array; however, parameter $value of Backpack\CRUD\app\Librar...Panel\CrudField::hint() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

549
                ->hint(/** @scrutinizer ignore-type */ __('medianet-dev.backpack-form::formbuilder.hints.captcha_config_error'))
Loading history...
550
                ->attributes(['disabled' => 'disabled']);
551
        }
552
553
        //$this->addFormioBuilderScript();
554
    }
555
556
    /**
557
     * Add JavaScript for handling the Form.io builder
558
     */
559
    protected function addFormioBuilderScript()
560
    {
561
        $this->crud->addField([
562
            'name' => 'formio_builder_script',
563
            'type' => 'form_builder_custom_html',
564
            'tab' => __('medianet-dev.backpack-form::formbuilder.tabs.form_io_builder'),
565
        ]);
566
    }
567
    protected function setupUpdateOperation()
568
    {
569
        $this->setupCreateOperation();
570
    }
571
572
    public function store()
573
    {
574
        // \DB::beginTransaction();
575
        try {
576
            // dd(request()->all());
577
            $response = $this->traitStore();
578
            $form = $this->crud->getCurrentEntry();
579
            setAuditFields($form, 'create');
0 ignored issues
show
Bug introduced by
The function setAuditFields was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

579
            /** @scrutinizer ignore-call */ 
580
            setAuditFields($form, 'create');
Loading history...
580
581
            if (!$form || !$form->id) {
582
                return back()->withErrors(['message' => __('medianet-dev.backpack-form::formbuilder.validations.form_not_found')]);
583
            }
584
585
            // Save translations in bulk
586
            $translationsToInsert = [];
587
            $languages = languages();
0 ignored issues
show
Bug introduced by
The function languages was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

587
            $languages = /** @scrutinizer ignore-call */ languages();
Loading history...
588
            foreach ($languages as $locale => $language) {
589
                if (request()->has($locale)) {
590
                    $translationData = request()->input($locale);
591
                    if (isset($translationData['title']) && !empty($translationData['title'])) {
592
                        $translationsToInsert[] = [
593
                            'title' => $translationData['title'],
594
                            'text_button' => $translationData['text_button'] ?? null,
595
                            'slug' => $translationData['slug'] ?? null,
596
                            'description' => $translationData['description'] ?? null,
597
                            'header' => $translationData['header'] ?? null,
598
                            'footer' => $translationData['footer'] ?? null,
599
                            'form_id' => $form->id,
600
                            'locale' => $locale,
601
                            'created_at' => now(),
602
                            'updated_at' => now(),
603
                        ];
604
                    }
605
                }
606
            }
607
            if (!empty($translationsToInsert)) {
608
                FormbuilderTranslation::insert($translationsToInsert);
609
            }
610
611
            // Update form components if available
612
            if (request()->has('formio_component')) {
613
                $formComponents = json_decode(request()->formio_component);
614
                $form->update(['formio_component' => $formComponents]);
615
616
                $this->processFormStepsAndFields($form, $formComponents);
617
            }
618
619
            // \DB::commit();
620
            return $response;
621
        } catch (\Exception $e) {
622
            \Log::error('Form creation failed: ' . $e->getMessage());
623
            return back()->withErrors(['message' => __('medianet-dev.backpack-form::formbuilder.validations.form_creation_failed')]);
624
        }
625
    }
626
627
    public function update()
628
    {
629
        // \DB::beginTransaction();
630
        try {
631
            $response = $this->traitUpdate();
632
            $form = $this->crud->getCurrentEntry();
633
            setAuditFields($form, 'update');
0 ignored issues
show
Bug introduced by
The function setAuditFields was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

633
            /** @scrutinizer ignore-call */ 
634
            setAuditFields($form, 'update');
Loading history...
634
635
            if (!$form || !$form->id) {
636
                return back()->withErrors(['message' => __('medianet-dev.backpack-form::formbuilder.validations.form_not_found')]);
637
            }
638
639
            // Update translations in bulk
640
            $this->updateTranslation(FormbuilderTranslation::class, 'form_id');
641
642
            // Update form components if available
643
            if (request()->has('formio_component')) {
644
                $formComponents = json_decode(request()->formio_component);
645
                $form->update(['formio_component' => $formComponents]);
646
647
648
                $this->processFormStepsAndFields($form, $formComponents);
649
            }
650
651
            // \DB::commit();
652
            return $response;
653
        } catch (\Exception $e) {
654
            // \DB::rollBack();
655
            \Log::error('Form update failed: ' . $e->getMessage());
656
            return back()->withErrors(['message' => __('medianet-dev.backpack-form::formbuilder.validations.form_update_failed')]);
657
        }
658
    }
659
660
    /**
661
     * Save translations for a given form.
662
     */
663
    protected function saveTranslations($form)
664
    {
665
        $translationsToInsert = [];
666
        $languages = languages();
0 ignored issues
show
Bug introduced by
The function languages was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

666
        $languages = /** @scrutinizer ignore-call */ languages();
Loading history...
667
        foreach ($languages as $locale => $language) {
668
            if (request()->has($locale)) {
669
                $translationData = request()->input($locale);
670
                if (isset($translationData['title']) && !empty($translationData['title'])) {
671
                    $translationsToInsert[] = [
672
                        'title' => $translationData['title'],
673
                        'text_button' => $translationData['text_button'] ?? null,
674
                        'slug' => $translationData['slug'] ?? null,
675
                        'description' => $translationData['description'] ?? null,
676
                        'header' => $translationData['header'] ?? null,
677
                        'footer' => $translationData['footer'] ?? null,
678
                        'form_id' => $form->id,
679
                        'locale' => $locale,
680
                        'created_at' => now(),
681
                        'updated_at' => now(),
682
                    ];
683
                }
684
            }
685
        }
686
        if (!empty($translationsToInsert)) {
687
            FormbuilderTranslation::insert($translationsToInsert);
688
        }
689
    }
690
691
692
693
694
   private function processFormStepsAndFields($form, $formComponents)
695
{
696
    $currentLocale = App::getLocale();
697
    $allFields = [];
698
699
    // Delete all existing steps and fields for this form
700
    $existingSteps = FormStep::where('form_id', $form->id)->get();
701
    foreach ($existingSteps as $step) {
702
        $fields = FormField::where('form_step_id', $step->id)->get();
703
        foreach ($fields as $field) {
704
            FormFieldCondition::where('form_field_id', $field->id)->delete();
705
            $field->translation()->delete();
706
            $field->delete();
707
        }
708
        $step->translation()->delete();
709
        $step->delete();
710
    }
711
712
    // Delete standalone fields (without step association)
713
    $standaloneFields = FormField::where('form_id', $form->id)
714
                              ->whereNull('form_step_id')
715
                              ->get();
716
    foreach ($standaloneFields as $field) {
717
        FormFieldCondition::where('form_field_id', $field->id)->delete();
718
        $field->translation()->delete();
719
        $field->delete();
720
    }
721
722
    // Collect panel and non-panel components
723
    $panelComponents = [];
724
    $nonPanelComponents = [];
725
726
    foreach ($formComponents as $index => $component) {
727
        if ($component->type === 'panel') {
728
            $panelComponents[] = (object)[
729
                'component' => $component,
730
                'index' => $index
731
            ];
732
        } else {
733
            $nonPanelComponents[] = $component;
734
        }
735
    }
736
737
    // 1. Process panel components (which act as steps in the form)
738
    foreach ($panelComponents as $panelData) {
739
        $stepFields = $this->processFormStep($form, $panelData->component, $currentLocale, $panelData->index);
740
        $allFields = array_merge($allFields, $stepFields);
741
    }
742
743
    // 2. Process fields without a panel and add directly to form_fields with null form_step_id
744
    if (!empty($nonPanelComponents)) {
745
        $standaloneFields = $this->createStandaloneFields($form, $nonPanelComponents);
746
        $allFields = array_merge($allFields, $standaloneFields);
747
    }
748
749
    // 3. Process all conditions after all fields are created
750
    // For panel components
751
    foreach ($panelComponents as $panelData) {
752
        $panel = $panelData->component;
753
        $step = FormStep::where('form_id', $form->id)
754
            ->where('key', $panel->key)
755
            ->first();
756
757
        if ($step && isset($panel->components) && is_array($panel->components)) {
758
            foreach ($panel->components as $fieldComponent) {
759
                $this->processComponentCondition($form, $fieldComponent, $step);
760
            }
761
        }
762
    }
763
764
    // For non-panel components
765
    if (!empty($nonPanelComponents)) {
766
        foreach ($nonPanelComponents as $component) {
767
            $this->processComponentCondition($form, $component, null);
768
        }
769
    }
770
771
    return $allFields;
772
}
773
774
private function processFormStep($form, $panel, $currentLocale, $order)
775
{
776
    // Create new step from panel
777
    $step = new FormStep([
778
        'is_active' => true,
779
        'key' => $panel->key,
780
        'form_id' => $form->id,
781
        'is_default' => false,
782
        'order' => $order,
783
    ]);
784
    $step->save();
785
786
    FormStepTranslation::create([
787
        'title' => $panel->label,
788
        'form_step_id' => $step->id,
789
        'locale' => $currentLocale,
790
        'created_at' => now(),
791
        'updated_at' => now(),
792
    ]);
793
794
    // Process fields in the panel if any
795
    if (isset($panel->components) && is_array($panel->components)) {
796
        return $this->createStepFields($form, $panel->components, $step);
797
    }
798
799
    return [];
800
}
801
802
private function createStepFields($form, $components, $step)
803
{
804
    $fieldsMap = [];
805
806
    // Create fields for this step
807
    foreach ($components as $order => $component) {
808
        $field = new FormField([
809
            'form_step_id' => $step->id,
810
            'form_id' => $form->id,
811
            'key' => $component->key,
812
            'type' => $component->type,
813
            'is_active' => isset($component->isActive) ? $component->isActive : true,
814
            'is_required' => isset($component->validate) && isset($component->validate->required) ? $component->validate->required : false,
815
            'order' => isset($component->order) ? $component->order : $order,
816
        ]);
817
        $field->save();
818
819
        // Store field by key for condition processing
820
        $fieldsMap[$component->key] = $field;
821
822
        // Create field translation
823
        FormFieldTranslation::create([
824
            'label' => isset($component->label) ? $component->label : null,
825
            'placeholder' => isset($component->placeholder) ? $component->placeholder : null,
826
            'default_value' => isset($component->defaultValue) ? $component->defaultValue : null,
827
            'description' => isset($component->description) ? $component->description : null,
828
            'form_field_id' => $field->id,
829
            'locale' => App::getLocale(),
830
            'created_at' => now(),
831
            'updated_at' => now(),
832
        ]);
833
    }
834
835
    return $fieldsMap;
836
}
837
838
private function createStandaloneFields($form, $components)
839
{
840
    $fieldsMap = [];
841
842
    // Create standalone fields (not associated with any step)
843
    foreach ($components as $order => $component) {
844
        $field = new FormField([
845
            'form_step_id' => null, // No step association
846
            'form_id' => $form->id,
847
            'key' => $component->key,
848
            'type' => $component->type,
849
            'is_active' => isset($component->isActive) ? $component->isActive : true,
850
            'is_required' => isset($component->validate) && isset($component->validate->required) ? $component->validate->required : false,
851
            'order' => isset($component->order) ? $component->order : $order,
852
        ]);
853
        $field->save();
854
855
        // Store field by key for condition processing
856
        $fieldsMap[$component->key] = $field;
857
858
        // Create field translation
859
        FormFieldTranslation::create([
860
            'label' => isset($component->label) ? $component->label : null,
861
            'placeholder' => isset($component->placeholder) ? $component->placeholder : null,
862
            'default_value' => isset($component->defaultValue) ? $component->defaultValue : null,
863
            'description' => isset($component->description) ? $component->description : null,
864
            'form_field_id' => $field->id,
865
            'locale' => App::getLocale(),
866
            'created_at' => now(),
867
            'updated_at' => now(),
868
        ]);
869
    }
870
871
    return $fieldsMap;
872
}
873
874
private function processComponentCondition($form, $component, $step = null)
875
{
876
    // Check if component has conditional logic
877
    if (!isset($component->conditional) || !isset($component->conditional->when)) {
878
        return;
879
    }
880
881
    // Get the field for this component
882
    $field = FormField::where('form_id', $form->id)
883
        ->where('key', $component->key)
884
        ->first();
885
886
    if (!$field) {
887
        return;
888
    }
889
890
    // Find the field this condition depends on (across all steps)
891
    $whenField = FormField::where('form_id', $form->id)
892
        ->where('key', $component->conditional->when)
893
        ->first();
894
895
    if (!$whenField) {
896
        return;
897
    }
898
899
    // Delete any existing conditions for this field
900
    FormFieldCondition::where('form_field_id', $field->id)->delete();
901
902
    // Create new condition
903
    $conditionData = [
904
        'operation' => config('backpack-form.conditional_operations.equal', 'equal'),
905
        'show' => isset($component->conditional->show) && $component->conditional->show
906
            ? config('backpack-form.display_condition.show')
907
            : config('backpack-form.display_condition.hide'),
908
        'eq' => isset($component->conditional->eq) ? $component->conditional->eq : null,
909
        'when' => $whenField->id,
910
        'form_field_id' => $field->id,
911
        'form_step_id' => $step ? $step->id : null
912
    ];
913
914
    FormFieldCondition::create($conditionData);
915
}
916
}
917