dolibarr 25.0.0-alpha
enablefiletreeajax.tpl.php
1<?php
2/* Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
3 * Copyright (C) 2018 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2025-2026 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2026 MDW <mdeweerd@users.noreply.github.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 *
20 * Output javascript for interactions code of ecm module
21 * $conf, $module, $param, $preopened, $nameforformuserfile may be defined
22 */
23
29'
30@phan-var-force string $param
31';
32// Protection to avoid direct call of template
33if (empty($conf) || !is_object($conf)) {
34 print "Error, template enablefiletreeajax.tpl.php can't be called as URL";
35 exit;
36}
37// Must have set $module, $nameforformuserfile, $preopened
38?>
39
40<!-- BEGIN PHP TEMPLATE ecm/tpl/enablefiletreeajax.tpl.php -->
41<!-- Doc of fileTree plugin at https://www.abeautifulsite.net/jquery-file-tree -->
42
43<script type="text/javascript">
44
45<?php
46if (empty($module)) {
47 $module = 'ecm';
48}
49if (empty($nameforformuserfile)) {
50 $nameforformuserfile = '';
51}
52$paramwithoutsection = preg_replace('/&?section=(\d+)/', '', $param);
53
54$openeddir = '/'; // The root directory shown
55// $preopened // The dir to have preopened
56
57// We must use token=currentToken() and not newToken() here because ajaxdirtree has NOTOKENRENEWAL define so there
58// is no rollup of token so we must compare with the one valid on main page.
59$paramsdirtree = array(
60 'token' => currentToken(),
61 'modulepart' => $module,
62 'openeddir' => $openeddir,
63);
64if (!empty($preopened)) {
65 $paramsdirtree['preopened'] = $preopened;
66}
67// $paramwithoutsection is a raw, already-built query string fragment (starting with '&' when not empty), so it is
68// appended as-is after the encoded params above, not merged into $paramsdirtree.
69$dirtreeurl = dolBuildUrl(DOL_URL_ROOT.'/core/ajax/ajaxdirtree.php', $paramsdirtree).(empty($paramwithoutsection) ? '' : $paramwithoutsection);
70
71?>
72
73$(document).ready(function() {
74
75 $('#filetree').fileTree({
76 root: '<?php print dol_escape_js($openeddir); ?>',
77 // Ajax called if we click to expand a dir (not a file). Parameter 'dir' is provided as a POST parameter by fileTree code to this following URL.
78 script: '<?php echo $dirtreeurl; ?>',
79 folderEvent: 'click', // 'dblclick'
80 multiFolder: false },
81 // Called if we click on a file (not a dir)
82 function(file) {
83 console.log("We click on a file "+file);
84 $("#mesg").hide();
85 loadandshowpreview(file,0);
86 },
87 // Called if we click on a dir (not a file)
88 function(elem) {
89 id=elem.attr('id').substr(12); // We get id that is 'fmdirlia_id_xxx' (id we want is xxx)
90 rel=elem.attr('rel')
91 console.log("We click on a dir id="+id+", we call the ajaxdirtree.php with modulepart=<?php echo $module; ?>, param=<?php echo $paramwithoutsection; ?>");
92 console.log("We also save id and dir name into <?php echo $nameforformuserfile ?>_section_id|dir (vars into form to attach new file in filemanager.tpl.php) with id="+id+" and rel="+rel);
93 jQuery("#<?php echo $nameforformuserfile ?>_section_dir").val(rel);
94 jQuery("#<?php echo $nameforformuserfile ?>_section_id").val(id);
95 jQuery("#section_dir").val(rel);
96 jQuery("#section_id").val(id);
97 jQuery("#section").val(id);
98 jQuery('#<?php echo $nameforformuserfile ?>').show();
99 console.log("We also execute the loadandshowpreview() that is on the onclick of each li defined by ajaxdirtree");
100 }
101 // The loadanshowpreview is also call by the 'onclick' set on each li return by ajaxdirtree
102 );
103
104 $('#refreshbutton').click( function() {
105 console.log("Click on refreshbutton");
106
107 dolBlockUI("<?php echo $langs->transnoentities('PleaseBePatient'); ?>");
108
109 $.get("<?php echo DOL_URL_ROOT.'/ecm/ajax/ecmdatabase.php'; ?>", {
110 action: 'build',
111 token: '<?php echo currentToken(); ?>',
112 element: 'ecm'
113 }, function(response) {
114 setTimeout(() => {
115 dolUnblockUI();
116
117 location.href='<?php echo $_SERVER['PHP_SELF']; ?>';
118 }, 1000); // delai 1s
119 });
120 });
121});
122
123function loadandshowpreview(filedirname,section)
124{
125 //alert('filedirname='+filedirname);
126 //console.log(filedirname);
127 //console.log('loadandshowpreview for section='+section);
128
129 $('#ecmfileview').empty();
130
131 var url = '<?php echo dol_escape_js(dolBuildUrl(dol_buildpath('/core/ajax/ajaxdirpreview.php', 1), array('action' => 'preview', 'module' => $module))); ?>&section='+urlencode(section)+'&file='+urlencode(filedirname)<?php echo (empty($paramwithoutsection) ? '' : "+'".dol_escape_js($paramwithoutsection)."'"); ?>;
132 $.get(url, function(data) {
133 //alert('Load of url '+url+' was performed : '+data);
134 pos=data.indexOf("TYPE=directory",0);
135 //alert(pos);
136 if ((pos > 0) && (pos < 20))
137 {
138 filediractive=filedirname; // Save current dirname
139 filetypeactive='directory';
140 }
141 else
142 {
143 filediractive=filedirname; // Save current dirname
144 filetypeactive='file';
145 }
146 $('#ecmfileview').append(data);
147 });
148}
149
150</script>
151<!-- END PHP TEMPLATE ecm/tpl/enablefiletreeajax.tpl.php -->
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
currentToken()
Return the value of token currently saved into session with name 'token'.
dolBuildUrl($url, $params=[], $addtoken=false, $anchor='')
Return path of url.
dol_escape_js($stringtoescape, $mode=0, $noescapebackslashn=0)
Returns text escaped for inclusion into JavaScript code.
if(preg_match('/(crypted|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition repair.php:130