PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.20
VikAppointments Services Booking Calendar v1.2.20
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / models / reportsser.php
vikappointments / admin / models Last commit date
apiban.php 1 month ago apilog.php 1 month ago apiplugin.php 1 month ago apiuser.php 1 month ago apiuseroptions.php 1 month ago backup.php 1 month ago caldays.php 1 month ago calendar.php 1 month ago city.php 1 month ago closure.php 1 month ago configapp.php 1 month ago configcldays.php 1 month ago configcron.php 1 month ago configemp.php 1 month ago configsmsapi.php 1 month ago configuration.php 1 month ago conversion.php 1 month ago country.php 1 month ago coupon.php 1 month ago couponemployee.php 1 month ago coupongroup.php 1 month ago couponservice.php 1 month ago cronjob.php 1 month ago cronjoblog.php 1 month ago customer.php 1 month ago customf.php 1 month ago customfservice.php 1 month ago customizer.php 1 month ago empgroup.php 1 month ago employee.php 1 month ago empsettings.php 1 month ago file.php 1 month ago findreservation.php 1 month ago group.php 1 month ago import.php 1 month ago index.html 1 month ago invoice.php 1 month ago langcustomf.php 1 month ago langempgroup.php 1 month ago langemployee.php 1 month ago langgroup.php 1 month ago langmedia.php 1 month ago langoption.php 1 month ago langoptiongroup.php 1 month ago langoptionvar.php 1 month ago langpackage.php 1 month ago langpackgroup.php 1 month ago langpayment.php 1 month ago langservice.php 1 month ago langstatuscode.php 1 month ago langsubscr.php 1 month ago langtax.php 1 month ago langtaxrule.php 1 month ago location.php 1 month ago mailtext.php 1 month ago makerecurrence.php 1 month ago media.php 1 month ago multiorder.php 1 month ago option.php 1 month ago optiongroup.php 1 month ago optionvar.php 1 month ago orderstatus.php 1 month ago package.php 1 month ago packageservice.php 1 month ago packgroup.php 1 month ago packorder.php 1 month ago packorderitem.php 1 month ago payment.php 1 month ago rate.php 1 month ago reportsemp.php 1 month ago reportsser.php 1 month ago reservation.php 1 month ago resoptassoc.php 1 month ago restriction.php 1 month ago review.php 1 month ago serempassoc.php 1 month ago seroptassoc.php 1 month ago serrateassoc.php 1 month ago serrestrassoc.php 1 month ago service.php 1 month ago state.php 1 month ago statswidget.php 1 month ago statuscode.php 1 month ago subscription.php 1 month ago subscrorder.php 1 month ago tag.php 1 month ago tax.php 1 month ago taxrule.php 1 month ago updateprogram.php 1 month ago usernote.php 1 month ago waitinglist.php 1 month ago webhook.php 1 month ago worktime.php 1 month ago
reportsser.php
200 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 services reports model.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsModelReportsser extends JModelVAP
22 {
23 /**
24 * Loads the filters from the input request.
25 *
26 * @return array
27 */
28 public function getFiltersFromRequest()
29 {
30 $app = JFactory::getApplication();
31
32 $filters = array();
33 $filters['datefrom'] = $app->getUserStateFromRequest('vap.reportsser.datefrom', 'datefrom', null, 'string');
34 $filters['dateto'] = $app->getUserStateFromRequest('vap.reportsser.dateto', 'dateto', null, 'string');
35 $filters['valuetype'] = $app->getUserStateFromRequest('vap.reportsser.valuetype', 'valuetype', 'total', 'string');
36 $filters['checkin'] = $app->getUserStateFromRequest('vap.reportsser.checkin', 'checkin', 1, 'uint');
37
38 // obtain employees filter
39 $filters['employees'] = $app->input->get('employees', array(), 'uint');
40
41 if (VAPDateHelper::isNull($filters['datefrom']))
42 {
43 $filters['datefrom'] = null;
44 }
45
46 if (VAPDateHelper::isNull($filters['dateto']))
47 {
48 $filters['dateto'] = null;
49 }
50
51 return $filters;
52 }
53
54 /**
55 * Returns the form data of the specified services.
56 *
57 * @param mixed $id Either a service ID or an array.
58 * @param array $filters An array of filters.
59 *
60 * @return array An array of services.
61 */
62 public function getFormData($id, ?array $filters = null)
63 {
64 $id = (array) $id;
65
66 // load services list
67 $services = array();
68
69 if ($id)
70 {
71 $dbo = JFactory::getDbo();
72
73 $q = $dbo->getQuery(true)
74 ->select($dbo->qn(array('id', 'name')))
75 ->from($dbo->qn('#__vikappointments_service'))
76 ->where($dbo->qn('id') . ' IN (' . implode(',', array_map('intval', $id)) . ')');
77
78 $dbo->setQuery($q);
79 $services = $dbo->loadObjectList();
80 }
81
82 // import statistics framework
83 VAPLoader::import('libraries.statistics.factory');
84
85 if (!$filters)
86 {
87 // load filters from request
88 $filters = $this->getFiltersFromRequest();
89 }
90
91 foreach ($services as $service)
92 {
93 // prepare widget settings
94 $filters['service'] = $service->id;
95 $filters['chart'] = 'line';
96
97 // get widget instance
98 $service->lineChart = VAPStatisticsFactory::getInstance('services_employees_chart', $filters);
99
100 // get widget instance
101 $service->pieChart = VAPStatisticsFactory::getInstance('services_employees_count', $filters);
102 }
103
104 return $services;
105 }
106
107 /**
108 * Downloads the reports of the specified services.
109 *
110 * @param mixed $id Either a service ID or an array.
111 * @param array $filters An array of filters.
112 *
113 * @return void
114 */
115 public function download($id, ?array $filters = null)
116 {
117 // get available widgets
118 $data = $this->getFormData($id, $filters);
119
120 // count number of services
121 $count = count($data);
122
123 if ($count == 0)
124 {
125 // nothing to export here...
126 throw new UnexpectedValueException('Nothing to export', 400);
127 }
128
129 if ($count == 1)
130 {
131 // Directly download the report.
132 // This method automatically terminates the session.
133 $data[0]->lineChart->export();
134 }
135
136 // make sure the class to compress files exists
137 if (!class_exists('ZipArchive'))
138 {
139 // class not found, unable to create archive
140 throw new RuntimeException('The ZipArchive class is not installed on your server.', 500);
141 }
142
143 $files = array();
144
145 // iterate all widgets and save the exported reports in a file
146 foreach ($data as $service)
147 {
148 // save and obtain file path
149 $path = $service->lineChart->export('file');
150
151 // make sure the file exists
152 if (is_file($path))
153 {
154 $files[] = $path;
155 }
156 }
157
158 $app = JFactory::getApplication();
159
160 // obtain temporary folder
161 $path = rtrim($app->get('tmp_path'), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
162 // create ZIP path
163 $zipname = $path . 'reports-' . time() . '.zip';
164
165 // create archive
166 $zip = new ZipArchive;
167 $zip->open($zipname, ZipArchive::CREATE);
168
169 // include all the created report files
170 foreach ($files as $file)
171 {
172 // get readable file name
173 $name = preg_replace("/-[\d]+\.csv$/i", '.csv', basename($file));
174 // include file
175 $zip->addFile($file, $name);
176 }
177
178 // compress archive
179 $zip->close();
180
181 // send headers for download
182 $app->setHeader('Content-Type', 'application/zip');
183 $app->setHeader('Content-Disposition', 'attachment; filename=reports.zip');
184 $app->setHeader('Content-Length', filesize($zipname));
185 $app->sendHeaders();
186
187 // start downloading the file
188 readfile($zipname);
189
190 // delete archive on completion
191 unlink($zipname);
192
193 // delete single reports too
194 foreach ($files as $file)
195 {
196 unlink($file);
197 }
198 }
199 }
200