1
|
<?php
|
2
|
|
3
|
namespace App\Http\Controllers;
|
4
|
|
5
|
use Illuminate\Contracts\Foundation\Application;
|
6
|
use Illuminate\Contracts\View\Factory;
|
7
|
use Illuminate\Support\Facades\Auth;
|
8
|
use App\User;
|
9
|
use App\Artefact;
|
10
|
use App\Http\Controllers\Image;
|
11
|
use Illuminate\View\View;
|
12
|
|
13
|
class FavoriteArtefactsController extends Controller
|
14
|
{
|
15
|
const ORDER_COLUMN = 'page';
|
16
|
|
17
|
public function __construct()
|
18
|
{
|
19
|
$this->middleware('auth');
|
20
|
}
|
21
|
|
22
|
/**
|
23
|
* Prepare all necessary data for shown Artefact.
|
24
|
* These are current count of likes, if it set to
|
25
|
* a favorites by a logged user and all metadata
|
26
|
* connected to the artefact.
|
27
|
*
|
28
|
* @param $artefacts all artefacts
|
29
|
* @return mixed modified artefacts
|
30
|
*/
|
31
|
public function prepData($artefacts)
|
32
|
{
|
33
|
foreach($artefacts as $artefact)
|
34
|
{
|
35
|
$artefact['likes'] = Artefact::find($artefact->id)->users()->count();
|
36
|
$artefact['favourite'] = is_null(User::find(Auth::id())->likesArtefacts()->find($artefact->id)) ? false : true;
|
37
|
$metadata = Artefact::find($artefact->id)->metadata()->orderBy(self::ORDER_COLUMN)->get();
|
38
|
foreach ($metadata as $item)
|
39
|
{
|
40
|
$item['favourite'] = is_null(User::find(Auth::id())->likesMetadata()->find($item->id)) ? false : true;
|
41
|
}
|
42
|
$artefact['metadata'] = $metadata;
|
43
|
}
|
44
|
return $artefacts;
|
45
|
}
|
46
|
|
47
|
/**
|
48
|
* Display a listing of the resource.
|
49
|
*
|
50
|
* @return Application|Factory|View
|
51
|
*/
|
52
|
public function index()
|
53
|
{
|
54
|
if(Auth::check())
|
55
|
{
|
56
|
$id = Auth::id();
|
57
|
$artefacts = User::find($id)->likesArtefacts()->simplePaginate(1);
|
58
|
$artefacts = $this->prepData($artefacts);
|
59
|
return view('artefact.default', ['artefacts' => $artefacts]);
|
60
|
}
|
61
|
else
|
62
|
{
|
63
|
return view('pages.index');
|
64
|
}
|
65
|
}
|
66
|
|
67
|
/**
|
68
|
* Display the specified resource.
|
69
|
*
|
70
|
* @param int $id
|
71
|
* @return Application|Factory|View
|
72
|
*/
|
73
|
public function show($id)
|
74
|
{
|
75
|
$artefacts = User::find($id)->likesArtefacts()->simplePaginate(1);
|
76
|
$artefacts = $this->prepData($artefacts);
|
77
|
return view('artefact.default', ['artefacts' => $artefacts]);
|
78
|
}
|
79
|
|
80
|
}
|