filemanager.tpl.php 14 KB

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