|
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\Orderable; |
|
8
|
|
|
use Yaro\Jarboe\Table\Fields\Traits\Relations; |
|
9
|
|
|
|
|
10
|
|
|
class Radio extends AbstractField |
|
11
|
|
|
{ |
|
12
|
|
|
use Orderable; |
|
13
|
|
|
use Relations; |
|
14
|
|
|
|
|
15
|
|
|
protected $columns = 1; |
|
16
|
|
|
|
|
17
|
|
|
public function isCurrentOption($option, $model = null, $relationIndex = 0): bool |
|
18
|
|
|
{ |
|
19
|
|
|
if ($this->hasOld()) { |
|
20
|
|
View Code Duplication |
if ($this->isGroupedRelation()) { |
|
|
|
|
|
|
21
|
|
|
$option = crc32($this->relations[$relationIndex]['group']) .'~~~'. $option; |
|
22
|
|
|
} |
|
23
|
|
|
return $this->old() == $option; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
if (is_null($model)) { |
|
27
|
|
|
return $option == $this->getDefault(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
View Code Duplication |
if ($this->isRelationField()) { |
|
|
|
|
|
|
31
|
|
|
$related = $model->{$this->getRelationMethod($relationIndex)}; |
|
32
|
|
|
if ($related) { |
|
33
|
|
|
$relatedModelClass = get_class($model->{$this->getRelationMethod($relationIndex)}()->getRelated()); |
|
34
|
|
|
$freshRelatedModel = new $relatedModelClass; |
|
35
|
|
|
$collection = $related; |
|
36
|
|
|
if (!is_a($related, Collection::class)) { |
|
37
|
|
|
$collection = collect([$related]); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $collection->contains($freshRelatedModel->getKeyName(), $option); |
|
41
|
|
|
} |
|
42
|
|
|
return false; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $option == $model->{$this->name}; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function shouldSkip(Request $request) |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->isRelationField(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function afterStore($model, Request $request) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->afterUpdate($model, $request); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
View Code Duplication |
public function afterUpdate($model, Request $request) |
|
59
|
|
|
{ |
|
60
|
|
|
if (!$this->isRelationField()) { |
|
61
|
|
|
return; |
|
62
|
|
|
} |
|
63
|
|
|
if ($this->isReadonly()) { |
|
64
|
|
|
return; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$this->syncRelations($model, $request->get($this->name())); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function columns(int $columns) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->columns = $columns; |
|
73
|
|
|
|
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return int |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getColumns(): int |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->columns; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function getListView($model) |
|
86
|
|
|
{ |
|
87
|
|
|
return view('jarboe::crud.fields.radio.list', [ |
|
88
|
|
|
'model' => $model, |
|
89
|
|
|
'field' => $this, |
|
90
|
|
|
'options' => $this->getOptions(), |
|
91
|
|
|
]); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function getEditFormView($model) |
|
95
|
|
|
{ |
|
96
|
|
|
$template = $this->isReadonly() ? 'readonly' : 'edit'; |
|
97
|
|
|
|
|
98
|
|
|
return view('jarboe::crud.fields.radio.'. $template, [ |
|
99
|
|
|
'model' => $model, |
|
100
|
|
|
'field' => $this, |
|
101
|
|
|
]); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getCreateFormView() |
|
105
|
|
|
{ |
|
106
|
|
|
return view('jarboe::crud.fields.radio.create', [ |
|
107
|
|
|
'field' => $this, |
|
108
|
|
|
]); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
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.