1 |
74e290c2
|
Adam Mištera
|
<?php
|
2 |
|
|
|
3 |
|
|
namespace App\Http\Controllers;
|
4 |
|
|
|
5 |
|
|
use App\Artefact;
|
6 |
fb354d59
|
rizir01
|
use App\ArtefactCategory;
|
7 |
bfa73340
|
Adam Mištera
|
use App\User;
|
8 |
2fee1bb5
|
Marek Lovčí
|
use Illuminate\Contracts\View\Factory;
|
9 |
|
|
use Illuminate\Http\RedirectResponse;
|
10 |
bfa73340
|
Adam Mištera
|
use Illuminate\Support\Facades\Auth;
|
11 |
2fee1bb5
|
Marek Lovčí
|
use Illuminate\View\View;
|
12 |
74e290c2
|
Adam Mištera
|
|
13 |
|
|
class ArtefactController extends Controller
|
14 |
|
|
{
|
15 |
21570473
|
Adam Mištera
|
public function __construct()
|
16 |
|
|
{
|
17 |
|
|
$this->middleware('auth');
|
18 |
|
|
}
|
19 |
|
|
|
20 |
10222730
|
Adam Mištera
|
/**
|
21 |
|
|
* Returns view of all artefacts.
|
22 |
|
|
*
|
23 |
2fee1bb5
|
Marek Lovčí
|
* @return Factory|View
|
24 |
10222730
|
Adam Mištera
|
*/
|
25 |
74e290c2
|
Adam Mištera
|
public function default()
|
26 |
|
|
{
|
27 |
10222730
|
Adam Mištera
|
$artefacts = Artefact::all();
|
28 |
74e290c2
|
Adam Mištera
|
|
29 |
|
|
return view('artefact.default', ['artefacts' => $artefacts]);
|
30 |
|
|
}
|
31 |
10222730
|
Adam Mištera
|
|
32 |
fb354d59
|
rizir01
|
/**
|
33 |
|
|
* Returns view of artefacts related to the chosen category
|
34 |
|
|
*
|
35 |
2fee1bb5
|
Marek Lovčí
|
* @param $id id of the category
|
36 |
|
|
* @return Factory|View
|
37 |
fb354d59
|
rizir01
|
*/
|
38 |
|
|
public function showCategory($id)
|
39 |
|
|
{
|
40 |
2fee1bb5
|
Marek Lovčí
|
$categoryArtefacts = ArtefactCategory::where('category_id', $id)->get();
|
41 |
|
|
if(count($categoryArtefacts) > 0)
|
42 |
fb354d59
|
rizir01
|
{
|
43 |
|
|
$artefacts = array();
|
44 |
2fee1bb5
|
Marek Lovčí
|
foreach($categoryArtefacts as $ar)
|
45 |
fb354d59
|
rizir01
|
{
|
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 |
eadc983d
|
rizir01
|
/**
|
57 |
|
|
* Returns view of artefacts related to the chosen category
|
58 |
|
|
*
|
59 |
|
|
* @param $id string with categories ids
|
60 |
|
|
* @return Factory|View
|
61 |
|
|
*/
|
62 |
|
|
public function showCategories($id)
|
63 |
|
|
{
|
64 |
|
|
$textWithIds = $id;
|
65 |
|
|
$pieces = explode(",", $textWithIds);
|
66 |
|
|
$artefacts = array();
|
67 |
|
|
for($i = 0;$i < count($pieces); $i++)
|
68 |
|
|
{
|
69 |
|
|
if($pieces[$i] == null || !strcmp($pieces[$i], ""))
|
70 |
|
|
{
|
71 |
|
|
continue;
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
$categoryArtefacts = ArtefactCategory::where('category_id', $pieces[$i])->get();
|
75 |
|
|
if(count($categoryArtefacts) > 0)
|
76 |
|
|
{
|
77 |
|
|
foreach($categoryArtefacts as $ar)
|
78 |
|
|
{
|
79 |
|
|
array_push($artefacts, Artefact::where('id', $ar->artefact_id)->get());
|
80 |
|
|
}
|
81 |
|
|
}
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
if(count($artefacts) > 0)
|
85 |
|
|
{
|
86 |
|
|
return view('artefact.category', ['artefacts' => $artefacts]);
|
87 |
|
|
}
|
88 |
|
|
else
|
89 |
|
|
{
|
90 |
|
|
return view('artefact.category', ['artefacts' => array()]);
|
91 |
|
|
}
|
92 |
|
|
}
|
93 |
|
|
|
94 |
10222730
|
Adam Mištera
|
/**
|
95 |
|
|
* Returns view of single artefact given by its id.
|
96 |
|
|
*
|
97 |
|
|
* @param $id int id of the artefact
|
98 |
2fee1bb5
|
Marek Lovčí
|
* @return Factory|View
|
99 |
10222730
|
Adam Mištera
|
*/
|
100 |
|
|
public function view($id)
|
101 |
|
|
{
|
102 |
|
|
$artefact = Artefact::find($id);
|
103 |
70f69b41
|
Adam Mištera
|
$artefact['likes'] = Artefact::find($id)->users()->count();
|
104 |
bfa73340
|
Adam Mištera
|
$artefact['favourite'] = is_null(User::find(Auth::id())->likesArtefacts()->find($id)) ? false : true;
|
105 |
10222730
|
Adam Mištera
|
|
106 |
|
|
return view('artefact.view', ['artefact' => $artefact]);
|
107 |
|
|
}
|
108 |
bfa73340
|
Adam Mištera
|
|
109 |
|
|
/**
|
110 |
1bcdcb05
|
Adam Mištera
|
* Likes artefact given by its id.
|
111 |
bfa73340
|
Adam Mištera
|
*
|
112 |
|
|
* @param $id int id of metadata
|
113 |
2fee1bb5
|
Marek Lovčí
|
* @return RedirectResponse
|
114 |
bfa73340
|
Adam Mištera
|
*/
|
115 |
|
|
public function like($id)
|
116 |
|
|
{
|
117 |
|
|
$user = User::find(Auth::id());
|
118 |
|
|
$artefact = Artefact::find($id);
|
119 |
|
|
|
120 |
|
|
$user->likesArtefacts()->attach($artefact);
|
121 |
|
|
|
122 |
|
|
return back()->withInput();
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
/**
|
126 |
1bcdcb05
|
Adam Mištera
|
* Unlikes artefact given by its id.
|
127 |
bfa73340
|
Adam Mištera
|
*
|
128 |
|
|
* @param $id int id of metadata
|
129 |
2fee1bb5
|
Marek Lovčí
|
* @return RedirectResponse
|
130 |
bfa73340
|
Adam Mištera
|
*/
|
131 |
|
|
public function unlike($id)
|
132 |
|
|
{
|
133 |
|
|
$user = User::find(Auth::id());
|
134 |
|
|
$artefact = Artefact::find($id);
|
135 |
|
|
|
136 |
|
|
$user->likesArtefacts()->detach($artefact);
|
137 |
|
|
|
138 |
|
|
return back()->withInput();
|
139 |
|
|
}
|
140 |
74e290c2
|
Adam Mištera
|
}
|