PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / layouts / form / fields / file.php
vikappointments / admin / layouts / form / fields Last commit date
checkbox.php 2 days ago color.php 2 days ago date.php 2 days ago editor.php 2 days ago file.php 2 days ago hidden.php 2 days ago html.php 2 days ago index.html 2 days ago number.php 2 days ago password.php 2 days ago select.php 2 days ago separator.php 2 days ago tel.php 2 days ago text.php 2 days ago textarea.php 2 days ago
file.php
188 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 $name = !empty($displayData['name']) ? $displayData['name'] : 'name';
15 $id = !empty($displayData['id']) ? $displayData['id'] : $name;
16 $value = isset($displayData['value']) ? $displayData['value'] : '';
17 $class = isset($displayData['class']) ? $displayData['class'] : '';
18 $multiple = isset($displayData['multiple']) ? $displayData['multiple'] : false;
19 $files = isset($displayData['files']) ? $displayData['files'] : array();
20
21 $countFiles = count($files);
22
23 if ($countFiles > 1)
24 {
25 // display number of selected files
26 $text_value = JText::plural('VAP_DEF_N_SELECTED', $countFiles);
27
28 // load context menu to implement preview for multi-files
29 JHtml::fetch('vaphtml.assets.contextmenu');
30 }
31 else
32 {
33 // display name of uploaded file (if any)
34 $text_value = $files ? $files[0]['name'] : '';
35 }
36
37 ?>
38
39 <div class="<?php echo ($countFiles ? 'input-prepend ' : ''); ?>input-append file-field">
40 <?php
41 if ($countFiles > 1)
42 {
43 ?>
44 <button type="button" class="btn" id="<?php echo $id; ?>-preview">
45 <i class="fas fa-eye"></i>
46 </button>
47 <?php
48 }
49 else if ($countFiles == 1)
50 {
51 ?>
52 <a href="<?php echo $files[0]['uri']; ?>" class="btn" target="_blank">
53 <i class="fas fa-eye"></i>
54 </a>
55 <?php
56 }
57 ?>
58
59 <input
60 type="text"
61 id="<?php echo $this->escape($id); ?>-text"
62 value="<?php echo $this->escape($text_value); ?>"
63 placeholder="<?php echo $this->escape(JText::translate('VAP_DEF_N_SELECTED_0')); ?>"
64 size="30"
65 readonly
66 />
67
68 <button type="button" class="btn btn-primary" id="<?php echo $this->escape($id); ?>-trigger">
69 <i class="fas fa-upload"></i>
70 </button>
71 </div>
72
73 <input
74 type="file"
75 name="<?php echo $name; ?>"
76 id="<?php echo $id; ?>"
77 class="<?php echo $class; ?>"
78 style="display:none;"
79 <?php echo $multiple ? 'multiple' : ''; ?>
80 />
81
82 <?php
83 if ($value)
84 {
85 // fetch name for the input holding the uploaded file(s)
86 $old_input_name = 'old_' . $name;
87
88 if (!$multiple && $countFiles > 1)
89 {
90 // The input was multiple and the user already uploaded
91 // more than a file... In order to manage them in the
92 // correct way at the next upload, we need to treat the
93 // input as an array.
94 $old_input_name .= '[]';
95 }
96
97 // register already uploaded files for being able to remove
98 // them after uploading new files
99 foreach ((array) $value as $old)
100 {
101 ?>
102 <input type="hidden" name="<?php echo $old_input_name; ?>" value="<?php echo $old; ?>" />
103 <?php
104 }
105 }
106
107 JText::script('VAP_DEF_N_SELECTED');
108 ?>
109
110 <script>
111
112 jQuery(function($) {
113 let input = $('#<?php echo $id; ?>');
114
115 let oldFiles = <?php echo json_encode($files); ?>;
116
117 // manually force form enctype to support file uploads
118 $(input[0].form).attr('enctype', 'multipart/form-data');
119
120 // open file selection dialog
121 $('#<?php echo $id; ?>-trigger').on('click', () => {
122 // unset selected files before showing the dialog
123 input.val(null).trigger('click');
124 });
125
126 // update preview text
127 input.on('click change', () => {
128 let count = input[0].files.length;
129 let text = '';
130
131 if (count == 1) {
132 // set file name
133 text = input[0].files[0].name;
134 } else if (count > 1) {
135 // use count
136 text = Joomla.JText._('VAP_DEF_N_SELECTED').replace(/%d/, count);
137 } else {
138 // no selection, rely on uploaded files
139 if (oldFiles.length == 1) {
140 text = oldFiles[0].name;
141 } else if (count > 1) {
142 text = Joomla.JText._('VAP_DEF_N_SELECTED').replace(/%d/, oldFiles.length);
143 }
144 }
145
146 // update preview text
147 $('#<?php echo $id; ?>-text').val(text);
148 });
149
150 if (oldFiles.length > 1) {
151 // construct preview buttons
152 let buttons = [];
153
154 oldFiles.forEach((file) => {
155 buttons.push({
156 // use file name as button text
157 text: file.name,
158 // use icon to describe that the file will be
159 // opened in a different tab of the browser
160 icon: 'fas fa-external-link-square-alt',
161 // add separator between the files
162 separator: true,
163 // store an internal reference of the window
164 frame: null,
165 // action used to open the linked file
166 action: function() {
167 // check whether the window is currently open
168 if (this.frame && !this.frame.closed) {
169 // window already open, just focus it
170 this.frame.focus()
171 } else {
172 // open file within a new tab of the browser
173 // and save window reference
174 this.frame = window.open(file.uri, '_blank');
175 }
176 },
177 });
178 });
179
180 // open context menu while clicking the preview button
181 let cm = $('#<?php echo $id; ?>-preview').vikContextMenu({
182 buttons: buttons,
183 });
184 }
185 });
186
187 </script>
188