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