1
|
<?php
|
2
|
|
3
|
namespace App\Http\Controllers;
|
4
|
|
5
|
use App\Artefact;
|
6
|
use App\ArtefactCategory;
|
7
|
use App\User;
|
8
|
use Illuminate\Contracts\View\Factory;
|
9
|
use Illuminate\Http\RedirectResponse;
|
10
|
use Illuminate\Support\Facades\Auth;
|
11
|
use Illuminate\View\View;
|
12
|
|
13
|
class ArtefactController extends Controller
|
14
|
{
|
15
|
public function __construct()
|
16
|
{
|
17
|
$this->middleware('auth');
|
18
|
}
|
19
|
|
20
|
/**
|
21
|
* Returns view of all artefacts.
|
22
|
*
|
23
|
* @return Factory|View
|
24
|
*/
|
25
|
public function default()
|
26
|
{
|
27
|
$artefacts = Artefact::all();
|
28
|
|
29
|
return view('artefact.default', ['artefacts' => $artefacts]);
|
30
|
}
|
31
|
|
32
|
/**
|
33
|
* Returns view of artefacts related to the chosen category
|
34
|
*
|
35
|
* @param $id id of the category
|
36
|
* @return Factory|View
|
37
|
*/
|
38
|
public function showCategory($id)
|
39
|
{
|
40
|
$categoryArtefacts = ArtefactCategory::where('category_id', $id)->get();
|
41
|
if(count($categoryArtefacts) > 0)
|
42
|
{
|
43
|
$artefacts = array();
|
44
|
foreach($categoryArtefacts as $ar)
|
45
|
{
|
46
|
array_push($artefacts, Artefact::where('id', $ar->artefact_id)->get());
|
47
|
}
|
48
|
return view('artefact.category', ['artefacts' => $artefacts]);
|
49
|
}
|
50
|
else
|
51
|
{
|
52
|
return view('artefact.category', ['artefacts' => array()]);
|
53
|
}
|
54
|
}
|
55
|
|
56
|
/**
|
57
|
* Returns view of single artefact given by its id.
|
58
|
*
|
59
|
* @param $id int id of the artefact
|
60
|
* @return Factory|View
|
61
|
*/
|
62
|
public function view($id)
|
63
|
{
|
64
|
$artefact = Artefact::find($id);
|
65
|
$artefact['likes'] = Artefact::find($id)->users()->count();
|
66
|
$artefact['favourite'] = is_null(User::find(Auth::id())->likesArtefacts()->find($id)) ? false : true;
|
67
|
|
68
|
return view('artefact.view', ['artefact' => $artefact]);
|
69
|
}
|
70
|
|
71
|
/**
|
72
|
* Likes artefact given by its id.
|
73
|
*
|
74
|
* @param $id int id of metadata
|
75
|
* @return RedirectResponse
|
76
|
*/
|
77
|
public function like($id)
|
78
|
{
|
79
|
$user = User::find(Auth::id());
|
80
|
$artefact = Artefact::find($id);
|
81
|
|
82
|
$user->likesArtefacts()->attach($artefact);
|
83
|
|
84
|
return back()->withInput();
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* Unlikes artefact given by its id.
|
89
|
*
|
90
|
* @param $id int id of metadata
|
91
|
* @return RedirectResponse
|
92
|
*/
|
93
|
public function unlike($id)
|
94
|
{
|
95
|
$user = User::find(Auth::id());
|
96
|
$artefact = Artefact::find($id);
|
97
|
|
98
|
$user->likesArtefacts()->detach($artefact);
|
99
|
|
100
|
return back()->withInput();
|
101
|
}
|
102
|
}
|