Projekt

Obecné

Profil

Stáhnout (2 KB) Statistiky
| Větev: | Tag: | Revize:
1
<?php
2

    
3
namespace App\Http\Controllers;
4

    
5
use App\ArtefactUser;
6
use App\User;
7
use Illuminate\Http\Request;
8
use App\Metadata;
9
use App\Artefact;
10
use Illuminate\Support\Facades\Auth;
11

    
12
class DetailsController extends Controller
13
{
14
    const ORDER_COLUMN = 'page';
15

    
16
    public function __construct()
17
    {
18
        $this->middleware('auth');
19
    }
20

    
21
    /**
22
     * Display a listing of the resource.
23
     *
24
     * @return \Illuminate\Http\Response
25
     */
26
    public function index()
27
    {
28
        $data = array(
29
            'title' => 'Welcome to the MERLOT page',
30
        );
31
        //return view('index', compact('title'));
32
        return view('index') -> with($data);
33
    }
34

    
35
    /**
36
     * Display the specified resource.
37
     *
38
     * @param  int  $id
39
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
40
     */
41
    public function show($id)
42
    {
43
        if (is_null(Artefact::find($id)))
44
        {
45
            return view('detail.index', ['metadata' => []]);
46
        }
47

    
48
        $metadata = Artefact::find($id)->metadata()->orderBy(self::ORDER_COLUMN)->get();
49
        foreach($metadata as $item)
50
        {
51
            $item['favourite'] = is_null(User::find(Auth::id())->likesMetadata()->find($item->id)) ? false : true;
52
        }
53

    
54
        return view('detail.index', ['metadata' => $metadata]);
55
    }
56

    
57
    /**
58
     * Likes metadata given by its id.
59
     *
60
     * @param $id int id of metadata
61
     * @return \Illuminate\Http\RedirectResponse
62
     */
63
    public function like($id)
64
    {
65
        $user = User::find(Auth::id());
66
        $metadata = Metadata::find($id);
67

    
68
        $user->likesMetadata()->attach($metadata);
69

    
70
        return back()->withInput();
71
    }
72

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

    
84
        $user->likesMetadata()->detach($metadata);
85

    
86
        return back()->withInput();
87
    }
88
}
(5-5/10)