Projekt

Obecné

Profil

Stáhnout (4.29 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 609126cb rizir01
use App\Category;
8 bfa73340 Adam Mištera
use App\User;
9 2fee1bb5 Marek Lovčí
use Illuminate\Contracts\View\Factory;
10
use Illuminate\Http\RedirectResponse;
11 bfa73340 Adam Mištera
use Illuminate\Support\Facades\Auth;
12 2fee1bb5 Marek Lovčí
use Illuminate\View\View;
13 74e290c2 Adam Mištera
14
class ArtefactController extends Controller
15
{
16 af0a94fc Adam Mištera
    const ORDER_COLUMN = 'page';
17
18 21570473 Adam Mištera
    public function __construct()
19
    {
20
        $this->middleware('auth');
21
    }
22
23 10222730 Adam Mištera
    /**
24 609126cb rizir01
     * 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 10222730 Adam Mištera
     *
29 609126cb rizir01
     * @param       $artefacts  all artefacts
30
     * @return      mixed       modified artefacts
31 10222730 Adam Mištera
     */
32 609126cb rizir01
    public function prepData($artefacts)
33 74e290c2 Adam Mištera
    {
34 af0a94fc Adam Mištera
        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 74e290c2 Adam Mištera
48 609126cb rizir01
        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 74e290c2 Adam Mištera
        return view('artefact.default', ['artefacts' => $artefacts]);
61
    }
62 10222730 Adam Mištera
63 fb354d59 rizir01
    /**
64
     * Returns view of artefacts related to the chosen category
65
     *
66 2fee1bb5 Marek Lovčí
     * @param $id id of the category
67
     * @return Factory|View
68 fb354d59 rizir01
     */
69
    public function showCategory($id)
70
    {
71 609126cb rizir01
        $artefacts = Category::find($id)->artefacts()->simplePaginate(1);
72
        $artefacts = $this->prepData($artefacts);
73
        return view('artefact.default', ['artefacts' => $artefacts]);
74 fb354d59 rizir01
    }
75
76 eadc983d rizir01
    /**
77 609126cb rizir01
     * Returns view of artefacts related to the chosen categories
78 eadc983d rizir01
     *
79
     * @param $id string with categories ids
80
     * @return Factory|View
81
     */
82
    public function showCategories($id)
83
    {
84 609126cb rizir01
        //Parsing text for individual ids of categories
85 eadc983d rizir01
        $textWithIds  = $id;
86
        $pieces = explode(",", $textWithIds);
87 609126cb rizir01
        $arrStrToInt = array();
88 eadc983d rizir01
        for($i = 0;$i < count($pieces); $i++)
89
        {
90
            if($pieces[$i] == null || !strcmp($pieces[$i], ""))
91
            {
92
                continue;
93
            }
94
95 609126cb rizir01
            array_push($arrStrToInt, (int)$pieces[$i]);
96 eadc983d rizir01
        }
97
98 609126cb rizir01
        //Creating array of all artefacts ids
99
        $arrArtIds = array();
100
        foreach($arrStrToInt as $cat)
101 eadc983d rizir01
        {
102 609126cb rizir01
            $tmpArtefacts = Category::find($cat)->artefacts()->get();
103
            foreach($tmpArtefacts as $art)
104
            {
105
                array_push($arrArtIds, $art->id);
106
            }
107 eadc983d rizir01
        }
108 609126cb rizir01
109
        $artefacts = Artefact::whereIn('id', $arrArtIds)->simplePaginate(1);
110
        $artefacts = $this->prepData($artefacts);
111
        return view('artefact.default', ['artefacts' => $artefacts]);
112 eadc983d rizir01
    }
113
114 10222730 Adam Mištera
    /**
115
     * Returns view of single artefact given by its id.
116
     *
117
     * @param $id int id of the artefact
118 2fee1bb5 Marek Lovčí
     * @return Factory|View
119 10222730 Adam Mištera
     */
120
    public function view($id)
121
    {
122 609126cb rizir01
        $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 10222730 Adam Mištera
    }
127 bfa73340 Adam Mištera
128
    /**
129 1bcdcb05 Adam Mištera
     * Likes artefact given by its id.
130 bfa73340 Adam Mištera
     *
131
     * @param $id int id of metadata
132 2fee1bb5 Marek Lovčí
     * @return RedirectResponse
133 bfa73340 Adam Mištera
     */
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 1bcdcb05 Adam Mištera
     * Unlikes artefact given by its id.
146 bfa73340 Adam Mištera
     *
147
     * @param $id int id of metadata
148 2fee1bb5 Marek Lovčí
     * @return RedirectResponse
149 bfa73340 Adam Mištera
     */
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 74e290c2 Adam Mištera
}