'boolean', 'moduleSettings' => 'array' ]; /** * Assign observer for this model for logging */ protected static function boot() { parent::boot(); self::observe(ContentObserver::class); } /** * Mark relationship with its historical records * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function history() { return $this->hasMany(ContentHistory::class); } public function templateModel() { return $this->belongsTo(Template::class, 'template', 'template_name'); } /** * Mark relationship with users table * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function author() { return $this->belongsTo(User::class, 'userId'); } /** * Related files to content page * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function files() { return $this->belongsToMany(File::class, 'contents_has_files'); } /** * Category of content page * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function category() { if (!class_exists(Category::class)) { throw new \ErrorException('Categories are not installed'); } return $this->belongsTo(Category::class); } /** * Subpages assigned to page * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function subpages() { return $this->hasMany(Content::class, 'parentId')->orderBy('order')->orderBy('created_at', 'DESC'); } /** * Subpages assigned to page * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function publishedSubpages() { $query = $this->subpages(); if (\Auth::check()) { $query = $query->where('status', '!=', ContentContract::STATUS_DRAFT); } else { $query = $query->where('status', ContentContract::STATUS_PUBLIC); } return $query; } /** * Module relationship * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function module() { return $this->belongsTo(Module::class, 'moduleHandler', 'handler'); } /** * Parent page * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function parentPage() { return $this->belongsTo(Content::class, 'parentId'); } /** * Is current content draft? * @return bool */ public function isDraft() { return $this->status == ContentContract::STATUS_DRAFT; } /** * Is current content visible for all visitors? * @return bool */ public function isPublic() { return $this->status == ContentContract::STATUS_PUBLIC; } /** * Is current content visible only for logged users? * @return bool */ public function isProtected() { return $this->status == ContentContract::STATUS_PROTECTED; } /** * Is current content visible for logged user OR for everyone? * @return bool */ public function isVisible() { return $this->isPublic() || $this->isProtected(); } public function save(array $options = []) { $this->parentId = $this->nullable($this->parentId); $this->categoryId = $this->nullable($this->categoryId); $this->publishedFrom = $this->nullableDate($this->publishedFrom); $this->publishedTo = $this->nullableDate($this->publishedTo); $this->url = Str::slug($this->url); return parent::save($options); } /** * Increment number of viewed content */ public function view() { $this->timestamps = false; $this->increment('viewed'); } }