use BookStack\Auth\Permissions\EntityPermission;
use BookStack\Auth\Permissions\JointPermission;
use BookStack\Ownable;
+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 integer $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
{
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
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