2021-10-28 22:35:15 +02:00
|
|
|
<?php namespace Zoomyboy\Social\Models;
|
|
|
|
|
2021-12-17 00:16:39 +01:00
|
|
|
use MediaLibrary;
|
2021-10-28 22:35:15 +02:00
|
|
|
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() {
|
2021-10-30 12:16:27 +02:00
|
|
|
return 'social/'.$this->slug.'/';
|
2021-10-28 22:35:15 +02:00
|
|
|
}
|
|
|
|
|
2021-10-31 10:04:35 +01:00
|
|
|
public function getIconAttribute(): string
|
|
|
|
{
|
|
|
|
return "{$this->type}-slider";
|
|
|
|
}
|
|
|
|
|
2021-10-28 22:35:15 +02:00
|
|
|
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();
|
|
|
|
}
|
2021-10-31 00:55:05 +02:00
|
|
|
|
|
|
|
public static function forSelect(): array
|
|
|
|
{
|
|
|
|
return static::orderByRaw('type, name')
|
|
|
|
->selectRaw('id, CONCAT(name, " (", type, ")") AS name')
|
|
|
|
->pluck('name', 'id')
|
|
|
|
->toArray();
|
|
|
|
}
|
2021-10-28 22:35:15 +02:00
|
|
|
}
|