| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Models; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use FluentSupport\App\Services\EmailNotification\Settings; |
| 7 |
use FluentSupport\App\Services\Helper; |
| 8 |
use FluentSupport\Framework\Support\Arr; |
| 9 |
|
| 10 |
class MailBox extends Model |
| 11 |
{ |
| 12 |
protected $table = 'fs_mail_boxes'; |
| 13 |
|
| 14 |
protected $appends = ['settings']; |
| 15 |
|
| 16 |
/** |
| 17 |
* The attributes that are mass assignable. |
| 18 |
* |
| 19 |
* @var array |
| 20 |
*/ |
| 21 |
protected $fillable = [ |
| 22 |
'name', |
| 23 |
'slug', |
| 24 |
'email', |
| 25 |
'box_type', |
| 26 |
'mapped_email', |
| 27 |
'email_footer', |
| 28 |
'settings', |
| 29 |
'avatar', |
| 30 |
'created_by', |
| 31 |
'is_default' |
| 32 |
]; |
| 33 |
|
| 34 |
public static function boot() |
| 35 |
{ |
| 36 |
parent::boot(); |
| 37 |
|
| 38 |
static::creating(function ($model) { |
| 39 |
$model->slug = static::slugify($model->name); |
| 40 |
$model->settings = $model->settings ?: [ |
| 41 |
'admin_email_address' => '' |
| 42 |
]; |
| 43 |
}); |
| 44 |
} |
| 45 |
|
| 46 |
public function setSettingsAttribute($settings) |
| 47 |
{ |
| 48 |
$this->attributes['settings'] = \maybe_serialize($settings); |
| 49 |
} |
| 50 |
|
| 51 |
public function getSettingsAttribute($value) |
| 52 |
{ |
| 53 |
if (array_key_exists('settings', $this->attributes)) { |
| 54 |
return Helper::safeUnserialize($this->attributes['settings']); |
| 55 |
} |
| 56 |
|
| 57 |
return []; |
| 58 |
} |
| 59 |
|
| 60 |
public static function slugify($title) |
| 61 |
{ |
| 62 |
$slug = sanitize_title($title, 'business', 'display'); |
| 63 |
if (MailBox::where('slug', $slug)->first()) { |
| 64 |
$slug .= '-' . time(); |
| 65 |
} |
| 66 |
return $slug; |
| 67 |
} |
| 68 |
|
| 69 |
public function getMailerHeader() |
| 70 |
{ |
| 71 |
$headers = []; |
| 72 |
$fromString = $this->email; //$this->name; |
| 73 |
if($this->name) { |
| 74 |
$fromString = $this->name.' <'.$fromString.'>'; |
| 75 |
} |
| 76 |
|
| 77 |
if ($fromString) { |
| 78 |
$headers[] = 'From: '. $fromString; |
| 79 |
} |
| 80 |
|
| 81 |
// Set Reply-To Header |
| 82 |
$headers[] = 'Reply-To: '. $fromString; |
| 83 |
$headers[] = 'In-Reply-To: '. $fromString; |
| 84 |
|
| 85 |
return $headers; |
| 86 |
} |
| 87 |
|
| 88 |
public function getMeta($key, $default = '') |
| 89 |
{ |
| 90 |
$class = __NAMESPACE__ . '\MailBox'; |
| 91 |
|
| 92 |
$meta = Meta::where('object_type', $class) |
| 93 |
->where('object_id', $this->id) |
| 94 |
->where('key', $key) |
| 95 |
->first(); |
| 96 |
|
| 97 |
if($meta) { |
| 98 |
return Helper::safeUnserialize($meta->value); |
| 99 |
} |
| 100 |
|
| 101 |
return $default; |
| 102 |
} |
| 103 |
|
| 104 |
public function saveMeta($key, $value) |
| 105 |
{ |
| 106 |
$class = __NAMESPACE__ . '\MailBox'; |
| 107 |
|
| 108 |
$meta = Meta::where('object_type', $class) |
| 109 |
->where('object_id', $this->id) |
| 110 |
->where('key', $key) |
| 111 |
->first(); |
| 112 |
|
| 113 |
if($meta) { |
| 114 |
$meta->value = maybe_serialize($value); |
| 115 |
$meta->save(); |
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
Meta::insert([ |
| 120 |
'object_type' => $class, |
| 121 |
'object_id' => $this->id, |
| 122 |
'key' => $key, |
| 123 |
'value' => maybe_serialize($value) |
| 124 |
]); |
| 125 |
return true; |
| 126 |
} |
| 127 |
|
| 128 |
public function deleteMeta($key) |
| 129 |
{ |
| 130 |
$class = __NAMESPACE__ . '\MailBox'; |
| 131 |
|
| 132 |
$meta = Meta::where('object_type', $class) |
| 133 |
->where('object_id', $this->id) |
| 134 |
->where('key', $key) |
| 135 |
->delete(); |
| 136 |
} |
| 137 |
|
| 138 |
public function deleteAllMeta() |
| 139 |
{ |
| 140 |
$class = __NAMESPACE__ . '\MailBox'; |
| 141 |
|
| 142 |
$meta = Meta::where('object_type', $class) |
| 143 |
->where('object_id', $this->id) |
| 144 |
->delete(); |
| 145 |
} |
| 146 |
/** |
| 147 |
* This `getMailBox` method is used to get a mailbox by id. |
| 148 |
* @param int $id |
| 149 |
* @return MailBox |
| 150 |
*/ |
| 151 |
public function getMailBox ($id ) |
| 152 |
{ |
| 153 |
return MailBox::findOrFail($id); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* This `createMailBox` method is used to create a new MailBox. |
| 158 |
* @param array $data |
| 159 |
* @return MailBox |
| 160 |
*/ |
| 161 |
public function createMailBox ( $data ) |
| 162 |
{ |
| 163 |
if ($data['box_type'] == 'email') { |
| 164 |
$data['settings'] = [ |
| 165 |
'admin_email_address' => '' |
| 166 |
]; |
| 167 |
} else { |
| 168 |
$data['settings'] = [ |
| 169 |
'admin_email_address' => $data['email'] |
| 170 |
]; |
| 171 |
} |
| 172 |
|
| 173 |
if (!MailBox::first()) { |
| 174 |
$data['is_default'] = 'yes'; |
| 175 |
} |
| 176 |
|
| 177 |
return MailBox::create($data); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
/** |
| 182 |
* This `updateMailBox` method is used to update a mailbox. |
| 183 |
* @param array $data |
| 184 |
* @param int $mailBoxId |
| 185 |
* @return object $mailbox |
| 186 |
*/ |
| 187 |
public function updateMailBox ($data, $mailBoxId ) |
| 188 |
{ |
| 189 |
|
| 190 |
$mailbox = MailBox::findOrFail($mailBoxId); |
| 191 |
|
| 192 |
if ($data['box_type'] == 'email' && empty($data['mapped_email'])) { |
| 193 |
throw new \Exception('Mapped Email Address is required'); |
| 194 |
} |
| 195 |
|
| 196 |
$mailbox->fill($data); |
| 197 |
$mailbox->save(); |
| 198 |
|
| 199 |
return $mailbox; |
| 200 |
} |
| 201 |
} |
| 202 |
|