enablefiletreeajax.tpl.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /* Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
  3. * Copyright (C) 2018 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. *
  18. * Output javascript for interactions code of ecm module
  19. * $conf, $module, $param, $preopened, $nameforformuserfile may be defined
  20. */
  21. // Protection to avoid direct call of template
  22. if (empty($conf) || !is_object($conf)) {
  23. print "Error, template enablefiletreeajax.tpl.php can't be called as URL";
  24. exit;
  25. }
  26. // Must have set $module, $nameforformuserfile, $preopened
  27. ?>
  28. <!-- BEGIN PHP TEMPLATE ecm/tpl/enablefiletreeajax.tpl.php -->
  29. <!-- Doc of fileTree plugin at https://www.abeautifulsite.net/jquery-file-tree -->
  30. <script type="text/javascript">
  31. <?php
  32. if (empty($module)) {
  33. $module = 'ecm';
  34. }
  35. if (empty($nameforformuserfile)) {
  36. $nameforformuserfile = '';
  37. }
  38. $paramwithoutsection = preg_replace('/&?section=(\d+)/', '', $param);
  39. $openeddir = '/'; // The root directory shown
  40. // $preopened // The dir to have preopened
  41. ?>
  42. $(document).ready(function() {
  43. $('#filetree').fileTree({
  44. root: '<?php print dol_escape_js($openeddir); ?>',
  45. // 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.
  46. // We must use token=currentToken() and not newToken() here because ajaxdirtree has NOTOKENRENEWAL define so there is no rollup of token so we must compare with the one valid on main page
  47. script: '<?php echo DOL_URL_ROOT.'/core/ajax/ajaxdirtree.php?token='.currentToken().'&modulepart='.urlencode($module).(empty($preopened) ? '' : '&preopened='.urlencode($preopened)).'&openeddir='.urlencode($openeddir).(empty($paramwithoutsection) ? '' : $paramwithoutsection); ?>',
  48. folderEvent: 'click', // 'dblclick'
  49. multiFolder: false },
  50. // Called if we click on a file (not a dir)
  51. function(file) {
  52. console.log("We click on a file "+file);
  53. $("#mesg").hide();
  54. loadandshowpreview(file,0);
  55. },
  56. // Called if we click on a dir (not a file)
  57. function(elem) {
  58. id=elem.attr('id').substr(12); // We get id that is 'fmdirlia_id_xxx' (id we want is xxx)
  59. rel=elem.attr('rel')
  60. console.log("We click on a dir id="+id+", we call the ajaxdirtree.php with modulepart=<?php echo $module; ?>, param=<?php echo $paramwithoutsection; ?>");
  61. 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);
  62. jQuery("#<?php echo $nameforformuserfile ?>_section_dir").val(rel);
  63. jQuery("#<?php echo $nameforformuserfile ?>_section_id").val(id);
  64. jQuery("#section_dir").val(rel);
  65. jQuery("#section_id").val(id);
  66. jQuery("#section").val(id);
  67. jQuery('#<?php echo $nameforformuserfile ?>').show();
  68. console.log("We also execute the loadandshowpreview() that is on the onclick of each li defined by ajaxdirtree");
  69. }
  70. // The loadanshowpreview is also call by the 'onclick' set on each li return by ajaxdirtree
  71. );
  72. $('#refreshbutton').click( function() {
  73. console.log("Click on refreshbutton");
  74. $.pleaseBePatient("<?php echo $langs->trans('PleaseBePatient'); ?>");
  75. $.get("<?php echo DOL_URL_ROOT.'/ecm/ajax/ecmdatabase.php'; ?>", {
  76. action: 'build',
  77. token: '<?php echo newToken(); ?>',
  78. element: 'ecm'
  79. },
  80. function(response) {
  81. $.unblockUI();
  82. location.href='<?php echo $_SERVER['PHP_SELF']; ?>';
  83. });
  84. });
  85. });
  86. function loadandshowpreview(filedirname,section)
  87. {
  88. //alert('filedirname='+filedirname);
  89. //console.log(filedirname);
  90. //console.log('loadandshowpreview for section='+section);
  91. $('#ecmfileview').empty();
  92. var url = '<?php echo dol_buildpath('/core/ajax/ajaxdirpreview.php', 1); ?>?action=preview&module=<?php echo $module; ?>&section='+section+'&file='+urlencode(filedirname)<?php echo (empty($paramwithoutsection) ? '' : "+'".$paramwithoutsection."'"); ?>;
  93. $.get(url, function(data) {
  94. //alert('Load of url '+url+' was performed : '+data);
  95. pos=data.indexOf("TYPE=directory",0);
  96. //alert(pos);
  97. if ((pos > 0) && (pos < 20))
  98. {
  99. filediractive=filedirname; // Save current dirname
  100. filetypeactive='directory';
  101. }
  102. else
  103. {
  104. filediractive=filedirname; // Save current dirname
  105. filetypeactive='file';
  106. }
  107. $('#ecmfileview').append(data);
  108. });
  109. }
  110. </script>
  111. <!-- END PHP TEMPLATE ecm/tpl/enablefiletreeajax.tpl.php -->