1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
use DavideCasiraghi\LaravelEventsCalendar\Models\Event; |
5
|
|
|
use App\User; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\Log; |
8
|
|
|
use Illuminate\Support\Facades\Mail; |
9
|
|
|
use App\Mail\ExpiringEvent; |
10
|
|
|
|
11
|
|
|
use Illuminate\Http\Request; |
12
|
|
|
use Carbon\Carbon; |
13
|
|
|
|
14
|
|
|
class EventExpireAutoMailController extends Controller |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Check if there are expiring repetat events |
18
|
|
|
* and in case send emails to the organizers. |
19
|
|
|
* |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
|
|
public static function check(){ |
23
|
|
|
$activeEvents = Event::getActiveEvents(); |
24
|
|
|
$expiringEventsList = self::getExpiringRepetitiveEventsList($activeEvents); |
25
|
|
|
|
26
|
|
|
if(!empty($expiringEventsList)){ |
27
|
|
|
self::sendEmailToExpiringEventsOrganizers($expiringEventsList); |
28
|
|
|
|
29
|
|
|
$message = count($expiringEventsList).' events were expiring, mails sent to the organizers.'; |
30
|
|
|
} |
31
|
|
|
else{ |
32
|
|
|
$message = 'No events were expiring'; |
33
|
|
|
} |
34
|
|
|
Log::notice($message); |
35
|
|
|
return $message; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Return the list of the expiring repetitive events (the 7th day from now) |
40
|
|
|
* @param array $activeEvents |
41
|
|
|
* @return array $ret |
42
|
|
|
*/ |
43
|
|
|
public static function getExpiringRepetitiveEventsList($activeEvents){ |
44
|
|
|
|
45
|
|
|
$ret = $activeEvents |
46
|
|
|
->where('repeat_until', '<=', Carbon::now()->addWeek()->toDateString()) |
47
|
|
|
->where('repeat_until', '>', Carbon::now()->addWeek()->subDay()->toDateString()) |
48
|
|
|
//->where('repeat_type', '=',2); |
49
|
|
|
->whereIn('repeat_type', [2, 3]); // Weekly(2), Monthly(3), Multiple days(4) |
50
|
|
|
|
51
|
|
|
//dd($ret); |
52
|
|
|
return $ret; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Return the list of the expiring events titles and users |
57
|
|
|
* @param array $expiringEvents |
58
|
|
|
* @return array $ret |
59
|
|
|
*/ |
60
|
|
|
public static function getExpiringEventsTitleAndUser($expiringEvents){ |
61
|
|
|
$ret = []; |
62
|
|
|
foreach ($expiringEvents as $key => $expiringEvent) { |
63
|
|
|
$user = User::find($expiringEvents[$key]['created_by']); |
64
|
|
|
$ret[$key]['user_name'] = $user->name; |
65
|
|
|
$ret[$key]['user_email'] = $user->email; |
66
|
|
|
$ret[$key]['event_title'] = $expiringEvent['title']; |
67
|
|
|
} |
68
|
|
|
return $ret; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Send an email to the events which repetitive events are expiring |
73
|
|
|
* @param array $expiringEvents |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
public static function sendEmailToExpiringEventsOrganizers($expiringEvents){ |
77
|
|
|
$report = []; |
78
|
|
|
|
79
|
|
|
$report['emailFrom'] = env('ADMIN_MAIL'); |
80
|
|
|
$report['senderName'] = 'CI Global Calendar Administrator'; |
81
|
|
|
$report['subject'] = 'CI Global Calendar Administrator'; |
82
|
|
|
|
83
|
|
|
$expiringEventsTitleAndUser = self::getExpiringEventsTitleAndUser($expiringEvents); |
84
|
|
|
//dd($expiringEvents); |
85
|
|
|
foreach ($expiringEventsTitleAndUser as $key => $event) { |
86
|
|
|
$report['user_name'] = $event['user_name']; |
87
|
|
|
$report['emailTo'] = $event['user_email']; |
88
|
|
|
$report['event_title'] = $event['event_title']; |
89
|
|
|
|
90
|
|
|
Mail::send(new ExpiringEvent($report)); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|