1
|
|
|
<?php namespace Distilleries\Expendable\Http\Controllers\Backend\Base; |
2
|
|
|
|
3
|
|
|
use Distilleries\Expendable\Contracts\LayoutManagerContract; |
4
|
|
|
use Distilleries\Expendable\Helpers\TranslationUtils; |
5
|
|
|
use Distilleries\Expendable\Models\BaseModel; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
|
8
|
|
|
class ModelBaseController extends BaseController { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var \Distilleries\Expendable\Models\BaseModel $model |
12
|
|
|
* Injected by the constructor |
13
|
|
|
*/ |
14
|
|
|
protected $model; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
// ------------------------------------------------------------------------------------------------ |
18
|
|
|
|
19
|
|
|
public function __construct(BaseModel $model, LayoutManagerContract $layoutManager) |
20
|
|
|
{ |
21
|
|
|
parent::__construct($layoutManager); |
22
|
|
|
$this->model = $model; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
// ------------------------------------------------------------------------------------------------ |
26
|
|
|
// ------------------------------------------------------------------------------------------------ |
27
|
|
|
// ------------------------------------------------------------------------------------------------ |
28
|
|
|
|
29
|
|
|
public function putDestroy(Request $request) |
30
|
|
|
{ |
31
|
|
|
$validation = \Validator::make($request->all(), [ |
32
|
|
|
'id' => 'required' |
33
|
|
|
]); |
34
|
|
|
if ($validation->fails()) { |
35
|
|
|
return redirect()->back()->withErrors($validation)->withInput($request->all()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$data = $this->model->where($this->model->getKeyName(), $request->get('id'))->get()->last(); |
|
|
|
|
39
|
|
|
$data->delete(); |
40
|
|
|
|
41
|
|
|
return redirect()->to(action('\\' . get_class($this) . '@getIndex')); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// ------------------------------------------------------------------------------------------------ |
45
|
|
|
public function postSearch(Request $request, $query = null) |
46
|
|
|
{ |
47
|
|
|
|
48
|
|
|
$ids = $request->get('ids'); |
49
|
|
|
$local_override = $this->getParams($request, 'local_override', null); |
50
|
|
|
|
51
|
|
|
TranslationUtils::overrideLocal($local_override); |
52
|
|
|
|
53
|
|
|
if (empty($query)) { |
54
|
|
|
$query = $this->model; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (!empty($ids)) { |
58
|
|
|
return response()->json($this->generateResultSearchByIds($request, $query, $ids)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return response()->json($this->generateResultSearch($request, $query)); |
62
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function generateResultSearchByIds(Request $request, $query, $ids) |
66
|
|
|
{ |
67
|
|
|
$no_edit = $request->get('no_edit'); |
68
|
|
|
|
69
|
|
|
if (!empty($no_edit) && method_exists($this->model, 'withoutTranslation')) { |
70
|
|
|
$query = $query->withoutTranslation(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$data = $query->whereIn($this->model->getKeyName(), $ids)->get(); |
74
|
|
|
|
75
|
|
|
return $data; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function generateResultSearch(Request $request, $query) |
79
|
|
|
{ |
80
|
|
|
$term = $request->get('term'); |
81
|
|
|
$page = $this->getParams($request, 'page', 1); |
82
|
|
|
$paged = $this->getParams($request, 'page_limit', 10); |
83
|
|
|
|
84
|
|
|
if (empty($term)) { |
85
|
|
|
$elements = array(); |
86
|
|
|
$total = 0; |
87
|
|
|
} else { |
88
|
|
|
$elements = $query->search($term)->take($paged)->skip(($page - 1) * $paged)->get(); |
89
|
|
|
$total = $query->search($term)->count(); |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return [ |
94
|
|
|
'total' => $total, |
95
|
|
|
'elements' => $elements |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function getParams(Request $request, $key, $default_value) |
100
|
|
|
{ |
101
|
|
|
$element = $request->get($key); |
102
|
|
|
|
103
|
|
|
if (empty($element)) { |
104
|
|
|
$element = $default_value; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $element; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
} |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: