1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Grid\Displayers; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Admin; |
6
|
|
|
use Encore\Admin\Grid\Selectable; |
7
|
|
|
|
8
|
|
|
class BelongsTo extends AbstractDisplayer |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param int $multiple |
12
|
|
|
* |
13
|
|
|
* @return string |
14
|
|
|
*/ |
15
|
|
View Code Duplication |
protected function getLoadUrl($selectable, $multiple = 0) |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
$selectable = str_replace('\\', '_', $selectable); |
18
|
|
|
$args = [$multiple]; |
19
|
|
|
|
20
|
|
|
return route('admin.handle-selectable', compact('selectable', 'args')); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return mixed |
25
|
|
|
*/ |
26
|
|
|
protected function getOriginalData() |
27
|
|
|
{ |
28
|
|
|
return $this->getColumn()->getOriginal(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $selectable |
33
|
|
|
* @param string $column |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public function display($selectable = null, $column = '') |
38
|
|
|
{ |
39
|
|
View Code Duplication |
if (!class_exists($selectable) || !is_subclass_of($selectable, Selectable::class)) { |
|
|
|
|
40
|
|
|
throw new \InvalidArgumentException( |
41
|
|
|
"[Class [{$selectable}] must be a sub class of Encore\Admin\Grid\Selectable" |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return Admin::component('admin::grid.inline-edit.belongsto', [ |
46
|
|
|
'modal' => sprintf('modal-grid-selector-%s', $this->getClassName()), |
47
|
|
|
'key' => $this->getKey(), |
48
|
|
|
'original' => $this->getOriginalData(), |
49
|
|
|
'value' => $this->getValue(), |
50
|
|
|
'resource' => $this->getResource(), |
51
|
|
|
'name' => $column ?: $this->getName(), |
52
|
|
|
'relation' => get_called_class(), |
53
|
|
|
'url' => $this->getLoadUrl($selectable, get_called_class() == BelongsToMany::class), |
|
|
|
|
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
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.