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