1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Http\Models\Comments; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Support\Facades\Redirect; |
8
|
|
|
use Illuminate\Support\Facades\Session; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class CommentsController. |
12
|
|
|
*/ |
13
|
|
|
class CommentsController extends Controller |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @return \Illuminate\View\View |
17
|
|
|
*/ |
18
|
|
|
public function index() |
19
|
|
|
{ |
20
|
|
|
return view('Comments/index', [ |
|
|
|
|
21
|
|
|
'comments' => Comments::all(), |
22
|
|
|
]); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return \Illuminate\View\View |
27
|
|
|
*/ |
28
|
|
|
public function create() |
29
|
|
|
{ |
30
|
|
|
return view('Comments/create'); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return \Illuminate\View\View |
35
|
|
|
*/ |
36
|
|
|
public function read($id) |
37
|
|
|
{ |
38
|
|
|
return view('Comments/read', ['id' => $id]); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* |
43
|
|
|
*/ |
44
|
|
|
public function update(Request $request) |
45
|
|
|
{ |
46
|
|
|
$field = 'content'; |
47
|
|
|
$id = $request->id; |
48
|
|
|
$value = $request->value; |
49
|
|
|
|
50
|
|
|
$comment = Comments::find($id); |
51
|
|
|
$comment->update([$field => $value]); |
52
|
|
|
Session::flash('success', 'Le commentaire a bien été mis à jour'); |
53
|
|
|
|
54
|
|
|
return Redirect::route('comments.index'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Pour la suppression, il n'y a pas de vue dédiée. |
59
|
|
|
* On redirige donc vers l'index. |
60
|
|
|
* |
61
|
|
|
* @return \Illuminate\View\View |
62
|
|
|
*/ |
63
|
|
|
public function delete($id) |
64
|
|
|
{ |
65
|
|
|
return redirect('/comments', ['id' => $id]); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function search() |
69
|
|
|
{ |
70
|
|
|
return view('Comments/search'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function favoris(Request $request) |
74
|
|
|
{ |
75
|
|
|
$id = $request->input('id'); |
76
|
|
|
$action = $request->input('action'); |
77
|
|
|
// Récupération en session de l'item "favoris" |
78
|
|
|
$liked = session('commentsFavoris', []); |
79
|
|
|
|
80
|
|
|
if ($action == 'add') { |
81
|
|
|
|
82
|
|
|
// Enregistrement en variable de l'id souhaité |
83
|
|
|
$liked[] = $id; |
84
|
|
|
// Stockage de cette variable de la session |
85
|
|
|
Session::put('commentsFavoris', $liked); |
86
|
|
|
} else { |
87
|
|
|
|
88
|
|
|
// On cherche la position de l'id dans le tableau |
89
|
|
|
$position = array_search($id, $liked); |
90
|
|
|
// On supprime l'élément grâce à sa position |
91
|
|
|
unset($liked[$position]); |
92
|
|
|
|
93
|
|
|
// Stockage de cette variable de la session |
94
|
|
|
Session::put('commentsFavoris', $liked); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|