Completed
Push — master ( 95f4c8...504059 )
by Alexandr
01:49
created

FormSelect::setConnect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 3
1
<?php
2
3
namespace Larrock\Core\Helpers\FormBuilder;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Larrock\Core\Exceptions\LarrockFormBuilderRowException;
7
use View;
8
9
class FormSelect extends FBElement
10
{
11
    /** @var array|null */
12
    public $options;
13
14
    /** @var string */
15
    public $option_title;
16
17
    /** @var null|bool */
18
    public $allowCreate;
19
20
    /** @var null|mixed */
21
    public $connect;
22
23
    /**
24
     * @param array $options
25
     * @return $this
26
     */
27
    public function setOptions(array $options)
28
    {
29
        $this->options = $options;
30
        return $this;
31
    }
32
33
    /**
34
     * @param string $row
35
     * @return $this
36
     */
37
    public function setOptionsTitle($row)
38
    {
39
        $this->option_title = $row;
40
        return $this;
41
    }
42
43
    /**
44
     * @return $this
45
     */
46
    public function setAllowCreate()
47
    {
48
        $this->allowCreate = TRUE;
49
        return $this;
50
    }
51
52
    /**
53
     * Установка связи поля с какой-либо моделью
54
     * Сейчас применяется в FormSelect, FormCategory
55
     * @param Model $model
56
     * @param null $relation_name
57
     * @param null $group_by
58
     * @return $this
59
     */
60
    public function setConnect($model, $relation_name = NULL, $group_by = NULL)
61
    {
62
        $this->connect = collect();
63
        $this->connect->model = $model;
64
        $this->connect->relation_name = $relation_name;
65
        $this->connect->group_by = $group_by;
66
        return $this;
67
    }
68
69
    /**
70
     * Установка опции выборки значений для setConnect()
71
     * @param string $key
72
     * @param string $value
73
     * @return $this
74
     * @throws LarrockFormBuilderRowException
75
     */
76
    public function setWhereConnect(string $key, string $value)
77
    {
78
        if( !isset($this->connect->model)){
79
            throw new LarrockFormBuilderRowException('У поля '. $this->name .' сначала нужно определить setConnect');
80
        }
81
        $this->connect->where_key = $key;
82
        $this->connect->where_value = $value;
83
        return $this;
84
    }
85
86
    /**
87
     * @param $row_settings
88
     * @param $data
89
     * @return mixed
90
     */
91
    public function render($row_settings, $data)
92
    {
93
        if( !isset($data->{$row_settings->name}) && $row_settings->default && $row_settings->default !== NULL){
94
            $data->{$row_settings->name} = $row_settings->default;
95
        }
96
97
        if($row_settings->connect){
98
            if( !$row_settings->options){
99
                $row_settings->options = collect();
100
            }else{
101
                $row_settings->options = collect($row_settings->options);
102
            }
103
            $model = new $row_settings->connect->model;
104
            $get_options_query = $model;
105
            if(isset($row_settings->connect->where_key) && $row_settings->connect->where_key){
106
                $get_options_query = $get_options_query->where($row_settings->connect->where_key, '=', $row_settings->connect->where_value);
107
            }
108
109
            if(isset($row_settings->connect->group_by) && $row_settings->connect->group_by){
110
                $get_options_query = $get_options_query->groupBy([$row_settings->connect->group_by]);
111
            }
112
113
            if($get_options = $get_options_query->get()){
114
                foreach($get_options as $get_options_value){
115
                    $row_settings->options->push($get_options_value);
116
                }
117
            }
118
        }else{
119
            $row_settings->options = collect($row_settings->options);
120
        }
121
122
        $selected = [];
123
        if(\Request::input($row_settings->name)){
124
            $selected[] = \Request::input($row_settings->name);
125
        }
126
127
        return View::make('larrock::admin.formbuilder.select.value', ['row_key' => $row_settings->name,
128
            'row_settings' => $row_settings, 'data' => $data, 'selected' => $selected])->render();
129
    }
130
}