mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-10-01 01:36:00 -04:00
d0ff79ea60
- Removed old view system and started use of new query classes instead. - Finished off RelationMultiModelQuery but found it was less efficient than x-many queries due to the amount of tables being scanned. Adding now for history but will delete as not used. - Updated recently viewed to use same query system as popular items rather than running and joining x-entities queries. - Added "Most Viewed Faviourites" listing to homepages.
58 lines
1.3 KiB
PHP
58 lines
1.3 KiB
PHP
<?php namespace BookStack\Actions;
|
|
|
|
use BookStack\Interfaces\Viewable;
|
|
use BookStack\Model;
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
|
|
|
/**
|
|
* Class View
|
|
* Views are stored per-item per-person within the database.
|
|
* They can be used to find popular items or recently viewed items
|
|
* at a per-person level. They do not record every view instance as an
|
|
* activity. Only the latest and original view times could be recognised.
|
|
*
|
|
* @property int $views
|
|
* @property int $user_id
|
|
*/
|
|
class View extends Model
|
|
{
|
|
|
|
protected $fillable = ['user_id', 'views'];
|
|
|
|
/**
|
|
* Get all owning viewable models.
|
|
*/
|
|
public function viewable(): MorphTo
|
|
{
|
|
return $this->morphTo();
|
|
}
|
|
|
|
/**
|
|
* Increment the current user's view count for the given viewable model.
|
|
*/
|
|
public static function incrementFor(Viewable $viewable): int
|
|
{
|
|
$user = user();
|
|
if (is_null($user) || $user->isDefault()) {
|
|
return 0;
|
|
}
|
|
|
|
/** @var View $view */
|
|
$view = $viewable->views()->firstOrNew([
|
|
'user_id' => $user->id,
|
|
], ['views' => 0]);
|
|
|
|
$view->forceFill(['views' => $view->views + 1])->save();
|
|
|
|
return $view->views;
|
|
}
|
|
|
|
/**
|
|
* Clear all views from the system.
|
|
*/
|
|
public static function clearAll()
|
|
{
|
|
static::query()->truncate();
|
|
}
|
|
}
|