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