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 |
|
|
|
|
68
|
|
|
* @param null $group_by |
|
|
|
|
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; |
|
|
|
|
75
|
|
|
$this->connect->relation_name = $relation_name; |
|
|
|
|
76
|
|
|
$this->connect->group_by = $group_by; |
|
|
|
|
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
|
|
|
|