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