]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/FavouriteController.php
Apply fixes from StyleCI
[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
45         return redirect()->back();
46     }
47
48     /**
49      * Remove an item as a favourite.
50      */
51     public function remove(Request $request)
52     {
53         $favouritable = $this->getValidatedModelFromRequest($request);
54         $favouritable->favourites()->where([
55             'user_id' => user()->id,
56         ])->delete();
57
58         $this->showSuccessNotification(trans('activities.favourite_remove_notification', [
59             'name' => $favouritable->name,
60         ]));
61
62         return redirect()->back();
63     }
64
65     /**
66      * @throws \Illuminate\Validation\ValidationException
67      * @throws \Exception
68      */
69     protected function getValidatedModelFromRequest(Request $request): Favouritable
70     {
71         $modelInfo = $this->validate($request, [
72             'type' => 'required|string',
73             'id'   => 'required|integer',
74         ]);
75
76         if (!class_exists($modelInfo['type'])) {
77             throw new \Exception('Model not found');
78         }
79
80         /** @var Model $model */
81         $model = new $modelInfo['type']();
82         if (!$model instanceof Favouritable) {
83             throw new \Exception('Model not favouritable');
84         }
85
86         $modelInstance = $model->newQuery()
87             ->where('id', '=', $modelInfo['id'])
88             ->first(['id', 'name']);
89
90         $inaccessibleEntity = ($modelInstance instanceof Entity && !userCan('view', $modelInstance));
91         if (is_null($modelInstance) || $inaccessibleEntity) {
92             throw new \Exception('Model instance not found');
93         }
94
95         return $modelInstance;
96     }
97 }