1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YAWIK |
4
|
|
|
* |
5
|
|
|
* @filesource |
6
|
|
|
* @license MIT |
7
|
|
|
* @copyright https://yawik.org/COPYRIGHT.php |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** */ |
11
|
|
|
namespace Applications\Listener; |
12
|
|
|
|
13
|
|
|
use Applications\Entity\Application; |
14
|
|
|
use Applications\Entity\Status; |
15
|
|
|
use Core\Mail\MailService; |
16
|
|
|
use Applications\Listener\Events\ApplicationEvent; |
17
|
|
|
use Applications\Options\ModuleOptions; |
18
|
|
|
use Organizations\Entity\Employee; |
19
|
|
|
use Organizations\Entity\EmployeeInterface; |
20
|
|
|
use Organizations\Entity\WorkflowSettings; |
21
|
|
|
use Laminas\Mail\AddressList; |
22
|
|
|
use Laminas\Mvc\I18n\Translator; |
23
|
|
|
use Laminas\Session\Container; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Status Change Listener is called by the event \Applications\Listener\Events\ApplicationEvent::EVENT_APPLICATION_STATUS_CHANGE |
27
|
|
|
* |
28
|
|
|
* ${CARET} |
29
|
|
|
* |
30
|
|
|
* @author Bleek Carsten <[email protected]> |
31
|
|
|
* @todo write test |
32
|
|
|
*/ |
33
|
|
|
class StatusChange |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var Application $application |
37
|
|
|
*/ |
38
|
|
|
protected $application; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var MailService $mailService |
42
|
|
|
*/ |
43
|
|
|
protected $mailService; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Translator $translator |
47
|
|
|
*/ |
48
|
|
|
protected $translator; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param ModuleOptions $options |
52
|
|
|
* @param MailService $mailService |
53
|
|
|
* @param Translator $translator |
54
|
|
|
*/ |
55
|
|
|
public function __construct( |
56
|
|
|
ModuleOptions $options, |
57
|
|
|
MailService $mailService, |
58
|
|
|
Translator $translator |
59
|
|
|
) { |
60
|
|
|
$this->options = $options; |
|
|
|
|
61
|
|
|
$this->mailService = $mailService; |
62
|
|
|
$this->translator = $translator; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Prepares the Notification Mail |
68
|
|
|
* |
69
|
|
|
* @param ApplicationEvent $e |
70
|
|
|
*/ |
71
|
|
|
public function prepareFormData(ApplicationEvent $e) |
72
|
|
|
{ |
73
|
|
|
$target = $e->getTarget(); |
74
|
|
|
if ($target->isPostRequest()) { |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->application = $target->getApplicationEntity(); |
79
|
|
|
$status = $target->getStatus(); |
80
|
|
|
$user = $target->getUser(); |
81
|
|
|
$settings = $user->getSettings('Applications'); |
82
|
|
|
|
83
|
|
|
/* @var \Applications\Mail\StatusChange $mail */ |
84
|
|
|
$mail = $this->mailService->get('Applications/StatusChange'); |
85
|
|
|
|
86
|
|
|
switch ($status) { |
87
|
|
|
case Status::CONFIRMED: |
88
|
|
|
$key = 'mailConfirmationText'; |
89
|
|
|
break; |
90
|
|
|
case Status::INVITED: |
91
|
|
|
$key = 'mailInvitationText'; |
92
|
|
|
break; |
93
|
|
|
case Status::ACCEPTED: |
94
|
|
|
$key = 'mailAcceptedText'; |
95
|
|
|
break; |
96
|
|
|
case Status::REJECTED: |
97
|
|
|
$key = 'mailRejectionText'; |
98
|
|
|
break; |
99
|
|
|
default: |
100
|
|
|
throw new \InvalidArgumentException('Unknown status value: ' .$status); |
101
|
|
|
} |
102
|
|
|
$mailText = $settings->$key ? $settings->$key : ''; |
103
|
|
|
$mail->setBody($mailText); |
104
|
|
|
$mail->setApplication($this->application); |
105
|
|
|
$mailText = $mail->getBodyText(); |
106
|
|
|
$mailSubject = sprintf( |
107
|
|
|
$this->translator->translate('Your application dated %s'), |
108
|
|
|
strftime('%x', $this->application->getDateCreated()->getTimestamp()) |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$data = array( |
112
|
|
|
'applicationId' => $this->application->getId(), |
113
|
|
|
'status' => $status, |
114
|
|
|
'mailSubject' => $mailSubject, |
115
|
|
|
'mailText' => $mailText, |
116
|
|
|
'to' => $this->getRecipient($this->application, $status), |
117
|
|
|
); |
118
|
|
|
$target->setFormData($data); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Sends the Notification Mail. |
123
|
|
|
* |
124
|
|
|
* @param ApplicationEvent $event |
125
|
|
|
*/ |
126
|
|
|
public function sendMail(ApplicationEvent $event) |
127
|
|
|
{ |
128
|
|
|
$event = $event->getTarget(); |
129
|
|
|
if (!$event->isPostRequest()) { |
130
|
|
|
return; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$this->application = $event->getApplicationEntity(); |
134
|
|
|
$status = $event->getStatus(); |
135
|
|
|
$user = $event->getUser(); |
136
|
|
|
$post = $event->getPostData(); |
137
|
|
|
|
138
|
|
|
$settings = $user->getSettings('Applications'); |
139
|
|
|
$recipient = $this->getRecipient($this->application, $status); |
140
|
|
|
/* @var \Applications\Mail\StatusChange $mail */ |
141
|
|
|
$mail = $this->mailService->get('Applications/StatusChange'); |
142
|
|
|
|
143
|
|
|
$mail->setSubject($post['mailSubject']); |
144
|
|
|
$mail->setBody($post['mailText']); |
145
|
|
|
$mail->setTo($recipient); |
146
|
|
|
|
147
|
|
|
if ($from = $this->application->getJob()->getContactEmail()) { |
148
|
|
|
$mail->setFrom($from, $this->application->getJob()->getCompany()); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if ($settings->mailBCC) { |
152
|
|
|
$mail->addBcc($user->getInfo()->getEmail(), $user->getInfo()->getDisplayName()); |
153
|
|
|
} |
154
|
|
|
$job = $this->application->getJob(); |
155
|
|
|
$jobUser = $job->getUser(); |
156
|
|
|
if ($jobUser->getId() != $user->getId()) { |
157
|
|
|
$jobUserSettings = $jobUser->getSettings('Applications'); |
158
|
|
|
if ($jobUserSettings->getMailBCC()) { |
159
|
|
|
$mail->addBcc($jobUser->getInfo()->getEmail(), $jobUser->getInfo()->getDisplayName(false)); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$org = $job->getOrganization()->getParent(/*returnSelf*/ true); |
164
|
|
|
$orgUser = $org->getUser(); |
165
|
|
|
if ($orgUser->getId() != $user->getId() && $orgUser->getId() != $jobUser->getId()) { |
166
|
|
|
$orgUserSettings = $orgUser->getSettings('Applications'); |
167
|
|
|
if ($orgUserSettings->getMailBCC()) { |
168
|
|
|
$mail->addBcc($orgUser->getInfo()->getEmail(), $orgUser->getInfo()->getDisplayName(false)); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if ($this->options->getDelayApplicantRejectMail() |
173
|
|
|
&& $status == Status::REJECTED |
174
|
|
|
) { |
175
|
|
|
$this->mailService->queue($mail, [ 'delay' => $this->options->getDelayApplicantRejectMail() ]); |
176
|
|
|
} else { |
177
|
|
|
$this->mailService->send($mail); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
|
181
|
|
|
$historyText = sprintf($this->translator->translate('Mail was sent to %s'), key($recipient) ?: $recipient[0]); |
|
|
|
|
182
|
|
|
$this->application->changeStatus($status, $historyText); |
183
|
|
|
$event->setNotification($historyText); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param Application $application |
188
|
|
|
* @param $status |
189
|
|
|
* |
190
|
|
|
* @return AddressList |
191
|
|
|
*/ |
192
|
|
|
protected function getRecipient(Application $application, $status) |
193
|
|
|
{ |
194
|
|
|
$recipient = Status::ACCEPTED == $status |
195
|
|
|
? $application->getJob()->getUser()->getInfo() |
196
|
|
|
: $application->getContact(); |
197
|
|
|
|
198
|
|
|
$email = $recipient->getEmail(); |
199
|
|
|
$name = $recipient->getDisplayName(false); |
200
|
|
|
|
201
|
|
|
return $name ? [ $email => $name ] : [ $email ]; |
|
|
|
|
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|