use BookStack\Facades\Permissions;
use BookStack\Ownable;
use Carbon\Carbon;
+use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\MorphMany;
/**
* @property int $created_by
* @property int $updated_by
* @property boolean $restricted
+ * @property Collection $tags
+ * @method static Entity|Builder visible()
+ * @method static Entity|Builder hasPermission(string $permission)
+ * @method static Builder withLastView()
+ * @method static Builder withViewCount()
*
* @package BookStack\Entities
*/
public $searchFactor = 1.0;
/**
- * Get the morph class for this model.
- * Set here since, due to folder changes, the namespace used
- * in the database no longer matches the class namespace.
- * @return string
+ * Get the entities that are visible to the current user.
+ */
+ public function scopeVisible(Builder $query)
+ {
+ return $this->scopeHasPermission($query, 'view');
+ }
+
+ /**
+ * Scope the query to those entities that the current user has the given permission for.
*/
- public function getMorphClass()
+ public function scopeHasPermission(Builder $query, string $permission)
{
- return 'BookStack\\Entity';
+ return Permissions::restrictEntityQuery($query, $permission);
+ }
+
+ /**
+ * Query scope to get the last view from the current user.
+ */
+ public function scopeWithLastView(Builder $query)
+ {
+ $viewedAtQuery = View::query()->select('updated_at')
+ ->whereColumn('viewable_id', '=', $this->getTable() . '.id')
+ ->where('viewable_type', '=', $this->getMorphClass())
+ ->where('user_id', '=', user()->id)
+ ->take(1);
+
+ return $query->addSelect(['last_viewed_at' => $viewedAtQuery]);
+ }
+
+ /**
+ * Query scope to get the total view count of the entities.
+ */
+ public function scopeWithViewCount(Builder $query)
+ {
+ $viewCountQuery = View::query()->selectRaw('SUM(views) as view_count')
+ ->whereColumn('viewable_id', '=', $this->getTable() . '.id')
+ ->where('viewable_type', '=', $this->getMorphClass())->take(1);
+
+ $query->addSelect(['view_count' => $viewCountQuery]);
}
/**
/**
* Gets the activity objects for this entity.
- * @return \Illuminate\Database\Eloquent\Relations\MorphMany
+ * @return MorphMany
*/
public function activity()
{
/**
* Get the Tag models that have been user assigned to this entity.
- * @return \Illuminate\Database\Eloquent\Relations\MorphMany
+ * @return MorphMany
*/
public function tags()
{
/**
* Get the related search terms.
- * @return \Illuminate\Database\Eloquent\Relations\MorphMany
+ * @return MorphMany
*/
public function searchTerms()
{
/**
* Get the entity jointPermissions this is connected to.
- * @return \Illuminate\Database\Eloquent\Relations\MorphMany
+ * @return MorphMany
*/
public function jointPermissions()
{
}
/**
- * Allows checking of the exact class, Used to check entity type.
- * Cleaner method for is_a.
- * @param $type
- * @return bool
+ * Check if this instance or class is a certain type of entity.
+ * Examples of $type are 'page', 'book', 'chapter'
*/
- public static function isA($type)
+ public static function isA(string $type): bool
{
return static::getType() === strtolower($type);
}
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
/**
* Gets a limited-length version of the entities name.
- * @param int $length
- * @return string
*/
- public function getShortName($length = 25)
+ public function getShortName(int $length = 25): string
{
if (mb_strlen($this->name) <= $length) {
return $this->name;
return trim($text);
}
- /**
- * Return a generalised, common raw query that can be 'unioned' across entities.
- * @return string
- */
- public function entityRawQuery()
- {
- return '';
- }
-
/**
* Get the url of this entity
* @param $path
public function rebuildPermissions()
{
/** @noinspection PhpUnhandledExceptionInspection */
- Permissions::buildJointPermissionsForEntity($this);
+ Permissions::buildJointPermissionsForEntity(clone $this);
+ }
+
+ /**
+ * Index the current entity for search
+ */
+ public function indexForSearch()
+ {
+ $searchService = app()->make(SearchService::class);
+ $searchService->indexEntity(clone $this);
}
/**