| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Models; |
| 4 |
|
| 5 |
use FluentSupport\Framework\Support\Arr; |
| 6 |
|
| 7 |
class MailBox extends Model |
| 8 |
{ |
| 9 |
protected $table = 'fs_mail_boxes'; |
| 10 |
|
| 11 |
protected $appends = ['settings']; |
| 12 |
|
| 13 |
/** |
| 14 |
* The attributes that are mass assignable. |
| 15 |
* |
| 16 |
* @var array |
| 17 |
*/ |
| 18 |
protected $fillable = [ |
| 19 |
'name', |
| 20 |
'slug', |
| 21 |
'email', |
| 22 |
'box_type', |
| 23 |
'mapped_email', |
| 24 |
'email_footer', |
| 25 |
'settings', |
| 26 |
'avatar', |
| 27 |
'created_by', |
| 28 |
'is_default' |
| 29 |
]; |
| 30 |
|
| 31 |
public static function boot() |
| 32 |
{ |
| 33 |
static::creating(function ($model) { |
| 34 |
$model->slug = static::slugify($model->name); |
| 35 |
$model->settings = $model->settings ?: [ |
| 36 |
'admin_email_address' => '' |
| 37 |
]; |
| 38 |
}); |
| 39 |
} |
| 40 |
|
| 41 |
public function setSettingsAttribute($settings) |
| 42 |
{ |
| 43 |
$this->attributes['settings'] = \maybe_serialize($settings); |
| 44 |
} |
| 45 |
|
| 46 |
public function getSettingsAttribute($value) |
| 47 |
{ |
| 48 |
return \maybe_unserialize($this->attributes['settings']); |
| 49 |
} |
| 50 |
|
| 51 |
public static function slugify($title) |
| 52 |
{ |
| 53 |
$slug = sanitize_title($title, 'business', 'display'); |
| 54 |
if (MailBox::where('slug', $slug)->first()) { |
| 55 |
$slug .= '-' . time(); |
| 56 |
} |
| 57 |
return $slug; |
| 58 |
} |
| 59 |
|
| 60 |
public function getMailerHeader() |
| 61 |
{ |
| 62 |
$headers = []; |
| 63 |
$fromString = $this->email; //$this->name; |
| 64 |
if($this->name) { |
| 65 |
$fromString = $this->name.' <'.$fromString.'>'; |
| 66 |
} |
| 67 |
|
| 68 |
if ($fromString) { |
| 69 |
$headers[] = 'From: '. $fromString; |
| 70 |
} |
| 71 |
|
| 72 |
// Set Reply-To Header |
| 73 |
$headers[] = 'Reply-To: '. $fromString; |
| 74 |
|
| 75 |
return $headers; |
| 76 |
} |
| 77 |
|
| 78 |
public function getMeta($key, $default = '') |
| 79 |
{ |
| 80 |
$class = __NAMESPACE__ . '\MailBox'; |
| 81 |
|
| 82 |
$meta = Meta::where('object_type', $class) |
| 83 |
->where('object_id', $this->id) |
| 84 |
->where('key', $key) |
| 85 |
->first(); |
| 86 |
|
| 87 |
if($meta) { |
| 88 |
return maybe_unserialize($meta->value); |
| 89 |
} |
| 90 |
|
| 91 |
return $default; |
| 92 |
} |
| 93 |
|
| 94 |
public function saveMeta($key, $value) |
| 95 |
{ |
| 96 |
$class = __NAMESPACE__ . '\MailBox'; |
| 97 |
|
| 98 |
$meta = Meta::where('object_type', $class) |
| 99 |
->where('object_id', $this->id) |
| 100 |
->where('key', $key) |
| 101 |
->first(); |
| 102 |
|
| 103 |
if($meta) { |
| 104 |
$meta->value = maybe_serialize($value); |
| 105 |
$meta->save(); |
| 106 |
return true; |
| 107 |
} |
| 108 |
|
| 109 |
Meta::insert([ |
| 110 |
'object_type' => $class, |
| 111 |
'object_id' => $this->id, |
| 112 |
'key' => $key, |
| 113 |
'value' => maybe_serialize($value) |
| 114 |
]); |
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
public function deleteMeta($key) |
| 119 |
{ |
| 120 |
$class = __NAMESPACE__ . '\MailBox'; |
| 121 |
|
| 122 |
$meta = Meta::where('object_type', $class) |
| 123 |
->where('object_id', $this->id) |
| 124 |
->where('key', $key) |
| 125 |
->delete(); |
| 126 |
} |
| 127 |
|
| 128 |
public function deleteAllMeta() |
| 129 |
{ |
| 130 |
$class = __NAMESPACE__ . '\MailBox'; |
| 131 |
|
| 132 |
$meta = Meta::where('object_type', $class) |
| 133 |
->where('object_id', $this->id) |
| 134 |
->delete(); |
| 135 |
} |
| 136 |
} |
| 137 |
|