Projekt

Obecné

Profil

Stáhnout (2.61 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\Category;
8
use App\Metadata;
9
use App\User;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Facades\Auth;
12
use Illuminate\Support\Facades\DB;
13

    
14
class ArtefactController extends Controller
15
{
16
    public function __construct()
17
    {
18
        $this->middleware('auth');
19
    }
20

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

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

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

    
57
    /**
58
     * Returns view of single artefact given by its id.
59
     *
60
     * @param $id int id of the artefact
61
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
62
     */
63
    public function view($id)
64
    {
65
        $artefact = Artefact::find($id);
66
        $artefact['likes'] = Artefact::find($id)->users()->count();
67
        $artefact['favourite'] = is_null(User::find(Auth::id())->likesArtefacts()->find($id)) ? false : true;
68

    
69
        return view('artefact.view', ['artefact' => $artefact]);
70
    }
71

    
72
    /**
73
     * Likes artefact given by its id.
74
     *
75
     * @param $id int id of metadata
76
     * @return \Illuminate\Http\RedirectResponse
77
     */
78
    public function like($id)
79
    {
80
        $user = User::find(Auth::id());
81
        $artefact = Artefact::find($id);
82

    
83
        $user->likesArtefacts()->attach($artefact);
84

    
85
        return back()->withInput();
86
    }
87

    
88
    /**
89
     * Unlikes artefact given by its id.
90
     *
91
     * @param $id int id of metadata
92
     * @return \Illuminate\Http\RedirectResponse
93
     */
94
    public function unlike($id)
95
    {
96
        $user = User::find(Auth::id());
97
        $artefact = Artefact::find($id);
98

    
99
        $user->likesArtefacts()->detach($artefact);
100

    
101
        return back()->withInput();
102
    }
103
}
(1-1/8)