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