REFACTORING #1880 @4h30m: another ugly refactoring commit

This commit is contained in:
Michal Kváček
2016-10-25 16:50:04 +02:00
parent 28f67ffe3c
commit cc999dd24e
71 changed files with 827 additions and 608 deletions
+103 -14
View File
@@ -2,10 +2,14 @@
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\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
use Modules\Categories\Entities\Category;
/**
@@ -13,27 +17,41 @@ use Modules\Categories\Entities\Category;
*/
class Content extends Model
{
use SoftDeletes, ModelTraits\Content;
use SoftDeletes, ModelTraits\Content, Nullable;
public $timestamps = true;
protected $fillable = [
'user_id',
'parent_id',
'category_id',
'userId',
'parentId',
'categoryId',
'language',
'module_handler',
'published_from',
'published_to',
'title_photo',
'moduleHandler',
'publishedFrom',
'publishedTo',
'titlePhoto',
'template',
'is_homepage',
'isHomepage',
'title',
'url',
'meta_keywords',
'meta_description',
'content',
'module_settings',
'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
@@ -52,7 +70,7 @@ class Content extends Model
*/
public function author()
{
return $this->belongsTo(User::class, 'user_id');
return $this->belongsTo(User::class, 'userId');
}
/**
@@ -74,4 +92,75 @@ class Content extends Model
{
return $this->belongsTo(Category::class);
}
/**
* Subpages assigned to page
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function subpages()
{
return $this->hasMany(Content::class, 'parentId');
}
/**
* 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);
}
}