Passed
Push — master ( 08f1f5...7d3eb5 )
by Alexandr
02:22
created

FormSelectKey::getOptions()   D

Complexity

Conditions 9
Paths 17

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 18
nc 17
nop 0
dl 0
loc 30
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
namespace Larrock\Core\Helpers\FormBuilder;
4
5
use View;
6
use Illuminate\Database\Eloquent\Model;
7
use Larrock\Core\Exceptions\LarrockFormBuilderRowException;
8
9
/**
10
 * Select где в value следует ключ (он и сохраняется), а на выводе значение из массива options
11
 * Class FormSelectKey.
12
 */
13
class FormSelectKey extends FBElement
14
{
15
    /** @var null|mixed */
16
    public $options;
17
18
    /** @var string */
19
    public $option_title;
20
21
    /** @var string */
22
    public $option_key;
23
24
    /** @var null|mixed */
25
    public $connect;
26
27
    /** @var string Имя шаблона FormBuilder для отрисовки поля */
28
    public $FBTemplate = 'larrock::admin.formbuilder.select.key';
29
30
    /**
31
     * @param array $options
32
     * @return $this
33
     */
34
    public function setOptions(array $options)
35
    {
36
        $this->options = $options;
37
38
        return $this;
39
    }
40
41
    /**
42
     * @param $row
43
     * @return $this
44
     */
45
    public function setOptionsTitle($row)
46
    {
47
        $this->option_title = $row;
48
49
        return $this;
50
    }
51
52
    /**
53
     * @param $row
54
     * @return $this
55
     */
56
    public function setOptionsKey($row)
57
    {
58
        $this->option_key = $row;
59
60
        return $this;
61
    }
62
63
    /**
64
     * Установка связи поля с какой-либо моделью
65
     * Сейчас применяется в FormSelect, FormCategory.
66
     * @param Model $model
67
     * @param null $relation_name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $relation_name is correct as it would always require null to be passed?
Loading history...
68
     * @param null $group_by
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $group_by is correct as it would always require null to be passed?
Loading history...
69
     * @return $this
70
     */
71
    public function setConnect($model, $relation_name = null, $group_by = null)
72
    {
73
        $this->connect = collect();
74
        $this->connect->model = $model;
0 ignored issues
show
Bug introduced by
The property model does not seem to exist on Illuminate\Support\Collection.
Loading history...
75
        $this->connect->relation_name = $relation_name;
0 ignored issues
show
Bug introduced by
The property relation_name does not seem to exist on Illuminate\Support\Collection.
Loading history...
76
        $this->connect->group_by = $group_by;
0 ignored issues
show
Bug introduced by
The property group_by does not exist on Illuminate\Support\Collection. Did you mean groupBy?
Loading history...
77
78
        return $this;
79
    }
80
81
    /**
82
     * Установка опции выборки значений для setConnect().
83
     * @param string $key
84
     * @param string $value
85
     * @return $this
86
     * @throws LarrockFormBuilderRowException
87
     */
88
    public function setWhereConnect(string $key, string $value)
89
    {
90
        if (! isset($this->connect->model)) {
91
            throw new LarrockFormBuilderRowException('У поля '.$this->name.' сначала нужно определить setConnect');
92
        }
93
        $this->connect->where_key = $key;
94
        $this->connect->where_value = $value;
95
96
        return $this;
97
    }
98
99
    /**
100
     * Получени списка опций
101
     * @return \Illuminate\Support\Collection|mixed|null
102
     */
103
    public function getOptions()
104
    {
105
        if ($this->connect) {
106
            if (! $this->options) {
107
                $this->options = collect();
108
            } else {
109
                $this->options = collect($this->options);
110
            }
111
            $model = new $this->connect->model;
112
            $get_options_query = $model;
113
            if (isset($this->connect->where_key) && $this->connect->where_key) {
114
                $get_options_query = $get_options_query->where($this->connect->where_key, '=', $this->connect->where_value);
115
            }
116
117
            if (isset($this->connect->group_by) && $this->connect->group_by) {
118
                $get_options_query = $get_options_query->groupBy([$this->connect->group_by]);
119
            }
120
121
            if ($get_options = $get_options_query->get()) {
122
                foreach ($get_options as $get_options_value) {
123
                    $this->options->push($get_options_value);
124
                }
125
            }
126
127
            $this->options = $this->options->pluck($this->name);
128
        } else {
129
            $this->options = collect($this->options);
130
        }
131
132
        return $this->options;
133
    }
134
135
    /**
136
     * Отрисовка элемента формы.
137
     * @return string
138
     */
139
    public function __toString()
140
    {
141
        if ($this->data && ! isset($this->data->{$this->name}) && $this->default) {
142
            $this->data->{$this->name} = $this->default;
143
        }
144
145
        $this->options = $this->getOptions();
146
147
        $selected = [];
148
        if (\Request::input($this->name)) {
149
            $selected[] = \Request::input($this->name);
150
        } elseif ($this->data) {
151
            $selected[] = $this->data->{$this->name};
152
        }
153
154
        return View::make($this->FBTemplate, ['row_key' => $this->name,
155
            'row_settings' => $this, 'data' => $this->data, 'selected' => $selected, ])->render();
156
    }
157
}
158