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