176 lines
3.8 KiB
PHP
Executable File
176 lines
3.8 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\PageBuilder\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class Page extends Model
|
|
{
|
|
use SoftDeletes;
|
|
|
|
protected $table = 'pages';
|
|
protected $primaryKey = 'pages_id';
|
|
|
|
protected $fillable = [
|
|
'pages_title',
|
|
'pages_slug',
|
|
'pages_content',
|
|
'pages_meta_title',
|
|
'pages_meta_description',
|
|
'pages_meta_keywords',
|
|
'pages_is_published',
|
|
'pages_public',
|
|
'pages_published_at',
|
|
'pages_layout',
|
|
'pages_css_classes',
|
|
'pages_custom_css',
|
|
'pages_custom_js',
|
|
'pages_created_by',
|
|
'pages_updated_by'
|
|
];
|
|
|
|
protected $casts = [
|
|
'pages_is_published' => 'boolean',
|
|
'pages_public' => 'boolean',
|
|
'pages_published_at' => 'datetime',
|
|
'created_at' => 'datetime',
|
|
'updated_at' => 'datetime',
|
|
'pages_custom_css' => 'string',
|
|
'pages_custom_js' => 'string'
|
|
];
|
|
|
|
protected $dates = [
|
|
'deleted_at'
|
|
];
|
|
|
|
/**
|
|
* Gets the user who created the page.
|
|
*/
|
|
public function creator()
|
|
{
|
|
return $this->belongsTo(\Modules\Auth\Models\User::class, 'pages_created_by');
|
|
}
|
|
|
|
/**
|
|
* Gets the user who last updated the page.
|
|
*/
|
|
public function updater()
|
|
{
|
|
return $this->belongsTo(\Modules\Auth\Models\User::class, 'pages_updated_by');
|
|
}
|
|
|
|
/**
|
|
* Scope for published pages.
|
|
*/
|
|
public function scopePublished($query)
|
|
{
|
|
return $query->where('pages_is_published', true)
|
|
->where('pages_published_at', '<=', \Carbon\Carbon::now());
|
|
}
|
|
|
|
/**
|
|
* Scope for unpublished pages.
|
|
*/
|
|
public function scopeDraft($query)
|
|
{
|
|
return $query->where('pages_is_published', false);
|
|
}
|
|
|
|
/**
|
|
* Gets the page URL.
|
|
*/
|
|
public function getUrlAttribute()
|
|
{
|
|
return url('pages/' . $this->pages_slug);
|
|
}
|
|
|
|
/**
|
|
* Gets the content excerpt.
|
|
*/
|
|
public function getExcerptAttribute($length = 150)
|
|
{
|
|
$content = strip_tags($this->pages_content);
|
|
return strlen($content) > $length ? substr($content, 0, $length) . '...' : $content;
|
|
}
|
|
|
|
/**
|
|
* Publishes the page.
|
|
*/
|
|
public function publish()
|
|
{
|
|
$this->pages_is_published = true;
|
|
$this->pages_published_at = \Carbon\Carbon::now();
|
|
return $this->save();
|
|
}
|
|
|
|
/**
|
|
* Sets the page as draft.
|
|
*/
|
|
public function unpublish()
|
|
{
|
|
$this->pages_is_published = false;
|
|
return $this->save();
|
|
}
|
|
|
|
/**
|
|
* Checks if the page is published.
|
|
*/
|
|
public function isPublished()
|
|
{
|
|
return $this->pages_is_published && $this->pages_published_at <= \Carbon\Carbon::now();
|
|
}
|
|
|
|
/**
|
|
* Gets the page layout.
|
|
*/
|
|
public function getLayoutPath()
|
|
{
|
|
if ($this->pages_layout) {
|
|
$layout = $this->pages_layout;
|
|
if (substr($layout, -5) !== '.twig') {
|
|
$layout .= '.twig';
|
|
}
|
|
return $layout;
|
|
}
|
|
|
|
return 'layouts/base.twig';
|
|
}
|
|
|
|
/**
|
|
* Gets the page CSS classes.
|
|
*/
|
|
public function getCssClasses()
|
|
{
|
|
return $this->pages_css_classes ?: '';
|
|
}
|
|
|
|
/**
|
|
* Gets the page custom CSS.
|
|
*/
|
|
public function getCustomCss()
|
|
{
|
|
$css = '';
|
|
|
|
if ($this->pages_custom_css) {
|
|
$css .= '<style>' . $this->pages_custom_css . '</style>' . "\n";
|
|
}
|
|
|
|
return $css;
|
|
}
|
|
|
|
/**
|
|
* Gets the page custom JavaScript.
|
|
*/
|
|
public function getCustomJs()
|
|
{
|
|
$js = '';
|
|
|
|
if ($this->pages_custom_js) {
|
|
$js .= '<script>' . $this->pages_custom_js . '</script>' . "\n";
|
|
}
|
|
|
|
return $js;
|
|
}
|
|
}
|