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