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