2021-05-16 07:29:56 +08:00
|
|
|
<?php
|
|
|
|
|
2023-05-18 00:56:55 +08:00
|
|
|
namespace BookStack\Activity\Controllers;
|
2021-05-16 07:29:56 +08:00
|
|
|
|
2023-05-18 00:56:55 +08:00
|
|
|
use BookStack\Activity\Models\Favouritable;
|
|
|
|
use BookStack\App\Model;
|
2021-05-16 07:29:56 +08:00
|
|
|
use BookStack\Entities\Models\Entity;
|
2021-05-23 20:34:08 +08:00
|
|
|
use BookStack\Entities\Queries\TopFavourites;
|
2023-09-10 22:18:31 +08:00
|
|
|
use BookStack\Entities\Tools\MixedEntityRequestHelper;
|
2023-05-19 03:53:39 +08:00
|
|
|
use BookStack\Http\Controller;
|
2021-05-16 07:29:56 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
class FavouriteController extends Controller
|
|
|
|
{
|
2023-09-10 22:18:31 +08:00
|
|
|
public function __construct(
|
|
|
|
protected MixedEntityRequestHelper $entityHelper,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2021-05-23 20:34:08 +08:00
|
|
|
/**
|
|
|
|
* 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 23:23:15 +08:00
|
|
|
$favourites = (new TopFavourites())->run($viewCount + 1, (($page - 1) * $viewCount));
|
2021-05-23 20:34:08 +08:00
|
|
|
|
2021-06-26 23:23:15 +08:00
|
|
|
$hasMoreLink = ($favourites->count() > $viewCount) ? url('/favourites?page=' . ($page + 1)) : null;
|
2021-05-23 20:34:08 +08:00
|
|
|
|
2022-01-04 21:33:24 +08:00
|
|
|
$this->setPageTitle(trans('entities.my_favourites'));
|
|
|
|
|
2021-05-23 20:34:08 +08:00
|
|
|
return view('common.detailed-listing-with-more', [
|
2021-06-26 23:23:15 +08:00
|
|
|
'title' => trans('entities.my_favourites'),
|
|
|
|
'entities' => $favourites->slice(0, $viewCount),
|
2021-05-23 20:34:08 +08:00
|
|
|
'hasMoreLink' => $hasMoreLink,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-05-16 07:29:56 +08:00
|
|
|
/**
|
|
|
|
* Add a new item as a favourite.
|
|
|
|
*/
|
|
|
|
public function add(Request $request)
|
|
|
|
{
|
2023-09-10 22:18:31 +08:00
|
|
|
$modelInfo = $this->validate($request, $this->entityHelper->validationRules());
|
|
|
|
$entity = $this->entityHelper->getVisibleEntityFromRequestData($modelInfo);
|
|
|
|
$entity->favourites()->firstOrCreate([
|
2021-05-16 07:29:56 +08:00
|
|
|
'user_id' => user()->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->showSuccessNotification(trans('activities.favourite_add_notification', [
|
2023-09-10 22:18:31 +08:00
|
|
|
'name' => $entity->name,
|
2021-05-16 07:29:56 +08:00
|
|
|
]));
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2021-05-16 07:29:56 +08:00
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an item as a favourite.
|
|
|
|
*/
|
|
|
|
public function remove(Request $request)
|
|
|
|
{
|
2023-09-10 22:18:31 +08:00
|
|
|
$modelInfo = $this->validate($request, $this->entityHelper->validationRules());
|
|
|
|
$entity = $this->entityHelper->getVisibleEntityFromRequestData($modelInfo);
|
|
|
|
$entity->favourites()->where([
|
2021-05-16 07:29:56 +08:00
|
|
|
'user_id' => user()->id,
|
|
|
|
])->delete();
|
|
|
|
|
|
|
|
$this->showSuccessNotification(trans('activities.favourite_remove_notification', [
|
2023-09-10 22:18:31 +08:00
|
|
|
'name' => $entity->name,
|
2021-05-16 07:29:56 +08:00
|
|
|
]));
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2021-05-16 07:29:56 +08:00
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
}
|