Projekt

Obecné

Profil

Stáhnout (4.14 KB) Statistiky
| Větev: | Tag: | Revize:
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 af0a94fc Adam Mištera
    const ORDER_COLUMN = 'page';
16
17 21570473 Adam Mištera
    public function __construct()
18
    {
19
        $this->middleware('auth');
20
    }
21
22 10222730 Adam Mištera
    /**
23
     * Returns view of all artefacts.
24
     *
25 2fee1bb5 Marek Lovčí
     * @return Factory|View
26 10222730 Adam Mištera
     */
27 74e290c2 Adam Mištera
    public function default()
28
    {
29 af0a94fc Adam Mištera
        $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 74e290c2 Adam Mištera
44
        return view('artefact.default', ['artefacts' => $artefacts]);
45
    }
46 10222730 Adam Mištera
47 fb354d59 rizir01
    /**
48
     * Returns view of artefacts related to the chosen category
49
     *
50 2fee1bb5 Marek Lovčí
     * @param $id id of the category
51
     * @return Factory|View
52 fb354d59 rizir01
     */
53
    public function showCategory($id)
54
    {
55 2fee1bb5 Marek Lovčí
        $categoryArtefacts = ArtefactCategory::where('category_id', $id)->get();
56
        if(count($categoryArtefacts) > 0)
57 fb354d59 rizir01
        {
58
            $artefacts = array();
59 2fee1bb5 Marek Lovčí
            foreach($categoryArtefacts as $ar)
60 fb354d59 rizir01
            {
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 eadc983d rizir01
    /**
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 10222730 Adam Mištera
    /**
110
     * Returns view of single artefact given by its id.
111
     *
112
     * @param $id int id of the artefact
113 2fee1bb5 Marek Lovčí
     * @return Factory|View
114 10222730 Adam Mištera
     */
115
    public function view($id)
116
    {
117
        $artefact = Artefact::find($id);
118 70f69b41 Adam Mištera
        $artefact['likes'] = Artefact::find($id)->users()->count();
119 bfa73340 Adam Mištera
        $artefact['favourite'] = is_null(User::find(Auth::id())->likesArtefacts()->find($id)) ? false : true;
120 10222730 Adam Mištera
121
        return view('artefact.view', ['artefact' => $artefact]);
122
    }
123 bfa73340 Adam Mištera
124
    /**
125 1bcdcb05 Adam Mištera
     * Likes artefact given by its id.
126 bfa73340 Adam Mištera
     *
127
     * @param $id int id of metadata
128 2fee1bb5 Marek Lovčí
     * @return RedirectResponse
129 bfa73340 Adam Mištera
     */
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 1bcdcb05 Adam Mištera
     * Unlikes artefact given by its id.
142 bfa73340 Adam Mištera
     *
143
     * @param $id int id of metadata
144 2fee1bb5 Marek Lovčí
     * @return RedirectResponse
145 bfa73340 Adam Mištera
     */
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 74e290c2 Adam Mištera
}