PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.20
VikAppointments Services Booking Calendar v1.2.20
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / mail / templates / waitlist.php
vikappointments / site / helpers / libraries / mail / templates Last commit date
admin.php 1 month ago cancellation.php 1 month ago customer.php 1 month ago employee.php 1 month ago index.html 1 month ago packadmin.php 1 month ago package.php 1 month ago stock.php 1 month ago waitlist.php 1 month ago
waitlist.php
387 lines
1 <?php
2 /**
3 * @package VikAppointments
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 VAPLoader::import('libraries.mail.template');
15
16 /**
17 * Wrapper used to handle mail notifications for the
18 * customers subscribed to waiting list.
19 *
20 * @since 1.7
21 */
22 class VAPMailTemplateWaitlist implements VAPMailTemplate
23 {
24 /**
25 * The appointment object.
26 * Extracted from VAPOrderAppointment instance.
27 *
28 * @var object
29 */
30 protected $appointment;
31
32 /**
33 * The waiting list record.
34 *
35 * @var object
36 */
37 protected $record;
38
39 /**
40 * The check-in date (military format)
41 *
42 * @var string
43 */
44 protected $date;
45
46 /**
47 * The cancelled check-in times (in minutes).
48 *
49 * @var array
50 */
51 protected $times;
52
53 /**
54 * An optional template file to use.
55 *
56 * @var string
57 */
58 protected $templateFile;
59
60 /**
61 * An array of options.
62 *
63 * @var array
64 */
65 protected $options;
66
67 /**
68 * Class constructor.
69 *
70 * @param object $appointment The appointment details object.
71 * @param array $record The waiting list record to notify.
72 * @param string $date The check-in date.
73 * @param array $times A list of check-in times.
74 * @param array $options An array of options.
75 */
76 public function __construct($appointment, $record, $date, $times, array $options = array())
77 {
78 if (empty($options['lang']))
79 {
80 // use default site language
81 $options['lang'] = VikAppointments::getDefaultLanguage();
82 }
83
84 // use appointment as provided
85 $this->appointment = $appointment;
86
87 // save waiting list record
88 $this->record = (object) $record;
89
90 // save check-in date
91 $this->date = $date;
92
93 // save check-in times
94 $this->times = $times;
95
96 // register options
97 $this->options = $options;
98
99 // load given language to translate template contents
100 VikAppointments::loadLanguage($this->options['lang'], JPATH_SITE);
101 }
102
103 /**
104 * Returns the code of the template before being parsed.
105 *
106 * @param string $file An optional template file to use. If not specified,
107 * the one set in configuration will be used.
108 *
109 * @return void
110 */
111 public function setFile($file)
112 {
113 // use specified template file
114 $this->templateFile = $file;
115
116 // check if a filename or a path was passed
117 if ($file && !is_file($file))
118 {
119 // make sure we have a valid file path
120 $this->templateFile = VAPHELPERS . DIRECTORY_SEPARATOR . 'mail_tmpls' . DIRECTORY_SEPARATOR . $file;
121 }
122 }
123
124 /**
125 * Returns the code of the template before being parsed.
126 *
127 * @return string
128 */
129 public function getTemplate()
130 {
131 // copy appointment details in a local variable for being used directly
132 // within the template file
133 $appointment = $this->appointment;
134
135 if ($this->templateFile)
136 {
137 // use specified template file
138 $file = $this->templateFile;
139 }
140 else
141 {
142 // get template file from configuration
143 $file = VAPFactory::getConfig()->get('waitlistmailtmpl');
144
145 // build template path
146 $file = VAPHELPERS . DIRECTORY_SEPARATOR . 'mail_tmpls' . DIRECTORY_SEPARATOR . $file;
147 }
148
149 // make sure the file exists
150 if (!is_file($file))
151 {
152 // missing file, return empty string
153 return '';
154 }
155
156 // start output buffering
157 ob_start();
158 // include file to catch its contents
159 include $file;
160 // write template contents within a variable
161 $content = ob_get_contents();
162 // clear output buffer
163 ob_end_clean();
164
165 // free space
166 unset($order);
167
168 return $content;
169 }
170
171 /**
172 * Fetches the subject to be used in the e-mail.
173 *
174 * @return string
175 */
176 public function getSubject()
177 {
178 // get company name
179 $fromname = VAPFactory::getConfig()->getString('agencyname');
180
181 // fetch subject
182 $subject = JText::sprintf('VAPWAITLISTEMAILSUBJECT', $fromname);
183
184 // let plugins manipulate the subject for this e-mail template
185 $res = VAPMailFactory::letPluginsManipulateMail('waitlist', 'subject', $subject, $this->appointment, $this->record, $this->date, $this->times);
186
187 if ($res === false)
188 {
189 // a plugin prevented the e-mail sending
190 return '';
191 }
192
193 return $subject;
194 }
195
196 /**
197 * Parses the HTML of the template and returns it.
198 *
199 * @return string
200 */
201 public function getHtml()
202 {
203 $config = VAPFactory::getConfig();
204
205 // load template HTML
206 $tmpl = $this->getTemplate();
207
208 if ($this->appointment->service->id == $this->record->id_service)
209 {
210 // the customer did a cancellation for the same service
211 $service = $this->appointment->service;
212 }
213 else
214 {
215 // the customer did the cancellation for a different service,
216 // we need to fetch the details of the service for which this
217 // user subscribed into the waiting list
218 $service = JModelVAP::getInstance('service')->getItem($this->record->id_service);
219
220 if (!$service)
221 {
222 return false;
223 }
224 }
225
226 // Format check-in date. Force UTC timezone because the date should be
227 // displayed as it is.
228 $formatted_date = JHtml::fetch('date', $this->date, JText::translate('DATE_FORMAT_LC1'), 'UTC');
229
230 $formatted_times = array();
231
232 foreach ($this->times as $time)
233 {
234 // convert minutes in time
235 $formatted_times[] = JHtml::fetch('vikappointments.min2time', $time, true);
236 }
237
238 $vik = VAPApplication::getInstance();
239
240 // create link to access the service details page
241 $date = JHtml::fetch('date', $this->date, $config->get('dateformat'), 'UTC');
242 $url = "index.php?option=com_vikappointments&view=servicesearch&id_service={$service->id}&date={$date}";
243
244 if ($this->appointment->viewEmp > 0)
245 {
246 // include employee ID too, if selectable
247 $url .= '&id_emp=' . $this->appointment->employee->id;
248 }
249
250 $details_link_href = $vik->routeForExternalUse($url);
251
252 // create link to unsubscribe from waiting list
253 $unsubscribe_link_href = $vik->routeForExternalUse('index.php?option=com_vikappointments&view=unsubscr_waiting_list');
254
255 // fetch company logo image
256 $logo_str = $config->get('companylogo');
257
258 if ($logo_str && is_file(VAPMEDIA . DIRECTORY_SEPARATOR . $logo_str))
259 {
260 $logo_str = JHtml::fetch('vaphtml.media.display', $logo_str, [
261 'alt' => $config->get('agencyname'),
262 'small' => false,
263 'style' => 'max-width: 100%;',
264 ]);
265 }
266 else
267 {
268 $logo_str = '';
269 }
270
271 // build placeholders lookup
272 $placeholders = array(
273 'company_name' => $config->get('agencyname'),
274 'service' => $service->name,
275 'checkin_day' => $formatted_date,
276 'checkin_time' => $formatted_times[0],
277 'checkin_times' => implode(', ', $formatted_times),
278 'details_link' => $details_link_href,
279 'unsubscribe_link' => $unsubscribe_link_href,
280 'logo' => $logo_str,
281 );
282
283 // parse e-mail template placeholders
284 foreach ($placeholders as $tag => $value)
285 {
286 $tmpl = str_replace("{{$tag}}", $value, $tmpl);
287 }
288
289 // let plugins manipulate the content for this e-mail template
290 $res = VAPMailFactory::letPluginsManipulateMail('waitlist', 'content', $tmpl, $this->appointment, $this->record, $this->date, $this->times);
291
292 if ($res === false)
293 {
294 // a plugin prevented the e-mail sending
295 return '';
296 }
297
298 return $tmpl;
299 }
300
301 /**
302 * Sends the HTML contents via e-mail.
303 *
304 * @return boolean
305 */
306 public function send()
307 {
308 $config = VAPFactory::getConfig();
309
310 // get recipient from order detail
311 $recipient = $this->record->email;
312
313 if (!$recipient)
314 {
315 // missing recipient
316 return false;
317 }
318
319 // get administrator e-mail
320 $adminmail = VikAppointments::getAdminMail();
321 // get sender e-mail address
322 $sendermail = VikAppointments::getSenderMail();
323 // get company name
324 $fromname = $config->getString('agencyname');
325
326 /**
327 * Use the reply-to address specified from the global configuration.
328 *
329 * @since 1.7.7
330 */
331 $replyto = $config->get('replytoemail', null);
332
333 // fetch subject
334 $subject = $this->getSubject();
335
336 // parse e-mail template
337 $html = $this->getHtml();
338
339 if (empty($subject) || empty($html))
340 {
341 // do not send e-mail in case the subject or
342 // the content are empty
343 return false;
344 }
345
346 $attachments = array();
347
348 // let plugins manipulate the attachments for this e-mail template
349 $res = VAPMailFactory::letPluginsManipulateMail('waitlist', 'attachment', $attachments, $this->appointment, $this->record, $this->date, $this->times);
350
351 // send the e-mail notification
352 $sent = VAPApplication::getInstance()->sendMail(
353 $sendermail,
354 $fromname,
355 $recipient,
356 $replyto,
357 $subject,
358 $html,
359 array_keys($attachments)
360 );
361
362 // destroy any temporary attachment here
363 foreach ($attachments as $file => $keep)
364 {
365 // check if the file exists and it should be deleted
366 if (!$keep && is_file($file))
367 {
368 // permanently delete file
369 unlink($file);
370 }
371 }
372
373 return $sent;
374 }
375
376 /**
377 * Checks whether the notification should be sent.
378 *
379 * @return boolean
380 */
381 public function shouldSend()
382 {
383 // should send only in case the e-mail was specified
384 return !empty($this->record->email);
385 }
386 }
387