|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Http\Controllers\Operations; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Route; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
|
|
8
|
|
|
trait FetchOperation |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Define which routes are needed for this operation. |
|
12
|
|
|
* |
|
13
|
|
|
* @param string $segment Name of the current entity (singular). Used as first URL segment. |
|
14
|
|
|
* @param string $routeName Prefix of the route name. |
|
15
|
|
|
* @param string $controller Name of the current CrudController. |
|
16
|
|
|
*/ |
|
17
|
|
|
protected function setupFetchOperationRoutes($segment, $routeName, $controller) |
|
18
|
|
|
{ |
|
19
|
|
|
// get all method names on the current model that start with "fetch" (ex: fetchCategory) |
|
20
|
|
|
// if a method that looks like that is present, it means we need to add the routes that fetch that entity |
|
21
|
|
|
preg_match_all('/(?<=^|;)fetch([^;]+?)(;|$)/', implode(';', get_class_methods($this)), $matches); |
|
22
|
|
|
|
|
23
|
|
|
if (count($matches[1])) { |
|
24
|
|
|
foreach ($matches[1] as $methodName) { |
|
25
|
|
|
Route::post($segment.'/fetch/'.Str::kebab($methodName), [ |
|
26
|
|
|
'uses' => $controller.'@fetch'.$methodName, |
|
27
|
|
|
'operation' => 'FetchOperation', |
|
28
|
|
|
]); |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Gets items from database and returns to selects. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string|array $arg |
|
37
|
|
|
* @return void |
|
38
|
|
|
*/ |
|
39
|
|
|
private function fetch($arg) |
|
40
|
|
|
{ |
|
41
|
|
|
// get the actual words that were used to search for an item (the search term / search string) |
|
42
|
|
|
$search_string = request()->input('q') ?? false; |
|
43
|
|
|
|
|
44
|
|
|
// if the Class was passed as the sole argument, use that as the configured Model |
|
45
|
|
|
// otherwise assume the arguments are actually the configuration array |
|
46
|
|
|
$config = []; |
|
47
|
|
|
|
|
48
|
|
|
if (! is_array($arg)) { |
|
49
|
|
|
if (! class_exists($arg)) { |
|
50
|
|
|
return response()->json(['error' => 'Class: '.$arg.' does not exists'], 500); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
$config['model'] = $arg; |
|
53
|
|
|
} else { |
|
54
|
|
|
$config = $arg; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$model_instance = new $config['model']; |
|
58
|
|
|
// set configuration defaults |
|
59
|
|
|
$config['paginate'] = isset($config['paginate']) ? $config['paginate'] : 10; |
|
60
|
|
|
$config['searchable_attributes'] = $config['searchable_attributes'] ?? $model_instance->identifiableAttribute(); |
|
61
|
|
|
$config['query'] = isset($config['query']) && is_callable($config['query']) ? $config['query']($config['model']) : $model_instance; // if a closure that has been passed as "query", use the closure - otherwise use the model |
|
62
|
|
|
|
|
63
|
|
|
if(!is_array($config['searchable_attributes'])) { |
|
64
|
|
|
$config['searchable_attributes'] = [$config['searchable_attributes']]; |
|
65
|
|
|
} |
|
66
|
|
|
// FetchOperation sends an empty query to retrieve the default entry for select when field is not nullable. |
|
67
|
|
|
// Also sends an empty query in case we want to load all entities to emulate non-ajax fields |
|
68
|
|
|
// when using InlineCreate. |
|
69
|
|
|
|
|
70
|
|
|
if ($search_string === false) { |
|
71
|
|
|
return ($config['paginate'] !== false) ? |
|
72
|
|
|
$config['query']->paginate($config['paginate']) : |
|
73
|
|
|
$config['query']->get(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$textColumnTypes = ['string', 'json_string', 'text']; |
|
77
|
|
|
// for each searchable attribute, add a WHERE clause |
|
78
|
|
|
foreach ($config['searchable_attributes'] as $k => $searchColumn) { |
|
79
|
|
|
$operation = ($k == 0) ? 'where' : 'orWhere'; |
|
80
|
|
|
$columnType = $config['query']->getColumnType($searchColumn); |
|
81
|
|
|
|
|
82
|
|
|
if (in_array($columnType, $textColumnTypes)) { |
|
83
|
|
|
$config['query'] = $config['query']->{$operation}($searchColumn, 'LIKE', '%'.$search_string.'%'); |
|
84
|
|
|
} else { |
|
85
|
|
|
$config['query'] = $config['query']->{$operation}($searchColumn, $search_string); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// return the results with or without pagination |
|
90
|
|
|
return ($config['paginate'] !== false) ? |
|
91
|
|
|
$config['query']->paginate($config['paginate']) : |
|
92
|
|
|
$config['query']->get(); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|