PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / trunk
VikAppointments Services Booking Calendar vtrunk
trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / models / reportsser.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 5 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 5 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 5 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 5 months ago reportsser.php 5 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
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