dolibarr 25.0.0-alpha
filemanager.tpl.php
1<?php
2/* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024-2026 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * Output code for the filemanager
20 * $module must be defined ('ecm', 'medias', ...)
21 * $formalreadyopen can be set to 1 to avoid to open the <form> to submit files a second time
22 */
45// Protection to avoid direct call of template
46if (empty($conf) || !is_object($conf)) {
47 print "Error, template page filemanager.tpl.php can't be called as URL";
48 exit;
49}
50
51'
52@phan-var-force Website $website
53@phan-var-force string $filepathnoext
54@phan-var-force string $pageid
55@phan-var-force EcmDirectory $ecmdir
56@phan-var-force ?string $module
57@phan-var-force int $section
58@phan-var-force string $websitekey
59';
60
61?>
62
63<!-- BEGIN PHP TEMPLATE core/tpl/filemanager.tpl.php -->
64<!-- Doc of fileTree plugin at https://www.abeautifulsite.net/jquery-file-tree -->
65
66<?php
67
68require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
69
70$langs->load("ecm");
71
72if (!isset($module)) {
73 $module = 'ecm';
74}
75
76$permtoadd = 0;
77$permtoupload = 0;
78$showroot = 0;
79$error = 0;
80
81if ($module == 'ecm') {
82 $permtoadd = $user->hasRight("ecm", "setup");
83 $permtoupload = $user->hasRight("ecm", "upload");
84 $showroot = 0;
85}
86if ($module == 'medias') {
87 $permtoadd = $user->hasRight("website", "write");
88 $permtoupload = $user->hasRight("website", "write");
89 $showroot = 1;
90}
91
92if (!isset($section)) {
93 $section = 0;
94}
95
96// Confirm remove file (for non javascript users)
97if (($action == 'delete' || $action == 'file_manager_delete') && empty($conf->use_javascript_ajax)) {
98 // TODO Add website, pageid, filemanager if defined
99 print $form->formconfirm(dolBuildUrl($_SERVER["PHP_SELF"], array('section' => $section, 'urlfile' => GETPOST("urlfile"))), $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile', '', '', 1);
100}
101
102// Start container of all panels
103?>
104<!-- Begin div id="containerlayout" -->
105<div id="containerlayout">
106<div id="ecm-layout-north" class="toolbar largebutton">
107<?php
108
109// Keep the popup mode (no menus) on links when the file manager is open into a dialog popup
110$paramspopuparray = array();
111foreach (array('dol_hide_topmenu', 'dol_hide_leftmenu') as $tmpparam) {
112 if (GETPOSTINT($tmpparam)) {
113 $paramspopuparray[$tmpparam] = GETPOSTINT($tmpparam);
114 }
115}
116if (GETPOST('dol_openinpopup', 'aZ09')) {
117 $paramspopuparray['dol_openinpopup'] = GETPOST('dol_openinpopup', 'aZ09');
118}
119$paramspopup = (empty($paramspopuparray) ? '' : '&'.http_build_query($paramspopuparray));
120
121// Start top panel, toolbar
122print '<div class="inline-block toolbarbutton centpercent">';
123
124// Toolbar
125if ($permtoadd) {
126 $websitekeyandpageid = (!empty($websitekey) ? '&website='.urlencode($websitekey) : '').(!empty($pageid) ? '&pageid='.urlencode((string) $pageid) : '');
127 $paramscreatedir = array('action' => 'create', 'module' => $module);
128 if (!empty($websitekey)) {
129 $paramscreatedir['website'] = $websitekey;
130 }
131 if (!empty($pageid)) {
132 $paramscreatedir['pageid'] = $pageid;
133 }
134 $paramscreatedir = array_merge($paramscreatedir, $paramspopuparray);
135 $paramscreatedir['backtopage'] = $_SERVER["PHP_SELF"].'?file_manager=1'.$websitekeyandpageid.$paramspopup;
136 print '<a id="acreatedir" href="'.dolBuildUrl(DOL_URL_ROOT.'/ecm/dir_add_card.php', $paramscreatedir).'" class="inline-block valignmiddle toolbarbutton paddingtop" title="'.dol_escape_htmltag($langs->trans('ECMAddSection')).'">';
137 print img_picto('', 'folder-plus', '', 0, 0, 0, '', 'size15x marginrightonly');
138 print '</a>';
139} else {
140 print '<a id="acreatedir" href="#" class="inline-block valignmiddle toolbarbutton paddingtop" title="'.$langs->trans("NotAllowed").'">';
141 print img_picto('', 'folder-plus', 'disabled', 0, 0, 0, '', 'size15x marginrightonly');
142 print '</a>';
143}
144if ($module == 'ecm') {
145 if (!empty($conf->use_javascript_ajax) && !getDolGlobalString('MAIN_ECM_DISABLE_JS')) {
146 $tmpurl = '#';
147 } else {
148 $paramsrefresh = array('action' => 'refreshmanual', 'module' => $module);
149 if ($section) {
150 $paramsrefresh['section'] = $section;
151 }
152 $tmpurl = dolBuildUrl($_SERVER["PHP_SELF"], $paramsrefresh);
153 }
154 print '<a id="arefreshbutton" href="'.$tmpurl.'" class="inline-block valignmiddle toolbarbutton paddingtop" title="'.dol_escape_htmltag($langs->trans('ReSyncListOfDir')).'">';
155 print img_picto('', 'refresh', 'id="refreshbutton"', 0, 0, 0, '', 'size15x marginrightonly');
156 print '</a>';
157}
158if ($permtoadd && GETPOSTISSET('website')) { // If on file manager to manage medias of a web site
159 // @phan-suppress-next-line PhanTypeExpectedObjectPropAccess
160 print '<a id="agenerateimgwebp" href="'.dolBuildUrl($_SERVER["PHP_SELF"], array_merge(array('action' => 'confirmconvertimgwebp', 'website' => $website->ref), $paramspopuparray), true).'" class="inline-block valignmiddle toolbarbutton paddingtop" title="'.dol_escape_htmltag($langs->trans("GenerateImgWebp")).'">';
161 print img_picto('', 'images', '', 0, 0, 0, '', 'size15x flip marginrightonly');
162 print '</a>';
163} elseif ($permtoadd && $module == 'ecm') { // If on file manager medias in ecm
164 if (getDolGlobalInt('ECM_SHOW_GENERATE_WEBP_BUTTON')) {
165 print '<a id="agenerateimgwebp" href="'.dolBuildUrl($_SERVER["PHP_SELF"], array('action' => 'confirmconvertimgwebp'), true).'" class="inline-block valignmiddle toolbarbutton paddingtop" title="'.dol_escape_htmltag($langs->trans("GenerateImgWebp")).'">';
166 print img_picto('', 'images', '', 0, 0, 0, '', 'size15x flip marginrightonly');
167 print '</a>';
168 }
169}
170
171print "<script>
172$('#acreatedir').on('click', function() {
173 try{
174 section_dir = $('.directory.expanded')[$('.directory.expanded').length-1].children[0].rel;
175 section = $('.directory.expanded')[$('.directory.expanded').length-1].children[0].id.split('_')[2];
176 catParent = ";
177if ($module == 'ecm') {
178 print "section;";
179} else {
180 print "section_dir.substring(0, section_dir.length - 1);";
181}
182print "
183 } catch{
184 section_dir = '/';
185 section = 0;
186 catParent = ";
187if ($module == 'ecm') {
188 print "section;";
189} else {
190 print "section_dir;";
191}
192print "
193 }
194 console.log('We click to create a new directory, we set current section_dir='+section_dir+' into href url of button acreatedir');
195 $('#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));
196 console.log($('#acreatedir').attr('href'));
197});
198$('#agenerateimgwebp').on('click', function() {
199 try{
200 section_dir = $('.directory.expanded')[$('.directory.expanded').length-1].children[0].rel;
201 section = $('.directory.expanded')[$('.directory.expanded').length-1].children[0].id.split('_')[2];
202 } catch{
203 section_dir = '/';
204 section = 0;
205 }
206 console.log('We click to generate webp image, we set current section_dir='+section_dir+' into href url of button agenerateimgwebp');
207 $('#agenerateimgwebp').attr('href', $('#agenerateimgwebp').attr('href')+'&section_dir='+encodeURI(section_dir)+'&section='+encodeURI(section));
208 console.log($('#agenerateimgwebp').attr('href'));
209});
210</script>";
211
212// Start "Add new file" area
213$nameforformuserfile = 'formuserfileecm';
214
215print '<div class="inline-block valignmiddle floatright">';
216
217// Zone to attach a new file
218if ((!empty($conf->use_javascript_ajax) && !getDolGlobalString('MAIN_ECM_DISABLE_JS')) || !empty($section)) {
219 if ((empty($section) || $section == -1) && ($module != 'medias')) {
220 ?>
221 <script>
222 jQuery(document).ready(function() {
223 jQuery('#<?php echo $nameforformuserfile ?>').hide();
224 });
225 </script>
226 <?php
227 }
228
229 $sectiondir = GETPOST('file', 'alpha') ? GETPOST('file', 'alpha') : GETPOST('section_dir', 'alpha');
230
231 print '<!-- Start form to attach new file in filemanager.tpl.php sectionid='.$section.' sectiondir='.$sectiondir.' -->'."\n";
232 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
233 $formfile = new FormFile($db);
234 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);
235} else {
236 print '&nbsp;';
237}
238
239print '</div>';
240// End "Add new file" area
241
242
243print '</div>';
244// End top panel, toolbar
245
246?>
247</div>
248<div id="ecm-layout-west" class="inline-block">
249<?php
250// Start left area
251
252
253// Ask confirmation of deletion of directory
254if ($action == 'delete_section') {
255 print $form->formconfirm(dolBuildUrl($_SERVER["PHP_SELF"], array('section' => $section)), $langs->trans('DeleteSection'), $langs->trans('ConfirmDeleteSection', $ecmdir->label), 'confirm_deletesection', '', '', 1);
256}
257// End confirm
258
259// Ask confirmation to build webp images
260if ($action == 'confirmconvertimgwebp') {
261 $langs->load("ecm");
262
263 $section_dir = GETPOST('section_dir', 'alpha');
264 $section = GETPOST('section', 'alpha');
265 $file = GETPOST('filetoregenerate', 'alpha');
266 $form = new Form($db);
267 $formquestion = array();
268 $formquestion['section_dir'] = array('type' => 'hidden', 'value' => $section_dir, 'name' => 'section_dir');
269 $formquestion['section'] = array('type' => 'hidden', 'value' => $section, 'name' => 'section');
270 $formquestion['filetoregenerate'] = array('type' => 'hidden', 'value' => $file, 'name' => 'filetoregenerate');
271 if ($module == 'medias') {
272 $formquestion['website'] = array('type' => 'hidden', 'value' => $website->ref, 'name' => 'website'); // @phan-suppress-current-line PhanTypeExpectedObjectPropAccess
273 }
274 $paramsconvertimgwebp = $paramspopuparray;
275 if (!empty($sortfield)) {
276 $paramsconvertimgwebp['sortfield'] = $sortfield;
277 }
278 if (!empty($sortorder)) {
279 $paramsconvertimgwebp['sortorder'] = $sortorder;
280 }
281 print $form->formconfirm(dolBuildUrl($_SERVER["PHP_SELF"], $paramsconvertimgwebp), empty($file) ? $langs->trans('ConfirmImgWebpCreation') : $langs->trans('ConfirmChosenImgWebpCreation'), empty($file) ? $langs->trans('ConfirmGenerateImgWebp') : $langs->trans('ConfirmGenerateChosenImgWebp', basename($file)), 'convertimgwebp', $formquestion, "yes", 1);
282 $action = 'file_manager';
283}
284
285// Duplicate images into .webp
286if ($action == 'convertimgwebp' && $permtoadd) {
287 $file = GETPOST('filetoregenerate', 'alpha');
288
289 if ($module == 'medias') {
290 $imagefolder = $conf->website->dir_output.'/'.$websitekey.'/medias/'.dol_sanitizePathName(GETPOST('section_dir', 'alpha'));
291 } else {
292 $imagefolder = $conf->ecm->dir_output.'/'.dol_sanitizePathName(GETPOST('section_dir', 'alpha'));
293 }
294
295 include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
296
297 if (!empty($file)) {
298 $filelist = array();
299 $filelist[]["fullname"] = dol_osencode($imagefolder.'/'.$file); // get $imagefolder.'/'.$file infos
300 } else {
301 $regeximgext = getListOfPossibleImageExt();
302
303 $filelist = dol_dir_list($imagefolder, "files", 0, $regeximgext);
304 }
305
306 $nbconverted = 0;
307
308 foreach ($filelist as $filename) {
309 $filepath = $filename['fullname'];
310 if (!(substr_compare($filepath, 'webp', -strlen('webp')) === 0)) {
311 if (!empty($file) || !dol_is_file($filepathnoext.'.webp')) { // If file does not exists yet
312 if (image_format_supported($filepath) == 1) {
313 $filepathnoext = preg_replace("/\.[a-z0-9]+$/i", "", $filepath);
314 $result = dol_imageResizeOrCrop($filepath, 0, 0, 0, 0, 0, $filepathnoext.'.webp', 90);
315 if (!dol_is_file($result)) {
316 $error++;
317 setEventMessages($result, null, 'errors');
318 } else {
319 $nbconverted++;
320 }
321 }
322 }
323 }
324 if ($error) {
325 break;
326 }
327 }
328 if (!$error) {
329 if (!empty($file)) {
330 setEventMessages($langs->trans('SucessConvertChosenImgWebp'), null);
331 } else {
332 setEventMessages($langs->trans('SucessConvertImgWebp'), null);
333 }
334 }
335 $action = 'file_manager';
336}
337
338// List of directories
339if (empty($action) || $action == 'editfile' || $action == 'file_manager' || preg_match('/refresh/i', $action) || $action == 'delete') {
340 $langs->load("ecm");
341
342 print '<table class="liste centpercent noborder">'."\n";
343
344 print '<!-- Title for manual directories -->'."\n";
345 print '<tr class="liste_titre">'."\n";
346 print '<th class="liste_titre left">';
347 print '<span style="padding-left: 5px; padding-right: 5px;">'.$langs->trans("ECMSections").'</span>';
348 print '</th></tr>';
349
350 $showonrightsize = '';
351
352 // Manual section
353 $htmltooltip = $langs->trans("ECMAreaDesc2a");
354 $htmltooltip .= '<br>'.$langs->trans("ECMAreaDesc2b");
355
356 if (!empty($conf->use_javascript_ajax) && !getDolGlobalString('MAIN_ECM_DISABLE_JS')) {
357 // Show the link to "Root"
358 if ($showroot) {
359 $paramsroot = array('file_manager' => 1);
360 if (!empty($websitekey)) {
361 $paramsroot['website'] = $websitekey;
362 }
363 $paramsroot['pageid'] = $pageid;
364 print '<tr class="oddeven nohover"><td><div style="padding-left: 5px; padding-right: 5px;"><a href="'.dolBuildUrl($_SERVER["PHP_SELF"], $paramsroot).'">';
365 if ($module == 'medias') {
366 print $langs->trans("RootOfMedias");
367 } else {
368 print $langs->trans("Root");
369 }
370 print '</a></div></td></tr>';
371 }
372
373 print '<tr class="oddeven nohover"><td>';
374
375 // Show filemanager tree (will be filled by a call of ajax /ecm/tpl/enablefiletreeajax.tpl.php, later, that executes ajaxdirtree.php)
376 print '<div id="filetree" class="ecmfiletree"></div>';
377
378 if ($action == 'deletefile') {
379 print $form->formconfirm('eeeee', $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile', '', '', 'deletefile');
380 }
381
382 print '</td></tr>';
383 } else {
384 // Show filtree when ajax is disabled (rare)
385 print '<tr><td style="padding-left: 20px">';
386
387 $_POST['modulepart'] = $module;
388 $_POST['openeddir'] = GETPOST('openeddir');
389 $_POST['dir'] = empty($_POST['dir']) ? '/' : GETPOST('dir');
390
391 // Show filemanager tree (will be filled by direct include of ajaxdirtree.php in mode noajax, this will return all dir - all levels - to show)
392 print '<div id="filetree" class="ecmfiletree">';
393
394 // Variables that may be defined:
395 // $_GET['modulepart'], $_GET['openeddir'], $_GET['sortfield'], $_GET['sortorder']
396 // $_POST['dir']
397 $mode = 'noajax';
398 if (empty($url)) {
399 $url = DOL_URL_ROOT.'/ecm/index.php';
400 }
401 include DOL_DOCUMENT_ROOT.'/core/ajax/ajaxdirtree.php';
402
403 print '</div>';
404 print '</td></tr>';
405 }
406
407
408 print "</table>";
409}
410
411
412// End left panel
413?>
414</div>
415<div id="ecm-layout-center" class="inline-block">
416<div class="pane-in ecm-in-layout-center">
417<div id="ecmfileview" class="ecmfileview">
418<?php
419// Start right panel - List of content of a directory
420
421$mode = 'noajax';
422if (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)
423 if (!empty($module) && $module == 'medias' && !GETPOST('website')) {
424 $url = DOL_URL_ROOT.'/ecm/index_medias.php';
425 } elseif (GETPOSTISSET('website')) {
426 $url = DOL_URL_ROOT.'/website/index.php';
427 } else {
428 $url = DOL_URL_ROOT.'/ecm/index.php';
429 }
430}
431include DOL_DOCUMENT_ROOT.'/core/ajax/ajaxdirpreview.php'; // Show content of a directory on right side
432
433
434// End right panel
435?>
436</div>
437</div>
438
439</div>
440</div> <!-- End div id="containerlayout" -->
441<?php
442
443
444if (!empty($conf->use_javascript_ajax) && !getDolGlobalString('MAIN_ECM_DISABLE_JS')) { // Show filtree when ajax is enabled
445 //var_dump($modulepart);
446 // Variables that may be defined:
447 // $_GET['modulepart'], $_GET['openeddir'], $_GET['sortfield'], $_GET['sortorder']
448 // $_POST['dir']
449 // $_POST['section_dir'], $_POST['section_id'], $_POST['token'], $_POST['max_file_size'], $_POST['sendit']
450 if (GETPOST('section_dir', 'alpha')) {
451 $preopened = GETPOST('section_dir', 'alpha');
452 }
453
454 include DOL_DOCUMENT_ROOT.'/ecm/tpl/enablefiletreeajax.tpl.php';
455}
456
457?>
458<!-- END PHP TEMPLATE core/tpl/filemanager.tpl.php -->
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class to offer components to list and upload files.
Class to manage generation of HTML components Only common components must be here.
if(! $sortfield) if(! $sortorder) $module
Definition list.php:193
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:66
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dolBuildUrl($url, $params=[], $addtoken=false, $anchor='')
Return path of url.
dol_sanitizePathName($str, $newstr='_', $unaccent=0, $allowdash=0)
Clean a string to use it as a path name.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Definition html.lib.php:183
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.