1
|
|
|
<?php namespace Mascame\Artificer\Controllers; |
2
|
|
|
|
3
|
|
|
use Input; |
4
|
|
|
use Mascame\Artificer\Artificer; |
5
|
|
|
use Mascame\Artificer\Options\AdminOption; |
6
|
|
|
use Mascame\Artificer\Requests\ArtificerFormRequest; |
7
|
|
|
use Redirect; |
8
|
|
|
use Request; |
9
|
|
|
use Response; |
10
|
|
|
use URL; |
11
|
|
|
use View; |
12
|
|
|
|
13
|
|
|
class ModelController extends BaseModelController |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Show the form for creating a new resource. |
18
|
|
|
* |
19
|
|
|
* @return Response |
20
|
|
|
*/ |
21
|
|
|
public function create() |
22
|
|
|
{ |
23
|
|
|
$this->handleData($this->modelObject->schema->getInstance()); |
24
|
|
|
|
25
|
|
|
$form = array( |
26
|
|
|
'form_action_route' => 'admin.model.store', |
27
|
|
|
'form_method' => 'post' |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
return View::make($this->getView('edit'))->with('items', $this->data)->with($form); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param $modelName |
35
|
|
|
* @return $this |
36
|
|
|
*/ |
37
|
|
|
public function filter($modelName) |
38
|
|
|
{ |
39
|
|
|
$this->handleData($this->model->firstOrFail()); |
40
|
|
|
|
41
|
|
|
$sort = $this->getSort(); |
42
|
|
|
|
43
|
|
|
$data = $this->model->where(function ($query) { |
44
|
|
|
|
45
|
|
|
foreach (\Request::all() as $name => $value) { |
46
|
|
|
if ($value != '' && isset($this->fields[$name])) { |
47
|
|
|
$this->fields[$name]->filter($query, $value); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return null; |
52
|
|
|
}) |
53
|
|
|
->with($this->modelObject->getRelations()) |
54
|
|
|
->orderBy($sort['column'], $sort['direction']) |
55
|
|
|
->get(); |
56
|
|
|
|
57
|
|
|
return parent::all($modelName, $data, $sort); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Display the specified resource. |
62
|
|
|
* |
63
|
|
|
* @param int $id |
64
|
|
|
* @return Response |
65
|
|
|
*/ |
66
|
|
|
public function show($modelName, $id) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$this->handleData($this->model->findOrFail($id)); |
69
|
|
|
|
70
|
|
|
return View::make($this->getView('show'))->with('item', $this->data); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Display the specified post. |
75
|
|
|
* |
76
|
|
|
* @return Response |
77
|
|
|
*/ |
78
|
|
|
public function all($modelName, $data = null, $sort = null) |
79
|
|
|
{ |
80
|
|
|
$sort = $this->getSort(); |
81
|
|
|
|
82
|
|
|
$data = $this->model->with($this->modelObject->getRelations())->orderBy($sort['column'], |
83
|
|
|
$sort['direction'])->get(); |
84
|
|
|
|
85
|
|
|
return parent::all($modelName, $data, $sort); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Show the form for editing the specified post. |
90
|
|
|
* |
91
|
|
|
* @param int $id |
92
|
|
|
* @return Response |
93
|
|
|
*/ |
94
|
|
|
public function edit($modelName, $id) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$this->handleData( |
97
|
|
|
$this->model->with($this->modelObject->getRelations())->findOrFail($id) |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$form = array( |
101
|
|
|
'form_action_route' => 'admin.model.update', |
102
|
|
|
'form_method' => 'put' |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
return View::make($this->getView('edit')) |
106
|
|
|
->with('items', $this->data) |
107
|
|
|
->with($form); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function field($modelName, $id, $field) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$this->handleData($this->model->with($this->modelObject->getRelations())->findOrFail($id)); |
113
|
|
|
|
114
|
|
|
$this->fields[$field]->showFullField = true; |
115
|
|
|
|
116
|
|
|
return \HTML::field($this->fields[$field], AdminOption::get('icons')); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function handleAjaxResponse($item) |
120
|
|
|
{ |
121
|
|
|
return Response::json(array( |
122
|
|
|
'item' => $item->toArray(), |
123
|
|
|
'refresh' => URL::route('admin.model.field.edit', array( |
124
|
|
|
'slug' => Input::get('_standalone_origin'), |
125
|
|
|
'id' => Input::get('_standalone_origin_id'), |
126
|
|
|
'field' => ':fieldName:' |
127
|
|
|
)) |
128
|
|
|
) |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Update or create the specified resource in storage. |
134
|
|
|
* |
135
|
|
|
* @param int $id |
136
|
|
|
* @return Response |
|
|
|
|
137
|
|
|
*/ |
138
|
|
|
|
139
|
|
|
public function updateOrCreate(ArtificerFormRequest $request) |
140
|
|
|
{ |
141
|
|
|
$request->persist(); |
142
|
|
|
|
143
|
|
|
// Todo |
144
|
|
|
//if (Request::ajax()) { |
145
|
|
|
// return $this->handleAjaxResponse($model); |
146
|
|
|
//} |
147
|
|
|
|
148
|
|
|
return Redirect::route('admin.model.all', array('slug' => $this->modelObject->getRouteName())); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Remove the specified resource from storage. |
153
|
|
|
* |
154
|
|
|
* @param $modelName |
155
|
|
|
* @param $id |
156
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
157
|
|
|
*/ |
158
|
|
|
public function destroy($modelName, $id) |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
if ($this->model->destroy($id)) { |
161
|
|
|
// Todo Notify::success('<b>Success!</b> The record has been deleted!', true); |
162
|
|
|
} else { |
163
|
|
|
// Todo Notify::danger('<b>Failed!</b> The record could not be deleted!'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return Request::ajax() ? \Response::json([]) : Redirect::back(); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
} |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.