1
0
mirror of https://github.com/freescout-helpdesk/freescout.git synced 2024-11-25 20:02:30 +01:00
freescout/app/SendLog.php

178 lines
4.7 KiB
PHP
Raw Normal View History

2018-08-08 09:52:53 +02:00
<?php
/**
* Outgoing emails.
*/
2018-08-08 09:56:03 +02:00
2018-08-08 09:52:53 +02:00
namespace App;
use Illuminate\Database\Eloquent\Model;
class SendLog extends Model
{
/**
* Status of the email sent to the customer or user.
2018-08-08 09:56:03 +02:00
* https://documentation.mailgun.com/en/latest/api-events.html#event-types.
2018-08-08 09:52:53 +02:00
*/
2018-08-08 09:56:03 +02:00
const STATUS_ACCEPTED = 1; // accepted (for delivery)
const STATUS_SEND_ERROR = 2;
const STATUS_DELIVERY_SUCCESS = 4;
const STATUS_DELIVERY_ERROR = 5; // rejected, failed
const STATUS_OPENED = 6;
const STATUS_CLICKED = 7;
const STATUS_UNSUBSCRIBED = 8;
const STATUS_COMPLAINED = 9;
2018-08-08 09:52:53 +02:00
2018-12-09 10:53:36 +01:00
/**
* Status determining successfull sending.
2018-12-13 10:11:15 +01:00
*
2018-12-09 10:53:36 +01:00
* @var [type]
*/
public static $sent_success = [
self::STATUS_ACCEPTED,
self::STATUS_DELIVERY_SUCCESS,
self::STATUS_OPENED,
self::STATUS_CLICKED,
self::STATUS_UNSUBSCRIBED,
self::STATUS_COMPLAINED,
];
/**
* Error statuses.
2018-12-21 10:10:07 +01:00
*
* @var [type]
*/
public static $status_errors = [
self::STATUS_SEND_ERROR,
2018-12-21 10:10:07 +01:00
self::STATUS_DELIVERY_ERROR,
];
2018-08-24 14:24:11 +02:00
/**
* Mail types.
*/
const MAIL_TYPE_EMAIL_TO_CUSTOMER = 1;
const MAIL_TYPE_USER_NOTIFICATION = 2;
2018-11-12 10:12:57 +01:00
const MAIL_TYPE_AUTO_REPLY = 3;
const MAIL_TYPE_INVITE = 4;
const MAIL_TYPE_PASSWORD_CHANGED = 5;
2018-09-02 14:24:35 +02:00
const MAIL_TYPE_WRONG_USER_EMAIL_MESSAGE = 6;
const MAIL_TYPE_TEST = 7;
2018-09-24 15:07:07 +02:00
const MAIL_TYPE_ALERT = 8;
2018-08-24 14:24:11 +02:00
2018-08-08 09:52:53 +02:00
/**
* The attributes that are not mass assignable.
*
* @var array
*/
protected $guarded = ['id'];
/**
* Customer.
*/
public function customer()
{
return $this->belongsTo('App\Customer');
}
/**
* User.
*/
public function user()
{
return $this->belongsTo('App\User');
}
/**
* Thread.
*/
public function thread()
{
return $this->belongsTo('App\Thread');
}
/**
* Save log record.
*/
2018-08-24 14:24:11 +02:00
public static function log($thread_id, $message_id, $email, $mail_type, $status, $customer_id = null, $user_id = null, $status_message = null)
2018-08-08 09:52:53 +02:00
{
$send_log = new self();
$send_log->thread_id = $thread_id;
$send_log->message_id = $message_id;
$send_log->email = $email;
2018-08-24 14:24:11 +02:00
$send_log->mail_type = $mail_type;
2018-08-08 09:52:53 +02:00
$send_log->status = $status;
$send_log->customer_id = $customer_id;
$send_log->user_id = $user_id;
2018-08-20 19:41:51 +02:00
$send_log->status_message = $status_message;
2018-08-08 09:52:53 +02:00
$send_log->save();
return true;
}
2018-08-20 19:41:51 +02:00
/**
* Get name of the status.
*/
public function getStatusName()
{
switch ($this->status) {
case self::STATUS_ACCEPTED:
return __('Accepted for delivery');
case self::STATUS_SEND_ERROR:
return __('Send error');
case self::STATUS_DELIVERY_SUCCESS:
return __('Successfully delivered');
case self::STATUS_DELIVERY_ERROR:
return __('Delivery error');
case self::STATUS_OPENED:
return __('Recipient opened the message');
case self::STATUS_CLICKED:
return __('Recipient clicked a link in the message');
case self::STATUS_UNSUBSCRIBED:
return __('Recipient unsubscribed');
case self::STATUS_COMPLAINED:
return __('Recipient complained');
default:
return __('Unknown');
}
}
public function isErrorStatus()
{
if (in_array($this->status, self::$status_errors)) {
2018-08-20 19:41:51 +02:00
return true;
} else {
return false;
}
}
public function isSuccessStatus()
{
if (in_array($this->status, [self::STATUS_DELIVERY_SUCCESS])) {
return true;
} else {
return false;
}
}
2018-08-25 14:42:33 +02:00
public function getMailTypeName()
{
switch ($this->mail_type) {
case self::MAIL_TYPE_EMAIL_TO_CUSTOMER:
return __('Email to customer');
case self::MAIL_TYPE_USER_NOTIFICATION:
return __('User notification');
case self::MAIL_TYPE_AUTO_REPLY:
return __('Auto reply to customer');
2018-09-02 09:43:45 +02:00
case self::MAIL_TYPE_INVITE:
return __('User invite');
2018-09-02 14:24:35 +02:00
case self::MAIL_TYPE_PASSWORD_CHANGED:
return __('Password changed notification');
2018-08-25 14:42:33 +02:00
case self::MAIL_TYPE_WRONG_USER_EMAIL_MESSAGE:
return __('User using wrong email notification');
case self::MAIL_TYPE_TEST:
return __('Test email');
2018-09-24 15:07:07 +02:00
case self::MAIL_TYPE_ALERT:
return __('Alert email');
2018-08-25 14:42:33 +02:00
}
}
2018-08-08 09:52:53 +02:00
}