Projekt

Obecné

Profil

Stáhnout (3.5 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
    public function __construct()
16
    {
17
        $this->middleware('auth');
18
    }
19

    
20
    /**
21
     * Returns view of all artefacts.
22
     *
23
     * @return Factory|View
24
     */
25
    public function default()
26
    {
27
        $artefacts = Artefact::all();
28

    
29
        return view('artefact.default', ['artefacts' => $artefacts]);
30
    }
31

    
32
    /**
33
     * Returns view of artefacts related to the chosen category
34
     *
35
     * @param $id id of the category
36
     * @return Factory|View
37
     */
38
    public function showCategory($id)
39
    {
40
        $categoryArtefacts = ArtefactCategory::where('category_id', $id)->get();
41
        if(count($categoryArtefacts) > 0)
42
        {
43
            $artefacts = array();
44
            foreach($categoryArtefacts as $ar)
45
            {
46
                array_push($artefacts, Artefact::where('id', $ar->artefact_id)->get());
47
            }
48
            return view('artefact.category', ['artefacts' => $artefacts]);
49
        }
50
        else
51
        {
52
            return view('artefact.category', ['artefacts' => array()]);
53
        }
54
    }
55

    
56
    /**
57
     * Returns view of artefacts related to the chosen category
58
     *
59
     * @param $id string with categories ids
60
     * @return Factory|View
61
     */
62
    public function showCategories($id)
63
    {
64
        $textWithIds  = $id;
65
        $pieces = explode(",", $textWithIds);
66
        $artefacts = array();
67
        for($i = 0;$i < count($pieces); $i++)
68
        {
69
            if($pieces[$i] == null || !strcmp($pieces[$i], ""))
70
            {
71
                continue;
72
            }
73

    
74
            $categoryArtefacts = ArtefactCategory::where('category_id', $pieces[$i])->get();
75
            if(count($categoryArtefacts) > 0)
76
            {
77
                foreach($categoryArtefacts as $ar)
78
                {
79
                    array_push($artefacts, Artefact::where('id', $ar->artefact_id)->get());
80
                }
81
            }
82
        }
83

    
84
        if(count($artefacts) > 0)
85
        {
86
            return view('artefact.category', ['artefacts' => $artefacts]);
87
        }
88
        else
89
        {
90
            return view('artefact.category', ['artefacts' => array()]);
91
        }
92
    }
93

    
94
    /**
95
     * Returns view of single artefact given by its id.
96
     *
97
     * @param $id int id of the artefact
98
     * @return Factory|View
99
     */
100
    public function view($id)
101
    {
102
        $artefact = Artefact::find($id);
103
        $artefact['likes'] = Artefact::find($id)->users()->count();
104
        $artefact['favourite'] = is_null(User::find(Auth::id())->likesArtefacts()->find($id)) ? false : true;
105

    
106
        return view('artefact.view', ['artefact' => $artefact]);
107
    }
108

    
109
    /**
110
     * Likes artefact given by its id.
111
     *
112
     * @param $id int id of metadata
113
     * @return RedirectResponse
114
     */
115
    public function like($id)
116
    {
117
        $user = User::find(Auth::id());
118
        $artefact = Artefact::find($id);
119

    
120
        $user->likesArtefacts()->attach($artefact);
121

    
122
        return back()->withInput();
123
    }
124

    
125
    /**
126
     * Unlikes artefact given by its id.
127
     *
128
     * @param $id int id of metadata
129
     * @return RedirectResponse
130
     */
131
    public function unlike($id)
132
    {
133
        $user = User::find(Auth::id());
134
        $artefact = Artefact::find($id);
135

    
136
        $user->likesArtefacts()->detach($artefact);
137

    
138
        return back()->withInput();
139
    }
140
}
(1-1/9)