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