dolibarr 20.0.0
filemanager.tpl.php
1<?php
2/* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 *
18 * Output code for the filemanager
19 * $module must be defined ('ecm', 'medias', ...)
20 * $formalreadyopen can be set to 1 to avoid to open the <form> to submit files a second time
21 */
22
23// Protection to avoid direct call of template
24if (empty($conf) || !is_object($conf)) {
25 print "Error, template page filemanager.tpl.php can't be called as URL";
26 exit;
27}
28
29?>
30
31<!-- BEGIN PHP TEMPLATE core/tpl/filemanager.tpl.php -->
32<!-- Doc of fileTree plugin at https://www.abeautifulsite.net/jquery-file-tree -->
33
34<?php
35
36require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
37
38$langs->load("ecm");
39
40if (empty($module)) {
41 $module = 'ecm';
42}
43
44$permtoadd = 0;
45$permtoupload = 0;
46$showroot = 0;
47if ($module == 'ecm') {
48 $permtoadd = $user->hasRight("ecm", "setup");
49 $permtoupload = $user->hasRight("ecm", "upload");
50 $showroot = 0;
51}
52if ($module == 'medias') {
53 $permtoadd = ($user->hasRight("mailing", "creer") || $user->hasRight("website", "write"));
54 $permtoupload = ($user->hasRight("mailing", "creer") || $user->hasRight("website", "write"));
55 $showroot = 1;
56}
57
58if (!isset($section)) {
59 $section = 0;
60}
61
62// Confirm remove file (for non javascript users)
63if (($action == 'delete' || $action == 'file_manager_delete') && empty($conf->use_javascript_ajax)) {
64 // TODO Add website, pageid, filemanager if defined
65 print $form->formconfirm($_SERVER["PHP_SELF"].'?section='.$section.'&urlfile='.urlencode(GETPOST("urlfile")), $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile', '', '', 1);
66}
67
68// Start container of all panels
69?>
70<!-- Begin div id="containerlayout" -->
71<div id="containerlayout">
72<div id="ecm-layout-north" class="toolbar largebutton">
73<?php
74
75// Start top panel, toolbar
76print '<div class="inline-block toolbarbutton centpercent">';
77
78// Toolbar
79if ($permtoadd) {
80 $websitekeyandpageid = (!empty($websitekey) ? '&website='.urlencode($websitekey) : '').(!empty($pageid) ? '&pageid='.urlencode((string) $pageid) : '');
81 print '<a id="acreatedir" href="'.DOL_URL_ROOT.'/ecm/dir_add_card.php?action=create&module='.urlencode($module).$websitekeyandpageid.'&backtopage='.urlencode($_SERVER["PHP_SELF"].'?file_manager=1'.$websitekeyandpageid).'" class="inline-block valignmiddle toolbarbutton paddingtop" title="'.dol_escape_htmltag($langs->trans('ECMAddSection')).'">';
82 print img_picto('', 'folder-plus', '', false, 0, 0, '', 'size15x marginrightonly');
83 print '</a>';
84} else {
85 print '<a id="acreatedir" href="#" class="inline-block valignmiddle toolbarbutton paddingtop" title="'.$langs->trans("NotAllowed").'">';
86 print img_picto('', 'folder-plus', 'disabled', false, 0, 0, '', 'size15x marginrightonly');
87 print '</a>';
88}
89if ($module == 'ecm') {
90 $tmpurl = ((!empty($conf->use_javascript_ajax) && !getDolGlobalString('MAIN_ECM_DISABLE_JS')) ? '#' : ($_SERVER["PHP_SELF"].'?action=refreshmanual'.($module ? '&amp;module='.$module : '').($section ? '&amp;section='.$section : '')));
91 print '<a id="arefreshbutton" href="'.$tmpurl.'" class="inline-block valignmiddle toolbarbutton paddingtop" title="'.dol_escape_htmltag($langs->trans('ReSyncListOfDir')).'">';
92 print img_picto('', 'refresh', 'id="refreshbutton"', false, 0, 0, '', 'size15x marginrightonly');
93 print '</a>';
94}
95if ($permtoadd && GETPOSTISSET('website')) { // If on file manager to manage medias of a web site
96 print '<a id="agenerateimgwebp" href="'.$_SERVER["PHP_SELF"].'?action=confirmconvertimgwebp&token='.newToken().'&website='.urlencode($website->ref).'" class="inline-block valignmiddle toolbarbutton paddingtop" title="'.dol_escape_htmltag($langs->trans("GenerateImgWebp")).'">';
97 print img_picto('', 'images', '', false, 0, 0, '', 'size15x flip marginrightonly');
98 print '</a>';
99} elseif ($permtoadd && $module == 'ecm') { // If on file manager medias in ecm
100 if (getDolGlobalInt('ECM_SHOW_GENERATE_WEBP_BUTTON')) {
101 print '<a id="agenerateimgwebp" href="'.$_SERVER["PHP_SELF"].'?action=confirmconvertimgwebp&token='.newToken().'" class="inline-block valignmiddle toolbarbutton paddingtop" title="'.dol_escape_htmltag($langs->trans("GenerateImgWebp")).'">';
102 print img_picto('', 'images', '', false, 0, 0, '', 'size15x flip marginrightonly');
103 print '</a>';
104 }
105}
106
107print "<script>
108$('#acreatedir').on('click', function() {
109 try{
110 section_dir = $('.directory.expanded')[$('.directory.expanded').length-1].children[0].rel;
111 section = $('.directory.expanded')[$('.directory.expanded').length-1].children[0].id.split('_')[2];
112 catParent = ";
113if ($module == 'ecm') {
114 print "section;";
115} else {
116 print "section_dir.substring(0, section_dir.length - 1);";
117}
118print "
119 } catch{
120 section_dir = '/';
121 section = 0;
122 catParent = ";
123if ($module == 'ecm') {
124 print "section;";
125} else {
126 print "section_dir;";
127}
128print "
129 }
130 console.log('We click to create a new directory, we set current section_dir='+section_dir+' into href url of button acreatedir');
131 $('#acreatedir').attr('href', $('#acreatedir').attr('href')+'%26section_dir%3D'+encodeURI(section_dir)+'%26section%3D'+encodeURI(section)+'&section_dir='+encodeURI(section_dir)+'&section='+encodeURI(section)+'&catParent='+encodeURI(catParent));
132 console.log($('#acreatedir').attr('href'));
133});
134$('#agenerateimgwebp').on('click', function() {
135 try{
136 section_dir = $('.directory.expanded')[$('.directory.expanded').length-1].children[0].rel;
137 section = $('.directory.expanded')[$('.directory.expanded').length-1].children[0].id.split('_')[2];
138 } catch{
139 section_dir = '/';
140 section = 0;
141 }
142 console.log('We click to generate webp image, we set current section_dir='+section_dir+' into href url of button agenerateimgwebp');
143 $('#agenerateimgwebp').attr('href', $('#agenerateimgwebp').attr('href')+'&section_dir='+encodeURI(section_dir)+'&section='+encodeURI(section));
144 console.log($('#agenerateimgwebp').attr('href'));
145});
146</script>";
147
148// Start "Add new file" area
149$nameforformuserfile = 'formuserfileecm';
150
151print '<div class="inline-block valignmiddle floatright">';
152
153// Zone to attach a new file
154if ((!empty($conf->use_javascript_ajax) && !getDolGlobalString('MAIN_ECM_DISABLE_JS')) || !empty($section)) {
155 if ((empty($section) || $section == -1) && ($module != 'medias')) {
156 ?>
157 <script>
158 jQuery(document).ready(function() {
159 jQuery('#<?php echo $nameforformuserfile ?>').hide();
160 });
161 </script>
162 <?php
163 }
164
165 $sectiondir = GETPOST('file', 'alpha') ? GETPOST('file', 'alpha') : GETPOST('section_dir', 'alpha');
166
167 print '<!-- Start form to attach new file in filemanager.tpl.php sectionid='.$section.' sectiondir='.$sectiondir.' -->'."\n";
168 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
169 $formfile = new FormFile($db);
170 print $formfile->form_attach_new_file($_SERVER["PHP_SELF"], 'none', 0, ($section ? $section : -1), $permtoupload, 48, null, '', 0, '', 0, $nameforformuserfile, '', $sectiondir, empty($formalreadyopen) ? 0 : $formalreadyopen, 0, 0, 1);
171} else {
172 print '&nbsp;';
173}
174
175print '</div>';
176// End "Add new file" area
177
178
179print '</div>';
180// End top panel, toolbar
181
182?>
183</div>
184<div id="ecm-layout-west" class="inline-block">
185<?php
186// Start left area
187
188
189// Ask confirmation of deletion of directory
190if ($action == 'delete_section') {
191 print $form->formconfirm($_SERVER["PHP_SELF"].'?section='.$section, $langs->trans('DeleteSection'), $langs->trans('ConfirmDeleteSection', $ecmdir->label), 'confirm_deletesection', '', '', 1);
192}
193// End confirm
194
195// Ask confirmation to build webp images
196if ($action == 'confirmconvertimgwebp') {
197 $langs->load("ecm");
198
199 $section_dir = GETPOST('section_dir', 'alpha');
200 $section = GETPOST('section', 'alpha');
201 $file = GETPOST('filetoregenerate', 'alpha');
202 $form = new Form($db);
203 $formquestion = array();
204 $formquestion['section_dir'] = array('type' => 'hidden', 'value' => $section_dir, 'name' => 'section_dir');
205 $formquestion['section'] = array('type' => 'hidden', 'value' => $section, 'name' => 'section');
206 $formquestion['filetoregenerate'] = array('type' => 'hidden', 'value' => $file, 'name' => 'filetoregenerate');
207 if ($module == 'medias') {
208 $formquestion['website'] = array('type' => 'hidden', 'value' => $website->ref, 'name' => 'website');
209 }
210 $param = '';
211 if (!empty($sortfield)) {
212 $param .= '&sortfield='.urlencode($sortfield);
213 }
214 if (!empty($sortorder)) {
215 $param .= '&sortorder='.urlencode($sortorder);
216 }
217 print $form->formconfirm($_SERVER["PHP_SELF"].($param ? '?'.$param : ''), empty($file) ? $langs->trans('ConfirmImgWebpCreation') : $langs->trans('ConfirmChosenImgWebpCreation'), empty($file) ? $langs->trans('ConfirmGenerateImgWebp') : $langs->trans('ConfirmGenerateChosenImgWebp', basename($file)), 'convertimgwebp', $formquestion, "yes", 1);
218 $action = 'file_manager';
219}
220
221// Duplicate images into .webp
222if ($action == 'convertimgwebp' && $permtoadd) {
223 $file = GETPOST('filetoregenerate', 'alpha');
224
225 if ($module == 'medias') {
226 $imagefolder = $conf->website->dir_output.'/'.$websitekey.'/medias/'.dol_sanitizePathName(GETPOST('section_dir', 'alpha'));
227 } else {
228 $imagefolder = $conf->ecm->dir_output.'/'.dol_sanitizePathName(GETPOST('section_dir', 'alpha'));
229 }
230
231 include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
232
233 if (!empty($file)) {
234 $filelist = array();
235 $filelist[]["fullname"] = dol_osencode($imagefolder.'/'.$file); // get $imagefolder.'/'.$file infos
236 } else {
237 $regeximgext = getListOfPossibleImageExt();
238
239 $filelist = dol_dir_list($imagefolder, "files", 0, $regeximgext);
240 }
241
242 $nbconverted = 0;
243
244 foreach ($filelist as $filename) {
245 $filepath = $filename['fullname'];
246 if (!(substr_compare($filepath, 'webp', -strlen('webp')) === 0)) {
247 if (!empty($file) || !dol_is_file($filepathnoext.'.webp')) { // If file does not exists yet
248 if (image_format_supported($filepath) == 1) {
249 $filepathnoext = preg_replace("/\.[a-z0-9]+$/i", "", $filepath);
250 $result = dol_imageResizeOrCrop($filepath, 0, 0, 0, 0, 0, $filepathnoext.'.webp', 90);
251 if (!dol_is_file($result)) {
252 $error++;
253 setEventMessages($result, null, 'errors');
254 } else {
255 $nbconverted++;
256 }
257 }
258 }
259 }
260 if ($error) {
261 break;
262 }
263 }
264 if (!$error) {
265 if (!empty($file)) {
266 setEventMessages($langs->trans('SucessConvertChosenImgWebp'), null);
267 } else {
268 setEventMessages($langs->trans('SucessConvertImgWebp'), null);
269 }
270 }
271 $action = 'file_manager';
272}
273
274if (empty($action) || $action == 'editfile' || $action == 'file_manager' || preg_match('/refresh/i', $action) || $action == 'delete') {
275 $langs->load("ecm");
276
277 print '<table class="liste centpercent">'."\n";
278
279 print '<!-- Title for manual directories -->'."\n";
280 print '<tr class="liste_titre">'."\n";
281 print '<th class="liste_titre left">';
282 print '<span style="padding-left: 5px; padding-right: 5px;">'.$langs->trans("ECMSections").'</span>';
283 print '</th></tr>';
284
285 $showonrightsize = '';
286
287 // Manual section
288 $htmltooltip = $langs->trans("ECMAreaDesc2a");
289 $htmltooltip .= '<br>'.$langs->trans("ECMAreaDesc2b");
290
291 if (!empty($conf->use_javascript_ajax) && !getDolGlobalString('MAIN_ECM_DISABLE_JS')) {
292 // Show the link to "Root"
293 if ($showroot) {
294 print '<tr class="oddeven nohover"><td><div style="padding-left: 5px; padding-right: 5px;"><a href="'.$_SERVER["PHP_SELF"].'?file_manager=1'.(!empty($websitekey) ? '&website='.urlencode($websitekey) : '').'&pageid='.urlencode((string) $pageid).'">';
295 if ($module == 'medias') {
296 print $langs->trans("RootOfMedias");
297 } else {
298 print $langs->trans("Root");
299 }
300 print '</a></div></td></tr>';
301 }
302
303 print '<tr class="oddeven nohover"><td>';
304
305 // Show filemanager tree (will be filled by a call of ajax /ecm/tpl/enablefiletreeajax.tpl.php, later, that executes ajaxdirtree.php)
306 print '<div id="filetree" class="ecmfiletree"></div>';
307
308 if ($action == 'deletefile') {
309 print $form->formconfirm('eeeee', $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile', '', '', 'deletefile');
310 }
311
312 print '</td></tr>';
313 } else {
314 // Show filtree when ajax is disabled (rare)
315 print '<tr><td style="padding-left: 20px">';
316
317 $_POST['modulepart'] = $module;
318 $_POST['openeddir'] = GETPOST('openeddir');
319 $_POST['dir'] = empty($_POST['dir']) ? '/' : GETPOST('dir');
320
321 // Show filemanager tree (will be filled by direct include of ajaxdirtree.php in mode noajax, this will return all dir - all levels - to show)
322 print '<div id="filetree" class="ecmfiletree">';
323
324 // Variables that may be defined:
325 // $_GET['modulepart'], $_GET['openeddir'], $_GET['sortfield'], $_GET['sortorder']
326 // $_POST['dir']
327 $mode = 'noajax';
328 if (empty($url)) {
329 $url = DOL_URL_ROOT.'/ecm/index.php';
330 }
331 include DOL_DOCUMENT_ROOT.'/core/ajax/ajaxdirtree.php';
332
333 print '</div>';
334 print '</td></tr>';
335 }
336
337
338 print "</table>";
339}
340
341
342// End left panel
343?>
344</div>
345<div id="ecm-layout-center" class="inline-block">
346<div class="pane-in ecm-in-layout-center">
347<div id="ecmfileview" class="ecmfileview">
348<?php
349// Start right panel - List of content of a directory
350
351$mode = 'noajax';
352if (empty($url)) { // autoset $url but it is better to have it defined before (for example by ecm/index.php, ecm/index_medias.php, website/index.php)
353 if (!empty($module) && $module == 'medias' && !GETPOST('website')) {
354 $url = DOL_URL_ROOT.'/ecm/index_medias.php';
355 } elseif (GETPOSTISSET('website')) {
356 $url = DOL_URL_ROOT.'/website/index.php';
357 } else {
358 $url = DOL_URL_ROOT.'/ecm/index.php';
359 }
360}
361include DOL_DOCUMENT_ROOT.'/core/ajax/ajaxdirpreview.php'; // Show content of a directory on right side
362
363
364// End right panel
365?>
366</div>
367</div>
368
369</div>
370</div> <!-- End div id="containerlayout" -->
371<?php
372
373
374if (!empty($conf->use_javascript_ajax) && !getDolGlobalString('MAIN_ECM_DISABLE_JS')) { // Show filtree when ajax is enabled
375 //var_dump($modulepart);
376 // Variables that may be defined:
377 // $_GET['modulepart'], $_GET['openeddir'], $_GET['sortfield'], $_GET['sortorder']
378 // $_POST['dir']
379 // $_POST['section_dir'], $_POST['section_id'], $_POST['token'], $_POST['max_file_size'], $_POST['sendit']
380 if (GETPOST('section_dir', 'alpha')) {
381 $preopened = GETPOST('section_dir', 'alpha');
382 }
383
384 include DOL_DOCUMENT_ROOT.'/ecm/tpl/enablefiletreeajax.tpl.php';
385}
386
387?>
388<!-- END PHP TEMPLATE core/tpl/filemanager.tpl.php -->
Class to offer components to list and upload files.
Class to manage generation of HTML components Only common components must be here.
dol_is_file($pathoffile)
Return if path is a file.
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:63
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_sanitizePathName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a path name.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
getListOfPossibleImageExt($acceptsvg=0)
Return if a filename is file name of a supported image format.
dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x=0, $src_y=0, $filetowrite='', $newquality=0)
Resize or crop an image file (Supported extensions are gif, jpg, png, bmp and webp)
image_format_supported($file, $acceptsvg=0)
Return if a filename is file name of a supported image format.