]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/FavouriteController.php
Added force option for update-url command
[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         $this->setPageTitle(trans('entities.my_favourites'));
25
26         return view('common.detailed-listing-with-more', [
27             'title'       => trans('entities.my_favourites'),
28             'entities'    => $favourites->slice(0, $viewCount),
29             'hasMoreLink' => $hasMoreLink,
30         ]);
31     }
32
33     /**
34      * Add a new item as a favourite.
35      */
36     public function add(Request $request)
37     {
38         $favouritable = $this->getValidatedModelFromRequest($request);
39         $favouritable->favourites()->firstOrCreate([
40             'user_id' => user()->id,
41         ]);
42
43         $this->showSuccessNotification(trans('activities.favourite_add_notification', [
44             'name' => $favouritable->name,
45         ]));
46
47         return redirect()->back();
48     }
49
50     /**
51      * Remove an item as a favourite.
52      */
53     public function remove(Request $request)
54     {
55         $favouritable = $this->getValidatedModelFromRequest($request);
56         $favouritable->favourites()->where([
57             'user_id' => user()->id,
58         ])->delete();
59
60         $this->showSuccessNotification(trans('activities.favourite_remove_notification', [
61             'name' => $favouritable->name,
62         ]));
63
64         return redirect()->back();
65     }
66
67     /**
68      * @throws \Illuminate\Validation\ValidationException
69      * @throws \Exception
70      */
71     protected function getValidatedModelFromRequest(Request $request): Entity
72     {
73         $modelInfo = $this->validate($request, [
74             'type' => ['required', 'string'],
75             'id'   => ['required', 'integer'],
76         ]);
77
78         if (!class_exists($modelInfo['type'])) {
79             throw new \Exception('Model not found');
80         }
81
82         /** @var Model $model */
83         $model = new $modelInfo['type']();
84         if (!$model instanceof Favouritable) {
85             throw new \Exception('Model not favouritable');
86         }
87
88         $modelInstance = $model->newQuery()
89             ->where('id', '=', $modelInfo['id'])
90             ->first(['id', 'name', 'owned_by']);
91
92         $inaccessibleEntity = ($modelInstance instanceof Entity && !userCan('view', $modelInstance));
93         if (is_null($modelInstance) || $inaccessibleEntity) {
94             throw new \Exception('Model instance not found');
95         }
96
97         return $modelInstance;
98     }
99 }