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