|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Form\Element; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Routing\Router; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use SleepingOwl\Admin\Contracts\Initializable; |
|
8
|
|
|
use SleepingOwl\Admin\Contracts\WithRoutesInterface; |
|
9
|
|
|
use SleepingOwl\Admin\Contracts\Repositories\RepositoryInterface; |
|
10
|
|
|
|
|
11
|
|
|
class SelectAjax extends Select implements Initializable, WithRoutesInterface |
|
12
|
|
|
{ |
|
13
|
|
|
protected static $route = 'selectajax'; |
|
14
|
|
|
protected $view = 'form.element.selectajax'; |
|
15
|
|
|
protected $search_url = null; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param string $path |
|
19
|
|
|
* @param string|null $label |
|
20
|
|
|
* @param array|Model $options |
|
21
|
|
|
*/ |
|
22
|
|
View Code Duplication |
public function __construct($path, $label = null, $options = []) |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
|
|
parent::__construct($path, $label); |
|
25
|
|
|
|
|
26
|
|
|
$this->setLoadOptionsQueryPreparer(function ($item, $query) { |
|
27
|
|
|
$repository = app(RepositoryInterface::class); |
|
28
|
|
|
$repository->setModel($this->getModelForOptions()); |
|
29
|
|
|
$key = $repository->getModel()->getKeyName(); |
|
30
|
|
|
|
|
31
|
|
|
return $query->where([$key => $this->getValueFromModel()]); |
|
32
|
|
|
}); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param Router $router |
|
37
|
|
|
*/ |
|
38
|
285 |
View Code Duplication |
public static function registerRoutes(Router $router) |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
285 |
|
$routeName = 'admin.form.element.'.static::$route; |
|
41
|
|
|
|
|
42
|
285 |
|
if (! $router->has($routeName)) { |
|
43
|
285 |
|
$router->post('{adminModel}/'.static::$route.'/{field}/{id?}', [ |
|
44
|
285 |
|
'as' => $routeName, |
|
45
|
285 |
|
'uses' => 'SleepingOwl\Admin\Http\Controllers\FormElementController@selectSearch', |
|
46
|
285 |
|
]); |
|
47
|
285 |
|
} |
|
48
|
285 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param $url |
|
52
|
|
|
*/ |
|
53
|
|
|
public function setSearchUrl($url) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->search_url = $url; |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
View Code Duplication |
public function getSearchUrl() |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
return $this->search_url ? $this->search_url : route('admin.form.element.'.static::$route, [ |
|
66
|
|
|
'adminModel' => \AdminSection::getModel($this->model)->getAlias(), |
|
67
|
|
|
'field' => $this->getName(), |
|
68
|
|
|
'id' => $this->model->getKey(), |
|
69
|
|
|
]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return array |
|
74
|
|
|
*/ |
|
75
|
|
|
public function toArray() |
|
76
|
|
|
{ |
|
77
|
|
|
$attributes = [ |
|
78
|
|
|
'id' => $this->getName(), |
|
79
|
|
|
'size' => 2, |
|
80
|
|
|
'data-select-type' => 'single', |
|
81
|
|
|
'class' => 'form-control js-data-ajax', |
|
82
|
|
|
'model' => get_class($this->getModelForOptions()), |
|
83
|
|
|
'field' => $this->getDisplay(), |
|
84
|
|
|
'search_url' => $this->getSearchUrl(), |
|
85
|
|
|
]; |
|
86
|
|
|
|
|
87
|
|
|
return ['attributes' => $attributes] + parent::toArray(); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
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.