1
|
<?php
|
2
|
|
3
|
namespace App\Http\Controllers;
|
4
|
|
5
|
use App\Artefact;
|
6
|
use App\ArtefactCategory;
|
7
|
use App\Category;
|
8
|
use App\User;
|
9
|
use Illuminate\Contracts\View\Factory;
|
10
|
use Illuminate\Http\RedirectResponse;
|
11
|
use Illuminate\Support\Facades\Auth;
|
12
|
use Illuminate\View\View;
|
13
|
|
14
|
class ArtefactController extends Controller
|
15
|
{
|
16
|
const ORDER_COLUMN = 'page';
|
17
|
|
18
|
public function __construct()
|
19
|
{
|
20
|
$this->middleware('auth');
|
21
|
}
|
22
|
|
23
|
/**
|
24
|
* Prepare all necessary data for shown Artefact.
|
25
|
* These are current count of likes, if it set to
|
26
|
* a favorites by a logged user and all metadata
|
27
|
* connected to the artefact.
|
28
|
*
|
29
|
* @param $artefacts all artefacts
|
30
|
* @return mixed modified artefacts
|
31
|
*/
|
32
|
public function prepData($artefacts)
|
33
|
{
|
34
|
foreach($artefacts as $artefact)
|
35
|
{
|
36
|
$artefact['likes'] = Artefact::find($artefact->id)->users()->count();
|
37
|
$artefact['favourite'] = is_null(User::find(Auth::id())->likesArtefacts()->find($artefact->id)) ? false : true;
|
38
|
$metadata = Artefact::find($artefact->id)->metadata()->orderBy(self::ORDER_COLUMN)->get();
|
39
|
|
40
|
foreach ($metadata as $item)
|
41
|
{
|
42
|
$item['favourite'] = is_null(User::find(Auth::id())->likesMetadata()->find($item->id)) ? false : true;
|
43
|
}
|
44
|
|
45
|
$artefact['metadata'] = $metadata;
|
46
|
}
|
47
|
|
48
|
return $artefacts;
|
49
|
}
|
50
|
|
51
|
/**
|
52
|
* Returns view of all artefacts.
|
53
|
*
|
54
|
* @return Factory|View
|
55
|
*/
|
56
|
public function default()
|
57
|
{
|
58
|
$artefacts = Artefact::simplePaginate(1);
|
59
|
$artefacts = $this->prepData($artefacts);
|
60
|
return view('artefact.default', ['artefacts' => $artefacts]);
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* Returns view of artefacts related to the chosen category
|
65
|
*
|
66
|
* @param $id id of the category
|
67
|
* @return Factory|View
|
68
|
*/
|
69
|
public function showCategory($id)
|
70
|
{
|
71
|
$artefacts = Category::find($id)->artefacts()->simplePaginate(1);
|
72
|
$artefacts = $this->prepData($artefacts);
|
73
|
return view('artefact.default', ['artefacts' => $artefacts]);
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* Returns view of artefacts related to the chosen categories
|
78
|
*
|
79
|
* @param $id string with categories ids
|
80
|
* @return Factory|View
|
81
|
*/
|
82
|
public function showCategories($id)
|
83
|
{
|
84
|
//Parsing text for individual ids of categories
|
85
|
$textWithIds = $id;
|
86
|
$pieces = explode(",", $textWithIds);
|
87
|
$arrStrToInt = array();
|
88
|
for($i = 0;$i < count($pieces); $i++)
|
89
|
{
|
90
|
if($pieces[$i] == null || !strcmp($pieces[$i], ""))
|
91
|
{
|
92
|
continue;
|
93
|
}
|
94
|
|
95
|
array_push($arrStrToInt, (int)$pieces[$i]);
|
96
|
}
|
97
|
|
98
|
//Creating array of all artefacts ids
|
99
|
$arrArtIds = array();
|
100
|
foreach($arrStrToInt as $cat)
|
101
|
{
|
102
|
$tmpArtefacts = Category::find($cat)->artefacts()->get();
|
103
|
foreach($tmpArtefacts as $art)
|
104
|
{
|
105
|
array_push($arrArtIds, $art->id);
|
106
|
}
|
107
|
}
|
108
|
|
109
|
$artefacts = Artefact::whereIn('id', $arrArtIds)->simplePaginate(1);
|
110
|
$artefacts = $this->prepData($artefacts);
|
111
|
return view('artefact.default', ['artefacts' => $artefacts]);
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* Returns view of single artefact given by its id.
|
116
|
*
|
117
|
* @param $id int id of the artefact
|
118
|
* @return Factory|View
|
119
|
*/
|
120
|
public function view($id)
|
121
|
{
|
122
|
$artefact = Artefact::find($id)->simplePaginate(1);
|
123
|
$artefact = $this->prepData($artefact);
|
124
|
//return view('artefact.view', ['artefact' => $artefact]);
|
125
|
return view('artefact.default', ['artefacts' => $artefact]);
|
126
|
}
|
127
|
|
128
|
/**
|
129
|
* Likes artefact given by its id.
|
130
|
*
|
131
|
* @param $id int id of metadata
|
132
|
* @return RedirectResponse
|
133
|
*/
|
134
|
public function like($id)
|
135
|
{
|
136
|
$user = User::find(Auth::id());
|
137
|
$artefact = Artefact::find($id);
|
138
|
|
139
|
$user->likesArtefacts()->attach($artefact);
|
140
|
|
141
|
return back()->withInput();
|
142
|
}
|
143
|
|
144
|
/**
|
145
|
* Unlikes artefact given by its id.
|
146
|
*
|
147
|
* @param $id int id of metadata
|
148
|
* @return RedirectResponse
|
149
|
*/
|
150
|
public function unlike($id)
|
151
|
{
|
152
|
$user = User::find(Auth::id());
|
153
|
$artefact = Artefact::find($id);
|
154
|
|
155
|
$user->likesArtefacts()->detach($artefact);
|
156
|
|
157
|
return back()->withInput();
|
158
|
}
|
159
|
}
|