PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / trunk
VikAppointments Services Booking Calendar vtrunk
trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / controllers / import.php
vikappointments / admin / controllers Last commit date
analytics.php 4 years ago apiban.php 4 years ago apilog.php 4 years ago apiplugin.php 4 years ago apiuser.php 4 years ago backup.php 4 years ago calendar.php 4 years ago city.php 4 years ago closure.php 1 month ago configapp.php 4 years ago configcldays.php 2 years ago configcron.php 4 years ago configemp.php 4 years ago configsmsapi.php 4 years ago configuration.php 1 month ago conversion.php 1 year ago country.php 4 years ago coupon.php 4 years ago coupongroup.php 4 years ago cronjob.php 2 years ago cronjoblog.php 4 years ago customer.php 4 months ago customf.php 1 year ago dashboard.php 4 years ago emplocwdays.php 4 years ago employee.php 1 year ago emprates.php 4 years ago export.php 4 years ago exportres.php 4 years ago file.php 4 months ago findreservation.php 1 month ago group.php 4 years ago import.php 4 years ago index.html 4 years ago invoice.php 1 month ago langcustomf.php 4 years ago langemployee.php 4 years ago langgroup.php 4 years ago langmedia.php 4 years ago langoption.php 4 years ago langoptiongroup.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 4 years ago location.php 4 years ago mailtext.php 2 years ago makerecurrence.php 1 month ago media.php 4 years ago multiorder.php 4 years ago option.php 4 months ago optiongroup.php 4 years ago package.php 2 years ago packgroup.php 4 years ago packorder.php 1 year ago payment.php 4 years ago rate.php 4 years ago reportsemp.php 4 years ago reportsser.php 4 years ago reservation.php 1 month ago restriction.php 4 years ago review.php 4 years ago service.php 1 year ago serworkday.php 4 months ago state.php 4 years ago statuscode.php 4 years ago subscription.php 4 years ago subscrorder.php 4 years ago tag.php 4 years ago tax.php 4 years ago usernote.php 4 years ago waitinglist.php 4 years ago webhook.php 4 years ago wizard.php 1 year ago
import.php
292 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.controllers.admin');
15
16 /**
17 * VikAppointments import controller.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsControllerImport extends VAPControllerAdmin
22 {
23 /**
24 * Task used to access the creation page of a new record.
25 *
26 * @return boolean
27 */
28 public function add()
29 {
30 $app = JFactory::getApplication();
31 $user = JFactory::getUser();
32
33 // check user permissions
34 if (!$user->authorise('core.create', 'com_vikappointments'))
35 {
36 // back to main list, not authorised to create records
37 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
38 $this->cancel();
39
40 return false;
41 }
42
43 $type = $app->input->get('import_type', '', 'string');
44
45 $url = 'index.php?option=com_vikappointments&view=manageimport&import_type=' . $type;
46
47 $args = $app->input->get('import_args', array(), 'array');
48
49 if ($args)
50 {
51 $url .= '&' . http_build_query(array('import_args' => $args));
52 }
53
54 $this->setRedirect($url);
55
56 return true;
57 }
58
59 /**
60 * Task used to save the record data set in the request.
61 * After saving, the user is redirected to the management
62 * page of the record that has been saved.
63 *
64 * @return boolean
65 */
66 public function save()
67 {
68 $app = JFactory::getApplication();
69 $input = $app->input;
70 $user = JFactory::getUser();
71
72 /**
73 * Added token validation.
74 *
75 * @since 1.7
76 */
77 if (!JSession::checkToken())
78 {
79 // back to main list, missing CSRF-proof token
80 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
81 $this->cancel();
82
83 return false;
84 }
85
86 // check user permissions
87 if (!$user->authorise('core.create', 'com_vikappointments'))
88 {
89 // back to main list, not authorised to create/edit records
90 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
91 $this->cancel();
92
93 return false;
94 }
95
96 $args = array();
97 $args['assoc'] = $input->get('column', array(), 'array');
98 $args['type'] = $input->getString('import_type');
99 $args['args'] = $input->get('import_args', array(), 'array');
100
101 // get import model
102 $import = $this->getModel();
103
104 // try to import records
105 $result = $import->save($args);
106
107 // go ahead only in case of result
108 if ($result)
109 {
110 // display number of imported records
111 $app->enqueueMessage(
112 JText::sprintf('VAPIMPORTRECORDSADDED', $result['count'], $result['total']),
113 $result['count'] ? 'success' : 'error'
114 );
115
116 // look for any registered errors
117 $errors = $import->getErrors();
118
119 if (count($errors))
120 {
121 /**
122 * Directly display all the messages because the
123 * enqueueMessage() method now seems to use a filter
124 * to strip all blacklisted tags and attributes, and
125 * "onclick" seems to be one of them.
126 *
127 * @since 1.7
128 */
129 $app->enqueueMessage(implode('', $errors), 'error');
130 }
131 }
132
133 // back to import list
134 $this->cancel();
135
136 return true;
137 }
138
139 /**
140 * Task used to upload files via AJAX.
141 *
142 * @return void
143 */
144 public function dropupload()
145 {
146 $input = JFactory::getApplication()->input;
147
148 /**
149 * Added token validation.
150 *
151 * @since 1.7
152 */
153 if (!JSession::checkToken())
154 {
155 // missing CSRF-proof token
156 UIErrorFactory::raiseError(403, JText::translate('JINVALID_TOKEN'));
157 }
158
159 $csv = $input->files->get('source', null, 'array');
160 $type = $input->getString('import_type');
161
162 // get import model
163 $import = $this->getModel();
164
165 // try to upload file
166 $id = $import->upload($type, $csv);
167
168 if ($id === false)
169 {
170 // get string error
171 $error = $import->getError(null, true);
172
173 // something went wrong, raise error
174 UIErrorFactory::raiseError(500, $error);
175 }
176
177 // in case of success, retrieve file properties
178 $resp = AppointmentsHelper::getFileProperties($id);
179
180 // send file info to caller
181 $this->sendJSON($resp);
182 }
183
184 /**
185 * Deletes a list of records set in the request.
186 *
187 * @return boolean
188 */
189 public function delete()
190 {
191 $app = JFactory::getApplication();
192 $user = JFactory::getUser();
193
194 /**
195 * Added token validation.
196 * Both GET and POST are supported.
197 *
198 * @since 1.7
199 */
200 if (!JSession::checkToken() && !JSession::checkToken('get'))
201 {
202 // back to main list, missing CSRF-proof token
203 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
204 $this->cancel();
205
206 return false;
207 }
208
209 $type = $app->input->getString('import_type');
210
211 // check user permissions
212 if (!$user->authorise('core.delete', 'com_vikappointments'))
213 {
214 // back to main list, not authorised to delete records
215 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
216 $this->cancel();
217
218 return false;
219 }
220
221 // delete imported file of specified type
222 $this->getModel()->delete($type);
223
224 // back to main list
225 $this->cancel();
226
227 return true;
228 }
229
230 /**
231 * Downloads a sample file for the requested import type.
232 *
233 * @return void
234 */
235 public function downloadsample()
236 {
237 $app = JFactory::getApplication();
238 $input = $app->input;
239
240 $type = $input->getString('import_type');
241
242 VAPLoader::import('libraries.import.factory');
243 $handler = ImportFactory::getObject($type);
244
245 if (!$handler)
246 {
247 throw new Exception('Import type not supported.', 404);
248 }
249
250 $file = $handler->getSampleFile();
251
252 if ($file === false)
253 {
254 throw new Exception('This type does not own any sample data.', 404);
255 }
256
257 // prepare download headers
258 $app->setHeader('Content-Type', 'application/csv');
259 $app->setHeader('Content-Disposition', 'attachment; filename="' . basename($file) . '"');
260 $app->sendHeaders();
261
262 // read the file at once, because its size should be pretty small
263 readfile($file);
264
265 // terminate request
266 $app->close();
267 }
268
269 /**
270 * Redirects the users to the main records list.
271 *
272 * @return void
273 */
274 public function cancel()
275 {
276 $app = JFactory::getApplication();
277
278 $type = $app->input->get('import_type', '', 'string');
279
280 $url = 'index.php?option=com_vikappointments&view=import&import_type=' . $type;
281
282 $args = $app->input->get('import_args', array(), 'array');
283
284 if ($args)
285 {
286 $url .= '&' . http_build_query(array('import_args' => $args));
287 }
288
289 $this->setRedirect($url);
290 }
291 }
292