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