Projekt

Obecné

Profil

Stáhnout (3.08 KB) Statistiky
| Větev: | Tag: | Revize:
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 single artefact given by its id.
73
     *
74
     * @param $id int id of the artefact
75
     * @return Factory|View
76
     */
77
    public function view($id)
78
    {
79
        $artefact = Artefact::find($id);
80
        $artefact['likes'] = Artefact::find($id)->users()->count();
81
        $artefact['favourite'] = is_null(User::find(Auth::id())->likesArtefacts()->find($id)) ? false : true;
82

    
83
        return view('artefact.view', ['artefact' => $artefact]);
84
    }
85

    
86
    /**
87
     * Likes artefact given by its id.
88
     *
89
     * @param $id int id of metadata
90
     * @return RedirectResponse
91
     */
92
    public function like($id)
93
    {
94
        $user = User::find(Auth::id());
95
        $artefact = Artefact::find($id);
96

    
97
        $user->likesArtefacts()->attach($artefact);
98

    
99
        return back()->withInput();
100
    }
101

    
102
    /**
103
     * Unlikes artefact given by its id.
104
     *
105
     * @param $id int id of metadata
106
     * @return RedirectResponse
107
     */
108
    public function unlike($id)
109
    {
110
        $user = User::find(Auth::id());
111
        $artefact = Artefact::find($id);
112

    
113
        $user->likesArtefacts()->detach($artefact);
114

    
115
        return back()->withInput();
116
    }
117
}
(1-1/9)