PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / trunk
VikAppointments Services Booking Calendar vtrunk
trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / models / packages.php
vikappointments / site / models Last commit date
allorders.php 2 years ago calendarweek.php 1 month ago cart.php 1 month ago confirmapp.php 1 month ago empaccountstat.php 2 years ago empattachser.php 2 years ago empcoupons.php 2 years ago empcustfields.php 2 years ago empeditcoupon.php 3 years ago empeditcustfield.php 3 years ago empeditlocation.php 3 years ago empeditpay.php 3 years ago empeditprofile.php 4 months ago empeditservice.php 3 years ago empeditwdays.php 3 years ago emplocations.php 2 years ago emplocwdays.php 4 years ago emplogin.php 1 month ago employeesearch.php 1 month ago employeeslist.php 1 month ago empmanres.php 2 years ago emppaylist.php 2 years ago empserviceslist.php 2 years ago empsettingsman.php 4 years ago empsubscrcart.php 4 years ago empsubscrhistory.php 2 years ago empsubscrorder.php 4 months ago empwdays.php 2 years ago index.html 4 years ago packages.php 2 years ago packagescart.php 4 years ago packagesconfirm.php 4 months ago packorders.php 2 years ago servicesearch.php 2 years ago serviceslist.php 2 years ago subscrcart.php 2 years ago subscrhistory.php 2 years ago subscrpayment.php 4 months ago
packages.php
253 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 packages list view model.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsModelPackages extends JModelVAP
22 {
23 /**
24 * Loads a list of packages to be displayed within the
25 * packages list site view.
26 *
27 * @param array $filters
28 *
29 * @return array A list of packages, grouped by category.
30 */
31 public function getItems(array $filters = array())
32 {
33 $dispatcher = VAPFactory::getEventDispatcher();
34
35 $dbo = JFactory::getDbo();
36
37 $groups = array();
38
39 $q = $dbo->getQuery(true);
40
41 $q->select('p.*');
42 $q->select(array(
43 $dbo->qn('g.id', 'group_id'),
44 $dbo->qn('g.title', 'group_title'),
45 $dbo->qn('g.description', 'group_description'),
46 ));
47
48 $q->from($dbo->qn('#__vikappointments_package', 'p'));
49 $q->leftjoin($dbo->qn('#__vikappointments_package_group', 'g') . ' ON ' . $dbo->qn('p.id_group') . ' = ' . $dbo->qn('g.id'));
50
51 // get only the published packages
52 $q->where($dbo->qn('p.published') . ' = 1');
53
54 /**
55 * Retrieve only the packages that belong to the view
56 * access level of the current user.
57 *
58 * @since 1.6
59 */
60 $levels = JFactory::getUser()->getAuthorisedViewLevels();
61
62 if ($levels)
63 {
64 $q->where($dbo->qn('p.level') . ' IN (' . implode(', ', $levels) . ')');
65 }
66
67 if (!empty($filters['id_group']))
68 {
69 // retrieve only the packages that belong to the specified group
70 $q->where($dbo->qn('g.id') . ' = ' . (int) $filters['id_group']);
71 }
72
73 $now = JFactory::getDate();
74
75 // get the packages with a matching start publishing date
76 $q->andWhere(array(
77 $dbo->qn('p.start_ts') . ' IS NULL',
78 $dbo->qn('p.start_ts') . ' = ' . $dbo->q($dbo->getNullDate()),
79 $dbo->qn('p.start_ts') . ' <= ' . $dbo->q($now->toSql()),
80 ), 'OR');
81
82 // get the packages with a matching end publishing date
83 $q->andWhere(array(
84 $dbo->qn('p.end_ts') . ' IS NULL',
85 $dbo->qn('p.end_ts') . ' = ' . $dbo->q($dbo->getNullDate()),
86 $dbo->qn('p.end_ts') . ' > ' . $dbo->q($now->toSql()),
87 ), 'OR');
88
89 $q->order(array(
90 $dbo->qn('g.ordering') . ' ASC',
91 $dbo->qn('p.ordering') . ' ASC',
92 $dbo->qn('p.name') . ' ASC',
93 ));
94
95 /**
96 * Trigger hook to manipulate the query at runtime. Third party plugins
97 * can extend the query by applying further conditions or selecting
98 * additional data.
99 *
100 * @param mixed &$query Either a query builder or a query string.
101 * @param array $filters An array of filters.
102 *
103 * @return void
104 *
105 * @since 1.7
106 */
107 $dispatcher->trigger('onBuildPackagesListQuery', array(&$q, $filters));
108
109 $dbo->setQuery($q);
110
111 if ($rows = $dbo->loadObjectList())
112 {
113 $groups = $this->groupPackages($rows);
114 }
115
116 // translate groups
117 $this->translate($groups);
118
119 /**
120 * Trigger hook to manipulate the query response at runtime. Third party
121 * plugins can alter the resulting list of packages (and groups).
122 *
123 * @param array &$groups An array of groups and the related children.
124 * @param JModel $model The current model.
125 *
126 * @return void
127 *
128 * @since 1.7
129 */
130 $dispatcher->trigger('onBuildPackagesListData', array(&$groups, $this));
131
132 return $groups;
133 }
134
135 /**
136 * Groups the list of packages within parent blocks.
137 * The resulting list will be an array of groups, which
138 * contain the list of children packages.
139 *
140 * The packages with no group will be placed at the end
141 * of the list, within an empty group.
142 *
143 * @param array $packages The list of packages to group.
144 *
145 * @return array The grouped list.
146 */
147 protected function groupPackages(array $packages)
148 {
149 $groups = array();
150
151 foreach ($packages as $p)
152 {
153 // if the package doesn't belong to a group,
154 // the ID will be equals to 0 (as it is casted as INT).
155 $id_group = (int) $p->id_group;
156
157 if (!isset($groups[$id_group]))
158 {
159 $g = new stdClass;
160 $g->id = $p->group_id;
161 $g->title = $p->group_title;
162 $g->description = $p->group_description;
163 $g->packages = array();
164
165 $groups[$id_group] = $g;
166 }
167
168 $groups[$id_group]->packages[] = $p;
169 }
170
171 // check if there is the "uncategorized" group
172 if (isset($groups[0]))
173 {
174 // get the group containing the packages with no group
175 $uncategorized = $groups[0];
176 // unset that group
177 unset($groups[0]);
178 // move that group at the end of the list
179 $groups[0] = $uncategorized;
180 }
181
182 // reset array keys
183 return array_values($groups);
184 }
185
186 /**
187 * Translates the groups and the packages.
188 *
189 * @param array &$rows The rows to translate.
190 *
191 * @return void
192 */
193 protected function translate(&$rows)
194 {
195 /**
196 * Ignore translation in case the multilingual feature is disabled.
197 *
198 * @since 1.7.4
199 */
200 if (VAPFactory::getConfig()->getBool('ismultilang') == false)
201 {
202 return;
203 }
204
205 $langtag = JFactory::getLanguage()->getTag();
206
207 // get translator
208 $translator = VAPFactory::getTranslator();
209
210 $package_ids = array();
211 $group_ids = array();
212
213 foreach ($rows as $group)
214 {
215 $group_ids[] = $group->id;
216
217 foreach ($group->packages as $package)
218 {
219 $package_ids[] = $package->id;
220 }
221 }
222
223 // pre-load packages translations
224 $pkgLang = $translator->load('package', array_unique($package_ids), $langtag);
225 // pre-load packages groups translations
226 $groupLang = $translator->load('packgroup', array_unique($group_ids), $langtag);
227
228 foreach ($rows as $k => $group)
229 {
230 // translate group for the given language
231 $grp_tx = $groupLang->getTranslation($group->id, $langtag);
232
233 if ($grp_tx)
234 {
235 $rows[$k]->title = $grp_tx->title;
236 $rows[$k]->description = $grp_tx->description;
237 }
238
239 foreach ($group->packages as $j => $package)
240 {
241 // translate package for the given language
242 $pkg_tx = $pkgLang->getTranslation($package->id, $langtag);
243
244 if ($pkg_tx)
245 {
246 $rows[$k]->packages[$j]->name = $pkg_tx->name;
247 $rows[$k]->packages[$j]->description = $pkg_tx->description;
248 }
249 }
250 }
251 }
252 }
253