1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Taskforcedev\CrudApi\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Auth; |
6
|
|
|
use Validator; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Illuminate\Support\Facades\Log; |
9
|
|
|
|
10
|
|
|
use Taskforcedev\LaravelSupport\Http\Controllers\Controller; |
11
|
|
|
use Taskforcedev\CrudApi\Helpers\CrudApi; |
12
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class AdminController. |
16
|
|
|
*/ |
17
|
|
|
class AdminController extends Controller |
18
|
|
|
{ |
19
|
|
|
public $apiHelper; |
20
|
|
|
|
21
|
|
|
public function __construct() |
22
|
|
|
{ |
23
|
|
|
$this->apiHelper = new CrudApi(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function index($model) |
27
|
|
|
{ |
28
|
|
|
if (!Auth::check()) { |
29
|
|
|
Log::info('CrudAPI: User is not logged in.'); |
30
|
|
|
return redirect('/'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->apiHelper->setModel($model); |
34
|
|
|
$fqModel = $this->apiHelper->getModel(); |
35
|
|
|
|
36
|
|
|
if (Auth::user()->cannot('create', $fqModel)) { |
37
|
|
|
Log::info('CrudAPI: User is unable to create: ' . $fqModel); |
38
|
|
|
return redirect('/'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if ($fqModel === false) { |
42
|
|
|
return response('Model does not exist', 404); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$instance = new $fqModel(); |
46
|
|
|
$this->apiHelper->setInstance($instance); |
47
|
|
|
|
48
|
|
|
$data = $this->buildData(); |
49
|
|
|
|
50
|
|
|
$data['apiHelper'] = $this->apiHelper; |
51
|
|
|
$data['model'] = $model; |
52
|
|
|
$data['instance'] = $instance; |
53
|
|
|
$data['fields'] = $instance->getFillable(); |
54
|
|
|
$data['results'] = $fqModel::all(); |
55
|
|
|
|
56
|
|
|
return view('crudapi::admin.bs4.index', $data); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function store(Request $request, $model) |
60
|
|
|
{ |
61
|
|
|
$this->apiHelper->setModel($model); |
62
|
|
|
$fqModel = $this->apiHelper->getModel(); |
63
|
|
|
|
64
|
|
|
if ($fqModel === false) { |
65
|
|
|
return response('Model does not exist', 404); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$instance = new $fqModel(); |
69
|
|
|
$fields = $instance->getFillable(); |
70
|
|
|
|
71
|
|
|
/* Validation */ |
72
|
|
|
$this->validate($request, $instance->validation); |
73
|
|
|
|
74
|
|
|
$fqModel::create($request->only($fields)); |
75
|
|
|
|
76
|
|
|
return response('Item Created', 200); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function update(Request $request, $model) |
80
|
|
|
{ |
81
|
|
|
$this->apiHelper->setModel($model); |
82
|
|
|
$fqModel = $this->apiHelper->getModel(); |
83
|
|
|
|
84
|
|
|
if ($fqModel === false) { |
85
|
|
|
return response('Model does not exist', 404); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$instance = new $fqModel(); |
89
|
|
|
$fields = $instance->getFillable(); |
90
|
|
|
|
91
|
|
|
// Get the item |
92
|
|
|
$id = $request->get('id'); |
93
|
|
|
|
94
|
|
|
try { |
95
|
|
|
$item = $fqModel::where('id', $id)->firstOrFail(); |
96
|
|
|
|
97
|
|
|
foreach ($fields as $f) { |
98
|
|
|
if ($request->has($f)) { |
99
|
|
|
$value = $request->get($f); |
100
|
|
|
$item->$f = $value; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// Check the item will still be valid |
105
|
|
|
$validator = Validator::make($item->toArray(), $instance->validation); |
106
|
|
|
|
107
|
|
|
if ($validator->fails()) { |
108
|
|
|
return response('Model validation failed.', 406); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$item->save(); |
112
|
|
|
|
113
|
|
|
return response('Item Updated', 200); |
114
|
|
|
} catch (ModelNotFoundException $e) { |
115
|
|
|
return response('Not found', 404); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function delete(Request $request, $model) |
120
|
|
|
{ |
121
|
|
|
$this->apiHelper->setModel($model); |
122
|
|
|
$fqModel = $this->apiHelper->getModel(); |
123
|
|
|
|
124
|
|
|
if ($fqModel === false) { |
125
|
|
|
return response('Model does not exist', 404); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// Get the item |
129
|
|
|
$id = $request->get('id'); |
130
|
|
|
|
131
|
|
|
try { |
132
|
|
|
$item = $fqModel::where('id', $id)->firstOrFail(); |
133
|
|
|
$item->delete(); |
134
|
|
|
|
135
|
|
|
return response('Item Deleted', 200); |
136
|
|
|
} catch (ModelNotFoundException $e) { |
137
|
|
|
return response('Not found', 404); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|