oc-social-plugin/models/Page.php

105 lines
2.3 KiB
PHP

<?php namespace Zoomyboy\Social\Models;
use MediaLibrary;
use Model;
/**
* Page Model
*/
class Page extends Model
{
use \October\Rain\Database\Traits\Validation;
use \October\Rain\Database\Traits\Sluggable;
/**
* @var string The database table used by the model.
*/
public $table = 'zoomyboy_social_pages';
public $slugs = ['slug' => 'name'];
public static array $pageTypes = [
'facebook' => 'Facebook',
'instagram' => 'Instagram',
];
/**
* @var array Guarded fields
*/
protected $guarded = ['*'];
/**
* @var array Fillable fields
*/
protected $fillable = ['name', 'type', 'cover', 'slug', 'access_token', 'remote_id'];
/**
* @var array Validation rules for attributes
*/
public $rules = [];
/**
* @var array Attributes to be cast to native types
*/
protected $casts = [];
/**
* @var array Attributes to be cast to JSON
*/
protected $jsonable = [];
/**
* @var array Attributes to be appended to the API representation of the model (ex. toArray())
*/
protected $appends = [];
/**
* @var array Attributes to be removed from the API representation of the model (ex. toArray())
*/
protected $hidden = [];
/**
* @var array Attributes to be cast to Argon (Carbon) instances
*/
protected $dates = [
'created_at',
'updated_at'
];
public $hasMany = [
'posts' => [ Post::class, 'page_id' ]
];
public function getMediaPathAttribute() {
return 'social/'.$this->slug.'/';
}
public function getIconAttribute(): string
{
return "{$this->type}-slider";
}
public function beforeDelete() {
$this->posts->each->delete();
MediaLibrary::instance()->deleteFolder($this->mediaPath);
}
public static function select($type = null): array
{
if (is_null($type)) {
return static::get()->pluck('name', 'id')->toArray();
}
return static::whereType($type)->get()->pluck('name', 'id')->toArray();
}
public static function forSelect(): array
{
return static::orderByRaw('type, name')
->selectRaw('id, CONCAT(name, " (", type, ")") AS name')
->pluck('name', 'id')
->toArray();
}
}