dolibarr 23.0.3
expensereport_linktofile.tpl.php
1<?php
2/* Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
3 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
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
28'
29@phan-var-force int $colspan
30';
31
32// Add line to select existing file
33if (!getDolGlobalString('EXPENSEREPORT_DISABLE_ATTACHMENT_ON_LINES')) {
34 print '<!-- expensereport_linktofile.tpl.php -->'."\n";
35
36 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
37 require_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
38 require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
39 $upload_dir = $conf->expensereport->dir_output."/".dol_sanitizeFileName($object->ref);
40 $arrayoffiles = dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png|'.preg_quote(dol_sanitizeFileName($object->ref.'.pdf'), '/').')$');
41 $nbFiles = count($arrayoffiles);
42 $nbLinks = Link::count($db, $object->element, $object->id);
43
44 if ($nbFiles > 0) {
45 print '<tr class="trattachnewfilenow'.(empty($tredited) ? ' oddeven nohover' : ' '.$tredited).'"'.(!GETPOSTISSET('sendit') && !getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER') ? ' style="display: none"' : '').'>';
46
47 // Num line
48 if ($action == 'editline') {
49 print '<td></td>';
50 }
51
52 // Select image section
53 print '<td colspan="'.($action == 'editline' ? $colspan - 1 : $colspan).'">';
54 //print '<span class="opacitymedium">'.$langs->trans("AttachTheNewLineToTheDocument").'</span><br>';
55 $modulepart = 'expensereport';
56 $maxheightmini = 48;
57 $relativepath = (!empty($object->ref) ? dol_sanitizeFileName($object->ref) : '').'/';
58 $filei = 0;
59 $minifile = null;
60 // Loop on each attached file
61 foreach ($arrayoffiles as $file) {
62 $urlforhref = array();
63 $filei++;
64
65 print '<div class="inline-block margintoponly marginleftonly marginrightonly center valigntop">';
66 $fileinfo = pathinfo($file['fullname']);
67 if (image_format_supported($file['name']) > 0) {
68 $minifile = getImageFileNameForSize($file['name'], '_mini'); // For new thumbs using same ext (in lower case however) than original
69 //print $file['path'].'/'.$minifile.'<br>';
70 $urlforhref = getAdvancedPreviewUrl($modulepart, $relativepath.$fileinfo['filename'].'.'.strtolower($fileinfo['extension']), 1, '&entity='.(empty($object->entity) ? $conf->entity : $object->entity));
71 if (empty($urlforhref)) {
72 $urlforhref = DOL_URL_ROOT.'/viewimage.php?modulepart='.$modulepart.'&entity='.(empty($object->entity) ? $conf->entity : $object->entity).'&file='.urlencode($file['relativename'].'.'.strtolower($fileinfo['extension']));
73 print '<a href="'.$urlforhref.'" class="aphoto" target="_blank" rel="noopener noreferrer">';
74 } else {
75 print '<a href="'.$urlforhref['url'].'" class="'.$urlforhref['css'].'" target="'.$urlforhref['target'].'" mime="'.$urlforhref['mime'].'">';
76 }
77 print '<div class="photoref backgroundblank">';
78 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="">';
79 print '</div>';
80 print '</a>';
81 } else {
82 $error = 0;
83 $thumbshown = '';
84
85 if (preg_match('/\.pdf$/i', $file['name'])) {
86 $urlforhref = getAdvancedPreviewUrl($modulepart, $relativepath.$fileinfo['filename'].'.'.strtolower($fileinfo['extension']), 1, '&entity='.(empty($object->entity) ? $conf->entity : $object->entity));
87
88 $filepdf = $conf->expensereport->dir_output.'/'.$relativepath.$file['name'];
89 $fileimage = $conf->expensereport->dir_output.'/'.$relativepath.$file['name'].'_preview.png';
90 $relativepathimage = $relativepath.$file['name'].'_preview.png';
91
92 $pdfexists = file_exists($filepdf);
93
94 if ($pdfexists) {
95 // Conversion du PDF en image png si fichier png non existent
96 if (!file_exists($fileimage) || (filemtime($fileimage) < filemtime($filepdf))) {
97 if (!getDolGlobalString('MAIN_DISABLE_PDF_THUMBS')) { // If you experience trouble with pdf thumb generation and imagick, you can disable here.
98 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
99 $ret = dol_convert_file($filepdf, 'png', $fileimage, '0'); // Convert first page of PDF into a file _preview.png
100 if ($ret < 0) {
101 $error++;
102 }
103 }
104 }
105 }
106
107 if ($pdfexists && !$error) {
108 $heightforphotref = 70;
109 if (!empty($conf->dol_optimize_smallscreen)) {
110 $heightforphotref = 60;
111 }
112 // If the preview file is found
113 if (file_exists($fileimage)) {
114 $thumbshown = '<img height="'.$heightforphotref.'" class="photo photowithmargin photowithborder" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=apercu'.urlencode($modulepart).'&file='.urlencode($relativepathimage).'">';
115 }
116 }
117 }
118
119 if (empty($urlforhref) || empty($thumbshown)) {
120 print '<span href="" class="aphoto" target="_blank" rel="noopener noreferrer">';
121 } else {
122 print '<a href="'.$urlforhref['url'].'" class="'.$urlforhref['css'].'" target="'.$urlforhref['target'].'" mime="'.$urlforhref['mime'].'">';
123 }
124 print '<div class="photoref backgroundblank">';
125
126 // TODO: Check that $minifile has a proper value here (set in true part of if, not else part).
127 print $thumbshown ? $thumbshown : (empty($minifile) ? '' : img_mime($minifile));
128
129 print '</div>';
130 if (empty($urlforhref) || empty($thumbshown)) {
131 print '</span>';
132 } else {
133 print '</a>';
134 }
135 }
136 print '<br>';
137 $checked = '';
138 //var_dump(GETPOST($file['relativename']));
139 //var_dump($file['relativename']);
140 //var_dump($_FILES['userfile']['name']);
141 // If a file was just uploaded, we check to preselect it
142 if (is_array($_FILES['userfile']) && is_array($_FILES['userfile']['name'])) {
143 foreach ($_FILES['userfile']['name'] as $tmpfile) {
144 if ($file['relativename'] == (GETPOST('savingdocmask', 'alpha') ? dol_sanitizeFileName($object->ref.'-') : '').$tmpfile) {
145 $checked = ' checked';
146 break;
147 } elseif ($file['relativename'] && in_array($file['relativename'], GETPOST('attachfile', 'array'))) {
148 $checked = ' checked';
149 break;
150 }
151 }
152 }
153 // If we edit a line already linked, then $filenamelinked is defined to the filename (without path) of linked file
154 if (!empty($filenamelinked) && $filenamelinked == $file['relativename']) {
155 $checked = ' checked';
156 }
157 print '<div class="margintoponly minwidth150 maxwidth150 divoverflow"><input type="checkbox"'.$checked.' id="radio'.$filei.'" name="attachfile[]" class="checkboxattachfile valignmiddle" value="'.$file['relativename'].'">';
158 print '<label class="wordbreak checkboxattachfilelabel paddingrightonly valignmiddle" for="radio'.$filei.'" title="'.dol_escape_htmltag($file['relativename']).'">'.$file['relativename'].'</label>';
159 print '</div>';
160
161 print '</div>';
162 }
163
164 print '<script>';
165 print '$(document).ready(function() {';
166 print "$('.checkboxattachfile').on('change', function() { $('.checkboxattachfile').not(this).prop('checked', false); });";
167 print '});';
168 print '</script>';
169
170 print '</td></tr>';
171 } else {
172 if (empty($tredited)) {
173 $css = 'oddeven nohover trattachnewfilenow';
174 $newcolspan = $colspan;
175 } else {
176 $css = 'trattachnewfilenow tredited';
177 $newcolspan = $colspan - 1;
178 }
179 print '<tr class="'.$css.'"'.(!GETPOSTISSET('sendit') && !getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER') ? ' style="display: none"' : '').'>';
180 if (!empty($tredited)) {
181 print '<td></td>';
182 }
183 print '<td colspan="'.($newcolspan).'">';
184 print '<span class="opacitymedium">'.$langs->trans("NoFilesUploadedYet").'...</span>';
185 print '</td></tr>';
186 }
187}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
dol_convert_file($fileinput, $ext='png', $fileoutput='', $page='')
Convert an image file or a PDF into another image format.
dol_dir_list($utf8_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:64
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
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.
getDolGlobalString($key, $default='')
Return a 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.