PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.18
VikAppointments Services Booking Calendar v1.2.18
trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / models / mailtext.php
vikappointments / admin / models Last commit date
apiban.php 5 months ago apilog.php 5 months ago apiplugin.php 5 months ago apiuser.php 5 months ago apiuseroptions.php 5 months ago backup.php 5 months ago caldays.php 5 months ago calendar.php 5 months ago city.php 5 months ago closure.php 5 months ago configapp.php 5 months ago configcldays.php 5 months ago configcron.php 5 months ago configemp.php 5 months ago configsmsapi.php 5 months ago configuration.php 5 months ago conversion.php 5 months ago country.php 5 months ago coupon.php 5 months ago couponemployee.php 5 months ago coupongroup.php 5 months ago couponservice.php 5 months ago cronjob.php 5 months ago cronjoblog.php 5 months ago customer.php 5 months ago customf.php 5 months ago customfservice.php 5 months ago customizer.php 5 months ago empgroup.php 5 months ago employee.php 5 months ago empsettings.php 5 months ago file.php 5 months ago findreservation.php 5 months ago group.php 5 months ago import.php 5 months ago index.html 5 months ago invoice.php 5 months ago langcustomf.php 5 months ago langempgroup.php 5 months ago langemployee.php 5 months ago langgroup.php 5 months ago langmedia.php 5 months ago langoption.php 5 months ago langoptiongroup.php 5 months ago langoptionvar.php 5 months ago langpackage.php 5 months ago langpackgroup.php 5 months ago langpayment.php 5 months ago langservice.php 5 months ago langstatuscode.php 5 months ago langsubscr.php 5 months ago langtax.php 5 months ago langtaxrule.php 5 months ago location.php 5 months ago mailtext.php 5 months ago makerecurrence.php 5 months ago media.php 5 months ago multiorder.php 5 months ago option.php 5 months ago optiongroup.php 5 months ago optionvar.php 5 months ago orderstatus.php 5 months ago package.php 5 months ago packageservice.php 5 months ago packgroup.php 5 months ago packorder.php 5 months ago packorderitem.php 5 months ago payment.php 5 months ago rate.php 5 months ago reportsemp.php 5 months ago reportsser.php 5 months ago reservation.php 5 months ago resoptassoc.php 5 months ago restriction.php 5 months ago review.php 5 months ago serempassoc.php 5 months ago seroptassoc.php 5 months ago serrateassoc.php 5 months ago serrestrassoc.php 5 months ago service.php 5 months ago state.php 5 months ago statswidget.php 5 months ago statuscode.php 5 months ago subscription.php 5 months ago subscrorder.php 5 months ago tag.php 5 months ago tax.php 5 months ago taxrule.php 5 months ago updateprogram.php 5 months ago usernote.php 5 months ago waitinglist.php 5 months ago webhook.php 5 months ago worktime.php 5 months 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