2019-08-27 15:42:13 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Modules\Emails;
|
|
|
|
|
|
|
|
use Hooks\Manager;
|
|
|
|
use Notifications\EmailNotification;
|
|
|
|
|
|
|
|
class EmailHook extends Manager
|
|
|
|
{
|
2019-08-29 11:25:13 +02:00
|
|
|
public function isSingleton()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function needsExecution()
|
|
|
|
{
|
|
|
|
$diff = date('Y-m-d', strtotime('-4 hours'));
|
|
|
|
$failed = function ($query) use ($diff) {
|
|
|
|
$query->whereDate('failed_at', '<', $diff)
|
|
|
|
->orWhereNull('failed_at');
|
|
|
|
};
|
|
|
|
|
|
|
|
$remaining = Mail::whereNull('sent_at')
|
|
|
|
->where($failed)
|
|
|
|
->count();
|
|
|
|
|
|
|
|
return !empty($remaining);
|
|
|
|
}
|
|
|
|
|
2019-08-27 15:42:13 +02:00
|
|
|
public function execute()
|
|
|
|
{
|
2019-08-29 10:25:14 +02:00
|
|
|
$accounts = Account::all();
|
2019-08-27 15:42:13 +02:00
|
|
|
$diff = date('Y-m-d', strtotime('-4 hours'));
|
|
|
|
|
|
|
|
$list = [];
|
|
|
|
foreach ($accounts as $account) {
|
|
|
|
$mail = Mail::whereNull('sent_at')
|
|
|
|
->where('id_account', $account->id)
|
|
|
|
->whereNull('failed_at')
|
|
|
|
->orWhereDate('failed_at', '<', $diff)
|
|
|
|
->orderBy('created_at')
|
|
|
|
->first();
|
|
|
|
|
|
|
|
if (!empty($mail)) {
|
|
|
|
$list[] = $mail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($list as $mail) {
|
|
|
|
$email = EmailNotification::build($mail);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Invio mail
|
|
|
|
$email->send();
|
|
|
|
} catch (PHPMailer\PHPMailer\Exception $e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 11:25:13 +02:00
|
|
|
return $list;
|
2019-08-27 15:42:13 +02:00
|
|
|
}
|
|
|
|
|
2019-08-29 11:25:13 +02:00
|
|
|
public function response()
|
2019-08-27 15:42:13 +02:00
|
|
|
{
|
|
|
|
$yesterday = date('Y-m-d', strtotime('-1 days'));
|
|
|
|
$user = auth()->getUser();
|
|
|
|
|
2019-08-27 17:25:52 +02:00
|
|
|
$current = Mail::whereDate('sent_at', '>', $yesterday)
|
2019-08-27 15:42:13 +02:00
|
|
|
->where('created_by', $user->id)
|
|
|
|
->count();
|
|
|
|
$total = Mail::whereDate('sent_at', '>', $yesterday)
|
|
|
|
->orWhereNull('sent_at')
|
|
|
|
->where('created_by', $user->id)
|
|
|
|
->count();
|
|
|
|
|
2019-08-29 11:25:13 +02:00
|
|
|
$message = $total != $current ? tr('Invio email in corso...') : tr('Invio email completato!');
|
2019-08-28 11:52:04 +02:00
|
|
|
$message = empty($total) ? tr('Nessuna email presente...') : $message;
|
2019-08-27 15:42:13 +02:00
|
|
|
|
|
|
|
return [
|
|
|
|
'icon' => 'fa fa-envelope text-info',
|
|
|
|
'message' => $message,
|
|
|
|
'show' => true,
|
|
|
|
'progress' => [
|
|
|
|
'current' => $current,
|
|
|
|
'total' => $total,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|