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
|
const ORDER_COLUMN = 'page';
|
16
|
|
17
|
public function __construct()
|
18
|
{
|
19
|
$this->middleware('auth');
|
20
|
}
|
21
|
|
22
|
/**
|
23
|
* Returns view of all artefacts.
|
24
|
*
|
25
|
* @return Factory|View
|
26
|
*/
|
27
|
public function default()
|
28
|
{
|
29
|
$artefacts = Artefact::simplePaginate(1);
|
30
|
foreach($artefacts as $artefact)
|
31
|
{
|
32
|
$artefact['likes'] = Artefact::find($artefact->id)->users()->count();
|
33
|
$artefact['favourite'] = is_null(User::find(Auth::id())->likesArtefacts()->find($artefact->id)) ? false : true;
|
34
|
$metadata = Artefact::find($artefact->id)->metadata()->orderBy(self::ORDER_COLUMN)->get();
|
35
|
|
36
|
foreach ($metadata as $item)
|
37
|
{
|
38
|
$item['favourite'] = is_null(User::find(Auth::id())->likesMetadata()->find($item->id)) ? false : true;
|
39
|
}
|
40
|
|
41
|
$artefact['metadata'] = $metadata;
|
42
|
}
|
43
|
|
44
|
return view('artefact.default', ['artefacts' => $artefacts]);
|
45
|
}
|
46
|
|
47
|
/**
|
48
|
* Returns view of artefacts related to the chosen category
|
49
|
*
|
50
|
* @param $id id of the category
|
51
|
* @return Factory|View
|
52
|
*/
|
53
|
public function showCategory($id)
|
54
|
{
|
55
|
$categoryArtefacts = ArtefactCategory::where('category_id', $id)->get();
|
56
|
if(count($categoryArtefacts) > 0)
|
57
|
{
|
58
|
$artefacts = array();
|
59
|
foreach($categoryArtefacts as $ar)
|
60
|
{
|
61
|
array_push($artefacts, Artefact::where('id', $ar->artefact_id)->get());
|
62
|
}
|
63
|
return view('artefact.category', ['artefacts' => $artefacts]);
|
64
|
}
|
65
|
else
|
66
|
{
|
67
|
return view('artefact.category', ['artefacts' => array()]);
|
68
|
}
|
69
|
}
|
70
|
|
71
|
/**
|
72
|
* Returns view of artefacts related to the chosen category
|
73
|
*
|
74
|
* @param $id string with categories ids
|
75
|
* @return Factory|View
|
76
|
*/
|
77
|
public function showCategories($id)
|
78
|
{
|
79
|
$textWithIds = $id;
|
80
|
$pieces = explode(",", $textWithIds);
|
81
|
$artefacts = array();
|
82
|
for($i = 0;$i < count($pieces); $i++)
|
83
|
{
|
84
|
if($pieces[$i] == null || !strcmp($pieces[$i], ""))
|
85
|
{
|
86
|
continue;
|
87
|
}
|
88
|
|
89
|
$categoryArtefacts = ArtefactCategory::where('category_id', $pieces[$i])->get();
|
90
|
if(count($categoryArtefacts) > 0)
|
91
|
{
|
92
|
foreach($categoryArtefacts as $ar)
|
93
|
{
|
94
|
array_push($artefacts, Artefact::where('id', $ar->artefact_id)->get());
|
95
|
}
|
96
|
}
|
97
|
}
|
98
|
|
99
|
if(count($artefacts) > 0)
|
100
|
{
|
101
|
return view('artefact.category', ['artefacts' => $artefacts]);
|
102
|
}
|
103
|
else
|
104
|
{
|
105
|
return view('artefact.category', ['artefacts' => array()]);
|
106
|
}
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
* Returns view of single artefact given by its id.
|
111
|
*
|
112
|
* @param $id int id of the artefact
|
113
|
* @return Factory|View
|
114
|
*/
|
115
|
public function view($id)
|
116
|
{
|
117
|
$artefact = Artefact::find($id);
|
118
|
$artefact['likes'] = Artefact::find($id)->users()->count();
|
119
|
$artefact['favourite'] = is_null(User::find(Auth::id())->likesArtefacts()->find($id)) ? false : true;
|
120
|
|
121
|
return view('artefact.view', ['artefact' => $artefact]);
|
122
|
}
|
123
|
|
124
|
/**
|
125
|
* Likes artefact given by its id.
|
126
|
*
|
127
|
* @param $id int id of metadata
|
128
|
* @return RedirectResponse
|
129
|
*/
|
130
|
public function like($id)
|
131
|
{
|
132
|
$user = User::find(Auth::id());
|
133
|
$artefact = Artefact::find($id);
|
134
|
|
135
|
$user->likesArtefacts()->attach($artefact);
|
136
|
|
137
|
return back()->withInput();
|
138
|
}
|
139
|
|
140
|
/**
|
141
|
* Unlikes artefact given by its id.
|
142
|
*
|
143
|
* @param $id int id of metadata
|
144
|
* @return RedirectResponse
|
145
|
*/
|
146
|
public function unlike($id)
|
147
|
{
|
148
|
$user = User::find(Auth::id());
|
149
|
$artefact = Artefact::find($id);
|
150
|
|
151
|
$user->likesArtefacts()->detach($artefact);
|
152
|
|
153
|
return back()->withInput();
|
154
|
}
|
155
|
}
|