mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-11-25 17:57:28 +08:00
2fbf5527c7
Fixes #4444
76 lines
2.3 KiB
PHP
76 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace BookStack\Activity\Controllers;
|
|
|
|
use BookStack\Activity\Models\Favouritable;
|
|
use BookStack\App\Model;
|
|
use BookStack\Entities\Models\Entity;
|
|
use BookStack\Entities\Queries\TopFavourites;
|
|
use BookStack\Entities\Tools\MixedEntityRequestHelper;
|
|
use BookStack\Http\Controller;
|
|
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)
|
|
{
|
|
$viewCount = 20;
|
|
$page = intval($request->get('page', 1));
|
|
$favourites = (new TopFavourites())->run($viewCount + 1, (($page - 1) * $viewCount));
|
|
|
|
$hasMoreLink = ($favourites->count() > $viewCount) ? url('/favourites?page=' . ($page + 1)) : null;
|
|
|
|
$this->setPageTitle(trans('entities.my_favourites'));
|
|
|
|
return view('common.detailed-listing-with-more', [
|
|
'title' => trans('entities.my_favourites'),
|
|
'entities' => $favourites->slice(0, $viewCount),
|
|
'hasMoreLink' => $hasMoreLink,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 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([
|
|
'user_id' => user()->id,
|
|
]);
|
|
|
|
$this->showSuccessNotification(trans('activities.favourite_add_notification', [
|
|
'name' => $entity->name,
|
|
]));
|
|
|
|
return redirect()->back();
|
|
}
|
|
|
|
/**
|
|
* 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([
|
|
'user_id' => user()->id,
|
|
])->delete();
|
|
|
|
$this->showSuccessNotification(trans('activities.favourite_remove_notification', [
|
|
'name' => $entity->name,
|
|
]));
|
|
|
|
return redirect()->back();
|
|
}
|
|
}
|