enablefiletreeajax.tpl.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. ?>
  27. <!-- BEGIN PHP TEMPLATE ecm/tpl/enablefiletreeajax.tpl.php -->
  28. <!-- Doc of fileTree plugin at https://www.abeautifulsite.net/jquery-file-tree -->
  29. <script type="text/javascript">
  30. <?php
  31. if (empty($module)) {
  32. $module = 'ecm';
  33. }
  34. if (empty($nameforformuserfile)) {
  35. $nameforformuserfile = '';
  36. }
  37. $paramwithoutsection = preg_replace('/&?section=(\d+)/', '', $param);
  38. $openeddir = '/'; // The root directory shown
  39. // $preopened // The dir to have preopened
  40. ?>
  41. $(document).ready(function() {
  42. $('#filetree').fileTree({
  43. root: '<?php print dol_escape_js($openeddir); ?>',
  44. // 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.
  45. // 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
  46. 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); ?>',
  47. folderEvent: 'click', // 'dblclick'
  48. multiFolder: false },
  49. // Called if we click on a file (not a dir)
  50. function(file) {
  51. console.log("We click on a file");
  52. $("#mesg").hide();
  53. loadandshowpreview(file,0);
  54. },
  55. // Called if we click on a dir (not a file)
  56. function(elem) {
  57. id=elem.attr('id').substr(12); // We get id that is 'fmdirlia_id_xxx' (id we want is xxx)
  58. rel=elem.attr('rel')
  59. console.log("We click on a dir, we call the ajaxdirtree.php with modulepart=<?php echo $module; ?>, param=<?php echo $paramwithoutsection; ?>");
  60. 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);
  61. jQuery("#<?php echo $nameforformuserfile ?>_section_dir").val(rel);
  62. jQuery("#<?php echo $nameforformuserfile ?>_section_id").val(id);
  63. jQuery("#section_dir").val(rel);
  64. jQuery("#section_id").val(id);
  65. jQuery("#section").val(id);
  66. jQuery('#<?php echo $nameforformuserfile ?>').show();
  67. console.log("We also execute the loadandshowpreview() that is on the onclick of each li defined by ajaxdirtree");
  68. }
  69. // The loadanshowpreview is also call by the 'onclick' set on each li return by ajaxdirtree
  70. );
  71. $('#refreshbutton').click( function() {
  72. console.log("Click on refreshbutton");
  73. $.pleaseBePatient("<?php echo $langs->trans('PleaseBePatient'); ?>");
  74. $.get("<?php echo DOL_URL_ROOT.'/ecm/ajax/ecmdatabase.php'; ?>", {
  75. action: 'build',
  76. token: '<?php echo newToken(); ?>',
  77. element: 'ecm'
  78. },
  79. function(response) {
  80. $.unblockUI();
  81. location.href='<?php echo $_SERVER['PHP_SELF']; ?>';
  82. });
  83. });
  84. });
  85. function loadandshowpreview(filedirname,section)
  86. {
  87. //alert('filedirname='+filedirname);
  88. //console.log(filedirname);
  89. //console.log('loadandshowpreview for section='+section);
  90. $('#ecmfileview').empty();
  91. 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."'"); ?>;
  92. $.get(url, function(data) {
  93. //alert('Load of url '+url+' was performed : '+data);
  94. pos=data.indexOf("TYPE=directory",0);
  95. //alert(pos);
  96. if ((pos > 0) && (pos < 20))
  97. {
  98. filediractive=filedirname; // Save current dirname
  99. filetypeactive='directory';
  100. }
  101. else
  102. {
  103. filediractive=filedirname; // Save current dirname
  104. filetypeactive='file';
  105. }
  106. $('#ecmfileview').append(data);
  107. });
  108. }
  109. </script>
  110. <!-- END PHP TEMPLATE ecm/tpl/enablefiletreeajax.tpl.php -->