BookStack/app/Activity/Controllers/FavouriteController.php

73 lines
2.2 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
use BookStack\Entities\Queries\QueryTopFavourites;
use BookStack\Entities\Tools\MixedEntityRequestHelper;
use BookStack\Http\Controller;
2021-05-15 19:29:56 -04:00
use Illuminate\Http\Request;
class FavouriteController extends Controller
{
public function __construct(
protected MixedEntityRequestHelper $entityHelper,
) {
}
/**
* Show a listing of all favourite items for the current user.
*/
public function index(Request $request, QueryTopFavourites $topFavourites)
{
$viewCount = 20;
$page = intval($request->get('page', 1));
$favourites = $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)
{
$modelInfo = $this->validate($request, $this->entityHelper->validationRules());
$entity = $this->entityHelper->getVisibleEntityFromRequestData($modelInfo);
$entity->favourites()->firstOrCreate([
2021-05-15 19:29:56 -04:00
'user_id' => user()->id,
]);
$this->showSuccessNotification(trans('activities.favourite_add_notification', [
'name' => $entity->name,
2021-05-15 19:29:56 -04:00
]));
2021-06-26 11:23:15 -04:00
return redirect($entity->getUrl());
2021-05-15 19:29:56 -04:00
}
/**
* Remove an item as a favourite.
*/
public function remove(Request $request)
{
$modelInfo = $this->validate($request, $this->entityHelper->validationRules());
$entity = $this->entityHelper->getVisibleEntityFromRequestData($modelInfo);
$entity->favourites()->where([
2021-05-15 19:29:56 -04:00
'user_id' => user()->id,
])->delete();
$this->showSuccessNotification(trans('activities.favourite_remove_notification', [
'name' => $entity->name,
2021-05-15 19:29:56 -04:00
]));
2021-06-26 11:23:15 -04:00
return redirect($entity->getUrl());
2021-05-15 19:29:56 -04:00
}
}