Files
core/app/Models/Content/Content.php
T

203 lines
4.8 KiB
PHP

<?php
namespace App\Models\Content;
use App\CMS\Contracts\Repositories\Content as ContentContract;
use App\CMS\Helpers\Traits\Nullable;
use App\CMS\Observers\Content as ContentObserver;
use App\Models\FileManager\File;
use App\Models\Module;
use App\Models\Templates\Template;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
use Modules\Categories\Entities\Category;
/**
* Class Content
*/
class Content extends Model
{
use SoftDeletes, Nullable;
public $timestamps = true;
protected $fillable = [
'userId',
'parentId',
'categoryId',
'language',
'moduleHandler',
'publishedFrom',
'publishedTo',
'titlePhoto',
'template',
'isHomepage',
'title',
'url',
'order',
'metaKeywords',
'metaDescription',
'pageContent',
'moduleSettings',
'status'
];
protected $casts = [
'isHomepage' => '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');
}
}