dolibarr 18.0.6
expensereport_linktofile.tpl.php
1<?php
2// Add line to select existing file
3if (empty($conf->global->EXPENSEREPORT_DISABLE_ATTACHMENT_ON_LINES)) {
4 print '<!-- expensereport_linktofile.tpl.php -->'."\n";
5
6 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
7 require_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
8 require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
9 $upload_dir = $conf->expensereport->dir_output."/".dol_sanitizeFileName($object->ref);
10 $arrayoffiles = dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png|'.preg_quote(dol_sanitizeFileName($object->ref.'.pdf'), '/').')$');
11 $nbFiles = count($arrayoffiles);
12 $nbLinks = Link::count($db, $object->element, $object->id);
13
14 if ($nbFiles > 0) {
15 print '<tr class="trattachnewfilenow'.(empty($tredited) ? ' oddeven nohover' : ' '.$tredited).'"'.(!GETPOSTISSET('sendit') && empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER) ? ' style="display: none"' : '').'>';
16
17 // Num line
18 if ($action == 'editline') {
19 print '<td></td>';
20 }
21
22 // Select image section
23 print '<td colspan="'.($action == 'editline' ? $colspan - 1 : $colspan).'">';
24 //print '<span class="opacitymedium">'.$langs->trans("AttachTheNewLineToTheDocument").'</span><br>';
25 $modulepart = 'expensereport'; $maxheightmini = 48;
26 $relativepath = (!empty($object->ref) ?dol_sanitizeFileName($object->ref) : '').'/';
27 $filei = 0;
28 // Loop on each attached file
29 foreach ($arrayoffiles as $file) {
30 $urlforhref = array();
31 $filei++;
32
33 print '<div class="inline-block margintoponly marginleftonly marginrightonly center valigntop">';
34 $fileinfo = pathinfo($file['fullname']);
35 if (image_format_supported($file['name']) > 0) {
36 $minifile = getImageFileNameForSize($file['name'], '_mini'); // For new thumbs using same ext (in lower case however) than original
37 //print $file['path'].'/'.$minifile.'<br>';
38 $urlforhref = getAdvancedPreviewUrl($modulepart, $relativepath.$fileinfo['filename'].'.'.strtolower($fileinfo['extension']), 1, '&entity='.(!empty($object->entity) ? $object->entity : $conf->entity));
39 if (empty($urlforhref)) {
40 $urlforhref = DOL_URL_ROOT.'/viewimage.php?modulepart='.$modulepart.'&entity='.(!empty($object->entity) ? $object->entity : $conf->entity).'&file='.urlencode($fileinfo['relativename'].'.'.strtolower($fileinfo['extension']));
41 print '<a href="'.$urlforhref.'" class="aphoto" target="_blank" rel="noopener noreferrer">';
42 } else {
43 print '<a href="'.$urlforhref['url'].'" class="'.$urlforhref['css'].'" target="'.$urlforhref['target'].'" mime="'.$urlforhref['mime'].'">';
44 }
45 print '<div class="photoref backgroundblank">';
46 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="">';
47 print '</div>';
48 print '</a>';
49 } else {
50 $error = 0;
51 $thumbshown = '';
52
53 if (preg_match('/\.pdf$/i', $file['name'])) {
54 $urlforhref = getAdvancedPreviewUrl($modulepart, $relativepath.$fileinfo['filename'].'.'.strtolower($fileinfo['extension']), 1, '&entity='.(!empty($object->entity) ? $object->entity : $conf->entity));
55
56 $filepdf = $conf->expensereport->dir_output.'/'.$relativepath.$file['name'];
57 $fileimage = $conf->expensereport->dir_output.'/'.$relativepath.$file['name'].'_preview.png';
58 $relativepathimage = $relativepath.$file['name'].'_preview.png';
59
60 $pdfexists = file_exists($filepdf);
61
62 if ($pdfexists) {
63 // Conversion du PDF en image png si fichier png non existant
64 if (!file_exists($fileimage) || (filemtime($fileimage) < filemtime($filepdf))) {
65 if (empty($conf->global->MAIN_DISABLE_PDF_THUMBS)) { // If you experience trouble with pdf thumb generation and imagick, you can disable here.
66 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
67 $ret = dol_convert_file($filepdf, 'png', $fileimage, '0'); // Convert first page of PDF into a file _preview.png
68 if ($ret < 0) {
69 $error++;
70 }
71 }
72 }
73 }
74
75 if ($pdfexists && !$error) {
76 $heightforphotref = 70;
77 if (!empty($conf->dol_optimize_smallscreen)) {
78 $heightforphotref = 60;
79 }
80 // If the preview file is found
81 if (file_exists($fileimage)) {
82 $thumbshown = '<img height="'.$heightforphotref.'" class="photo photowithmargin photowithborder" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=apercu'.urlencode($modulepart).'&file='.urlencode($relativepathimage).'">';
83 }
84 }
85 }
86
87 if (empty($urlforhref) || empty($thumbshown)) {
88 print '<span href="" class="aphoto" target="_blank" rel="noopener noreferrer">';
89 } else {
90 print '<a href="'.$urlforhref['url'].'" class="'.$urlforhref['css'].'" target="'.$urlforhref['target'].'" mime="'.$urlforhref['mime'].'">';
91 }
92 print '<div class="photoref backgroundblank">';
93
94 print $thumbshown ? $thumbshown : img_mime($minifile);
95
96 print '</div>';
97 if (empty($urlforhref) || empty($thumbshown)) {
98 print '</span>';
99 } else {
100 print '</a>';
101 }
102 }
103 print '<br>';
104 $checked = '';
105 //var_dump(GETPOST($file['relativename']));
106 //var_dump($file['relativename']);
107 //var_dump($_FILES['userfile']['name']);
108 // If a file was just uploaded, we check to preselect it
109 if (is_array($_FILES['userfile']) && is_array($_FILES['userfile']['name'])) {
110 foreach ($_FILES['userfile']['name'] as $tmpfile) {
111 if ($file['relativename'] == (GETPOST('savingdocmask', 'alpha') ? dol_sanitizeFileName($object->ref.'-') : '').$tmpfile) {
112 $checked = ' checked';
113 break;
114 } elseif ($file['relativename'] && in_array($file['relativename'], GETPOST('attachfile', 'array'))) {
115 $checked = ' checked';
116 break;
117 }
118 }
119 }
120 // If we edit a line already linked, then $filenamelinked is defined to the filename (without path) of linked file
121 if (!empty($filenamelinked) && $filenamelinked == $file['relativename']) {
122 $checked = ' checked';
123 }
124 print '<div class="margintoponly minwidth150 maxwidth150 divoverflow"><input type="checkbox"'.$checked.' id="radio'.$filei.'" name="attachfile[]" class="checkboxattachfile valignmiddle" value="'.$file['relativename'].'">';
125 print '<label class="wordbreak checkboxattachfilelabel paddingrightonly valignmiddle" for="radio'.$filei.'" title="'.dol_escape_htmltag($file['relativename']).'">'.$file['relativename'].'</label>';
126 print '</div>';
127
128 print '</div>';
129 }
130
131 print '<script>';
132 print '$(document).ready(function() {';
133 print "$('.checkboxattachfile').on('change', function() { $('.checkboxattachfile').not(this).prop('checked', false); });";
134 print '});';
135 print '</script>';
136
137 print '</td></tr>';
138 } else {
139 if (empty($tredited)) {
140 $css = 'oddeven nohover trattachnewfilenow';
141 $newcolspan = $colspan;
142 } else {
143 $css = 'trattachnewfilenow tredited';
144 $newcolspan = $colspan - 1;
145 }
146 print '<tr class="'.$css.'"'.(!GETPOSTISSET('sendit') && empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER) ? ' style="display: none"' : '').'>';
147 if (!empty($tredited)) {
148 print '<td></td>';
149 }
150 print '<td colspan="'.($newcolspan).'">';
151 print '<span class="opacitymedium">'.$langs->trans("NoFilesUploadedYet").'</span>';
152 print '</td></tr>';
153 }
154}
dol_convert_file($fileinput, $ext='png', $fileoutput='', $page='')
Convert an image file or a PDF into another image format.
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:62
img_mime($file, $titlealt='', $morecss='')
Show MIME img of a file.
getImageFileNameForSize($file, $extName, $extImgTarget='')
Return the filename of file to get the thumbs.
getAdvancedPreviewUrl($modulepart, $relativepath, $alldata=0, $param='')
Return URL we can use for advanced preview links.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
image_format_supported($file, $acceptsvg=0)
Return if a filename is file name of a supported image format.