1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Models\Task; |
6
|
|
|
use Auth; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Illuminate\View\View; |
9
|
|
|
use Redirect; |
10
|
|
|
|
11
|
|
|
class TasksController extends Controller |
12
|
|
|
{ |
13
|
|
|
protected $rules = [ |
14
|
|
|
'name' => 'required|max:60', |
15
|
|
|
'description' => 'max:155', |
16
|
|
|
'completed' => 'numeric', |
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Create a new controller instance. |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
public function __construct() |
25
|
|
|
{ |
26
|
|
|
$this->middleware('auth'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Display a listing of the resource. |
31
|
|
|
* |
32
|
|
|
* @return \Illuminate\Http\Response |
33
|
|
|
*/ |
34
|
|
|
public function index() |
35
|
|
|
{ |
36
|
|
|
$user = Auth::user(); |
37
|
|
|
$data = [ |
38
|
|
|
'tasks' => Task::orderBy('created_at', 'asc')->where('user_id', $user->id)->get(), |
|
|
|
|
39
|
|
|
'tasksInComplete' => Task::orderBy('created_at', 'asc')->where('user_id', $user->id)->where('completed', '0')->get(), |
40
|
|
|
'tasksComplete' => Task::orderBy('created_at', 'asc')->where('user_id', $user->id)->where('completed', '1')->get(), |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
return view('tasks.index', $data); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Display a listing of the resource. |
48
|
|
|
* |
49
|
|
|
* @return \Illuminate\Http\Response |
50
|
|
|
*/ |
51
|
|
|
public function index_all() |
52
|
|
|
{ |
53
|
|
|
$user = Auth::user(); |
54
|
|
|
$data = [ |
55
|
|
|
'tasks' => Task::orderBy('created_at', 'asc')->where('user_id', $user->id)->get(), |
|
|
|
|
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
return view('tasks.filtered', $data); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Display a listing of the resource. |
63
|
|
|
* |
64
|
|
|
* @return \Illuminate\Http\Response |
65
|
|
|
*/ |
66
|
|
|
public function index_incomplete() |
67
|
|
|
{ |
68
|
|
|
$user = Auth::user(); |
69
|
|
|
$data = [ |
70
|
|
|
'tasks' => Task::orderBy('created_at', 'asc')->where('user_id', $user->id)->where('completed', '0')->get(), |
|
|
|
|
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
return view('tasks.filtered', $data); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Display a listing of the resource. |
78
|
|
|
* |
79
|
|
|
* @return \Illuminate\Http\Response |
80
|
|
|
*/ |
81
|
|
|
public function index_complete() |
82
|
|
|
{ |
83
|
|
|
$user = Auth::user(); |
84
|
|
|
|
85
|
|
|
return view('tasks.filtered', [ |
|
|
|
|
86
|
|
|
'tasks' => Task::orderBy('created_at', 'asc')->where('user_id', $user->id)->where('completed', '1')->get(), |
|
|
|
|
87
|
|
|
]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Show the form for creating a new resource. |
92
|
|
|
* |
93
|
|
|
* @return \Illuminate\Http\Response |
94
|
|
|
*/ |
95
|
|
|
public function create() |
96
|
|
|
{ |
97
|
|
|
return view('tasks.create'); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Store a newly created resource in storage. |
102
|
|
|
* |
103
|
|
|
* @param \Illuminate\Http\Request $request |
104
|
|
|
* |
105
|
|
|
* @return \Illuminate\Http\Response |
106
|
|
|
*/ |
107
|
|
|
public function store(Request $request) |
108
|
|
|
{ |
109
|
|
|
$this->validate($request, $this->rules); |
110
|
|
|
|
111
|
|
|
$user = Auth::user(); |
112
|
|
|
$task = $request->all(); |
113
|
|
|
$task['user_id'] = $user->id; |
|
|
|
|
114
|
|
|
|
115
|
|
|
Task::create($task); |
116
|
|
|
|
117
|
|
|
return redirect('/tasks')->with('status', 'Task created'); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Display the specified resource. |
122
|
|
|
* |
123
|
|
|
* @param int $id |
124
|
|
|
* |
125
|
|
|
* @return \Illuminate\Http\Response |
126
|
|
|
*/ |
127
|
|
|
public function show($id) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
// |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Show the form for editing the specified resource. |
134
|
|
|
* |
135
|
|
|
* @param int $id |
136
|
|
|
* |
137
|
|
|
* @return \Illuminate\Http\Response |
138
|
|
|
*/ |
139
|
|
|
public function edit($id) |
140
|
|
|
{ |
141
|
|
|
$task = Task::findOrFail($id); |
142
|
|
|
|
143
|
|
|
return view('tasks.edit')->withTask($task); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Update the specified resource in storage. |
148
|
|
|
* |
149
|
|
|
* @param \Illuminate\Http\Request $request |
150
|
|
|
* @param int $id |
151
|
|
|
* |
152
|
|
|
* @return \Illuminate\Http\Response |
153
|
|
|
*/ |
154
|
|
|
public function update(Request $request, $id) |
155
|
|
|
{ |
156
|
|
|
$this->validate($request, $this->rules); |
157
|
|
|
|
158
|
|
|
$task = Task::findOrFail($id); |
159
|
|
|
$task->name = $request->input('name'); |
160
|
|
|
$task->description = $request->input('description'); |
161
|
|
|
$task->completed = $request->input('completed'); |
162
|
|
|
|
163
|
|
|
if ($task->completed == '1') { |
164
|
|
|
$return_msg = 'Task Completed !!!'; |
165
|
|
|
} else { |
166
|
|
|
$task->completed = 0; |
167
|
|
|
$return_msg = 'Task Updated'; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$task->save(); |
171
|
|
|
|
172
|
|
|
return Redirect::back()->with('status', $return_msg); |
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Remove the specified resource from storage. |
177
|
|
|
* |
178
|
|
|
* @param int $id |
179
|
|
|
* |
180
|
|
|
* @return \Illuminate\Http\Response |
181
|
|
|
*/ |
182
|
|
|
public function destroy($id) |
183
|
|
|
{ |
184
|
|
|
Task::findOrFail($id)->delete(); |
185
|
|
|
|
186
|
|
|
return redirect('/tasks')->with('status', 'Task Deleted'); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Return current users tasks using View::Composer. |
191
|
|
|
* |
192
|
|
|
* @param \App\Providers\ComposerServiceProvider.php |
193
|
|
|
* @param obj $view |
|
|
|
|
194
|
|
|
* |
195
|
|
|
* @return \Illuminate\View\View |
196
|
|
|
*/ |
197
|
|
|
public function getAllTasks(View $view) |
198
|
|
|
{ |
199
|
|
|
$user = Auth::user(); |
200
|
|
|
$queryTasks = Task::orderBy('created_at', 'asc'); |
201
|
|
|
$Alltasks = $queryTasks->where('user_id', $user->id)->get(); |
|
|
|
|
202
|
|
|
$view->with('allTasks', $Alltasks); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Return current users INCOMPLETE tasks using View::Composer. |
207
|
|
|
* |
208
|
|
|
* @param \App\Providers\ComposerServiceProvider.php |
209
|
|
|
* @param obj $view |
210
|
|
|
* |
211
|
|
|
* @return \Illuminate\View\View |
212
|
|
|
*/ |
213
|
|
|
public function getIncompleteTasks(View $view) |
214
|
|
|
{ |
215
|
|
|
$user = Auth::user(); |
216
|
|
|
$queryTasks = Task::orderBy('created_at', 'asc'); |
217
|
|
|
$tasksInComplete = $queryTasks->where([ |
218
|
|
|
['user_id', '=', $user->id], |
|
|
|
|
219
|
|
|
['completed', '=', '0'], |
220
|
|
|
])->get(); |
221
|
|
|
|
222
|
|
|
$view->with('incompleteTasks', $tasksInComplete); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Return current users COMPLETE tasks using View::Composer. |
227
|
|
|
* |
228
|
|
|
* @param \App\Providers\ComposerServiceProvider.php |
229
|
|
|
* @param obj $view |
230
|
|
|
* |
231
|
|
|
* @return \Illuminate\View\View |
232
|
|
|
*/ |
233
|
|
|
public function getCompleteTasks(View $view) |
234
|
|
|
{ |
235
|
|
|
$user = Auth::user(); |
236
|
|
|
$queryTasks = Task::orderBy('created_at', 'asc'); |
237
|
|
|
$tasksCompleted = $queryTasks->where([ |
238
|
|
|
['user_id', '=', $user->id], |
|
|
|
|
239
|
|
|
['completed', '=', '1'], |
240
|
|
|
])->get(); |
241
|
|
|
$view->with('completeTasks', $tasksCompleted); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|