expensereport_linktofile.tpl.php 7.2 KB

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