PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / mail / templates / cancellation.php
vikappointments / site / helpers / libraries / mail / templates Last commit date
admin.php 4 days ago cancellation.php 4 days ago customer.php 4 days ago employee.php 4 days ago index.html 4 days ago packadmin.php 4 days ago package.php 4 days ago stock.php 4 days ago waitlist.php 4 days ago
cancellation.php
421 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 VAPLoader::import('libraries.order.factory');
16
17 /**
18 * Wrapper used to handle mail notifications for the
19 * administrators and employees after a cancellation.
20 *
21 * @since 1.7
22 */
23 class VAPMailTemplateCancellation implements VAPMailTemplate
24 {
25 /**
26 * The order object.
27 *
28 * @var VAPOrderAppointment
29 */
30 protected $order;
31
32 /**
33 * An optional template file to use.
34 *
35 * @var string
36 */
37 protected $templateFile;
38
39 /**
40 * An array of options.
41 *
42 * @var array
43 */
44 protected $options;
45
46 /**
47 * Class constructor.
48 *
49 * @param mixed $order Either the order ID or the order object.
50 * @param array $options An array of options.
51 */
52 public function __construct($order, array $options = array())
53 {
54 if (empty($options['lang']))
55 {
56 // always use default language in case it is not specified
57 $options['lang'] = VikAppointments::getDefaultLanguage();
58 }
59
60 if (is_numeric($order))
61 {
62 // recover order details for the given language
63 $this->order = VAPOrderFactory::getAppointments($order, $options['lang']);
64 }
65 else
66 {
67 // use order as provided
68 $this->order = $order;
69 }
70
71 // register options
72 $this->options = $options;
73
74 // load given language to translate template contents
75 VikAppointments::loadLanguage($this->options['lang'], JPATH_SITE);
76 }
77
78 /**
79 * Returns the code of the template before being parsed.
80 *
81 * @param string $file An optional template file to use. If not specified,
82 * the one set in configuration will be used.
83 *
84 * @return void
85 */
86 public function setFile($file)
87 {
88 // use specified template file
89 $this->templateFile = $file;
90
91 // check if a filename or a path was passed
92 if ($file && !is_file($file))
93 {
94 // make sure we have a valid file path
95 $this->templateFile = VAPHELPERS . DIRECTORY_SEPARATOR . 'mail_tmpls' . DIRECTORY_SEPARATOR . $file;
96 }
97 }
98
99 /**
100 * Returns the code of the template before being parsed.
101 *
102 * @param string $who The entity to check (admin or employee).
103 *
104 * @return string
105 */
106 public function getTemplate($who = 'admin')
107 {
108 // copy order details in a local variable for being used directly
109 // within the template file
110 $order = $this->order;
111
112 if (!$this->templateFile)
113 {
114 // get template file from configuration
115 $file = VAPFactory::getConfig()->get('cancmailtmpl');
116
117 // build template path
118 $this->templateFile = VAPHELPERS . DIRECTORY_SEPARATOR . 'mail_tmpls' . DIRECTORY_SEPARATOR . $file;
119 }
120
121 // make sure the file exists
122 if (!is_file($this->templateFile))
123 {
124 // missing file, return empty string
125 return '';
126 }
127
128 // start output buffering
129 ob_start();
130 // include file to catch its contents
131 include $this->templateFile;
132 // write template contents within a variable
133 $content = ob_get_contents();
134 // clear output buffer
135 ob_end_clean();
136
137 // free space
138 unset($order);
139
140 return $content;
141 }
142
143 /**
144 * Fetches the subject to be used in the e-mail.
145 *
146 * @return string
147 */
148 public function getSubject()
149 {
150 // get company name
151 $fromname = VAPFactory::getConfig()->getString('agencyname');
152
153 // fetch subject
154 $subject = JText::sprintf('VAPORDERCANCELEDSUBJECT', $fromname);
155
156 // let plugins manipulate the subject for this e-mail template
157 $res = VAPMailFactory::letPluginsManipulateMail('cancellation', 'subject', $subject, $this->order);
158
159 if ($res === false)
160 {
161 // a plugin prevented the e-mail sending
162 return '';
163 }
164
165 /**
166 * Parse e-mail subject to replace tags with the related order details.
167 *
168 * @since 1.6.6
169 */
170 VikAppointments::parseEmailSubject($subject, $this->order);
171
172 return $subject;
173 }
174
175 /**
176 * Parses the HTML of the template and returns it.
177 *
178 * @param string $who The entity to check (admin or employee).
179 *
180 * @return string
181 */
182 public function getHtml($who = 'admin')
183 {
184 $config = VAPFactory::getConfig();
185 $currency = VAPFactory::getCurrency();
186
187 // load template HTML
188 $tmpl = $this->getTemplate($who);
189
190 // fetch payment name
191 if ($this->order->payment)
192 {
193 // use payment name
194 $payment_name = $this->order->payment->name;
195 }
196 else
197 {
198 $payment_name = '';
199 }
200
201 // fetch cancellation content
202 if ($who == 'admin')
203 {
204 $cancellation_content = JText::translate('VAPORDERCANCELEDCONTENT');
205 }
206 else
207 {
208 $cancellation_content = JText::translate('VAPORDERCANCELEDCONTENTEMP');
209 }
210
211 $vik = VAPApplication::getInstance();
212
213 // fetch order link HREF
214 $order_link_href = "index.php?option=com_vikappointments&view=order&ordnum={$this->order->id}&ordkey={$this->order->sid}";
215 $order_link_href = $vik->routeForExternalUse($order_link_href);
216
217 // fetch company logo image
218 $logo_str = $config->get('companylogo');
219
220 if ($logo_str && is_file(VAPMEDIA . DIRECTORY_SEPARATOR . $logo_str))
221 {
222 $logo_str = JHtml::fetch('vaphtml.media.display', $logo_str, [
223 'alt' => $config->get('agencyname'),
224 'small' => false,
225 'style' => 'max-width: 100%;',
226 ]);
227 }
228 else
229 {
230 $logo_str = '';
231 }
232
233 // get name chunks
234 $customerNameChunks = preg_split("/\s+/", (string) $this->order->purchaser_nominative);
235
236 // extract last name from the list
237 $customerLastName = array_pop($customerNameChunks);
238 // join remaining chunks into the first name
239 $customerFirstName = implode(' ', $customerNameChunks);
240
241 if (!$customerFirstName)
242 {
243 // only one name provided, use it as first name
244 $customerFirstName = $customerLastName;
245 $customerLastName = '';
246 }
247
248 // build placeholders lookup
249 $placeholders = array(
250 'logo' => $logo_str,
251 'company_name' => $config->get('agencyname'),
252 'cancellation_content' => $cancellation_content,
253 'order_payment' => $payment_name,
254 'order_total_cost' => $currency->format($this->order->totals->gross),
255 'order_total_net' => $currency->format($this->order->totals->net),
256 'order_total_tax' => $currency->format($this->order->totals->tax),
257 'order_total_discount' => $currency->format($this->order->totals->discount),
258 'order_link' => $order_link_href,
259 'user_name' => $this->order->author ? $this->order->author->name : '',
260 'user_username' => $this->order->author ? $this->order->author->username : '',
261 'user_email' => $this->order->author ? $this->order->author->email : '',
262 'customer_full_name' => $this->order->purchaser_nominative,
263 'customer_first_name' => $customerFirstName,
264 'customer_last_name' => $customerLastName,
265 );
266
267 // get e-mail custom text model
268 $model = JModelVAP::getInstance('mailtext');
269
270 // set up options
271 $options = $this->options;
272 $options['file'] = $this->templateFile;
273
274 // parse e-mail template
275 $tmpl = $model->parseTemplate($tmpl, $this->order, $options);
276
277 // parse e-mail template placeholders
278 foreach ($placeholders as $tag => $value)
279 {
280 $tmpl = str_replace("{{$tag}}", $value, $tmpl);
281 }
282
283 // let plugins manipulate the content for this e-mail template
284 $res = VAPMailFactory::letPluginsManipulateMail('cancellation', 'content', $tmpl, $this->order);
285
286 if ($res === false)
287 {
288 // a plugin prevented the e-mail sending
289 return '';
290 }
291
292 return $tmpl;
293 }
294
295 /**
296 * Sends the HTML contents via e-mail.
297 *
298 * @return boolean
299 */
300 public function send()
301 {
302 $config = VAPFactory::getConfig();
303
304 // get administrators e-mail
305 $adminmails = VikAppointments::getAdminMailList();
306 // get sender e-mail address
307 $sendermail = VikAppointments::getSenderMail();
308 // get company name
309 $fromname = $config->getString('agencyname');
310
311 // fetch subject
312 $subject = $this->getSubject();
313
314 if (empty($subject))
315 {
316 // do not send e-mail in case the subject or is empty
317 return false;
318 }
319
320 $attachments = array();
321
322 // let plugins manipulate the attachments for this e-mail template
323 $res = VAPMailFactory::letPluginsManipulateMail('cancellation', 'attachment', $attachments, $this->order);
324
325 if ($res === false)
326 {
327 // a plugin prevented the e-mail sending
328 return false;
329 }
330
331 $sent = false;
332
333 if ($this->shouldSend('admin'))
334 {
335 // parse e-mail template
336 $html = $this->getHtml('admin');
337
338 if ($html)
339 {
340 foreach ($adminmails as $recipient)
341 {
342 // send the e-mail notification
343 $sent = VAPApplication::getInstance()->sendMail(
344 $sendermail,
345 $fromname,
346 $recipient,
347 $this->order->purchaser_mail,
348 $subject,
349 $html,
350 array_keys($attachments)
351 ) || $sent;
352 }
353 }
354 }
355
356 /**
357 * Send the e-mail notification to the employee only in case the order doesn't own
358 * several appointments assigned to different employees.
359 *
360 * @since 1.7.9 Make sure the employee owns a valid e-mail address.
361 */
362 if ($this->order->sameEmp && $this->shouldSend('employee') && $this->order->appointments[0]->employee->email)
363 {
364 // parse e-mail template
365 $html = $this->getHtml('employee');
366
367 if ($html)
368 {
369 $sent = VAPApplication::getInstance()->sendMail(
370 $sendermail,
371 $fromname,
372 $this->order->appointments[0]->employee->email,
373 $this->order->purchaser_mail,
374 $subject,
375 $html,
376 array_keys($attachments)
377 ) || $sent;
378 }
379 }
380
381 // destroy any temporary attachment here
382 foreach ($attachments as $file => $keep)
383 {
384 // check if the file exists and it should be deleted
385 if (!$keep && is_file($file))
386 {
387 // permanently delete file
388 unlink($file);
389 }
390 }
391
392 return $sent;
393 }
394
395 /**
396 * Checks whether the notification should be sent.
397 *
398 * @param string $who The entity to check (admin or employee).
399 *
400 * @return boolean
401 */
402 public function shouldSend($who = null)
403 {
404 if (!is_null($who))
405 {
406 // fetch configuration key
407 $key = $who == 'admin' ? 'mailadminwhen' : 'mailempwhen';
408
409 // get list of statuses for which the notification should be sent
410 $list = VAPFactory::getConfig()->getArray('mailadminwhen');
411
412 // make sure the order status is contained within the list
413 return in_array($this->order->status, $list);
414 }
415
416 // Recursively check both the entities in case of no
417 // specified parameter. At least one of them should be ok.
418 return $this->shouldSend('admin') || $this->shouldSend('employee');
419 }
420 }
421