PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / trunk
VikAppointments Services Booking Calendar vtrunk
trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / views / import / tmpl / default.php
vikappointments / admin / views / import / tmpl Last commit date
default.php 4 years ago index.html 6 years ago
default.php
279 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 $vik = VAPApplication::getInstance();
15
16 if ($this->file)
17 {
18 $filename = basename($this->file);
19 }
20 else
21 {
22 $filename = '';
23 }
24
25 ?>
26
27 <!-- UPLOAD TARGET -->
28
29 <?php echo $vik->openCard(); ?>
30 <div class="span12">
31 <?php echo $vik->openEmptyFieldset(); ?>
32
33 <div class="vap-media-droptarget" style="position: relative;">
34 <p class="icon">
35 <i class="fas fa-upload" style="font-size: 48px;"></i>
36 </p>
37
38 <div class="lead">
39 <a href="javascript: void(0);" id="upload-file"><?php echo JText::translate('VAPMANUALUPLOAD'); ?></a>&nbsp;<?php echo JText::translate('VAPCSVDRAGDROP'); ?>
40 </div>
41
42 <p class="maxsize">
43 <?php echo JText::sprintf('JGLOBAL_MAXIMUM_UPLOAD_SIZE_LIMIT', JHtml::fetch('vikappointments.maxuploadsize')); ?>
44 </p>
45
46 <input type="file" id="legacy-upload" style="display: none;"/>
47
48 <div id="vap-selected-file" style="position: absolute; bottom: 6px; left: 6px; display: none;">
49 <?php
50 if ($this->file)
51 {
52 ?>
53 <span class="badge badge-info">
54 <?php echo substr($filename, strpos($filename, '_') + 1); ?>
55 (<?php echo JHtml::fetch('number.bytes', filesize($this->file)); ?>)
56 </span>
57
58 <a href="javascript: void(0);" onclick="deleteExistingFile();">
59 <i class="fas fa-trash" style="margin-left: 4px;"></i>
60 </a>
61 <?php
62 }
63 ?>
64 </div>
65
66 <div class="vap-upload-progress" style="position: absolute; bottom: 6px; right: 6px; display: flex; visibility: hidden;">
67 <progress value="0" max="100">0%</progress>
68 </div>
69 </div>
70
71 <?php echo $vik->closeEmptyFieldset(); ?>
72 </div>
73 <?php echo $vik->closeCard(); ?>
74
75 <form action="index.php" method="post" name="adminForm">
76
77 <?php echo JHtml::fetch('form.token'); ?>
78
79 <input type="hidden" name="task" value="" />
80 <input type="hidden" name="view" value="import" />
81 <input type="hidden" name="option" value="com_vikappointments" />
82 <input type="hidden" name="import_type" value="<?php echo $this->type; ?>" />
83
84 <?php
85 foreach ($this->args as $k => $v)
86 {
87 ?>
88 <input type="hidden" name="<?php echo $k; ?>" value="<?php echo $this->escape($v); ?>" />
89 <input type="hidden" name="import_args[<?php echo $k; ?>]" value="<?php echo $this->escape($v); ?>" />
90 <?php
91 }
92 ?>
93
94 </form>
95
96 <?php
97 JText::script('VAPIMPORTCSVUPLOADALERT');
98 JText::script('VAPCONNECTIONLOSTERROR');
99 ?>
100
101 <script>
102
103 (function($) {
104 'use strict';
105
106 let dragCounter = 0;
107
108 let FILE_UPLOADED = false;
109 let IS_UPLOADING = false;
110
111 const isCsv = (name) => {
112 return name.toLowerCase().match(/\.csv$/);
113 }
114
115 const fileUploadThread = (file) => {
116 // manually reset task before uploading a file
117 document.adminForm.task.value = '';
118
119 const progressBox = $('.vap-upload-progress');
120 progressBox.css('visibility', 'visible');
121 progressBox.find('progress').val(0).text('0%');
122
123 const formData = new FormData();
124 formData.append('source', file);
125 formData.append('import_type', '<?php echo $this->type; ?>');
126
127 UIAjax.upload(
128 // end-point URL
129 '<?php echo $vik->ajaxUrl('index.php?option=com_vikappointments&task=import.dropupload'); ?>',
130 // file post data
131 formData,
132 // success callback
133 (resp) => {
134 // force progress to 100%
135 progressBox.find('progress').val(100).text('100%');
136
137 IS_UPLOADING = false;
138
139 setTimeout(() => {
140 // auto-submit for import
141 Joomla.submitform('import.add', document.adminForm);
142 }, 256);
143 },
144 // failure callback
145 (error) => {
146 alert(error.responseText || Joomla.JText._('VAPCONNECTIONLOSTERROR'));
147
148 progressBox.css('visibility', 'hidden');
149
150 IS_UPLOADING = false;
151 },
152 // progress callback
153 (progress) => {
154 // update progress
155 progressBox.find('progress').val(progress).text(progress + '%');
156 }
157 );
158 }
159
160 const execUpload = (files) => {
161 if (IS_UPLOADING) {
162 return false;
163 }
164
165 const bar = $('#vap-selected-file');
166
167 // hide bar
168 bar.hide().html();
169
170 if (!files || !files.length) {
171 // invalid argument
172 return false;
173 }
174
175 let file = files[0];
176
177 // make sure we have a valid CSV file
178 if (!isCsv(file.name)) {
179 return false;
180 }
181
182 FILE_UPLOADED = false;
183 IS_UPLOADING = true;
184
185 let sizeStr = "";
186
187 if (file.size > 1024 * 1024) {
188 sizeStr = (file.size / (1024 * 1024)).toFixed(2) + " MB";
189 } else if (file.size > 1024) {
190 sizeStr = (file.size / 1024).toFixed(2) + " kB";
191 } else {
192 sizeStr = file.size.toFixed(2) + " b";
193 }
194
195 // display badge
196 const badge = $('<span class="badge badge-info"></span>').text(file.name + ' (' + sizeStr + ')');
197 bar.html(badge).show();
198
199 fileUploadThread(file);
200
201 return true;
202 }
203
204 $(function() {
205
206 // drag&drop actions on target div
207
208 $('.vap-media-droptarget').on('drag dragstart dragend dragover dragenter dragleave drop', function(e) {
209 e.preventDefault();
210 e.stopPropagation();
211 });
212
213 $('.vap-media-droptarget').on('dragenter', function(e) {
214 // increase the drag counter because we may
215 // enter into a child element
216 dragCounter++;
217
218 $(this).addClass('drag-enter');
219 });
220
221 $('.vap-media-droptarget').on('dragleave', function(e) {
222 // decrease the drag counter to check if we
223 // left the main container
224 dragCounter--;
225
226 if (dragCounter <= 0) {
227 $(this).removeClass('drag-enter');
228 }
229 });
230
231 $('.vap-media-droptarget').on('drop', function(e) {
232
233 $(this).removeClass('drag-enter');
234
235 var files = e.originalEvent.dataTransfer.files;
236
237 execUpload(files);
238
239 });
240
241 $('.vap-media-droptarget #upload-file').on('click', function() {
242
243 $('input#legacy-upload').trigger('click');
244
245 });
246
247 $('input#legacy-upload').on('change', function() {
248
249 execUpload($(this)[0].files);
250
251 });
252
253 Joomla.submitbutton = function(task) {
254 if (task == 'import.add' && !FILE_UPLOADED) {
255 alert(Joomla.JText._('VAPIMPORTCSVUPLOADALERT'));
256
257 return false;
258 }
259
260 Joomla.submitform(task, document.adminForm);
261 }
262
263 <?php if ($this->file) { ?>
264
265 $('#vap-selected-file').show();
266
267 // this avoids to auto-aubmit the form
268 FILE_UPLOADED = true;
269
270 <?php } ?>
271 });
272 })(jQuery);
273
274 function deleteExistingFile() {
275 Joomla.submitform('import.delete', document.adminForm);
276 }
277
278 </script>
279