2018-09-25 07:30:50 -04:00
|
|
|
<?php namespace BookStack\Actions;
|
2015-11-21 12:22:14 -05:00
|
|
|
|
2018-09-25 07:30:50 -04:00
|
|
|
use BookStack\Auth\Permissions\PermissionService;
|
2020-11-21 19:17:45 -05:00
|
|
|
use BookStack\Entities\Models\Book;
|
|
|
|
use BookStack\Entities\Models\Entity;
|
2019-03-30 12:54:15 -04:00
|
|
|
use BookStack\Entities\EntityProvider;
|
2019-09-19 13:03:17 -04:00
|
|
|
use DB;
|
2019-03-30 12:54:15 -04:00
|
|
|
use Illuminate\Support\Collection;
|
2015-11-21 12:22:14 -05:00
|
|
|
|
|
|
|
class ViewService
|
|
|
|
{
|
|
|
|
protected $view;
|
2016-05-01 16:20:50 -04:00
|
|
|
protected $permissionService;
|
2019-03-30 12:54:15 -04:00
|
|
|
protected $entityProvider;
|
2015-11-21 12:22:14 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ViewService constructor.
|
2019-09-19 13:03:17 -04:00
|
|
|
* @param View $view
|
|
|
|
* @param PermissionService $permissionService
|
2019-03-30 12:54:15 -04:00
|
|
|
* @param EntityProvider $entityProvider
|
2015-11-21 12:22:14 -05:00
|
|
|
*/
|
2019-03-30 12:54:15 -04:00
|
|
|
public function __construct(View $view, PermissionService $permissionService, EntityProvider $entityProvider)
|
2015-11-21 12:22:14 -05:00
|
|
|
{
|
|
|
|
$this->view = $view;
|
2016-05-01 16:20:50 -04:00
|
|
|
$this->permissionService = $permissionService;
|
2019-03-30 12:54:15 -04:00
|
|
|
$this->entityProvider = $entityProvider;
|
2015-11-21 12:22:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a view to the given entity.
|
2020-11-21 19:17:45 -05:00
|
|
|
* @param \BookStack\Entities\Models\Entity $entity
|
2015-11-21 12:22:14 -05:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function add(Entity $entity)
|
|
|
|
{
|
2017-01-01 12:33:06 -05:00
|
|
|
$user = user();
|
2018-01-28 11:58:52 -05:00
|
|
|
if ($user === null || $user->isDefault()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-01-01 12:33:06 -05:00
|
|
|
$view = $entity->views()->where('user_id', '=', $user->id)->first();
|
2015-11-21 12:22:14 -05:00
|
|
|
// Add view if model exists
|
|
|
|
if ($view) {
|
|
|
|
$view->increment('views');
|
|
|
|
return $view->views;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise create new view count
|
2019-10-05 07:55:01 -04:00
|
|
|
$entity->views()->save($this->view->newInstance([
|
2017-01-01 16:21:11 -05:00
|
|
|
'user_id' => $user->id,
|
2015-11-21 12:22:14 -05:00
|
|
|
'views' => 1
|
|
|
|
]));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-12-02 15:22:41 -05:00
|
|
|
/**
|
|
|
|
* Get the entities with the most views.
|
2016-02-28 14:03:04 -05:00
|
|
|
* @param int $count
|
|
|
|
* @param int $page
|
2019-03-30 12:54:15 -04:00
|
|
|
* @param string|array $filterModels
|
2018-04-14 13:00:16 -04:00
|
|
|
* @param string $action - used for permission checking
|
2019-03-30 12:54:15 -04:00
|
|
|
* @return Collection
|
2015-12-02 15:22:41 -05:00
|
|
|
*/
|
2019-10-05 07:55:01 -04:00
|
|
|
public function getPopular(int $count = 10, int $page = 0, array $filterModels = null, string $action = 'view')
|
2015-12-02 15:22:41 -05:00
|
|
|
{
|
|
|
|
$skipCount = $count * $page;
|
2019-04-07 04:57:48 -04:00
|
|
|
$query = $this->permissionService
|
2021-03-14 15:52:07 -04:00
|
|
|
->filterRestrictedEntityRelations($this->view->newQuery(), 'views', 'viewable_id', 'viewable_type', $action)
|
2019-09-19 13:03:17 -04:00
|
|
|
->select('*', 'viewable_id', 'viewable_type', DB::raw('SUM(views) as view_count'))
|
2015-12-02 15:22:41 -05:00
|
|
|
->groupBy('viewable_id', 'viewable_type')
|
|
|
|
->orderBy('view_count', 'desc');
|
|
|
|
|
2019-03-30 12:54:15 -04:00
|
|
|
if ($filterModels) {
|
|
|
|
$query->whereIn('viewable_type', $this->entityProvider->getMorphClasses($filterModels));
|
2017-08-28 08:38:32 -04:00
|
|
|
}
|
2015-12-02 15:22:41 -05:00
|
|
|
|
2021-01-03 14:02:50 -05:00
|
|
|
return $query->with('viewable')
|
|
|
|
->skip($skipCount)
|
|
|
|
->take($count)
|
|
|
|
->get()
|
|
|
|
->pluck('viewable')
|
|
|
|
->filter();
|
2015-12-02 15:22:41 -05:00
|
|
|
}
|
|
|
|
|
2015-11-21 12:22:14 -05:00
|
|
|
/**
|
|
|
|
* Get all recently viewed entities for the current user.
|
|
|
|
*/
|
2020-09-27 18:24:33 -04:00
|
|
|
public function getUserRecentlyViewed(int $count = 10, int $page = 1)
|
2015-11-21 12:22:14 -05:00
|
|
|
{
|
2017-01-01 12:33:06 -05:00
|
|
|
$user = user();
|
2018-01-28 11:58:52 -05:00
|
|
|
if ($user === null || $user->isDefault()) {
|
|
|
|
return collect();
|
|
|
|
}
|
2016-04-09 09:26:42 -04:00
|
|
|
|
2020-09-27 18:24:33 -04:00
|
|
|
$all = collect();
|
|
|
|
/** @var Entity $instance */
|
|
|
|
foreach ($this->entityProvider->all() as $name => $instance) {
|
|
|
|
$items = $instance::visible()->withLastView()
|
|
|
|
->orderBy('last_viewed_at', 'desc')
|
|
|
|
->skip($count * ($page - 1))
|
|
|
|
->take($count)
|
|
|
|
->get();
|
|
|
|
$all = $all->concat($items);
|
2018-01-28 11:58:52 -05:00
|
|
|
}
|
2015-11-21 12:22:14 -05:00
|
|
|
|
2020-09-27 18:24:33 -04:00
|
|
|
return $all->sortByDesc('last_viewed_at')->slice(0, $count);
|
2015-11-21 12:22:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset all view counts by deleting all views.
|
|
|
|
*/
|
|
|
|
public function resetAll()
|
|
|
|
{
|
|
|
|
$this->view->truncate();
|
|
|
|
}
|
2018-01-28 11:58:52 -05:00
|
|
|
}
|