File: /home/xedaptot/ai.naniguide.com/app/Model/TrackingLog.php
<?php
/**
* TrackingLog class.
*
* Model class for delivery log
*
* LICENSE: This product includes software developed at
* the Acelle Co., Ltd. (http://acellemail.com/).
*
* @category MVC Model
*
* @author N. Pham <[email protected]>
* @author L. Pham <[email protected]>
* @copyright Acelle Co., Ltd
* @license Acelle Co., Ltd
*
* @version 1.0
*
* @link http://acellemail.com
*/
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class TrackingLog extends Model
{
use HasFactory;
protected static function newFactory()
{
return \Database\Factories\TrackingLogFactory::new();
}
// Delivery status — set once at send time, never updated afterwards.
// Post-delivery events (bounce, feedback, open, click) are stored in their own tables:
// bounce_logs, feedback_logs, open_logs, click_logs — all joined via message_id.
public const STATUS_SENT = 'sent';
public const STATUS_FAILED = 'failed';
public const STATUS_DUPLICATE = 'duplicate'; // subscriber was a duplicate, email not sent
protected $fillable = ['campaign_id', 'automation_email_id', 'message_id', 'runtime_message_id', 'subscriber_id', 'sending_server_id', 'customer_id', 'status', 'error', 'auto_trigger_id', 'mail_list_id', 'ab_test_variant_id'];
/**
* Associations.
*
* @var object | collect
*/
public function customer()
{
return $this->belongsTo('App\Model\Customer');
}
public function campaign()
{
return $this->belongsTo('App\Model\Campaign');
}
public function automationEmail()
{
return $this->belongsTo('App\Model\AutomationEmail');
}
public function mailList()
{
return $this->belongsTo('App\Model\MailList');
}
public function sendingServer()
{
throw new \Exception("Cross DB reference: tracking log -> sending server");
}
public function getSendingServer()
{
return SendingServer::find($this->sending_server_id);
}
public function subscriber()
{
return $this->belongsTo('App\Model\Subscriber');
}
/**
* Get all items.
*
* @return collect
*/
public static function getAll()
{
return self::select('tracking_logs.*');
}
/**
* Filter items.
*
* @return collect
*/
public static function filter($request)
{
$query = self::select('tracking_logs.*');
$query = $query->leftJoin('subscribers', 'subscribers.id', '=', 'tracking_logs.subscriber_id');
$query = $query->leftJoin('campaigns', 'campaigns.id', '=', 'tracking_logs.campaign_id');
// Cross DB ref, not supported
// $query = $query->leftJoin('sending_servers', 'sending_servers.id', '=', 'tracking_logs.sending_server_id');
$query = $query->leftJoin('customers', 'customers.id', '=', 'tracking_logs.customer_id');
// Keyword
if (!empty(trim($request->keyword))) {
foreach (explode(' ', trim($request->keyword)) as $keyword) {
$query = $query->where(function ($q) use ($keyword) {
$q->orwhere('campaigns.name', 'like', '%'.$keyword.'%')
->orwhere('tracking_logs.status', 'like', '%'.$keyword.'%')
->orwhere('subscribers.email', 'like', '%'.$keyword.'%');
});
}
}
// filters
$filters = $request->all();
if (!empty($filters)) {
if (!empty($filters['campaign_uid'])) {
$query = $query->where('campaigns.uid', '=', $filters['campaign_uid']);
}
}
return $query;
}
public function scopeSent($query)
{
return $query->where('status', self::STATUS_SENT);
}
public function scopeFailed($query)
{
return $query->where('status', self::STATUS_FAILED);
}
/**
* Search items.
*
* @return collect
*/
public static function search($request, $subject = null)
{
$query = self::filter($request);
if ($subject instanceof Campaign) {
$query = $query->where('tracking_logs.campaign_id', '=', $subject->id);
} elseif ($subject instanceof AutomationEmail) {
$query = $query->where('tracking_logs.automation_email_id', '=', $subject->id);
}
$query = $query->orderBy($request->sort_order ?: 'created_at', $request->sort_direction ?: 'desc');
return $query;
}
/**
* Items per page.
*
* @var array
*/
public static $itemsPerPage = 25;
}