1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Form\Element; |
4
|
|
|
|
5
|
|
|
use AdminSection; |
|
|
|
|
6
|
|
|
use Illuminate\Routing\Router; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
use SleepingOwl\Admin\Contracts\Initializable; |
9
|
|
|
use SleepingOwl\Admin\Traits\SelectAjaxFunctions; |
10
|
|
|
use SleepingOwl\Admin\Contracts\WithRoutesInterface; |
11
|
|
|
use SleepingOwl\Admin\Contracts\Repositories\RepositoryInterface; |
12
|
|
|
|
13
|
|
|
class SelectAjax extends Select implements Initializable, WithRoutesInterface |
14
|
|
|
{ |
15
|
|
|
use SelectAjaxFunctions; |
16
|
|
|
|
17
|
|
|
protected static $route = 'selectajax'; |
18
|
|
|
protected $view = 'form.element.selectajax'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string|null |
22
|
|
|
*/ |
23
|
|
|
protected $language; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* SelectAjax constructor. |
27
|
|
|
* @param $path |
28
|
|
|
* @param null $label |
|
|
|
|
29
|
|
|
* @throws \SleepingOwl\Admin\Exceptions\Form\Element\SelectException |
30
|
|
|
* @throws \SleepingOwl\Admin\Exceptions\Form\FormElementException |
31
|
|
|
*/ |
32
|
|
|
public function __construct($path, $label = null) |
33
|
|
|
{ |
34
|
|
|
parent::__construct($path, $label); |
35
|
|
|
|
36
|
|
|
/* |
|
|
|
|
37
|
|
|
// This code add closure, that modify query so, that select will have only one value. |
38
|
|
|
// This feature need for optimized selectajax initiation for also existing records. |
39
|
|
|
// Make some changes for availability to use setLoadOptionsQueryPreparer() in Sections. |
40
|
|
|
$this->setLoadOptionsQueryPreparer(function ($item, Builder $query) { |
41
|
|
|
$repository = app(RepositoryInterface::class); |
42
|
|
|
$repository->setModel($this->getModelForOptions()); |
43
|
|
|
$key = $repository->getModel()->getKeyName(); |
44
|
|
|
|
45
|
|
|
$return = $query->where([$key => $this->getValueFromModel()]); |
46
|
|
|
return $return; |
47
|
|
|
}); |
48
|
|
|
*/ |
49
|
|
|
|
50
|
|
|
$this->default_query_preparer = function ($item, Builder $query) { |
51
|
|
|
$repository = app(RepositoryInterface::class); |
52
|
|
|
$repository->setModel($this->getModelForOptions()); |
53
|
|
|
$key = $repository->getModel()->getKeyName(); |
54
|
|
|
|
55
|
|
|
return $query->where([$key => $this->getValueFromModel()]); |
56
|
|
|
}; |
57
|
|
|
|
58
|
|
|
$this->setLanguage(config('app.locale')); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return string|null |
63
|
|
|
*/ |
64
|
|
|
public function getLanguage() |
65
|
|
|
{ |
66
|
|
|
return $this->language; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $language |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function setLanguage($language) |
74
|
|
|
{ |
75
|
|
|
$this->language = $language; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param Router $router |
82
|
|
|
*/ |
83
|
|
|
public static function registerRoutes(Router $router) |
84
|
|
|
{ |
85
|
|
|
$routeName = 'admin.form.element.'.static::$route; |
86
|
|
|
|
87
|
|
|
if (! $router->has($routeName)) { |
88
|
|
|
$router->post('{adminModel}/'.static::$route.'/{field}/{id?}', [ |
89
|
|
|
'as' => $routeName, |
90
|
|
|
'uses' => 'SleepingOwl\Admin\Http\Controllers\FormElementController@selectSearch', |
91
|
|
|
]); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Getter of search url. |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function getSearchUrl() |
100
|
|
|
{ |
101
|
|
|
return $this->search_url ? $this->search_url : route('admin.form.element.'.static::$route, [ |
102
|
|
|
'adminModel' => AdminSection::getModel($this->model)->getAlias(), |
103
|
|
|
'field' => $this->getName(), |
104
|
|
|
'id' => $this->model->getKey(), |
105
|
|
|
]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Search url for ajax. |
110
|
|
|
* @param $url |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
|
|
public function setSearchUrl($url) |
114
|
|
|
{ |
115
|
|
|
$this->search_url = $url; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
public function toArray() |
124
|
|
|
{ |
125
|
|
|
$this->setLoadOptionsQueryPreparer($this->default_query_preparer); |
126
|
|
|
|
127
|
|
|
$this->setHtmlAttributes([ |
128
|
|
|
'id' => $this->getId(), |
129
|
|
|
'class' => 'form-control js-data-ajax', |
130
|
|
|
'data-select-type' => 'single', |
131
|
|
|
//'model' => get_class($this->getModelForOptions()), |
|
|
|
|
132
|
|
|
//'field' => $this->getDisplay(), |
|
|
|
|
133
|
|
|
//'search' => $this->getSearch(), |
|
|
|
|
134
|
|
|
'search_url' => $this->getSearchUrl(), |
135
|
|
|
'data-min-symbols' => $this->getMinSymbols(), |
136
|
|
|
]); |
137
|
|
|
|
138
|
|
|
if (count($this->getDataDependsArray())) { |
139
|
|
|
$depends = $this->getDataDependsArray(); |
140
|
|
|
$depends = array_map(function ($el) { |
141
|
|
|
return strtr($el, ['.' => '__']); |
142
|
|
|
}, $depends); |
143
|
|
|
$depends = json_encode($depends); |
144
|
|
|
|
145
|
|
|
$this->setHtmlAttributes([ |
146
|
|
|
'data-language' => $this->getLanguage(), |
147
|
|
|
'data-depends' => $depends, |
148
|
|
|
'data-url' => $this->getSearchUrl(), |
149
|
|
|
'class' => 'input-select input-select-dependent', |
150
|
|
|
]); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return ['attributes' => $this->getHtmlAttributes()] + parent::toArray(); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths