expensereport_linktofile.tpl.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. // Add line to select existing file
  3. if (empty($conf->global->EXPENSEREPORT_DISABLE_ATTACHMENT_ON_LINES)) {
  4. print '<!-- expensereport_linktofile.tpl.php -->'."\n";
  5. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  6. require_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
  7. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  8. $upload_dir = $conf->expensereport->dir_output."/".dol_sanitizeFileName($object->ref);
  9. $arrayoffiles = dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png|'.preg_quote(dol_sanitizeFileName($object->ref.'.pdf'), '/').')$');
  10. $nbFiles = count($arrayoffiles);
  11. $nbLinks = Link::count($db, $object->element, $object->id);
  12. if ($nbFiles > 0) {
  13. print '<tr class="trattachnewfilenow'.(empty($tredited) ? ' oddeven nohover' : ' '.$tredited).'"'.(!GETPOSTISSET('sendit') && empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER) ? ' style="display: none"' : '').'>';
  14. // Num line
  15. if ($action == 'editline') {
  16. print '<td></td>';
  17. }
  18. // Select image section
  19. print '<td colspan="'.($action == 'editline' ? $colspan - 1 : $colspan).'">';
  20. //print '<span class="opacitymedium">'.$langs->trans("AttachTheNewLineToTheDocument").'</span><br>';
  21. $modulepart = 'expensereport'; $maxheightmini = 48;
  22. $relativepath = (!empty($object->ref) ?dol_sanitizeFileName($object->ref) : '').'/';
  23. $filei = 0;
  24. // Loop on each attached file
  25. foreach ($arrayoffiles as $file) {
  26. $urlforhref = array();
  27. $filei++;
  28. print '<div class="inline-block margintoponly marginleftonly marginrightonly center valigntop">';
  29. $fileinfo = pathinfo($file['fullname']);
  30. if (image_format_supported($file['name']) > 0) {
  31. $minifile = getImageFileNameForSize($file['name'], '_mini'); // For new thumbs using same ext (in lower case however) than original
  32. //print $file['path'].'/'.$minifile.'<br>';
  33. $urlforhref = getAdvancedPreviewUrl($modulepart, $relativepath.$fileinfo['filename'].'.'.strtolower($fileinfo['extension']), 1, '&entity='.(!empty($object->entity) ? $object->entity : $conf->entity));
  34. if (empty($urlforhref)) {
  35. $urlforhref = DOL_URL_ROOT.'/viewimage.php?modulepart='.$modulepart.'&entity='.(!empty($object->entity) ? $object->entity : $conf->entity).'&file='.urlencode($fileinfo['relativename'].'.'.strtolower($fileinfo['extension']));
  36. print '<a href="'.$urlforhref.'" class="aphoto" target="_blank" rel="noopener noreferrer">';
  37. } else {
  38. print '<a href="'.$urlforhref['url'].'" class="'.$urlforhref['css'].'" target="'.$urlforhref['target'].'" mime="'.$urlforhref['mime'].'">';
  39. }
  40. print '<div class="photoref backgroundblank">';
  41. print '<img class="photoexpensereport photorefcenter" height="'.$maxheightmini.'" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$modulepart.'&entity='.(!empty($object->entity) ? $object->entity : $conf->entity).'&file='.urlencode($relativepath.$minifile).'" title="">';
  42. print '</div>';
  43. print '</a>';
  44. } else {
  45. $error = 0;
  46. $thumbshown = '';
  47. if (preg_match('/\.pdf$/i', $file['name'])) {
  48. $urlforhref = getAdvancedPreviewUrl($modulepart, $relativepath.$fileinfo['filename'].'.'.strtolower($fileinfo['extension']), 1, '&entity='.(!empty($object->entity) ? $object->entity : $conf->entity));
  49. $filepdf = $conf->expensereport->dir_output.'/'.$relativepath.$file['name'];
  50. $fileimage = $conf->expensereport->dir_output.'/'.$relativepath.$file['name'].'_preview.png';
  51. $relativepathimage = $relativepath.$file['name'].'_preview.png';
  52. $pdfexists = file_exists($filepdf);
  53. if ($pdfexists) {
  54. // Conversion du PDF en image png si fichier png non existant
  55. if (!file_exists($fileimage) || (filemtime($fileimage) < filemtime($filepdf))) {
  56. if (empty($conf->global->MAIN_DISABLE_PDF_THUMBS)) { // If you experience trouble with pdf thumb generation and imagick, you can disable here.
  57. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  58. $ret = dol_convert_file($filepdf, 'png', $fileimage, '0'); // Convert first page of PDF into a file _preview.png
  59. if ($ret < 0) {
  60. $error++;
  61. }
  62. }
  63. }
  64. }
  65. if ($pdfexists && !$error) {
  66. $heightforphotref = 70;
  67. if (!empty($conf->dol_optimize_smallscreen)) {
  68. $heightforphotref = 60;
  69. }
  70. // If the preview file is found
  71. if (file_exists($fileimage)) {
  72. $thumbshown = '<img height="'.$heightforphotref.'" class="photo photowithmargin photowithborder" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=apercu'.urlencode($modulepart).'&file='.urlencode($relativepathimage).'">';
  73. }
  74. }
  75. }
  76. if (empty($urlforhref) || empty($thumbshown)) {
  77. print '<span href="" class="aphoto" target="_blank" rel="noopener noreferrer">';
  78. } else {
  79. print '<a href="'.$urlforhref['url'].'" class="'.$urlforhref['css'].'" target="'.$urlforhref['target'].'" mime="'.$urlforhref['mime'].'">';
  80. }
  81. print '<div class="photoref backgroundblank">';
  82. print $thumbshown ? $thumbshown : img_mime($minifile);
  83. print '</div>';
  84. if (empty($urlforhref) || empty($thumbshown)) {
  85. print '</span>';
  86. } else {
  87. print '</a>';
  88. }
  89. }
  90. print '<br>';
  91. $checked = '';
  92. //var_dump(GETPOST($file['relativename']));
  93. //var_dump($file['relativename']);
  94. //var_dump($_FILES['userfile']['name']);
  95. // If a file was just uploaded, we check to preselect it
  96. if (is_array($_FILES['userfile']['name'])) {
  97. foreach ($_FILES['userfile']['name'] as $tmpfile) {
  98. if ($file['relativename'] == (GETPOST('savingdocmask', 'alpha') ? dol_sanitizeFileName($object->ref.'-') : '').$tmpfile) {
  99. $checked = ' checked';
  100. break;
  101. } elseif ($file['relativename'] && in_array($file['relativename'], GETPOST('attachfile', 'array'))) {
  102. $checked = ' checked';
  103. break;
  104. }
  105. }
  106. }
  107. // If we edit a line already linked, then $filenamelinked is defined to the filename (without path) of linked file
  108. if (!empty($filenamelinked) && $filenamelinked == $file['relativename']) {
  109. $checked = ' checked';
  110. }
  111. print '<div class="margintoponly minwidth150 maxwidth150 divoverflow"><input type="checkbox"'.$checked.' id="radio'.$filei.'" name="attachfile[]" class="checkboxattachfile valignmiddle" value="'.$file['relativename'].'">';
  112. print '<label class="wordbreak checkboxattachfilelabel paddingrightonly valignmiddle" for="radio'.$filei.'" title="'.dol_escape_htmltag($file['relativename']).'">'.$file['relativename'].'</label>';
  113. print '</div>';
  114. print '</div>';
  115. }
  116. print '<script>';
  117. print '$(document).ready(function() {';
  118. print "$('.checkboxattachfile').on('change', function() { $('.checkboxattachfile').not(this).prop('checked', false); });";
  119. print '});';
  120. print '</script>';
  121. print '</td></tr>';
  122. } else {
  123. print '<tr class="oddeven nohover trattachnewfilenow"'.(!GETPOSTISSET('sendit') && empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER) ? ' style="display: none"' : '').'>';
  124. print '<td colspan="'.$colspan.'">';
  125. print '<span class="opacitymedium">'.$langs->trans("NoFilesUploadedYet").'</span>';
  126. print '</td></tr>';
  127. }
  128. }