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