1: <?php
2:
3: namespace App\Http\Controllers;
4:
5: use Illuminate\Http\Request;
6:
7: use App\Http\Requests;
8: use App\Http\Controllers\Controller;
9: use App\Comment;
10: use App\Driver;
11: use Route, View;
12: use Illuminate\Support\Facades\Auth;
13: use Illuminate\Support\Facades\Validator;
14:
15:
16: class CommentController extends Controller
17: {
18: 19: 20: 21: 22: 23:
24: public function showComment(){
25: $comments = Comment::with('driver')->where('approved','!=',1)->get();
26:
27:
28: $driver = Driver::with('user')->where('user_id',Auth::user()->id)->first();
29:
30: if(Auth::user()->user_rank == 'admin'){
31: $commentsApproved = Comment::with('driver')->where('approved','=',1)->get();
32: }else{
33: $commentsApproved = Comment::with('driver')->where('approved','=',1)->where('driver_id',$driver->id)->get();
34: }
35:
36: foreach($commentsApproved as $comment){
37: if($driver){
38: $comment->where('driver_id',$driver->id)->update(['seen' => 1]);
39: }
40: }
41:
42: return View::make('/opmerkingen', compact('comments','commentsApproved'));
43: }
44:
45: 46: 47: 48: 49: 50:
51: public function showCommentsApproved(){
52: $commentsApproved = Comment::with('driver')->where('approved','=',1)->get();
53: return View::make('/opmerkingen-verwerkt', compact('comments','commentsApproved'));
54: }
55:
56: 57: 58: 59: 60: 61: 62:
63: public function showCommentEdit(){
64: $id = Route::current()->getParameter('id');
65: $comment = Comment::with('driver')->where('id',$id)->first();
66: return View::make('/opmerkingwijzigen',compact('comment','id'));
67: }
68:
69: 70: 71: 72: 73: 74: 75: 76:
77: public function editComment(Request $request){
78: $id = Route::current()->getParameter('id');
79: $comment = Comment::where('id',$id)->first();
80: if($comment->approved == 0){
81: $redir = 1;
82: }else{
83: $redir = 0;
84: }
85: $data = array(
86: 'comment' => $request['comment'],
87: 'approved' => $request['approved']
88: );
89: $rules = array(
90: 'comment' => 'required|max:255'
91: );
92: $valid = Validator::make($data,$rules);
93: if($valid->fails()){
94: return redirect('opmerkingwijzigen/'.$id)->withErrors($valid)->withInput($data);
95: }
96: $comment->update($data);
97: $request->session()->flash('alert-success', 'Opmerking '. $request['name'] .' is gewijziged.');
98:
99: if($redir == 1){
100: return redirect('/opmerkingen');
101: }
102: return redirect('/opmerkingen/verwerkt');
103:
104:
105: }
106:
107: 108: 109: 110: 111: 112: 113:
114: public function togglesStateComment(){
115: $id = Route::current()->getParameter('id');
116: $redir = Route::current()->getParameter('redir');
117: $msg = Comment::where('id',$id)->first();
118: if(!$msg->approved > 0){
119: $msg->where('id',$id)->update(['approved' => 1]);
120: }else{
121: $msg->where('id',$id)->update(['approved' => 0]);
122: }
123: session()->flash('alert-success', 'Opmerking status aangepast.');
124: if($redir < 1){
125: return redirect('/opmerkingen');
126: }else{
127: return redirect('/opmerkingen/verwerkt');
128: }
129:
130: }
131:
132: 133: 134: 135: 136: 137:
138: public function deleteComment(){
139: $id = Route::current()->getParameter('id');
140:
141: $comment = Comment::where('id',$id)->first();
142: $comment->delete();
143: session()->flash('alert-success', 'Commentaar verwijderd.');
144: return redirect()->route('opmerkingen');
145: }
146:
147: }