forked from matthusby/maintenance
/
Controller.php
188 lines (169 loc) · 4.26 KB
/
Controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
namespace Stevebauman\Maintenance\Http\Controllers;
use Stevebauman\Purify\Facades\Purify;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Request;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
abstract class Controller extends BaseController
{
use DispatchesJobs, ValidatesRequests;
/**
* Stores the URL to redirect to
*
* @var string
*/
protected $redirect;
/**
* Stores the message to display to the user
*
* @var string
*/
protected $message;
/**
* Stores the type of message that is displayed to the user
*
* @var string
*/
protected $messageType;
/**
* Holds validator errors, either array or json string
*
* @var array|string
*/
protected $errors;
/**
* Asks the request if it's ajax or not
*
* @return bool
*/
public function isAjax()
{
return Request::ajax();
}
/**
* Returns the proper response to user. If the request was made from ajax, then an json response is sent.
* If a request is a typical request without ajax, a user is sent a redirect with session flash messages
*
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
*/
public function response()
{
if ($this->isAjax()) {
if ($this->errors) {
return $this->responseJson([
'errors' => $this->errors,
]);
} else {
return $this->responseJson([
'message' => $this->message,
'messageType' => $this->messageType,
'redirect' => $this->redirect
]);
}
} else {
if ($this->errors) {
return redirect($this->redirect)
->withInput()
->withErrors($this->errors);
} else {
return redirect($this->redirect)
->withInput()
->with('message', $this->message)
->with('messageType', $this->messageType);
}
}
}
/**
* Returns a JSON response to the client
*
* @param array $data
*
* @return \Illuminate\Http\JsonResponse
*/
public function responseJson($data)
{
return Response::json($data);
}
/**
* Returns input from the client. If clean is set to true, the input will be
* ran through the purifier before it is returned.
*
* @param string $name
* @param boolean $clean
*
* @return mixed
*/
protected function input($name, $clean = FALSE)
{
if ($this->inputHas($name)) {
if ($clean) {
return $this->clean(Input::get($name));
} else {
return Input::get($name);
}
}
return NULL;
}
/**
* Returns the specified uploaded file by it's input name.
*
* @param string $name
*
* @return bool|array|UploadedFile
*/
protected function inputFile($name)
{
if($this->inputHasFile($name)) {
return Input::file($name);
}
return false;
}
/**
* Returns all input
*
* @return array
*/
protected function inputAll()
{
return Input::all();
}
/**
* Returns true / false if the current request
* contains an input field of the specified name.
*
* @param string $name
*
* @return bool
*/
protected function inputHas($name)
{
return Input::has($name);
}
/**
* Returns true / false if the current request
* contains an uploaded file field with the
* specified name.
*
* @param string $name
*
* @return mixed
*/
protected function inputHasFile($name)
{
return Input::hasFile($name);
}
/**
* Cleans the specified input.
*
* @param array|string $input
*
* @return array|string
*/
public function clean($input)
{
return Purify::clean($input);
}
}