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