]> BookStack Code Mirror - bookstack/blob - app/Actions/View.php
Added code editor changes mobile design handling
[bookstack] / app / Actions / View.php
1 <?php
2
3 namespace BookStack\Actions;
4
5 use BookStack\Interfaces\Viewable;
6 use BookStack\Model;
7 use Illuminate\Database\Eloquent\Relations\MorphTo;
8
9 /**
10  * Class View
11  * Views are stored per-item per-person within the database.
12  * They can be used to find popular items or recently viewed items
13  * at a per-person level. They do not record every view instance as an
14  * activity. Only the latest and original view times could be recognised.
15  *
16  * @property int $views
17  * @property int $user_id
18  */
19 class View extends Model
20 {
21     protected $fillable = ['user_id', 'views'];
22
23     /**
24      * Get all owning viewable models.
25      */
26     public function viewable(): MorphTo
27     {
28         return $this->morphTo();
29     }
30
31     /**
32      * Increment the current user's view count for the given viewable model.
33      */
34     public static function incrementFor(Viewable $viewable): int
35     {
36         $user = user();
37         if (is_null($user) || $user->isDefault()) {
38             return 0;
39         }
40
41         /** @var View $view */
42         $view = $viewable->views()->firstOrNew([
43             'user_id' => $user->id,
44         ], ['views' => 0]);
45
46         $view->forceFill(['views' => $view->views + 1])->save();
47
48         return $view->views;
49     }
50
51     /**
52      * Clear all views from the system.
53      */
54     public static function clearAll()
55     {
56         static::query()->truncate();
57     }
58 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.