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

135 lines
2.9 KiB
PHP

<?php
namespace App\Models\Content;
use App\Events\CreateNewContent;
use App\Events\Event;
use App\Models\FileManager\File;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Modules\Categories\Entities\Category;
/**
* Class Content
*/
class Content extends Model
{
use SoftDeletes, ModelTraits\Content;
public $timestamps = true;
protected $fillable = [
'user_id',
'parent_id',
'category_id',
'language',
'module_handler',
'published_from',
'published_to',
'title_photo',
'template',
'is_homepage',
'title',
'url',
'meta_keywords',
'meta_description',
'content',
'module_settings',
'status'
];
const STATUS_DRAFT = 'draft';
const STATUS_PROTECTED = 'protected';
const STATUS_PUBLIC = 'public';
/**
* Is current content draft?
*
* @return bool
*/
public function isDraft()
{
return $this->status == self::STATUS_DRAFT;
}
/**
* Is current content visible for all visitors?
*
* @return bool
*/
public function isPublic()
{
return $this->status == self::STATUS_PUBLIC;
}
/**
* Is current content visible only for logged users?
*
* @return bool
*/
public function isProtected()
{
return $this->status == self::STATUS_PROTECTED;
}
/**
* Is current content visible for logged user OR for everyone?
*
* @return bool
*/
public function isVisible()
{
return $this->isPublic() || $this->isProtected();
}
/**
* Mark relationship with its historical records
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function history()
{
return $this->hasMany(\App\Models\Content\ContentHistory::class);
}
/**
* Mark relationship with users table
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function author()
{
return $this->belongsTo(User::class, 'user_id');
}
/**
* 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()
{
return $this->belongsTo(Category::class);
}
public function save(array $options = [])
{
$saved = parent::save($options);
// fire event about creating new content page
if ($this->wasRecentlyCreated) {
Event::fire(new CreateNewContent($this));
}
return $saved;
}
}