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// Start top panel, toolbar
110print '<div class="inline-block toolbarbutton centpercent">';
111
112// Toolbar
113if ($permtoadd) {
114 $websitekeyandpageid = (!empty($websitekey) ? '&website='.urlencode($websitekey) : '').(!empty($pageid) ? '&pageid='.urlencode((string) $pageid) : '');
115 $paramscreatedir = array('action' => 'create', 'module' => $module);
116 if (!empty($websitekey)) {
117 $paramscreatedir['website'] = $websitekey;
118 }
119 if (!empty($pageid)) {
120 $paramscreatedir['pageid'] = $pageid;
121 }
122 $paramscreatedir['backtopage'] = $_SERVER["PHP_SELF"].'?file_manager=1'.$websitekeyandpageid;
123 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')).'">';
124 print img_picto('', 'folder-plus', '', 0, 0, 0, '', 'size15x marginrightonly');
125 print '</a>';
126} else {
127 print '<a id="acreatedir" href="#" class="inline-block valignmiddle toolbarbutton paddingtop" title="'.$langs->trans("NotAllowed").'">';
128 print img_picto('', 'folder-plus', 'disabled', 0, 0, 0, '', 'size15x marginrightonly');
129 print '</a>';
130}
131if ($module == 'ecm') {
132 if (!empty($conf->use_javascript_ajax) && !getDolGlobalString('MAIN_ECM_DISABLE_JS')) {
133 $tmpurl = '#';
134 } else {
135 $paramsrefresh = array('action' => 'refreshmanual', 'module' => $module);
136 if ($section) {
137 $paramsrefresh['section'] = $section;
138 }
139 $tmpurl = dolBuildUrl($_SERVER["PHP_SELF"], $paramsrefresh);
140 }
141 print '<a id="arefreshbutton" href="'.$tmpurl.'" class="inline-block valignmiddle toolbarbutton paddingtop" title="'.dol_escape_htmltag($langs->trans('ReSyncListOfDir')).'">';
142 print img_picto('', 'refresh', 'id="refreshbutton"', 0, 0, 0, '', 'size15x marginrightonly');
143 print '</a>';
144}
145if ($permtoadd && GETPOSTISSET('website')) { // If on file manager to manage medias of a web site
146 // @phan-suppress-next-line PhanTypeExpectedObjectPropAccess
147 print '<a id="agenerateimgwebp" href="'.dolBuildUrl($_SERVER["PHP_SELF"], array('action' => 'confirmconvertimgwebp', 'website' => $website->ref), true).'" class="inline-block valignmiddle toolbarbutton paddingtop" title="'.dol_escape_htmltag($langs->trans("GenerateImgWebp")).'">';
148 print img_picto('', 'images', '', 0, 0, 0, '', 'size15x flip marginrightonly');
149 print '</a>';
150} elseif ($permtoadd && $module == 'ecm') { // If on file manager medias in ecm
151 if (getDolGlobalInt('ECM_SHOW_GENERATE_WEBP_BUTTON')) {
152 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")).'">';
153 print img_picto('', 'images', '', 0, 0, 0, '', 'size15x flip marginrightonly');
154 print '</a>';
155 }
156}
157
158print "<script>
159$('#acreatedir').on('click', function() {
160 try{
161 section_dir = $('.directory.expanded')[$('.directory.expanded').length-1].children[0].rel;
162 section = $('.directory.expanded')[$('.directory.expanded').length-1].children[0].id.split('_')[2];
163 catParent = ";
164if ($module == 'ecm') {
165 print "section;";
166} else {
167 print "section_dir.substring(0, section_dir.length - 1);";
168}
169print "
170 } catch{
171 section_dir = '/';
172 section = 0;
173 catParent = ";
174if ($module == 'ecm') {
175 print "section;";
176} else {
177 print "section_dir;";
178}
179print "
180 }
181 console.log('We click to create a new directory, we set current section_dir='+section_dir+' into href url of button acreatedir');
182 $('#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));
183 console.log($('#acreatedir').attr('href'));
184});
185$('#agenerateimgwebp').on('click', function() {
186 try{
187 section_dir = $('.directory.expanded')[$('.directory.expanded').length-1].children[0].rel;
188 section = $('.directory.expanded')[$('.directory.expanded').length-1].children[0].id.split('_')[2];
189 } catch{
190 section_dir = '/';
191 section = 0;
192 }
193 console.log('We click to generate webp image, we set current section_dir='+section_dir+' into href url of button agenerateimgwebp');
194 $('#agenerateimgwebp').attr('href', $('#agenerateimgwebp').attr('href')+'&section_dir='+encodeURI(section_dir)+'&section='+encodeURI(section));
195 console.log($('#agenerateimgwebp').attr('href'));
196});
197</script>";
198
199// Start "Add new file" area
200$nameforformuserfile = 'formuserfileecm';
201
202print '<div class="inline-block valignmiddle floatright">';
203
204// Zone to attach a new file
205if ((!empty($conf->use_javascript_ajax) && !getDolGlobalString('MAIN_ECM_DISABLE_JS')) || !empty($section)) {
206 if ((empty($section) || $section == -1) && ($module != 'medias')) {
207 ?>
208 <script>
209 jQuery(document).ready(function() {
210 jQuery('#<?php echo $nameforformuserfile ?>').hide();
211 });
212 </script>
213 <?php
214 }
215
216 $sectiondir = GETPOST('file', 'alpha') ? GETPOST('file', 'alpha') : GETPOST('section_dir', 'alpha');
217
218 print '<!-- Start form to attach new file in filemanager.tpl.php sectionid='.$section.' sectiondir='.$sectiondir.' -->'."\n";
219 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
220 $formfile = new FormFile($db);
221 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);
222} else {
223 print '&nbsp;';
224}
225
226print '</div>';
227// End "Add new file" area
228
229
230print '</div>';
231// End top panel, toolbar
232
233?>
234</div>
235<div id="ecm-layout-west" class="inline-block">
236<?php
237// Start left area
238
239
240// Ask confirmation of deletion of directory
241if ($action == 'delete_section') {
242 print $form->formconfirm(dolBuildUrl($_SERVER["PHP_SELF"], array('section' => $section)), $langs->trans('DeleteSection'), $langs->trans('ConfirmDeleteSection', $ecmdir->label), 'confirm_deletesection', '', '', 1);
243}
244// End confirm
245
246// Ask confirmation to build webp images
247if ($action == 'confirmconvertimgwebp') {
248 $langs->load("ecm");
249
250 $section_dir = GETPOST('section_dir', 'alpha');
251 $section = GETPOST('section', 'alpha');
252 $file = GETPOST('filetoregenerate', 'alpha');
253 $form = new Form($db);
254 $formquestion = array();
255 $formquestion['section_dir'] = array('type' => 'hidden', 'value' => $section_dir, 'name' => 'section_dir');
256 $formquestion['section'] = array('type' => 'hidden', 'value' => $section, 'name' => 'section');
257 $formquestion['filetoregenerate'] = array('type' => 'hidden', 'value' => $file, 'name' => 'filetoregenerate');
258 if ($module == 'medias') {
259 $formquestion['website'] = array('type' => 'hidden', 'value' => $website->ref, 'name' => 'website'); // @phan-suppress-current-line PhanTypeExpectedObjectPropAccess
260 }
261 $paramsconvertimgwebp = array();
262 if (!empty($sortfield)) {
263 $paramsconvertimgwebp['sortfield'] = $sortfield;
264 }
265 if (!empty($sortorder)) {
266 $paramsconvertimgwebp['sortorder'] = $sortorder;
267 }
268 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);
269 $action = 'file_manager';
270}
271
272// Duplicate images into .webp
273if ($action == 'convertimgwebp' && $permtoadd) {
274 $file = GETPOST('filetoregenerate', 'alpha');
275
276 if ($module == 'medias') {
277 $imagefolder = $conf->website->dir_output.'/'.$websitekey.'/medias/'.dol_sanitizePathName(GETPOST('section_dir', 'alpha'));
278 } else {
279 $imagefolder = $conf->ecm->dir_output.'/'.dol_sanitizePathName(GETPOST('section_dir', 'alpha'));
280 }
281
282 include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
283
284 if (!empty($file)) {
285 $filelist = array();
286 $filelist[]["fullname"] = dol_osencode($imagefolder.'/'.$file); // get $imagefolder.'/'.$file infos
287 } else {
288 $regeximgext = getListOfPossibleImageExt();
289
290 $filelist = dol_dir_list($imagefolder, "files", 0, $regeximgext);
291 }
292
293 $nbconverted = 0;
294
295 foreach ($filelist as $filename) {
296 $filepath = $filename['fullname'];
297 if (!(substr_compare($filepath, 'webp', -strlen('webp')) === 0)) {
298 if (!empty($file) || !dol_is_file($filepathnoext.'.webp')) { // If file does not exists yet
299 if (image_format_supported($filepath) == 1) {
300 $filepathnoext = preg_replace("/\.[a-z0-9]+$/i", "", $filepath);
301 $result = dol_imageResizeOrCrop($filepath, 0, 0, 0, 0, 0, $filepathnoext.'.webp', 90);
302 if (!dol_is_file($result)) {
303 $error++;
304 setEventMessages($result, null, 'errors');
305 } else {
306 $nbconverted++;
307 }
308 }
309 }
310 }
311 if ($error) {
312 break;
313 }
314 }
315 if (!$error) {
316 if (!empty($file)) {
317 setEventMessages($langs->trans('SucessConvertChosenImgWebp'), null);
318 } else {
319 setEventMessages($langs->trans('SucessConvertImgWebp'), null);
320 }
321 }
322 $action = 'file_manager';
323}
324
325// List of directories
326if (empty($action) || $action == 'editfile' || $action == 'file_manager' || preg_match('/refresh/i', $action) || $action == 'delete') {
327 $langs->load("ecm");
328
329 print '<table class="liste centpercent noborder">'."\n";
330
331 print '<!-- Title for manual directories -->'."\n";
332 print '<tr class="liste_titre">'."\n";
333 print '<th class="liste_titre left">';
334 print '<span style="padding-left: 5px; padding-right: 5px;">'.$langs->trans("ECMSections").'</span>';
335 print '</th></tr>';
336
337 $showonrightsize = '';
338
339 // Manual section
340 $htmltooltip = $langs->trans("ECMAreaDesc2a");
341 $htmltooltip .= '<br>'.$langs->trans("ECMAreaDesc2b");
342
343 if (!empty($conf->use_javascript_ajax) && !getDolGlobalString('MAIN_ECM_DISABLE_JS')) {
344 // Show the link to "Root"
345 if ($showroot) {
346 $paramsroot = array('file_manager' => 1);
347 if (!empty($websitekey)) {
348 $paramsroot['website'] = $websitekey;
349 }
350 $paramsroot['pageid'] = $pageid;
351 print '<tr class="oddeven nohover"><td><div style="padding-left: 5px; padding-right: 5px;"><a href="'.dolBuildUrl($_SERVER["PHP_SELF"], $paramsroot).'">';
352 if ($module == 'medias') {
353 print $langs->trans("RootOfMedias");
354 } else {
355 print $langs->trans("Root");
356 }
357 print '</a></div></td></tr>';
358 }
359
360 print '<tr class="oddeven nohover"><td>';
361
362 // Show filemanager tree (will be filled by a call of ajax /ecm/tpl/enablefiletreeajax.tpl.php, later, that executes ajaxdirtree.php)
363 print '<div id="filetree" class="ecmfiletree"></div>';
364
365 if ($action == 'deletefile') {
366 print $form->formconfirm('eeeee', $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile', '', '', 'deletefile');
367 }
368
369 print '</td></tr>';
370 } else {
371 // Show filtree when ajax is disabled (rare)
372 print '<tr><td style="padding-left: 20px">';
373
374 $_POST['modulepart'] = $module;
375 $_POST['openeddir'] = GETPOST('openeddir');
376 $_POST['dir'] = empty($_POST['dir']) ? '/' : GETPOST('dir');
377
378 // Show filemanager tree (will be filled by direct include of ajaxdirtree.php in mode noajax, this will return all dir - all levels - to show)
379 print '<div id="filetree" class="ecmfiletree">';
380
381 // Variables that may be defined:
382 // $_GET['modulepart'], $_GET['openeddir'], $_GET['sortfield'], $_GET['sortorder']
383 // $_POST['dir']
384 $mode = 'noajax';
385 if (empty($url)) {
386 $url = DOL_URL_ROOT.'/ecm/index.php';
387 }
388 include DOL_DOCUMENT_ROOT.'/core/ajax/ajaxdirtree.php';
389
390 print '</div>';
391 print '</td></tr>';
392 }
393
394
395 print "</table>";
396}
397
398
399// End left panel
400?>
401</div>
402<div id="ecm-layout-center" class="inline-block">
403<div class="pane-in ecm-in-layout-center">
404<div id="ecmfileview" class="ecmfileview">
405<?php
406// Start right panel - List of content of a directory
407
408$mode = 'noajax';
409if (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)
410 if (!empty($module) && $module == 'medias' && !GETPOST('website')) {
411 $url = DOL_URL_ROOT.'/ecm/index_medias.php';
412 } elseif (GETPOSTISSET('website')) {
413 $url = DOL_URL_ROOT.'/website/index.php';
414 } else {
415 $url = DOL_URL_ROOT.'/ecm/index.php';
416 }
417}
418include DOL_DOCUMENT_ROOT.'/core/ajax/ajaxdirpreview.php'; // Show content of a directory on right side
419
420
421// End right panel
422?>
423</div>
424</div>
425
426</div>
427</div> <!-- End div id="containerlayout" -->
428<?php
429
430
431if (!empty($conf->use_javascript_ajax) && !getDolGlobalString('MAIN_ECM_DISABLE_JS')) { // Show filtree when ajax is enabled
432 //var_dump($modulepart);
433 // Variables that may be defined:
434 // $_GET['modulepart'], $_GET['openeddir'], $_GET['sortfield'], $_GET['sortorder']
435 // $_POST['dir']
436 // $_POST['section_dir'], $_POST['section_id'], $_POST['token'], $_POST['max_file_size'], $_POST['sendit']
437 if (GETPOST('section_dir', 'alpha')) {
438 $preopened = GETPOST('section_dir', 'alpha');
439 }
440
441 include DOL_DOCUMENT_ROOT.'/ecm/tpl/enablefiletreeajax.tpl.php';
442}
443
444?>
445<!-- 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.
if(! $sortfield) if(! $sortorder) $module
Definition list.php:193
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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:65
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.
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:181
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.