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

FormSelectKey::setWhereConnect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
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
/**
10
 * Select где в value следует ключ (он и сохраняется), а на выводе значение из массива options
11
 * Class FormSelectKey
12
 * @package Larrock\Core\Helpers\FormBuilder
13
 */
14
class FormSelectKey extends FBElement
15
{
16
    /** @var array|null */
17
    public $options;
18
19
    /** @var string */
20
    public $option_title;
21
22
    /** @var string */
23
    public $option_key;
24
25
    /**
26
     * @param array $options
27
     * @return $this
28
     */
29
    public function setOptions(array $options)
30
    {
31
        $this->options = $options;
32
        return $this;
33
    }
34
35
    /**
36
     * @param $row
37
     * @return $this
38
     */
39
    public function setOptionsTitle($row)
40
    {
41
        $this->option_title = $row;
42
        return $this;
43
    }
44
45
    /**
46
     * @param $row
47
     * @return $this
48
     */
49
    public function setOptionsKey($row)
50
    {
51
        $this->option_key = $row;
52
        return $this;
53
    }
54
55
    /**
56
     * Установка связи поля с какой-либо моделью
57
     * Сейчас применяется в FormSelect, FormCategory
58
     * @param Model $model
59
     * @param null $relation_name
60
     * @param null $group_by
61
     * @return $this
62
     */
63
    public function setConnect($model, $relation_name = NULL, $group_by = NULL)
64
    {
65
        $this->connect = collect();
0 ignored issues
show
Bug introduced by
The property connect does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
66
        $this->connect->model = $model;
67
        $this->connect->relation_name = $relation_name;
68
        $this->connect->group_by = $group_by;
69
        return $this;
70
    }
71
72
    /**
73
     * Установка опции выборки значений для setConnect()
74
     * @param string $key
75
     * @param string $value
76
     * @return $this
77
     * @throws LarrockFormBuilderRowException
78
     */
79
    public function setWhereConnect(string $key, string $value)
80
    {
81
        if( !isset($this->connect->model)){
82
            throw new LarrockFormBuilderRowException('У поля '. $this->name .' сначала нужно определить setConnect');
83
        }
84
        $this->connect->where_key = $key;
85
        $this->connect->where_value = $value;
86
        return $this;
87
    }
88
89
    /**
90
     * @param $row_settings
91
     * @param $data
92
     * @return mixed
93
     */
94
    public function render($row_settings, $data)
95
    {
96
        if( !isset($data->{$row_settings->name}) && $row_settings->default){
97
            $data->{$row_settings->name} = $row_settings->default;
98
        }
99
100
        if($row_settings->connect){
101
            if( !$row_settings->options){
102
                $row_settings->options = collect();
103
            }else{
104
                $row_settings->options = collect($row_settings->options);
105
            }
106
            $model = new $row_settings->connect->model;
107
            $get_options_query = $model;
108
            if(isset($row_settings->connect->where_key) && $row_settings->connect->where_key){
109
                $get_options_query = $get_options_query->where($row_settings->connect->where_key, '=', $row_settings->connect->where_value);
110
            }
111
112
            if(isset($row_settings->connect->group_by) && $row_settings->connect->group_by){
113
                $get_options_query = $get_options_query->groupBy([$row_settings->connect->group_by]);
114
            }
115
116
            if($get_options = $get_options_query->get()){
117
                foreach($get_options as $get_options_value){
118
                    $row_settings->options->push($get_options_value);
119
                }
120
            }
121
        }else{
122
            $row_settings->options = collect($row_settings->options);
123
        }
124
125
        $selected = [];
126
        if(\Request::input($row_settings->name)){
127
            $selected[] = \Request::input($row_settings->name);
128
        }else{
129
            $selected[] = $data->{$row_settings->name};
130
        }
131
132
        return View::make('larrock::admin.formbuilder.select.key', ['row_key' => $row_settings->name,
133
            'row_settings' => $row_settings, 'data' => $data, 'selected' => $selected])->render();
134
    }
135
}