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