X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/919660678bec2b94eaa84ac60d0313f5ef07dfb7..refs/pull/1688/head:/app/Entities/Entity.php diff --git a/app/Entities/Entity.php b/app/Entities/Entity.php index 87d679fdb..4e54a9e27 100644 --- a/app/Entities/Entity.php +++ b/app/Entities/Entity.php @@ -2,13 +2,31 @@ use BookStack\Actions\Activity; use BookStack\Actions\Comment; +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 BookStack\Actions\Tag; -use BookStack\Actions\View; +use Carbon\Carbon; use Illuminate\Database\Eloquent\Relations\MorphMany; +/** + * Class Entity + * The base class for book-like items such as pages, chapters & books. + * This is not a database model in itself but extended. + * + * @property int $id + * @property string $name + * @property string $slug + * @property Carbon $created_at + * @property Carbon $updated_at + * @property int $created_by + * @property int $updated_by + * @property boolean $restricted + * + * @package BookStack\Entities + */ class Entity extends Ownable { @@ -74,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'); } /** @@ -163,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 @@ -201,6 +228,20 @@ class Entity extends Ownable return $this->{$this->textField}; } + /** + * Get an excerpt of this entity's descriptive content to the specified length. + * @param int $length + * @return mixed + */ + public function getExcerpt(int $length = 100) + { + $text = $this->getText(); + if (mb_strlen($text) > $length) { + $text = mb_substr($text, 0, $length-3) . '...'; + } + return trim($text); + } + /** * Return a generalised, common raw query that can be 'unioned' across entities. * @return string @@ -219,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; + } }