Completed
Push — master ( 188ffb...31684b )
by Maxime
02:48
created

ComponentForm::buildForm()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
rs 8.8571
cc 1
eloc 23
nc 1
nop 0
1
<?php namespace Distilleries\Expendable\Http\Forms\Component;
2
3
use Distilleries\Expendable\Helpers\StaticLabel;
4
use \File;
5
6
class ComponentForm extends \Distilleries\FormBuilder\FormValidator {
7
8
    public static $rules = [
9
        'libelle'   => 'required'
10
    ];
11
    public static $rules_update = null;
12
13
    public function buildForm()
14
    {
15
16
        $this
17
            ->add('libelle', 'text', [
18
                'validation' => 'required',
19
                'label'      => trans('expendable::form.name'),
20
                'help'       => trans('expendable::form.auto_sufix')
21
            ])
22
            ->add('state', 'choice', [
23
                'choices'     => StaticLabel::states(),
24
                'empty_value' => '-',
25
                'label'       => trans('expendable::form.state'),
26
                'expanded'    => true,
27
                'multiple'    => true
28
            ])
29
            ->add('models', 'choice', [
30
                'choices'     => $this->getChoiceModels(),
31
                'empty_value' => '-',
32
                'label'       => trans('expendable::form.model')
33
            ])
34
            ->add('colon_datatable', 'tag', [
35
                'label' => trans('expendable::form.columns'),
36
                'help'  => trans('expendable::form.help_colon_datatable')
37
            ])
38
            ->add('fields_form', 'tag', [
39
                'label' => trans('expendable::form.fields'),
40
                'help'  => trans('expendable::form.help_fields_form')
41
            ])
42
            ->addDefaultActions();
43
    }
44
45
    protected function getChoiceModels()
46
    {
47
        $allModels = [
48
            [
49
                'path'      => app_path() . DIRECTORY_SEPARATOR,
50
                'namespace' => '{{app}}',
51
            ],
52
            [
53
                'path'      => __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'Models' . DIRECTORY_SEPARATOR,
54
                'namespace' => 'Distilleries\\Expendable\\Models\\',
55
            ],
56
        ];
57
58
        $choices = [];
59
        foreach ($allModels as $config)
60
        {
61
            $models = app('files')->files($config['path']);
62
            foreach ($models as $model)
63
            {
64
                $choice                               = explode('/', $model);
65
                $model                                = preg_replace('/.php/i', '', last($choice));
66
                $choices[$config['namespace'] . $model] = $model;
67
            }
68
        }
69
70
        return $choices;
71
    }
72
}