BookStack/app/Activity/Controllers/FavouriteController.php

101 lines
3.0 KiB
PHP
Raw Normal View History

2021-05-15 19:29:56 -04:00
<?php
2023-05-17 12:56:55 -04:00
namespace BookStack\Activity\Controllers;
2021-05-15 19:29:56 -04:00
2023-05-17 12:56:55 -04:00
use BookStack\Activity\Models\Favouritable;
use BookStack\App\Model;
2021-05-15 19:29:56 -04:00
use BookStack\Entities\Models\Entity;
use BookStack\Entities\Queries\TopFavourites;
use BookStack\Http\Controller;
2021-05-15 19:29:56 -04:00
use Illuminate\Http\Request;
class FavouriteController extends Controller
{
/**
* Show a listing of all favourite items for the current user.
*/
public function index(Request $request)
{
$viewCount = 20;
$page = intval($request->get('page', 1));
2021-06-26 11:23:15 -04:00
$favourites = (new TopFavourites())->run($viewCount + 1, (($page - 1) * $viewCount));
2021-06-26 11:23:15 -04:00
$hasMoreLink = ($favourites->count() > $viewCount) ? url('/favourites?page=' . ($page + 1)) : null;
$this->setPageTitle(trans('entities.my_favourites'));
return view('common.detailed-listing-with-more', [
2021-06-26 11:23:15 -04:00
'title' => trans('entities.my_favourites'),
'entities' => $favourites->slice(0, $viewCount),
'hasMoreLink' => $hasMoreLink,
]);
}
2021-05-15 19:29:56 -04:00
/**
* Add a new item as a favourite.
*/
public function add(Request $request)
{
$favouritable = $this->getValidatedModelFromRequest($request);
$favouritable->favourites()->firstOrCreate([
'user_id' => user()->id,
]);
$this->showSuccessNotification(trans('activities.favourite_add_notification', [
'name' => $favouritable->name,
]));
2021-06-26 11:23:15 -04:00
2021-05-15 19:29:56 -04:00
return redirect()->back();
}
/**
* Remove an item as a favourite.
*/
public function remove(Request $request)
{
$favouritable = $this->getValidatedModelFromRequest($request);
$favouritable->favourites()->where([
'user_id' => user()->id,
])->delete();
$this->showSuccessNotification(trans('activities.favourite_remove_notification', [
'name' => $favouritable->name,
]));
2021-06-26 11:23:15 -04:00
2021-05-15 19:29:56 -04:00
return redirect()->back();
}
/**
* @throws \Illuminate\Validation\ValidationException
* @throws \Exception
*/
2021-11-05 20:32:01 -04:00
protected function getValidatedModelFromRequest(Request $request): Entity
2021-05-15 19:29:56 -04:00
{
$modelInfo = $this->validate($request, [
'type' => ['required', 'string'],
'id' => ['required', 'integer'],
2021-05-15 19:29:56 -04:00
]);
if (!class_exists($modelInfo['type'])) {
throw new \Exception('Model not found');
}
/** @var Model $model */
2021-06-26 11:23:15 -04:00
$model = new $modelInfo['type']();
if (!$model instanceof Favouritable) {
2021-05-15 19:29:56 -04:00
throw new \Exception('Model not favouritable');
}
$modelInstance = $model->newQuery()
->where('id', '=', $modelInfo['id'])
->first(['id', 'name', 'owned_by']);
2021-05-15 19:29:56 -04:00
$inaccessibleEntity = ($modelInstance instanceof Entity && !userCan('view', $modelInstance));
if (is_null($modelInstance) || $inaccessibleEntity) {
throw new \Exception('Model instance not found');
}
return $modelInstance;
}
}