dolibarr  16.0.5
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
23 if (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 
35 require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
36 
37 $langs->load("ecm");
38 
39 if (empty($module)) {
40  $module = 'ecm';
41 }
42 
43 $permtoadd = 0;
44 $permtoupload = 0;
45 $showroot = 0;
46 if ($module == 'ecm') {
47  $permtoadd = $user->rights->ecm->setup;
48  $permtoupload = $user->rights->ecm->upload;
49  $showroot = 0;
50 }
51 if ($module == 'medias') {
52  $permtoadd = ($user->rights->mailing->creer || $user->rights->website->write);
53  $permtoupload = ($user->rights->mailing->creer || $user->rights->website->write);
54  $showroot = 1;
55 }
56 
57 
58 
59 // Confirm remove file (for non javascript users)
60 if (($action == 'delete' || $action == 'file_manager_delete') && empty($conf->use_javascript_ajax)) {
61  // TODO Add website, pageid, filemanager if defined
62  print $form->formconfirm($_SERVER["PHP_SELF"].'?section='.$section.'&urlfile='.urlencode($_GET["urlfile"]), $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile', '', '', 1);
63 }
64 
65 // Start container of all panels
66 ?>
67 <!-- Begin div id="containerlayout" -->
68 <div id="containerlayout">
69 <div id="ecm-layout-north" class="toolbar largebutton">
70 <?php
71 
72 // Start top panel, toolbar
73 print '<div class="inline-block toolbarbutton centpercent">';
74 
75 // Toolbar
76 if ($permtoadd) {
77  $websitekeyandpageid = (!empty($websitekey) ? '&website='.urlencode($websitekey) : '').(!empty($pageid) ? '&pageid='.urlencode($pageid) : '');
78  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')).'">';
79  print img_picto('', 'folder-plus', '', false, 0, 0, '', 'size15x marginrightonly');
80  print '</a>';
81 } else {
82  print '<a id="acreatedir" href="#" class="inline-block valignmiddle toolbarbutton paddingtop" title="'.$langs->trans("NotAllowed").'">';
83  print img_picto('', 'folder-plus', 'disabled', false, 0, 0, '', 'size15x marginrightonly');
84  print '</a>';
85 }
86 if ($module == 'ecm') {
87  $tmpurl = ((!empty($conf->use_javascript_ajax) && empty($conf->global->MAIN_ECM_DISABLE_JS)) ? '#' : ($_SERVER["PHP_SELF"].'?action=refreshmanual'.($module ? '&amp;module='.$module : '').($section ? '&amp;section='.$section : '')));
88  print '<a id="arefreshbutton" href="'.$tmpurl.'" class="inline-block valignmiddle toolbarbutton paddingtop" title="'.dol_escape_htmltag($langs->trans('ReSyncListOfDir')).'">';
89  print img_picto('', 'refresh', 'id="refreshbutton"', false, 0, 0, '', 'size15x marginrightonly');
90  print '</a>';
91 }
92 if ($permtoadd && GETPOSTISSET('website')) { // If on file manager to manage medias of a web site
93  print '<a id="agenerateimgwebp" href="'.$_SERVER["PHP_SELF"].'?action=confirmconvertimgwebp&token='.newToken().'&website='.$website->ref.'" class="inline-block valignmiddle toolbarbutton paddingtop" title="'.dol_escape_htmltag($langs->trans("GenerateImgWebp")).'">';
94  print img_picto('', 'images', '', false, 0, 0, '', 'size15x flip marginrightonly');
95  print '</a>';
96 } elseif ($permtoadd && $module == 'ecm') { // If on file manager medias in ecm
97  if (getDolGlobalInt('ECM_SHOW_GENERATE_WEBP_BUTTON')) {
98  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")).'">';
99  print img_picto('', 'images', '', false, 0, 0, '', 'size15x flip marginrightonly');
100  print '</a>';
101  }
102 }
103 
104 print "<script>
105 $('#acreatedir').on('click', function() {
106  try{
107  section_dir = $('.directory.expanded')[$('.directory.expanded').length-1].children[0].rel;
108  section = $('.directory.expanded')[$('.directory.expanded').length-1].children[0].id.split('_')[2];
109  } catch{
110  section_dir = '/';
111  section = 0;
112  }
113  console.log('We click to create a new directory, we set current section_dir='+section_dir+' into href url of button acreatedir');
114  $('#acreatedir').attr('href', $('#acreatedir').attr('href')+'&section_dir='+encodeURI(section_dir)+'&section='+encodeURI(section));
115  console.log($('#acreatedir').attr('href'));
116 });
117 $('#agenerateimgwebp').on('click', function() {
118  try{
119  section_dir = $('.directory.expanded')[$('.directory.expanded').length-1].children[0].rel;
120  section = $('.directory.expanded')[$('.directory.expanded').length-1].children[0].id.split('_')[2];
121  } catch{
122  section_dir = '/';
123  section = 0;
124  }
125  console.log('We click to generate webp image, we set current section_dir='+section_dir+' into href url of button agenerateimgwebp');
126  $('#agenerateimgwebp').attr('href', $('#agenerateimgwebp').attr('href')+'&section_dir='+encodeURI(section_dir)+'&section='+encodeURI(section));
127  console.log($('#agenerateimgwebp').attr('href'));
128 });
129 </script>";
130 
131 // Start "Add new file" area
132 $nameforformuserfile = 'formuserfileecm';
133 
134 print '<div class="inline-block valignmiddle floatright">';
135 
136 // For to attach a new file
137 if ((!empty($conf->use_javascript_ajax) && empty($conf->global->MAIN_ECM_DISABLE_JS)) || !empty($section)) {
138  if ((empty($section) || $section == -1) && ($module != 'medias')) {
139  ?>
140  <script>
141  jQuery(document).ready(function() {
142  jQuery('#<?php echo $nameforformuserfile ?>').hide();
143  });
144  </script>
145  <?php
146  }
147 
148  $sectiondir = GETPOST('file', 'alpha') ?GETPOST('file', 'alpha') : GETPOST('section_dir', 'alpha');
149 
150  print '<!-- Start form to attach new file in filemanager.tpl.php sectionid='.$section.' sectiondir='.$sectiondir.' -->'."\n";
151  include_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
152  $formfile = new FormFile($db);
153  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);
154 } else {
155  print '&nbsp;';
156 }
157 
158 print '</div>';
159 // End "Add new file" area
160 
161 
162 print '</div>';
163 // End top panel, toolbar
164 
165 ?>
166 </div>
167 <div id="ecm-layout-west" class="inline-block">
168 <?php
169 // Start left area
170 
171 
172 // Ask confirmation of deletion of directory
173 if ($action == 'delete_section') {
174  print $form->formconfirm($_SERVER["PHP_SELF"].'?section='.$section, $langs->trans('DeleteSection'), $langs->trans('ConfirmDeleteSection', $ecmdir->label), 'confirm_deletesection', '', '', 1);
175 }
176 // End confirm
177 
178 // Ask confirmation to build webp images
179 if ($action == 'confirmconvertimgwebp') {
180  $langs->load("ecm");
181 
182  $section_dir=GETPOST('section_dir', 'alpha');
183  $section=GETPOST('section', 'alpha');
184  $form = new Form($db);
185  $formquestion['section_dir']=array('type'=>'hidden', 'value'=>$section_dir, 'name'=>'section_dir');
186  $formquestion['section']=array('type'=>'hidden', 'value'=>$section, 'name'=>'section');
187  if ($module == 'medias') {
188  $formquestion['website']=array('type'=>'hidden', 'value'=>$website->ref, 'name'=>'website');
189  }
190  print $form->formconfirm($_SERVER["PHP_SELF"], $langs->trans('ConfirmImgWebpCreation'), $langs->trans('ConfirmGenerateImgWebp', $object->ref), 'convertimgwebp', $formquestion, "yes", 1);
191  $action = 'file_manager';
192 }
193 
194 // Duplicate images into .webp
195 if ($action == 'convertimgwebp' && $permtoadd) {
196  if ($module == 'medias') {
197  $imagefolder = $conf->website->dir_output.'/'.$websitekey.'/medias/'.dol_sanitizePathName(GETPOST('section_dir', 'alpha'));
198  } else {
199  $imagefolder = $conf->ecm->dir_output.'/'.dol_sanitizePathName(GETPOST('section_dir', 'alpha'));
200  }
201 
202  include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
203 
204  $regeximgext = getListOfPossibleImageExt();
205 
206  $filelist = dol_dir_list($imagefolder, "files", 0, $regeximgext);
207 
208  $nbconverted = 0;
209 
210  foreach ($filelist as $filename) {
211  $filepath = $filename['fullname'];
212  if (!(substr_compare($filepath, 'webp', -strlen('webp')) === 0)) {
213  if (image_format_supported($filepath) == 1) {
214  $filepathnoext = preg_replace("/\.[a-z0-9]+$/i", "", $filepath);
215 
216  if (! dol_is_file($filepathnoext.'.webp')) { // If file does not exists yet
217  $result = dol_imageResizeOrCrop($filepath, 0, 0, 0, 0, 0, $filepathnoext.'.webp', 90);
218  if (!dol_is_file($result)) {
219  $error++;
220  setEventMessages($result, null, 'errors');
221  } else {
222  $nbconverted++;
223  }
224  }
225  }
226  }
227  if ($error) {
228  break;
229  }
230  }
231  if (!$error) {
232  setEventMessages($langs->trans('SucessConvertImgWebp'), null);
233  }
234  $action = 'file_manager';
235 }
236 
237 if (empty($action) || $action == 'editfile' || $action == 'file_manager' || preg_match('/refresh/i', $action) || $action == 'delete') {
238  $langs->load("ecm");
239 
240  print '<table class="liste centpercent">'."\n";
241 
242  print '<!-- Title for manual directories -->'."\n";
243  print '<tr class="liste_titre">'."\n";
244  print '<th class="liste_titre left">';
245  print '<span style="padding-left: 5px; padding-right: 5px;">'.$langs->trans("ECMSections").'</span>';
246  print '</th></tr>';
247 
248  $showonrightsize = '';
249 
250  // Manual section
251  $htmltooltip = $langs->trans("ECMAreaDesc2");
252 
253  if (!empty($conf->use_javascript_ajax) && empty($conf->global->MAIN_ECM_DISABLE_JS)) {
254  // Show the link to "Root"
255  if ($showroot) {
256  print '<tr><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).'">';
257  if ($module == 'medias') {
258  print $langs->trans("RootOfMedias");
259  } else {
260  print $langs->trans("Root");
261  }
262  print '</a></div></td></tr>';
263  }
264 
265  print '<tr><td>';
266 
267  // Show filemanager tree (will be filled by a call of ajax /ecm/tpl/enablefiletreeajax.tpl.php, later, that executes ajaxdirtree.php)
268  print '<div id="filetree" class="ecmfiletree"></div>';
269 
270  if ($action == 'deletefile') {
271  print $form->formconfirm('eeeee', $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile', '', '', 'deletefile');
272  }
273 
274  print '</td></tr>';
275  } else { // Show filtree when ajax is disabled (rare)
276  print '<tr><td style="padding-left: 20px">';
277 
278  $_POST['modulepart'] = $module;
279  $_POST['openeddir'] = GETPOST('openeddir');
280  $_POST['dir'] = empty($_POST['dir']) ? '/' : $_POST['dir'];
281 
282  // Show filemanager tree (will be filled by direct include of ajaxdirtree.php in mode noajax, this will return all dir - all levels - to show)
283  print '<div id="filetree" class="ecmfiletree">';
284 
285  // Variables that may be defined:
286  // $_GET['modulepart'], $_GET['openeddir'], $_GET['sortfield'], $_GET['sortorder']
287  // $_POST['dir']
288  $mode = 'noajax';
289  if (empty($url)) {
290  $url = DOL_URL_ROOT.'/ecm/index.php';
291  }
292  include DOL_DOCUMENT_ROOT.'/core/ajax/ajaxdirtree.php';
293 
294  print '</div>';
295  print '</td></tr>';
296  }
297 
298 
299  print "</table>";
300 }
301 
302 
303 // End left panel
304 ?>
305 </div>
306 <div id="ecm-layout-center" class="inline-block">
307 <div class="pane-in ecm-in-layout-center">
308 <div id="ecmfileview" class="ecmfileview">
309 <?php
310 // Start right panel - List of content of a directory
311 
312 
313 $mode = 'noajax';
314 if (empty($url)) {
315  $url = DOL_URL_ROOT.'/ecm/index.php';
316 }
317 include DOL_DOCUMENT_ROOT.'/core/ajax/ajaxdirpreview.php'; // Show content of a directory on right side
318 
319 
320 // End right panel
321 ?>
322 </div>
323 </div>
324 
325 </div>
326 </div> <!-- End div id="containerlayout" -->
327 <?php
328 
329 
330 if (!empty($conf->use_javascript_ajax) && empty($conf->global->MAIN_ECM_DISABLE_JS)) { // Show filtree when ajax is enabled
331  //var_dump($modulepart);
332  // Variables that may be defined:
333  // $_GET['modulepart'], $_GET['openeddir'], $_GET['sortfield'], $_GET['sortorder']
334  // $_POST['dir']
335  // $_POST['section_dir'], $_POST['section_id'], $_POST['token'], $_POST['max_file_size'], $_POST['sendit']
336  if (GETPOST('section_dir', 'alpha')) {
337  $preopened = GETPOST('section_dir', 'alpha');
338  }
339 
340  include DOL_DOCUMENT_ROOT.'/ecm/tpl/enablefiletreeajax.tpl.php';
341 }
342 
343 ?>
344 <!-- END PHP TEMPLATE core/tpl/filemanager.tpl.php -->
dol_sanitizePathName
dol_sanitizePathName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a path name.
Definition: functions.lib.php:1251
dol_escape_htmltag
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
Definition: functions.lib.php:1468
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_imageResizeOrCrop
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)
Definition: images.lib.php:177
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
image_format_supported
image_format_supported($file, $acceptsvg=0)
Return if a filename is file name of a supported image format.
Definition: images.lib.php:80
dol_dir_list
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0)
Scan a directory and return a list of files/directories.
Definition: files.lib.php:60
getListOfPossibleImageExt
getListOfPossibleImageExt($acceptsvg=0)
Return if a filename is file name of a supported image format.
Definition: images.lib.php:61
dol_is_file
dol_is_file($pathoffile)
Return if path is a file.
Definition: files.lib.php:477
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
FormFile
Class to offer components to list and upload files.
Definition: html.formfile.class.php:36
newToken
newToken()
Return the value of token currently saved into session with name 'newtoken'.
Definition: functions.lib.php:10878
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:386
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
getDolGlobalInt
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
Definition: functions.lib.php:93