Completed
Pull Request — master (#14)
by
unknown
15:13 queued 07:44
created

Select::getEditFormValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Yaro\Jarboe\Table\Fields;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Collection;
7
use Yaro\Jarboe\Table\Fields\Traits\Nullable;
8
use Yaro\Jarboe\Table\Fields\Traits\Orderable;
9
use Yaro\Jarboe\Table\Fields\Traits\Relations;
10
11
class Select extends AbstractField
12
{
13
    use Orderable;
14
    use Relations;
15
    use Nullable;
16
17
    const SIMPLE   = 'simple';
18
    const SELECT_2 = 'select2';
19
20
    const ALLOWED_TYPES = [
21
        self::SIMPLE,
22
        self::SELECT_2,
23
    ];
24
25
    protected $multiple = false;
26
    protected $type = self::SIMPLE;
27
    protected $ajax = false;
28
    protected $searchCallback;
29
30
    public function isSelect2Type()
31
    {
32
        return $this->type == self::SELECT_2;
33
    }
34
35
    public function isSimpleType()
36
    {
37
        return $this->type == self::SIMPLE;
38
    }
39
40
    public function type($type)
41
    {
42
        $type = strtolower($type);
43
        if (!in_array($type, self::ALLOWED_TYPES)) {
44
            throw new \RuntimeException(sprintf('Not allowed type [%s] for SelectField', $type));
45
        }
46
47
        $this->type = $type;
48
49
        return $this;
50
    }
51
52
    public function getType()
53
    {
54
        return $this->type;
55
    }
56
57
    public function ajax(bool $ajax = true)
58
    {
59
        $this->ajax = $ajax;
60
61
        return $this;
62
    }
63
64
    public function isAjax()
65
    {
66
        return $this->ajax;
67
    }
68
69
    public function multiple(bool $multiple = true)
70
    {
71
        $this->multiple = $multiple;
72
73
        return $this;
74
    }
75
76
    public function isMultiple()
77
    {
78
        return $this->multiple;
79
    }
80
81
    public function isCurrentOption($option, $model = null, $relationIndex = 0): bool
82
    {
83
        if ($this->hasOld()) {
84 View Code Duplication
            if ($this->isGroupedRelation()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
                $option = crc32($this->relations[$relationIndex]['group']) .'~~~'. $option;
86
            }
87
            if ($this->isMultiple()) {
88
                return in_array($option, $this->old());
89
            }
90
            return $this->old() == $option;
91
        }
92
93
        if (is_null($model)) {
94
            return (string) $option === (string) $this->getDefault();
95
        }
96
97
        if ($this->isMultiple() && !$this->isRelationField()) {
98
            return in_array($option, $model->{$this->name});
99
        }
100
101 View Code Duplication
        if ($this->isRelationField()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
            $related = $model->{$this->getRelationMethod($relationIndex)};
103
            if ($related) {
104
                $relatedModelClass = get_class($model->{$this->getRelationMethod($relationIndex)}()->getRelated());
105
                $freshRelatedModel = new $relatedModelClass;
106
                $collection = $related;
107
                if (!is_a($related, Collection::class)) {
108
                    $collection = collect([$related]);
109
                }
110
111
                return $collection->contains($freshRelatedModel->getKeyName(), $option);
112
            }
113
            return false;
114
        }
115
116
        return (string) $option === (string) $model->{$this->name};
117
    }
118
119
    public function relationSearch(\Closure $callback)
120
    {
121
        $this->searchCallback = $callback;
122
123
        return $this;
124
    }
125
126
    public function value(Request $request)
127
    {
128
        $value = $request->get($this->name());
129
130
        if ($this->isMultiple() && !$this->isRelationField()) {
131
            $value = $value ?: [];
132
        }
133
134
        if (!$value && $this->isNullable()) {
135
            return null;
136
        }
137
138
        return $value;
139
    }
140
141
    public function shouldSkip(Request $request)
142
    {
143
        return $this->isRelationField();
144
    }
145
146
    public function afterStore($model, Request $request)
147
    {
148
        $this->afterUpdate($model, $request);
149
    }
150
151 View Code Duplication
    public function afterUpdate($model, Request $request)
152
    {
153
        if (!$this->isRelationField()) {
154
            return;
155
        }
156
        if ($this->isReadonly()) {
157
            return;
158
        }
159
160
        $this->syncRelations($model, $request->get($this->name()));
161
    }
162
163
    public function getListView($model)
164
    {
165
        return view('jarboe::crud.fields.select.list', [
166
            'model' => $model,
167
            'field' => $this,
168
            'options' => $this->getOptions(),
169
        ]);
170
    }
171
172
    public function getEditFormView($model)
173
    {
174
        $template = $this->isReadonly() ? 'readonly' : 'edit';
175
176
        return view('jarboe::crud.fields.select.'. $template, [
177
            'model' => $model,
178
            'field' => $this,
179
        ]);
180
    }
181
182
    public function getCreateFormView()
183
    {
184
        return view('jarboe::crud.fields.select.create', [
185
            'field' => $this,
186
        ]);
187
    }
188
189
    private function filterValuesForMorphToManyRelation($ids, $relationHash)
190
    {
191
        $filtered = [];
192
        foreach ($ids as $id) {
193
            if (strpos($id, $relationHash .'~~~') !== false) {
194
                $id = substr($id, 13);
195
                $filtered[] = $id;
196
            }
197
        }
198
199
        return $filtered;
200
    }
201
}
202