]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/FavouriteController.php
f4aeb4faa1389c16f484fbaed3113a283b07aa7d
[bookstack] / app / Http / Controllers / FavouriteController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Entities\Models\Entity;
6 use BookStack\Entities\Queries\TopFavourites;
7 use BookStack\Interfaces\Favouritable;
8 use BookStack\Model;
9 use Illuminate\Http\Request;
10
11 class FavouriteController extends Controller
12 {
13     /**
14      * Show a listing of all favourite items for the current user.
15      */
16     public function index(Request $request)
17     {
18         $viewCount = 20;
19         $page = intval($request->get('page', 1));
20         $favourites = (new TopFavourites)->run($viewCount + 1, (($page - 1) * $viewCount));
21
22         $hasMoreLink = ($favourites->count() > $viewCount) ? url("/favourites?page=" . ($page+1)) : null;
23
24         return view('common.detailed-listing-with-more', [
25             'title' => trans('entities.my_favourites'),
26             'entities' => $favourites->slice(0, $viewCount),
27             'hasMoreLink' => $hasMoreLink,
28         ]);
29     }
30
31     /**
32      * Add a new item as a favourite.
33      */
34     public function add(Request $request)
35     {
36         $favouritable = $this->getValidatedModelFromRequest($request);
37         $favouritable->favourites()->firstOrCreate([
38             'user_id' => user()->id,
39         ]);
40
41         $this->showSuccessNotification(trans('activities.favourite_add_notification', [
42             'name' => $favouritable->name,
43         ]));
44         return redirect()->back();
45     }
46
47     /**
48      * Remove an item as a favourite.
49      */
50     public function remove(Request $request)
51     {
52         $favouritable = $this->getValidatedModelFromRequest($request);
53         $favouritable->favourites()->where([
54             'user_id' => user()->id,
55         ])->delete();
56
57         $this->showSuccessNotification(trans('activities.favourite_remove_notification', [
58             'name' => $favouritable->name,
59         ]));
60         return redirect()->back();
61     }
62
63     /**
64      * @throws \Illuminate\Validation\ValidationException
65      * @throws \Exception
66      */
67     protected function getValidatedModelFromRequest(Request $request): Favouritable
68     {
69         $modelInfo = $this->validate($request, [
70             'type' => 'required|string',
71             'id' => 'required|integer',
72         ]);
73
74         if (!class_exists($modelInfo['type'])) {
75             throw new \Exception('Model not found');
76         }
77
78         /** @var Model $model */
79         $model = new $modelInfo['type'];
80         if (! $model instanceof Favouritable) {
81             throw new \Exception('Model not favouritable');
82         }
83
84         $modelInstance = $model->newQuery()
85             ->where('id', '=', $modelInfo['id'])
86             ->first(['id', 'name']);
87
88         $inaccessibleEntity = ($modelInstance instanceof Entity && !userCan('view', $modelInstance));
89         if (is_null($modelInstance) || $inaccessibleEntity) {
90             throw new \Exception('Model instance not found');
91         }
92
93         return $modelInstance;
94     }
95 }