]> BookStack Code Mirror - bookstack/blobdiff - app/Entities/Entity.php
Show bookshelves that a book belongs to on a book view
[bookstack] / app / Entities / Entity.php
index 482d217662ae20b54f62578d88367b8daacb1098..4e54a9e2796e90aa65742e7502447b08aa78929c 100644 (file)
@@ -6,6 +6,7 @@ use BookStack\Actions\Tag;
 use BookStack\Actions\View;
 use BookStack\Auth\Permissions\EntityPermission;
 use BookStack\Auth\Permissions\JointPermission;
+use BookStack\Facades\Permissions;
 use BookStack\Ownable;
 use Carbon\Carbon;
 use Illuminate\Database\Eloquent\Relations\MorphMany;
@@ -15,7 +16,7 @@ use Illuminate\Database\Eloquent\Relations\MorphMany;
  * The base class for book-like items such as pages, chapters & books.
  * This is not a database model in itself but extended.
  *
- * @property integer $id
+ * @property int $id
  * @property string $name
  * @property string $slug
  * @property Carbon $created_at
@@ -91,7 +92,8 @@ class Entity extends Ownable
      */
     public function activity()
     {
-        return $this->morphMany(Activity::class, 'entity')->orderBy('created_at', 'desc');
+        return $this->morphMany(Activity::class, 'entity')
+            ->orderBy('created_at', 'desc');
     }
 
     /**
@@ -102,11 +104,6 @@ class Entity extends Ownable
         return $this->morphMany(View::class, 'viewable');
     }
 
-    public function viewCountQuery()
-    {
-        return $this->views()->selectRaw('viewable_id, sum(views) as view_count')->groupBy('viewable_id');
-    }
-
     /**
      * Get the Tag models that have been user assigned to this entity.
      * @return \Illuminate\Database\Eloquent\Relations\MorphMany
@@ -185,6 +182,14 @@ class Entity extends Ownable
         return strtolower(static::getClassName());
     }
 
+    /**
+     * Get the type of this entity.
+     */
+    public function type(): string
+    {
+        return static::getType();
+    }
+
     /**
      * Get an instance of an entity of the given type.
      * @param $type
@@ -255,4 +260,23 @@ class Entity extends Ownable
     {
         return $path;
     }
+
+    /**
+     * Rebuild the permissions for this entity.
+     */
+    public function rebuildPermissions()
+    {
+        /** @noinspection PhpUnhandledExceptionInspection */
+        Permissions::buildJointPermissionsForEntity($this);
+    }
+
+    /**
+     * Generate and set a new URL slug for this model.
+     */
+    public function refreshSlug(): string
+    {
+        $generator = new SlugGenerator($this);
+        $this->slug = $generator->generate();
+        return $this->slug;
+    }
 }