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 / admin / models / mailtext.php
vikappointments / admin / models Last commit date
apiban.php 2 days ago apilog.php 2 days ago apiplugin.php 2 days ago apiuser.php 2 days ago apiuseroptions.php 2 days ago backup.php 2 days ago caldays.php 2 days ago calendar.php 2 days ago city.php 2 days ago closure.php 2 days ago configapp.php 2 days ago configcldays.php 2 days ago configcron.php 2 days ago configemp.php 2 days ago configsmsapi.php 2 days ago configuration.php 2 days ago conversion.php 2 days ago country.php 2 days ago coupon.php 2 days ago couponemployee.php 2 days ago coupongroup.php 2 days ago couponservice.php 2 days ago cronjob.php 2 days ago cronjoblog.php 2 days ago customer.php 2 days ago customf.php 2 days ago customfservice.php 2 days ago customizer.php 2 days ago empgroup.php 2 days ago employee.php 2 days ago empsettings.php 2 days ago file.php 2 days ago findreservation.php 2 days ago group.php 2 days ago import.php 2 days ago index.html 2 days ago invoice.php 2 days ago langcustomf.php 2 days ago langempgroup.php 2 days ago langemployee.php 2 days ago langgroup.php 2 days ago langmedia.php 2 days ago langoption.php 2 days ago langoptiongroup.php 2 days ago langoptionvar.php 2 days ago langpackage.php 2 days ago langpackgroup.php 2 days ago langpayment.php 2 days ago langservice.php 2 days ago langstatuscode.php 2 days ago langsubscr.php 2 days ago langtax.php 2 days ago langtaxrule.php 2 days ago location.php 2 days ago mailtext.php 2 days ago makerecurrence.php 2 days ago media.php 2 days ago multiorder.php 2 days ago option.php 2 days ago optiongroup.php 2 days ago optionvar.php 2 days ago orderstatus.php 2 days ago package.php 2 days ago packageservice.php 2 days ago packgroup.php 2 days ago packorder.php 2 days ago packorderitem.php 2 days ago payment.php 2 days ago rate.php 2 days ago reportsemp.php 2 days ago reportsser.php 2 days ago reservation.php 2 days ago resoptassoc.php 2 days ago restriction.php 2 days ago review.php 2 days ago serempassoc.php 2 days ago seroptassoc.php 2 days ago serrateassoc.php 2 days ago serrestrassoc.php 2 days ago service.php 2 days ago state.php 2 days ago statswidget.php 2 days ago statuscode.php 2 days ago subscription.php 2 days ago subscrorder.php 2 days ago tag.php 2 days ago tax.php 2 days ago taxrule.php 2 days ago updateprogram.php 2 days ago usernote.php 2 days ago waitinglist.php 2 days ago webhook.php 2 days ago worktime.php 2 days ago
mailtext.php
248 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.mvc.model');
15
16 /**
17 * VikAppointments e-mail custom text model.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsModelMailtext extends JModelVAP
22 {
23 /**
24 * A list of attachments fetched while parsing the compatible mail texts.
25 *
26 * @var string[]
27 * @since 1.7.4
28 */
29 protected $attachments = [];
30
31 /**
32 * Parses the e-mail custom texts.
33 *
34 * @param string $tmpl The e-mail template (HTML).
35 * @param mixed $order An object containing the order details.
36 * @param array $options An array of options:
37 * - lang string The language tag to use.
38 * - file string The template file to search for.
39 * - id mixed Either an ID or a list of custom texts.
40 * - default bool True to load the default custom fields,
41 * false to use only the specified ID.
42 *
43 * @return string The parsed HTML template.
44 */
45 public function parseTemplate($tmpl, $order, array $options = [])
46 {
47 $dbo = JFactory::getDbo();
48
49 if (empty($options['lang']))
50 {
51 // use order lang tag in case it was not specified
52 $options['lang'] = $order->langtag;
53
54 if (!$options['lang'])
55 {
56 // the order is not assigned to any lang tag, use the current one
57 $options['lang'] = JFactory::getLanguage()->getTag();
58 }
59 }
60
61 $services = array();
62 $employees = array();
63 $payments = array();
64
65 // extract all booked services and employees
66 foreach ($order->appointments as $app)
67 {
68 if (!in_array($app->service->id, $services))
69 {
70 $services[] = $app->service->id;
71 }
72
73 if (!in_array($app->employee->id, $employees))
74 {
75 $employees[] = $app->employee->id;
76 }
77 }
78
79 if ($order->payment)
80 {
81 $payments[] = $order->payment->id;
82 }
83
84 // push 0 to catch all the records that do not specify a service, an employee or a payment
85 array_unshift($services, 0);
86 array_unshift($employees, 0);
87 array_unshift($payments, 0);
88
89 $rows = array();
90
91 // find custom e-mail custom texts
92 $q = $dbo->getQuery(true);
93 $q->select('m.*');
94 $q->from($dbo->qn('#__vikappointments_cust_mail', 'm'));
95
96 /**
97 * Take only published custom texts.
98 *
99 * @since 1.6.5
100 */
101 $q->where($dbo->qn('m.published') . ' = 1');
102
103 // filter by tag
104 $q->where($dbo->qn('m.tag') . ' = ' . $dbo->q($options['lang']));
105
106 // filter by services
107 $q->where($dbo->qn('m.id_service') . ' IN (' . implode(', ', array_map('intval', $services)) . ')');
108
109 // filter by employees
110 $q->where($dbo->qn('m.id_employee') . ' IN (' . implode(', ', array_map('intval', $employees)) . ')');
111
112 /**
113 * Filter by payment method.
114 *
115 * @since 1.7.4
116 */
117 $q->where($dbo->qn('m.id_payment') . ' IN (' . implode(', ', array_map('intval', $payments)) . ')');
118
119 // filter by file
120 if (!empty($options['file']))
121 {
122 /**
123 * Added support to empty files.
124 *
125 * @since 1.6.5
126 */
127 $q->andWhere(array(
128 $dbo->qn('m.file') . ' = ' . $dbo->q(''),
129 $dbo->qn('m.file') . ' = ' . $dbo->q(basename($options['file'])),
130 ), 'OR');
131 }
132
133 /**
134 * Added support to empty statuses.
135 *
136 * @since 1.6.5
137 */
138 $q->andWhere(array(
139 $dbo->qn('status') . ' = ' . $dbo->q(''),
140 $dbo->qn('status') . ' = ' . $dbo->q($order->status),
141 ), 'OR');
142
143 /**
144 * Trigger event to allow the plugins to manipulate the query used to retrieve
145 * the available mail custom texts.
146 *
147 * @param mixed &$query The query string or a query builder object.
148 * @param mixed $order An object containing the order details.
149 * @param array $options An array of options.
150 *
151 * @return void
152 *
153 * @since 1.7
154 */
155 VAPFactory::getEventDispatcher()->trigger('onFetchCompatibleMailTexts', array(&$q, $order, $options));
156
157 /**
158 * In case a specific e-mail text was specified (or more than one),
159 * always take it even if the arguments don't match.
160 *
161 * Do it after executing any plugin hook so that the following code
162 * is applied at the end.
163 *
164 * @since 1.6.5
165 */
166 if (!empty($options['id']))
167 {
168 $default = isset($options['default']) ? (bool) $options['default'] : true;
169
170 // cast to array to support multiple values
171 $options['id'] = (array) $options['id'];
172
173 // check if we should preserve the default conditions
174 if ($default)
175 {
176 // load together with default custom texts
177 $q->orWhere($dbo->qn('id') . ' IN (' . implode(',', array_map('intval', (array) $options['id'])) . ')');
178 }
179 else
180 {
181 // load only the custom text with the specified ID
182 $q->clear('where');
183 $q->where($dbo->qn('id') . ' IN (' . implode(',', array_map('intval', (array) $options['id'])) . ')');
184 }
185 }
186
187 $dbo->setQuery($q);
188 $rows = $dbo->loadObjectList();
189
190 // lookup of available positions
191 $positions = array(
192 '{custom_position_top}' => '',
193 '{custom_position_middle}' => '',
194 '{custom_position_bottom}' => '',
195 '{custom_position_footer}' => '',
196 );
197
198 // always reset the attachments whenever this method is invoked
199 $this->attachments = [];
200
201 // Iterate the records to attach the mail contents.
202 // It is required to replace the placeholders after the iteration
203 // because 2 or more records may share the same position.
204 foreach ($rows as $r)
205 {
206 /**
207 * Render HTML description to interpret attached plugins.
208 *
209 * @since 1.6.3
210 */
211 $r->content = VikAppointments::renderHtmlDescription($r->content, 'custmail');
212
213 // append the content to the existing position
214 $positions[$r->position] .= $r->content;
215
216 /**
217 * Register the configured attachments within a list for later use.
218 *
219 * @since 1.7.4
220 */
221 if ($r->attachments)
222 {
223 $this->attachments = array_merge($this->attachments, (array) json_decode($r->attachments, true));
224 }
225 }
226
227 // replace any existing placeholder
228 foreach ($positions as $k => $v)
229 {
230 $tmpl = str_replace($k, $v, $tmpl);
231 }
232
233 return $tmpl;
234 }
235
236 /**
237 * Returns the compatible e-mail attachments.
238 *
239 * @return string[]
240 *
241 * @since 1.7.4
242 */
243 public function getAttachments()
244 {
245 return $this->attachments;
246 }
247 }
248