| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Models; |
| 4 |
|
| 5 |
use FluentCommunity\App\Models\XProfile; |
| 6 |
|
| 7 |
/** |
| 8 |
* Meta Model - DB Model for Notifications table |
| 9 |
* |
| 10 |
* Database Model |
| 11 |
* |
| 12 |
* @package FluentCommunity\App\Models |
| 13 |
* |
| 14 |
* @version 1.0.0 |
| 15 |
*/ |
| 16 |
class Media extends Model |
| 17 |
{ |
| 18 |
protected $table = 'fcom_media_archive'; |
| 19 |
|
| 20 |
protected $primaryKey = 'id'; |
| 21 |
|
| 22 |
protected $guarded = ['id']; |
| 23 |
|
| 24 |
protected $appends = ['public_url']; |
| 25 |
|
| 26 |
protected $fillable = [ |
| 27 |
'object_source', |
| 28 |
'user_id', |
| 29 |
'media_key', |
| 30 |
'feed_id', |
| 31 |
'is_active', |
| 32 |
'sub_object_id', |
| 33 |
'media_type', |
| 34 |
'driver', |
| 35 |
'media_path', |
| 36 |
'media_url', |
| 37 |
'settings' |
| 38 |
]; |
| 39 |
|
| 40 |
public static function boot() |
| 41 |
{ |
| 42 |
parent::boot(); |
| 43 |
|
| 44 |
static::creating(function ($model) { |
| 45 |
if (!isset($model->user_id)) { |
| 46 |
$model->user_id = get_current_user_id(); |
| 47 |
} |
| 48 |
|
| 49 |
$model->media_key = $model->media_key ?: md5($model->media_url . '_' . time()); |
| 50 |
}); |
| 51 |
|
| 52 |
static::deleting(function ($media) { |
| 53 |
$media->deleteFile(); |
| 54 |
}); |
| 55 |
} |
| 56 |
|
| 57 |
public function scopeBySource($query, $sources = []) |
| 58 |
{ |
| 59 |
if (!empty($sources)) { |
| 60 |
$query->whereIn('object_source', $sources); |
| 61 |
} |
| 62 |
|
| 63 |
return $query; |
| 64 |
} |
| 65 |
|
| 66 |
public function scopeByMediaKey($query, $key) |
| 67 |
{ |
| 68 |
return $query->where('media_key', $key); |
| 69 |
} |
| 70 |
|
| 71 |
public function scopeByUser($query, $userId) |
| 72 |
{ |
| 73 |
if ($userId) { |
| 74 |
return $query->where('user_id', $userId); |
| 75 |
} |
| 76 |
|
| 77 |
return $query; |
| 78 |
} |
| 79 |
|
| 80 |
public function setSettingsAttribute($value) |
| 81 |
{ |
| 82 |
$this->attributes['settings'] = maybe_serialize($value); |
| 83 |
} |
| 84 |
|
| 85 |
public function getSettingsAttribute($value) |
| 86 |
{ |
| 87 |
$settings = maybe_unserialize($value); |
| 88 |
|
| 89 |
if (!$settings) { |
| 90 |
$settings = []; |
| 91 |
} |
| 92 |
|
| 93 |
return $settings; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
public function feed() |
| 98 |
{ |
| 99 |
return $this->belongsTo(Feed::class, 'feed_id'); |
| 100 |
} |
| 101 |
|
| 102 |
public function user() |
| 103 |
{ |
| 104 |
return $this->belongsTo(User::class, 'user_id'); |
| 105 |
} |
| 106 |
|
| 107 |
public function xprofile() |
| 108 |
{ |
| 109 |
return $this->belongsTo(XProfile::class, 'user_id', 'user_id'); |
| 110 |
} |
| 111 |
|
| 112 |
public function getPublicUrlAttribute() |
| 113 |
{ |
| 114 |
return apply_filters('fluent_community/media_public_url_' . $this->driver, $this->media_url, $this); |
| 115 |
} |
| 116 |
|
| 117 |
public function deleteFile() |
| 118 |
{ |
| 119 |
if ($this->driver == 'local') { |
| 120 |
if (file_exists($this->media_path)) { |
| 121 |
wp_delete_file($this->media_path); |
| 122 |
} |
| 123 |
} else { |
| 124 |
do_action('fluent_community/delete_remote_media_' . $this->driver, $this); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|