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 / packadmin.php
vikappointments / site / helpers / libraries / mail / templates Last commit date
admin.php 5 days ago cancellation.php 5 days ago customer.php 5 days ago employee.php 5 days ago index.html 5 days ago packadmin.php 5 days ago package.php 5 days ago stock.php 5 days ago waitlist.php 5 days ago
packadmin.php
389 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 after a package purchase.
20 *
21 * @since 1.7
22 */
23 class VAPMailTemplatePackadmin implements VAPMailTemplate
24 {
25 /**
26 * The order object.
27 *
28 * @var VAPOrderPackage
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::getPackages($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 * @return string
103 */
104 public function getTemplate()
105 {
106 // copy order details in a local variable for being used directly
107 // within the template file
108 $order = $this->order;
109
110 if ($this->templateFile)
111 {
112 // use specified template file
113 $file = $this->templateFile;
114 }
115 else
116 {
117 // get template file from configuration
118 $file = VAPFactory::getConfig()->get('packmailtmpl');
119
120 // build template path
121 $file = VAPHELPERS . DIRECTORY_SEPARATOR . 'mail_tmpls' . DIRECTORY_SEPARATOR . $file;
122 }
123
124 // make sure the file exists
125 if (!is_file($file))
126 {
127 // missing file, return empty string
128 return '';
129 }
130
131 // start output buffering
132 ob_start();
133 // include file to catch its contents
134 include $file;
135 // write template contents within a variable
136 $content = ob_get_contents();
137 // clear output buffer
138 ob_end_clean();
139
140 // free space
141 unset($order);
142
143 return $content;
144 }
145
146 /**
147 * Fetches the subject to be used in the e-mail.
148 *
149 * @return string
150 */
151 public function getSubject()
152 {
153 // get company name
154 $fromname = VAPFactory::getConfig()->getString('agencyname');
155
156 // fetch subject
157 $subject = JText::sprintf('VAPPACKAGESADMINEMAILSUBJECT', $fromname);
158
159 // let plugins manipulate the subject for this e-mail template
160 $res = VAPMailFactory::letPluginsManipulateMail('packadmin', 'subject', $subject, $this->order);
161
162 if ($res === false)
163 {
164 // a plugin prevented the e-mail sending
165 return '';
166 }
167
168 return $subject;
169 }
170
171 /**
172 * Parses the HTML of the template and returns it.
173 *
174 * @return string
175 */
176 public function getHtml()
177 {
178 $config = VAPFactory::getConfig();
179 $currency = VAPFactory::getCurrency();
180
181 // load template HTML
182 $tmpl = $this->getTemplate();
183
184 $payment_name = '';
185 $payment_notes = '';
186
187 // fetch payment name and notes
188 if ($this->order->payment)
189 {
190 // use payment name
191 $payment_name = $this->order->payment->name;
192
193 if ($this->order->statusRole == 'PENDING')
194 {
195 // show notes before purchase when waiting for the payment
196 $payment_notes = $this->order->payment->notes->beforePurchase;
197 }
198 else if ($this->order->statusRole == 'APPROVED')
199 {
200 // show notes after purchase when the order has been confirmed
201 $payment_notes = $this->order->payment->notes->afterPurchase;
202 }
203 }
204
205 // fetch coupon string
206 if ($this->order->coupon)
207 {
208 $coupon_str = $this->order->coupon->code;
209
210 if ($this->order->coupon->amount > 0)
211 {
212 $coupon_str .= ' : ';
213
214 if ($this->order->coupon->type == 1)
215 {
216 $coupon_str .= $this->order->coupon->amount . '%';
217 }
218 else
219 {
220 $coupon_str .= $currency->format($this->order->coupon->amount);
221 }
222 }
223 }
224 else
225 {
226 $coupon_str = '';
227 }
228
229 $vik = VAPApplication::getInstance();
230
231 // fetch order link HREF
232 $order_link_href = "index.php?option=com_vikappointments&view=packagesorder&ordnum={$this->order->id}&ordkey={$this->order->sid}";
233 $order_link_href = $vik->routeForExternalUse($order_link_href);
234
235 // fetch company logo image
236 $logo_str = $config->get('companylogo');
237
238 if ($logo_str && is_file(VAPMEDIA . DIRECTORY_SEPARATOR . $logo_str))
239 {
240 $logo_str = JHtml::fetch('vaphtml.media.display', $logo_str, [
241 'alt' => $config->get('agencyname'),
242 'small' => false,
243 'style' => 'max-width: 100%;',
244 ]);
245 }
246 else
247 {
248 $logo_str = '';
249 }
250
251 // get name chunks
252 $customerNameChunks = preg_split("/\s+/", (string) $this->order->purchaser_nominative);
253
254 // extract last name from the list
255 $customerLastName = array_pop($customerNameChunks);
256 // join remaining chunks into the first name
257 $customerFirstName = implode(' ', $customerNameChunks);
258
259 if (!$customerFirstName)
260 {
261 // only one name provided, use it as first name
262 $customerFirstName = $customerLastName;
263 $customerLastName = '';
264 }
265
266 // build placeholders lookup
267 $placeholders = array(
268 'logo' => $logo_str,
269 'company_name' => $config->get('agencyname'),
270 'order_number' => $this->order->id,
271 'order_key' => $this->order->sid,
272 'order_status' => JHtml::fetch('vaphtml.status.display', $this->order->status),
273 'order_payment' => $payment_name,
274 'order_payment_notes' => $payment_notes,
275 'order_total_cost' => $currency->format($this->order->totals->gross),
276 'order_total_net' => $currency->format($this->order->totals->net),
277 'order_total_tax' => $currency->format($this->order->totals->tax),
278 'order_total_discount' => $currency->format($this->order->totals->discount),
279 'order_coupon_code' => $coupon_str,
280 'order_link' => $order_link_href,
281 'user_name' => $this->order->author ? $this->order->author->name : '',
282 'user_username' => $this->order->author ? $this->order->author->username : '',
283 'user_email' => $this->order->author ? $this->order->author->email : '',
284 'customer_full_name' => $this->order->purchaser_nominative,
285 'customer_first_name' => $customerFirstName,
286 'customer_last_name' => $customerLastName,
287 );
288
289 // parse e-mail template placeholders
290 foreach ($placeholders as $tag => $value)
291 {
292 $tmpl = str_replace("{{$tag}}", $value, $tmpl);
293 }
294
295 // let plugins manipulate the content for this e-mail template
296 $res = VAPMailFactory::letPluginsManipulateMail('packadmin', 'content', $tmpl, $this->order);
297
298 if ($res === false)
299 {
300 // a plugin prevented the e-mail sending
301 return '';
302 }
303
304 return $tmpl;
305 }
306
307 /**
308 * Sends the HTML contents via e-mail.
309 *
310 * @return boolean
311 */
312 public function send()
313 {
314 $config = VAPFactory::getConfig();
315
316 // get administrators e-mail
317 $adminmails = VikAppointments::getAdminMailList();
318 // get sender e-mail address
319 $sendermail = VikAppointments::getSenderMail();
320 // get company name
321 $fromname = $config->getString('agencyname');
322
323 // fetch subject
324 $subject = $this->getSubject();
325
326 // parse e-mail template
327 $html = $this->getHtml();
328
329 if (empty($subject) || empty($html))
330 {
331 // do not send e-mail in case the subject or
332 // the content are empty
333 return false;
334 }
335
336 $attachments = array();
337
338 // let plugins manipulate the attachments for this e-mail template
339 $res = VAPMailFactory::letPluginsManipulateMail('packadmin', 'attachment', $attachments, $this->order);
340
341 $sent = false;
342
343 // make sure the plugin didn't prevent e-mail sending
344 if ($res !== false)
345 {
346 foreach ($adminmails as $recipient)
347 {
348 // send the e-mail notification
349 $sent = VAPApplication::getInstance()->sendMail(
350 $sendermail,
351 $fromname,
352 $recipient,
353 $this->order->purchaser_mail,
354 $subject,
355 $html,
356 array_keys($attachments)
357 ) || $sent;
358 }
359 }
360
361 // destroy any temporary attachment here
362 foreach ($attachments as $file => $keep)
363 {
364 // check if the file exists and it should be deleted
365 if (!$keep && is_file($file))
366 {
367 // permanently delete file
368 unlink($file);
369 }
370 }
371
372 return $sent;
373 }
374
375 /**
376 * Checks whether the notification should be sent.
377 *
378 * @return boolean
379 */
380 public function shouldSend()
381 {
382 // get list of statuses for which the notification should be sent
383 $list = VAPFactory::getConfig()->getArray('mailadminwhen');
384
385 // make sure the order status is contained within the list
386 return in_array($this->order->status, $list);
387 }
388 }
389