90 lines
2.1 KiB
PHP
90 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\GeneralConfigType;
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Mail\SendContactMail;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Kwaadpepper\Enum\Traits\CastsEnums;
|
|
|
|
class Contact extends Model
|
|
{
|
|
use HasFactory;
|
|
use CastsEnums;
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array
|
|
*/
|
|
public $casts = [
|
|
'done' => 'bool'
|
|
];
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'firstname',
|
|
'lastname',
|
|
'subject',
|
|
'email',
|
|
'message',
|
|
'order_number',
|
|
'file',
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted()
|
|
{
|
|
// * EVENT CLOSURES.
|
|
static::creating(function (self $model) {
|
|
// * Store joined file.
|
|
if ($model->file) {
|
|
$model->file = FileStorageHelper::storeFile($model, $model->file, true);
|
|
}
|
|
static::sendEmailContact($model);
|
|
});
|
|
static::deleted(function (self $model) {
|
|
if ($model->file) {
|
|
FileStorageHelper::removeFile($model, 'file');
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Send the email contact.
|
|
*
|
|
* @param \App\Models\Contact $model
|
|
* @return void
|
|
*/
|
|
public function sendEmailContact(Contact $model)
|
|
{
|
|
$toMail = GeneralConfig::query()
|
|
->where('type', GeneralConfigType::global())
|
|
->firstOrFail()
|
|
->data->contact_us_destination_email->value ?? config('mail.from.address');
|
|
Mail::to($toMail)->send(new SendContactMail($model));
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* BelongsTo order relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function order()
|
|
{
|
|
return $this->belongsTo(Order::class, 'order_number', 'reference');
|
|
}
|
|
}
|