html.formfile.class.php 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122
  1. <?php
  2. /* Copyright (C) 2008-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2010-2014 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2010-2016 Juanjo Menent <jmenent@2byte.es>
  5. * Copyright (C) 2013 Charles-Fr BENKE <charles.fr@benke.fr>
  6. * Copyright (C) 2013 Cédric Salvador <csalvador@gpcsolutions.fr>
  7. * Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
  8. * Copyright (C) 2015 Bahfir Abbes <bafbes@gmail.com>
  9. * Copyright (C) 2016-2017 Ferran Marcet <fmarcet@2byte.es>
  10. * Copyright (C) 2019-2022 Frédéric France <frederic.france@netlogic.fr>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  24. */
  25. /**
  26. * \file htdocs/core/class/html.formfile.class.php
  27. * \ingroup core
  28. * \brief File of class to offer components to list and upload files
  29. */
  30. /**
  31. * Class to offer components to list and upload files
  32. */
  33. class FormFile
  34. {
  35. private $db;
  36. /**
  37. * @var string Error code (or message)
  38. */
  39. public $error;
  40. public $numoffiles;
  41. public $infofiles; // Used to return informations by function getDocumentsLink
  42. /**
  43. * Constructor
  44. *
  45. * @param DoliDB $db Database handler
  46. */
  47. public function __construct($db)
  48. {
  49. $this->db = $db;
  50. $this->numoffiles = 0;
  51. }
  52. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  53. /**
  54. * Show form to upload a new file.
  55. *
  56. * @param string $url Url
  57. * @param string $title Title zone (Title or '' or 'none')
  58. * @param int $addcancel 1=Add 'Cancel' button
  59. * @param int $sectionid If upload must be done inside a particular ECM section (is sectionid defined, sectiondir must not be)
  60. * @param int $perm Value of permission to allow upload
  61. * @param int $size Length of input file area. Deprecated.
  62. * @param Object $object Object to use (when attachment is done on an element)
  63. * @param string $options Add an option column
  64. * @param integer $useajax Use fileupload ajax (0=never, 1=if enabled, 2=always whatever is option).
  65. * Deprecated 2 should never be used and if 1 is used, option should not be enabled.
  66. * @param string $savingdocmask Mask to use to define output filename. For example 'XXXXX-__YYYYMMDD__-__file__'
  67. * @param integer $linkfiles 1=Also add form to link files, 0=Do not show form to link files
  68. * @param string $htmlname Name and id of HTML form ('formuserfile' by default, 'formuserfileecm' when used to upload a file in ECM)
  69. * @param string $accept Specifies the types of files accepted (This is not a security check but an user interface facility. eg '.pdf,image/*' or '.png,.jpg' or 'video/*')
  70. * @param string $sectiondir If upload must be done inside a particular directory (if sectiondir defined, sectionid must not be)
  71. * @param int $usewithoutform 0=Default, 1=Disable <form> and <input hidden> to use in existing form area, 2=Disable the tag <form> only
  72. * @param int $capture 1=Add tag capture="capture" to force use of micro or video recording to generate file. When setting this to 1, you must also provide a value for $accept.
  73. * @param int $disablemulti 0=Default, 1=Disable multiple file upload
  74. * @param int $nooutput 0=Output result with print, 1=Return result
  75. * @return int|string <0 if KO, >0 if OK, or string if $noouput=1
  76. */
  77. public function form_attach_new_file($url, $title = '', $addcancel = 0, $sectionid = 0, $perm = 1, $size = 50, $object = '', $options = '', $useajax = 1, $savingdocmask = '', $linkfiles = 1, $htmlname = 'formuserfile', $accept = '', $sectiondir = '', $usewithoutform = 0, $capture = 0, $disablemulti = 0, $nooutput = 0)
  78. {
  79. // phpcs:enable
  80. global $conf, $langs, $hookmanager;
  81. $hookmanager->initHooks(array('formfile'));
  82. // Deprecation warning
  83. if ($useajax == 2) {
  84. dol_syslog(__METHOD__.": using 2 for useajax is deprecated and should be not used", LOG_WARNING);
  85. }
  86. if (!empty($conf->browser->layout) && $conf->browser->layout != 'classic') {
  87. $useajax = 0;
  88. }
  89. if ((!empty($conf->global->MAIN_USE_JQUERY_FILEUPLOAD) && $useajax) || ($useajax == 2)) {
  90. // TODO: Check this works with 2 forms on same page
  91. // TODO: Check this works with GED module, otherwise, force useajax to 0
  92. // TODO: This does not support option savingdocmask
  93. // TODO: This break feature to upload links too
  94. // TODO: Thisdoes not work when param nooutput=1
  95. //return $this->_formAjaxFileUpload($object);
  96. return 'Feature too bugged so removed';
  97. } else {
  98. //If there is no permission and the option to hide unauthorized actions is enabled, then nothing is printed
  99. if (!$perm && !empty($conf->global->MAIN_BUTTON_HIDE_UNAUTHORIZED)) {
  100. if ($nooutput) {
  101. return '';
  102. } else {
  103. return 1;
  104. }
  105. }
  106. $out = "\n\n".'<!-- Start form attach new file --><div class="formattachnewfile">'."\n";
  107. if (empty($title)) {
  108. $title = $langs->trans("AttachANewFile");
  109. }
  110. if ($title != 'none') {
  111. $out .= load_fiche_titre($title, null, null);
  112. }
  113. if (empty($usewithoutform)) { // Try to avoid this and set instead the form by the caller.
  114. // Add a param as GET parameter to detect when POST were cleaned by PHP because a file larger than post_max_size
  115. $url .= (strpos($url, '?') === false ? '?' : '&').'uploadform=1';
  116. $out .= '<form name="'.$htmlname.'" id="'.$htmlname.'" action="'.$url.'" enctype="multipart/form-data" method="POST">'."\n";
  117. }
  118. if (empty($usewithoutform) || $usewithoutform == 2) {
  119. $out .= '<input type="hidden" name="token" value="'.newToken().'">'."\n";
  120. $out .= '<input type="hidden" id="'.$htmlname.'_section_dir" name="section_dir" value="'.$sectiondir.'">'."\n";
  121. $out .= '<input type="hidden" id="'.$htmlname.'_section_id" name="section_id" value="'.$sectionid.'">'."\n";
  122. $out .= '<input type="hidden" name="sortfield" value="'.GETPOST('sortfield', 'aZ09comma').'">'."\n";
  123. $out .= '<input type="hidden" name="sortorder" value="'.GETPOST('sortorder', 'aZ09comma').'">'."\n";
  124. $out .= '<input type="hidden" name="page_y" value="">'."\n";
  125. }
  126. $out .= '<table class="nobordernopadding centpercent">';
  127. $out .= '<tr>';
  128. if (!empty($options)) {
  129. $out .= '<td>'.$options.'</td>';
  130. }
  131. $out .= '<td class="valignmiddle nowrap">';
  132. $maxfilesizearray = getMaxFileSizeArray();
  133. $max = $maxfilesizearray['max'];
  134. $maxmin = $maxfilesizearray['maxmin'];
  135. $maxphptoshow = $maxfilesizearray['maxphptoshow'];
  136. $maxphptoshowparam = $maxfilesizearray['maxphptoshowparam'];
  137. if ($maxmin > 0) {
  138. $out .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).'">'; // MAX_FILE_SIZE must precede the field type=file
  139. }
  140. $out .= '<input class="flat minwidth400 maxwidth200onsmartphone" type="file"';
  141. $out .= ((!empty($conf->global->MAIN_DISABLE_MULTIPLE_FILEUPLOAD) || $disablemulti) ? ' name="userfile"' : ' name="userfile[]" multiple');
  142. $out .= (empty($conf->global->MAIN_UPLOAD_DOC) || empty($perm) ? ' disabled' : '');
  143. $out .= (!empty($accept) ? ' accept="'.$accept.'"' : ' accept=""');
  144. $out .= (!empty($capture) ? ' capture="capture"' : '');
  145. $out .= '>';
  146. $out .= ' ';
  147. if ($sectionid) { // Show overwrite if exists for ECM module only
  148. $langs->load('link');
  149. $out .= '<span class="nowraponsmartphone"><input style="margin-right: 2px;" type="checkbox" id="overwritefile" name="overwritefile" value="1"><label for="overwritefile">'.$langs->trans("OverwriteIfExists").'</label></span>';
  150. }
  151. $out .= '<input type="submit" class="button small reposition" name="sendit" value="'.$langs->trans("Upload").'"';
  152. $out .= (empty($conf->global->MAIN_UPLOAD_DOC) || empty($perm) ? ' disabled' : '');
  153. $out .= '>';
  154. if ($addcancel) {
  155. $out .= ' &nbsp; ';
  156. $out .= '<input type="submit" class="button small button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
  157. }
  158. if (!empty($conf->global->MAIN_UPLOAD_DOC)) {
  159. if ($perm) {
  160. $menudolibarrsetupmax = $langs->transnoentitiesnoconv("Home").' - '.$langs->transnoentitiesnoconv("Setup").' - '.$langs->transnoentitiesnoconv("Security");
  161. $langs->load('other');
  162. $out .= ' ';
  163. $out .= info_admin($langs->trans("ThisLimitIsDefinedInSetupAt", $menudolibarrsetupmax, $max, $maxphptoshowparam, $maxphptoshow), 1);
  164. }
  165. } else {
  166. $out .= ' ('.$langs->trans("UploadDisabled").')';
  167. }
  168. $out .= "</td></tr>";
  169. if ($savingdocmask) {
  170. //add a global variable for disable the auto renaming on upload
  171. $rename = (empty($conf->global->MAIN_DOC_UPLOAD_NOT_RENAME_BY_DEFAULT) ? 'checked' : '');
  172. $out .= '<tr>';
  173. if (!empty($options)) {
  174. $out .= '<td>'.$options.'</td>';
  175. }
  176. $out .= '<td valign="middle" class="nowrap">';
  177. $out .= '<input type="checkbox" '.$rename.' class="savingdocmask" name="savingdocmask" id="savingdocmask" value="'.dol_escape_js($savingdocmask).'"> ';
  178. $out .= '<label class="opacitymedium small" for="savingdocmask">';
  179. $out .= $langs->trans("SaveUploadedFileWithMask", preg_replace('/__file__/', $langs->transnoentitiesnoconv("OriginFileName"), $savingdocmask), $langs->transnoentitiesnoconv("OriginFileName"));
  180. $out .= '</label>';
  181. $out .= '</td>';
  182. $out .= '</tr>';
  183. }
  184. $out .= "</table>";
  185. if (empty($usewithoutform)) {
  186. $out .= '</form>';
  187. if (empty($sectionid)) {
  188. $out .= '<br>';
  189. }
  190. }
  191. $out .= "\n</div><!-- End form attach new file -->\n";
  192. if ($linkfiles) {
  193. $out .= "\n".'<!-- Start form link new url --><div class="formlinknewurl">'."\n";
  194. $langs->load('link');
  195. $title = $langs->trans("LinkANewFile");
  196. $out .= load_fiche_titre($title, null, null);
  197. if (empty($usewithoutform)) {
  198. $out .= '<form name="'.$htmlname.'_link" id="'.$htmlname.'_link" action="'.$url.'" method="POST">'."\n";
  199. $out .= '<input type="hidden" name="token" value="'.newToken().'">'."\n";
  200. $out .= '<input type="hidden" id="'.$htmlname.'_link_section_dir" name="link_section_dir" value="">'."\n";
  201. $out .= '<input type="hidden" id="'.$htmlname.'_link_section_id" name="link_section_id" value="'.$sectionid.'">'."\n";
  202. $out .= '<input type="hidden" name="page_y" value="">'."\n";
  203. }
  204. $out .= '<div class="valignmiddle">';
  205. $out .= '<div class="inline-block" style="padding-right: 10px;">';
  206. if (!empty($conf->global->OPTIMIZEFORTEXTBROWSER)) {
  207. $out .= '<label for="link">'.$langs->trans("URLToLink").':</label> ';
  208. }
  209. $out .= '<input type="text" name="link" class="flat minwidth400imp" id="link" placeholder="'.dol_escape_htmltag($langs->trans("URLToLink")).'">';
  210. $out .= '</div>';
  211. $out .= '<div class="inline-block" style="padding-right: 10px;">';
  212. if (!empty($conf->global->OPTIMIZEFORTEXTBROWSER)) {
  213. $out .= '<label for="label">'.$langs->trans("Label").':</label> ';
  214. }
  215. $out .= '<input type="text" class="flat" name="label" id="label" placeholder="'.dol_escape_htmltag($langs->trans("Label")).'">';
  216. $out .= '<input type="hidden" name="objecttype" value="'.$object->element.'">';
  217. $out .= '<input type="hidden" name="objectid" value="'.$object->id.'">';
  218. $out .= '</div>';
  219. $out .= '<div class="inline-block" style="padding-right: 10px;">';
  220. $out .= '<input type="submit" class="button small reposition" name="linkit" value="'.$langs->trans("ToLink").'"';
  221. $out .= (empty($conf->global->MAIN_UPLOAD_DOC) || empty($perm) ? ' disabled' : '');
  222. $out .= '>';
  223. $out .= '</div>';
  224. $out .= '</div>';
  225. if (empty($usewithoutform)) {
  226. $out .= '<div class="clearboth"></div>';
  227. $out .= '</form><br>';
  228. }
  229. $out .= "\n</div><!-- End form link new url -->\n";
  230. }
  231. $parameters = array('socid'=>(isset($GLOBALS['socid']) ? $GLOBALS['socid'] : ''), 'id'=>(isset($GLOBALS['id']) ? $GLOBALS['id'] : ''), 'url'=>$url, 'perm'=>$perm, 'options'=>$options);
  232. $res = $hookmanager->executeHooks('formattachOptions', $parameters, $object);
  233. if (empty($res)) {
  234. $out = '<div class="'.($usewithoutform ? 'inline-block valignmiddle' : 'attacharea attacharea'.$htmlname).'">'.$out.'</div>';
  235. }
  236. $out .= $hookmanager->resPrint;
  237. if ($nooutput) {
  238. return $out;
  239. } else {
  240. print $out;
  241. return 1;
  242. }
  243. }
  244. }
  245. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  246. /**
  247. * Show the box with list of available documents for object
  248. *
  249. * @param string $modulepart propal, facture, facture_fourn, ...
  250. * @param string $modulesubdir Sub-directory to scan (Example: '0/1/10', 'FA/DD/MM/YY/9999'). Use '' if file is not into subdir of module.
  251. * @param string $filedir Directory to scan
  252. * @param string $urlsource Url of origin page (for return)
  253. * @param int $genallowed Generation is allowed (1/0 or array of formats)
  254. * @param int $delallowed Remove is allowed (1/0)
  255. * @param string $modelselected Model to preselect by default
  256. * @param integer $allowgenifempty Show warning if no model activated
  257. * @param integer $forcenomultilang Do not show language option (even if MAIN_MULTILANGS defined)
  258. * @param int $iconPDF Show only PDF icon with link (1/0)
  259. * @param int $notused Not used
  260. * @param integer $noform Do not output html form tags
  261. * @param string $param More param on http links
  262. * @param string $title Title to show on top of form
  263. * @param string $buttonlabel Label on submit button
  264. * @param string $codelang Default language code to use on lang combo box if multilang is enabled
  265. * @return int <0 if KO, number of shown files if OK
  266. * @deprecated Use print xxx->showdocuments() instead.
  267. */
  268. public function show_documents($modulepart, $modulesubdir, $filedir, $urlsource, $genallowed, $delallowed = 0, $modelselected = '', $allowgenifempty = 1, $forcenomultilang = 0, $iconPDF = 0, $notused = 0, $noform = 0, $param = '', $title = '', $buttonlabel = '', $codelang = '')
  269. {
  270. // phpcs:enable
  271. $this->numoffiles = 0;
  272. print $this->showdocuments($modulepart, $modulesubdir, $filedir, $urlsource, $genallowed, $delallowed, $modelselected, $allowgenifempty, $forcenomultilang, $iconPDF, $notused, $noform, $param, $title, $buttonlabel, $codelang);
  273. return $this->numoffiles;
  274. }
  275. /**
  276. * Return a string to show the box with list of available documents for object.
  277. * This also set the property $this->numoffiles
  278. *
  279. * @param string $modulepart Module the files are related to ('propal', 'facture', 'facture_fourn', 'mymodule', 'mymodule:MyObject', 'mymodule_temp', ...)
  280. * @param string $modulesubdir Existing (so sanitized) sub-directory to scan (Example: '0/1/10', 'FA/DD/MM/YY/9999'). Use '' if file is not into a subdir of module.
  281. * @param string $filedir Directory to scan (must not end with a /). Example: '/mydolibarrdocuments/facture/FAYYMM-1234'
  282. * @param string $urlsource Url of origin page (for return)
  283. * @param int|string[] $genallowed Generation is allowed (1/0 or array list of templates)
  284. * @param int $delallowed Remove is allowed (1/0)
  285. * @param string $modelselected Model to preselect by default
  286. * @param integer $allowgenifempty Allow generation even if list of template ($genallowed) is empty (show however a warning)
  287. * @param integer $forcenomultilang Do not show language option (even if MAIN_MULTILANGS defined)
  288. * @param int $iconPDF Deprecated, see getDocumentsLink
  289. * @param int $notused Not used
  290. * @param integer $noform Do not output html form tags
  291. * @param string $param More param on http links
  292. * @param string $title Title to show on top of form. Example: '' (Default to "Documents") or 'none'
  293. * @param string $buttonlabel Label on submit button
  294. * @param string $codelang Default language code to use on lang combo box if multilang is enabled
  295. * @param string $morepicto Add more HTML content into cell with picto
  296. * @param Object $object Object when method is called from an object card.
  297. * @param int $hideifempty Hide section of generated files if there is no file
  298. * @param string $removeaction (optional) The action to remove a file
  299. * @param string $tooltipontemplatecombo Text to show on a tooltip after the combo list of templates
  300. * @return string Output string with HTML array of documents (might be empty string)
  301. */
  302. public function showdocuments($modulepart, $modulesubdir, $filedir, $urlsource, $genallowed, $delallowed = 0, $modelselected = '', $allowgenifempty = 1, $forcenomultilang = 0, $iconPDF = 0, $notused = 0, $noform = 0, $param = '', $title = '', $buttonlabel = '', $codelang = '', $morepicto = '', $object = null, $hideifempty = 0, $removeaction = 'remove_file', $tooltipontemplatecombo = '')
  303. {
  304. global $dolibarr_main_url_root;
  305. // Deprecation warning
  306. if (!empty($iconPDF)) {
  307. dol_syslog(__METHOD__.": passing iconPDF parameter is deprecated", LOG_WARNING);
  308. }
  309. global $langs, $conf, $user, $hookmanager;
  310. global $form;
  311. $reshook = 0;
  312. if (is_object($hookmanager)) {
  313. $parameters = array(
  314. 'modulepart'=>&$modulepart,
  315. 'modulesubdir'=>&$modulesubdir,
  316. 'filedir'=>&$filedir,
  317. 'urlsource'=>&$urlsource,
  318. 'genallowed'=>&$genallowed,
  319. 'delallowed'=>&$delallowed,
  320. 'modelselected'=>&$modelselected,
  321. 'allowgenifempty'=>&$allowgenifempty,
  322. 'forcenomultilang'=>&$forcenomultilang,
  323. 'noform'=>&$noform,
  324. 'param'=>&$param,
  325. 'title'=>&$title,
  326. 'buttonlabel'=>&$buttonlabel,
  327. 'codelang'=>&$codelang,
  328. 'morepicto'=>&$morepicto,
  329. 'hideifempty'=>&$hideifempty,
  330. 'removeaction'=>&$removeaction
  331. );
  332. $reshook = $hookmanager->executeHooks('showDocuments', $parameters, $object); // Note that parameters may have been updated by hook
  333. // May report error
  334. if ($reshook < 0) {
  335. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  336. }
  337. }
  338. // Remode default action if $reskook > 0
  339. if ($reshook > 0) {
  340. return $hookmanager->resPrint;
  341. }
  342. if (!is_object($form)) {
  343. $form = new Form($this->db);
  344. }
  345. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  346. // For backward compatibility
  347. if (!empty($iconPDF)) {
  348. return $this->getDocumentsLink($modulepart, $modulesubdir, $filedir);
  349. }
  350. // Add entity in $param if not already exists
  351. if (!preg_match('/entity\=[0-9]+/', $param)) {
  352. $param .= ($param ? '&' : '').'entity='.(!empty($object->entity) ? $object->entity : $conf->entity);
  353. }
  354. $printer = 0;
  355. // The direct print feature is implemented only for such elements
  356. if (in_array($modulepart, array('contract', 'facture', 'supplier_proposal', 'propal', 'proposal', 'order', 'commande', 'expedition', 'commande_fournisseur', 'expensereport', 'delivery', 'ticket'))) {
  357. $printer = (!empty($user->rights->printing->read) && !empty($conf->printing->enabled)) ?true:false;
  358. }
  359. $hookmanager->initHooks(array('formfile'));
  360. // Get list of files
  361. $file_list = null;
  362. if (!empty($filedir)) {
  363. $file_list = dol_dir_list($filedir, 'files', 0, '', '(\.meta|_preview.*.*\.png)$', 'date', SORT_DESC);
  364. }
  365. if ($hideifempty && empty($file_list)) {
  366. return '';
  367. }
  368. $out = '';
  369. $forname = 'builddoc';
  370. $headershown = 0;
  371. $showempty = 0;
  372. $i = 0;
  373. $out .= "\n".'<!-- Start show_document -->'."\n";
  374. //print 'filedir='.$filedir;
  375. if (preg_match('/massfilesarea_/', $modulepart)) {
  376. $out .= '<div id="show_files"><br></div>'."\n";
  377. $title = $langs->trans("MassFilesArea").' <a href="" id="togglemassfilesarea" ref="shown">('.$langs->trans("Hide").')</a>';
  378. $title .= '<script>
  379. jQuery(document).ready(function() {
  380. jQuery(\'#togglemassfilesarea\').click(function() {
  381. if (jQuery(\'#togglemassfilesarea\').attr(\'ref\') == "shown")
  382. {
  383. jQuery(\'#'.$modulepart.'_table\').hide();
  384. jQuery(\'#togglemassfilesarea\').attr("ref", "hidden");
  385. jQuery(\'#togglemassfilesarea\').text("('.dol_escape_js($langs->trans("Show")).')");
  386. }
  387. else
  388. {
  389. jQuery(\'#'.$modulepart.'_table\').show();
  390. jQuery(\'#togglemassfilesarea\').attr("ref","shown");
  391. jQuery(\'#togglemassfilesarea\').text("('.dol_escape_js($langs->trans("Hide")).')");
  392. }
  393. return false;
  394. });
  395. });
  396. </script>';
  397. }
  398. $titletoshow = $langs->trans("Documents");
  399. if (!empty($title)) {
  400. $titletoshow = ($title == 'none' ? '' : $title);
  401. }
  402. $addcolumforpicto = ($delallowed || $printer || $morepicto);
  403. $colspan = (4 + ($addcolumforpicto ? 1 : 0));
  404. $colspanmore = 0;
  405. // Show table
  406. if ($genallowed) {
  407. $modellist = array();
  408. if ($modulepart == 'company') {
  409. $showempty = 1; // can have no template active
  410. if (is_array($genallowed)) {
  411. $modellist = $genallowed;
  412. } else {
  413. include_once DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php';
  414. $modellist = ModeleThirdPartyDoc::liste_modeles($this->db);
  415. }
  416. } elseif ($modulepart == 'propal') {
  417. if (is_array($genallowed)) {
  418. $modellist = $genallowed;
  419. } else {
  420. include_once DOL_DOCUMENT_ROOT.'/core/modules/propale/modules_propale.php';
  421. $modellist = ModelePDFPropales::liste_modeles($this->db);
  422. }
  423. } elseif ($modulepart == 'supplier_proposal') {
  424. if (is_array($genallowed)) {
  425. $modellist = $genallowed;
  426. } else {
  427. include_once DOL_DOCUMENT_ROOT.'/core/modules/supplier_proposal/modules_supplier_proposal.php';
  428. $modellist = ModelePDFSupplierProposal::liste_modeles($this->db);
  429. }
  430. } elseif ($modulepart == 'commande') {
  431. if (is_array($genallowed)) {
  432. $modellist = $genallowed;
  433. } else {
  434. include_once DOL_DOCUMENT_ROOT.'/core/modules/commande/modules_commande.php';
  435. $modellist = ModelePDFCommandes::liste_modeles($this->db);
  436. }
  437. } elseif ($modulepart == 'expedition') {
  438. if (is_array($genallowed)) {
  439. $modellist = $genallowed;
  440. } else {
  441. include_once DOL_DOCUMENT_ROOT.'/core/modules/expedition/modules_expedition.php';
  442. $modellist = ModelePDFExpedition::liste_modeles($this->db);
  443. }
  444. } elseif ($modulepart == 'reception') {
  445. if (is_array($genallowed)) {
  446. $modellist = $genallowed;
  447. } else {
  448. include_once DOL_DOCUMENT_ROOT.'/core/modules/reception/modules_reception.php';
  449. $modellist = ModelePdfReception::liste_modeles($this->db);
  450. }
  451. } elseif ($modulepart == 'delivery') {
  452. if (is_array($genallowed)) {
  453. $modellist = $genallowed;
  454. } else {
  455. include_once DOL_DOCUMENT_ROOT.'/core/modules/delivery/modules_delivery.php';
  456. $modellist = ModelePDFDeliveryOrder::liste_modeles($this->db);
  457. }
  458. } elseif ($modulepart == 'ficheinter') {
  459. if (is_array($genallowed)) {
  460. $modellist = $genallowed;
  461. } else {
  462. include_once DOL_DOCUMENT_ROOT.'/core/modules/fichinter/modules_fichinter.php';
  463. $modellist = ModelePDFFicheinter::liste_modeles($this->db);
  464. }
  465. } elseif ($modulepart == 'facture') {
  466. if (is_array($genallowed)) {
  467. $modellist = $genallowed;
  468. } else {
  469. include_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
  470. $modellist = ModelePDFFactures::liste_modeles($this->db);
  471. }
  472. } elseif ($modulepart == 'contract') {
  473. $showempty = 1; // can have no template active
  474. if (is_array($genallowed)) {
  475. $modellist = $genallowed;
  476. } else {
  477. include_once DOL_DOCUMENT_ROOT.'/core/modules/contract/modules_contract.php';
  478. $modellist = ModelePDFContract::liste_modeles($this->db);
  479. }
  480. } elseif ($modulepart == 'project') {
  481. if (is_array($genallowed)) {
  482. $modellist = $genallowed;
  483. } else {
  484. include_once DOL_DOCUMENT_ROOT.'/core/modules/project/modules_project.php';
  485. $modellist = ModelePDFProjects::liste_modeles($this->db);
  486. }
  487. } elseif ($modulepart == 'project_task') {
  488. if (is_array($genallowed)) {
  489. $modellist = $genallowed;
  490. } else {
  491. include_once DOL_DOCUMENT_ROOT.'/core/modules/project/task/modules_task.php';
  492. $modellist = ModelePDFTask::liste_modeles($this->db);
  493. }
  494. } elseif ($modulepart == 'product') {
  495. if (is_array($genallowed)) {
  496. $modellist = $genallowed;
  497. } else {
  498. include_once DOL_DOCUMENT_ROOT.'/core/modules/product/modules_product.class.php';
  499. $modellist = ModelePDFProduct::liste_modeles($this->db);
  500. }
  501. } elseif ($modulepart == 'product_batch') {
  502. if (is_array($genallowed)) {
  503. $modellist = $genallowed;
  504. } else {
  505. include_once DOL_DOCUMENT_ROOT.'/core/modules/product_batch/modules_product_batch.class.php';
  506. $modellist = ModelePDFProductBatch::liste_modeles($this->db);
  507. }
  508. } elseif ($modulepart == 'stock') {
  509. if (is_array($genallowed)) {
  510. $modellist = $genallowed;
  511. } else {
  512. include_once DOL_DOCUMENT_ROOT.'/core/modules/stock/modules_stock.php';
  513. $modellist = ModelePDFStock::liste_modeles($this->db);
  514. }
  515. } elseif ($modulepart == 'movement') {
  516. if (is_array($genallowed)) {
  517. $modellist = $genallowed;
  518. } else {
  519. include_once DOL_DOCUMENT_ROOT.'/core/modules/stock/modules_movement.php';
  520. $modellist = ModelePDFMovement::liste_modeles($this->db);
  521. }
  522. } elseif ($modulepart == 'export') {
  523. if (is_array($genallowed)) {
  524. $modellist = $genallowed;
  525. } else {
  526. include_once DOL_DOCUMENT_ROOT.'/core/modules/export/modules_export.php';
  527. $modellist = ModeleExports::liste_modeles($this->db);
  528. }
  529. } elseif ($modulepart == 'commande_fournisseur' || $modulepart == 'supplier_order') {
  530. if (is_array($genallowed)) {
  531. $modellist = $genallowed;
  532. } else {
  533. include_once DOL_DOCUMENT_ROOT.'/core/modules/supplier_order/modules_commandefournisseur.php';
  534. $modellist = ModelePDFSuppliersOrders::liste_modeles($this->db);
  535. }
  536. } elseif ($modulepart == 'facture_fournisseur' || $modulepart == 'supplier_invoice') {
  537. $showempty = 1; // can have no template active
  538. if (is_array($genallowed)) {
  539. $modellist = $genallowed;
  540. } else {
  541. include_once DOL_DOCUMENT_ROOT.'/core/modules/supplier_invoice/modules_facturefournisseur.php';
  542. $modellist = ModelePDFSuppliersInvoices::liste_modeles($this->db);
  543. }
  544. } elseif ($modulepart == 'supplier_payment') {
  545. if (is_array($genallowed)) {
  546. $modellist = $genallowed;
  547. } else {
  548. include_once DOL_DOCUMENT_ROOT.'/core/modules/supplier_payment/modules_supplier_payment.php';
  549. $modellist = ModelePDFSuppliersPayments::liste_modeles($this->db);
  550. }
  551. } elseif ($modulepart == 'remisecheque') {
  552. if (is_array($genallowed)) {
  553. $modellist = $genallowed;
  554. } else {
  555. include_once DOL_DOCUMENT_ROOT.'/core/modules/cheque/modules_chequereceipts.php';
  556. $modellist = ModeleChequeReceipts::liste_modeles($this->db);
  557. }
  558. } elseif ($modulepart == 'donation') {
  559. if (is_array($genallowed)) {
  560. $modellist = $genallowed;
  561. } else {
  562. include_once DOL_DOCUMENT_ROOT.'/core/modules/dons/modules_don.php';
  563. $modellist = ModeleDon::liste_modeles($this->db);
  564. }
  565. } elseif ($modulepart == 'member') {
  566. if (is_array($genallowed)) {
  567. $modellist = $genallowed;
  568. } else {
  569. include_once DOL_DOCUMENT_ROOT.'/core/modules/member/modules_cards.php';
  570. $modellist = ModelePDFCards::liste_modeles($this->db);
  571. }
  572. } elseif ($modulepart == 'agenda' || $modulepart == 'actions') {
  573. if (is_array($genallowed)) {
  574. $modellist = $genallowed;
  575. } else {
  576. include_once DOL_DOCUMENT_ROOT.'/core/modules/action/modules_action.php';
  577. $modellist = ModeleAction::liste_modeles($this->db);
  578. }
  579. } elseif ($modulepart == 'expensereport') {
  580. if (is_array($genallowed)) {
  581. $modellist = $genallowed;
  582. } else {
  583. include_once DOL_DOCUMENT_ROOT.'/core/modules/expensereport/modules_expensereport.php';
  584. $modellist = ModeleExpenseReport::liste_modeles($this->db);
  585. }
  586. } elseif ($modulepart == 'unpaid') {
  587. $modellist = '';
  588. } elseif ($modulepart == 'user') {
  589. if (is_array($genallowed)) {
  590. $modellist = $genallowed;
  591. } else {
  592. include_once DOL_DOCUMENT_ROOT.'/core/modules/user/modules_user.class.php';
  593. $modellist = ModelePDFUser::liste_modeles($this->db);
  594. }
  595. } elseif ($modulepart == 'usergroup') {
  596. if (is_array($genallowed)) {
  597. $modellist = $genallowed;
  598. } else {
  599. include_once DOL_DOCUMENT_ROOT.'/core/modules/usergroup/modules_usergroup.class.php';
  600. $modellist = ModelePDFUserGroup::liste_modeles($this->db);
  601. }
  602. } else {
  603. $submodulepart = $modulepart;
  604. // modulepart = 'nameofmodule' or 'nameofmodule:NameOfObject'
  605. $tmp = explode(':', $modulepart);
  606. if (!empty($tmp[1])) {
  607. $modulepart = $tmp[0];
  608. $submodulepart = $tmp[1];
  609. }
  610. // For normalized standard modules
  611. $file = dol_buildpath('/core/modules/'.$modulepart.'/modules_'.strtolower($submodulepart).'.php', 0);
  612. if (file_exists($file)) {
  613. $res = include_once $file;
  614. } else {
  615. // For normalized external modules.
  616. $file = dol_buildpath('/'.$modulepart.'/core/modules/'.$modulepart.'/modules_'.strtolower($submodulepart).'.php', 0);
  617. $res = include_once $file;
  618. }
  619. $class = 'ModelePDF'.ucfirst($submodulepart);
  620. if (class_exists($class)) {
  621. $modellist = call_user_func($class.'::liste_modeles', $this->db);
  622. } else {
  623. dol_print_error($this->db, "Bad value for modulepart '".$modulepart."' in showdocuments (class ".$class." for Doc generation not found)");
  624. return -1;
  625. }
  626. }
  627. // Set headershown to avoid to have table opened a second time later
  628. $headershown = 1;
  629. if (empty($buttonlabel)) {
  630. $buttonlabel = $langs->trans('Generate');
  631. }
  632. if ($conf->browser->layout == 'phone') {
  633. $urlsource .= '#'.$forname.'_form'; // So we switch to form after a generation
  634. }
  635. if (empty($noform)) {
  636. $out .= '<form action="'.$urlsource.'" id="'.$forname.'_form" method="post">';
  637. }
  638. $out .= '<input type="hidden" name="action" value="builddoc">';
  639. $out .= '<input type="hidden" name="page_y" value="">';
  640. $out .= '<input type="hidden" name="token" value="'.newToken().'">';
  641. $out .= load_fiche_titre($titletoshow, '', '');
  642. $out .= '<div class="div-table-responsive-no-min">';
  643. $out .= '<table class="liste formdoc noborder centpercent">';
  644. $out .= '<tr class="liste_titre">';
  645. $out .= '<th colspan="'.$colspan.'" class="formdoc liste_titre maxwidthonsmartphone center">';
  646. // Model
  647. if (!empty($modellist)) {
  648. asort($modellist);
  649. $out .= '<span class="hideonsmartphone">'.$langs->trans('Model').' </span>';
  650. if (is_array($modellist) && count($modellist) == 1) { // If there is only one element
  651. $arraykeys = array_keys($modellist);
  652. $modelselected = $arraykeys[0];
  653. }
  654. $morecss = 'minwidth75 maxwidth200';
  655. if ($conf->browser->layout == 'phone') {
  656. $morecss = 'maxwidth100';
  657. }
  658. $out .= $form->selectarray('model', $modellist, $modelselected, $showempty, 0, 0, '', 0, 0, 0, '', $morecss);
  659. if ($conf->use_javascript_ajax) {
  660. $out .= ajax_combobox('model');
  661. }
  662. $out .= $form->textwithpicto('', $tooltipontemplatecombo, 1, 'help', 'marginrightonly', 0, 3, '', 0);
  663. } else {
  664. $out .= '<div class="float">'.$langs->trans("Files").'</div>';
  665. }
  666. // Language code (if multilang)
  667. if (($allowgenifempty || (is_array($modellist) && count($modellist) > 0)) && getDolGlobalInt('MAIN_MULTILANGS') && !$forcenomultilang && (!empty($modellist) || $showempty)) {
  668. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
  669. $formadmin = new FormAdmin($this->db);
  670. $defaultlang = ($codelang && $codelang != 'auto') ? $codelang : $langs->getDefaultLang();
  671. $morecss = 'maxwidth150';
  672. if ($conf->browser->layout == 'phone') {
  673. $morecss = 'maxwidth100';
  674. }
  675. $out .= $formadmin->select_language($defaultlang, 'lang_id', 0, null, 0, 0, 0, $morecss);
  676. } else {
  677. $out .= '&nbsp;';
  678. }
  679. // Button
  680. $genbutton = '<input class="button buttongen reposition nomargintop nomarginbottom" id="'.$forname.'_generatebutton" name="'.$forname.'_generatebutton"';
  681. $genbutton .= ' type="submit" value="'.$buttonlabel.'"';
  682. if (!$allowgenifempty && !is_array($modellist) && empty($modellist)) {
  683. $genbutton .= ' disabled';
  684. }
  685. $genbutton .= '>';
  686. if ($allowgenifempty && !is_array($modellist) && empty($modellist) && empty($conf->dol_no_mouse_hover) && $modulepart != 'unpaid') {
  687. $langs->load("errors");
  688. $genbutton .= ' '.img_warning($langs->transnoentitiesnoconv("WarningNoDocumentModelActivated"));
  689. }
  690. if (!$allowgenifempty && !is_array($modellist) && empty($modellist) && empty($conf->dol_no_mouse_hover) && $modulepart != 'unpaid') {
  691. $genbutton = '';
  692. }
  693. if (empty($modellist) && !$showempty && $modulepart != 'unpaid') {
  694. $genbutton = '';
  695. }
  696. $out .= $genbutton;
  697. $out .= '</th>';
  698. if (!empty($hookmanager->hooks['formfile'])) {
  699. foreach ($hookmanager->hooks['formfile'] as $module) {
  700. if (method_exists($module, 'formBuilddocLineOptions')) {
  701. $colspanmore++;
  702. $out .= '<th></th>';
  703. }
  704. }
  705. }
  706. $out .= '</tr>';
  707. // Execute hooks
  708. $parameters = array('colspan'=>($colspan + $colspanmore), 'socid'=>(isset($GLOBALS['socid']) ? $GLOBALS['socid'] : ''), 'id'=>(isset($GLOBALS['id']) ? $GLOBALS['id'] : ''), 'modulepart'=>$modulepart);
  709. if (is_object($hookmanager)) {
  710. $reshook = $hookmanager->executeHooks('formBuilddocOptions', $parameters, $GLOBALS['object']);
  711. $out .= $hookmanager->resPrint;
  712. }
  713. }
  714. // Get list of files
  715. if (!empty($filedir)) {
  716. $link_list = array();
  717. if (is_object($object)) {
  718. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  719. $link = new Link($this->db);
  720. $sortfield = $sortorder = null;
  721. $res = $link->fetchAll($link_list, $object->element, $object->id, $sortfield, $sortorder);
  722. }
  723. $out .= '<!-- html.formfile::showdocuments -->'."\n";
  724. // Show title of array if not already shown
  725. if ((!empty($file_list) || !empty($link_list) || preg_match('/^massfilesarea/', $modulepart))
  726. && !$headershown) {
  727. $headershown = 1;
  728. $out .= '<div class="titre">'.$titletoshow.'</div>'."\n";
  729. $out .= '<div class="div-table-responsive-no-min">';
  730. $out .= '<table class="noborder centpercent" id="'.$modulepart.'_table">'."\n";
  731. }
  732. // Loop on each file found
  733. if (is_array($file_list)) {
  734. // Defined relative dir to DOL_DATA_ROOT
  735. $relativedir = '';
  736. if ($filedir) {
  737. $relativedir = preg_replace('/^'.preg_quote(DOL_DATA_ROOT, '/').'/', '', $filedir);
  738. $relativedir = preg_replace('/^[\\/]/', '', $relativedir);
  739. }
  740. // Get list of files stored into database for same relative directory
  741. if ($relativedir) {
  742. completeFileArrayWithDatabaseInfo($file_list, $relativedir);
  743. //var_dump($sortfield.' - '.$sortorder);
  744. if (!empty($sortfield) && !empty($sortorder)) { // If $sortfield is for example 'position_name', we will sort on the property 'position_name' (that is concat of position+name)
  745. $file_list = dol_sort_array($file_list, $sortfield, $sortorder);
  746. }
  747. }
  748. foreach ($file_list as $file) {
  749. // Define relative path for download link (depends on module)
  750. $relativepath = $file["name"]; // Cas general
  751. if ($modulesubdir) {
  752. $relativepath = $modulesubdir."/".$file["name"]; // Cas propal, facture...
  753. }
  754. if ($modulepart == 'export') {
  755. $relativepath = $file["name"]; // Other case
  756. }
  757. $out .= '<tr class="oddeven">';
  758. $documenturl = DOL_URL_ROOT.'/document.php';
  759. if (isset($conf->global->DOL_URL_ROOT_DOCUMENT_PHP)) {
  760. $documenturl = $conf->global->DOL_URL_ROOT_DOCUMENT_PHP; // To use another wrapper
  761. }
  762. // Show file name with link to download
  763. $imgpreview = $this->showPreview($file, $modulepart, $relativepath, 0, $param);;
  764. $out .= '<td class="minwidth200 tdoverflowmax300">';
  765. if ($imgpreview) {
  766. $out .= '<span class="spanoverflow widthcentpercentminusx valignmiddle">';
  767. } else {
  768. $out .= '<span class="spanoverflow">';
  769. }
  770. $out .= '<a class="documentdownload paddingright" href="'.$documenturl.'?modulepart='.$modulepart.'&file='.urlencode($relativepath).($param ? '&'.$param : '').'"';
  771. $mime = dol_mimetype($relativepath, '', 0);
  772. if (preg_match('/text/', $mime)) {
  773. $out .= ' target="_blank" rel="noopener noreferrer"';
  774. }
  775. $out .= ' title="'.dol_escape_htmltag($file["name"]).'"';
  776. $out .= '>';
  777. $out .= img_mime($file["name"], $langs->trans("File").': '.$file["name"]);
  778. $out .= dol_trunc($file["name"], 150);
  779. $out .= '</a>';
  780. $out .= '</span>'."\n";
  781. $out .= $imgpreview;
  782. $out .= '</td>';
  783. // Show file size
  784. $size = (!empty($file['size']) ? $file['size'] : dol_filesize($filedir."/".$file["name"]));
  785. $out .= '<td class="nowraponall right">'.dol_print_size($size, 1, 1).'</td>';
  786. // Show file date
  787. $date = (!empty($file['date']) ? $file['date'] : dol_filemtime($filedir."/".$file["name"]));
  788. $out .= '<td class="nowrap right">'.dol_print_date($date, 'dayhour', 'tzuser').'</td>';
  789. // Show share link
  790. $out .= '<td class="nowraponall">';
  791. if (!empty($file['share'])) {
  792. // Define $urlwithroot
  793. $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
  794. $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  795. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  796. //print '<span class="opacitymedium">'.$langs->trans("Hash").' : '.$file['share'].'</span>';
  797. $forcedownload = 0;
  798. $paramlink = '';
  799. if (!empty($file['share'])) {
  800. $paramlink .= ($paramlink ? '&' : '').'hashp='.$file['share']; // Hash for public share
  801. }
  802. if ($forcedownload) {
  803. $paramlink .= ($paramlink ? '&' : '').'attachment=1';
  804. }
  805. $fulllink = $urlwithroot.'/document.php'.($paramlink ? '?'.$paramlink : '');
  806. $out .= '<a href="'.$fulllink.'" target="_blank" rel="noopener">'.img_picto($langs->trans("FileSharedViaALink"), 'globe').'</a> ';
  807. $out .= '<input type="text" class="quatrevingtpercentminusx width75 nopadding small" id="downloadlink'.$file['rowid'].'" name="downloadexternallink" title="'.dol_escape_htmltag($langs->trans("FileSharedViaALink")).'" value="'.dol_escape_htmltag($fulllink).'">';
  808. $out .= ajax_autoselect('downloadlink'.$file['rowid']);
  809. } else {
  810. //print '<span class="opacitymedium">'.$langs->trans("FileNotShared").'</span>';
  811. }
  812. $out .= '</td>';
  813. // Show picto delete, print...
  814. if ($delallowed || $printer || $morepicto) {
  815. $out .= '<td class="right nowraponall">';
  816. if ($delallowed) {
  817. $tmpurlsource = preg_replace('/#[a-zA-Z0-9_]*$/', '', $urlsource);
  818. $out .= '<a class="reposition" href="'.$tmpurlsource.((strpos($tmpurlsource, '?') === false) ? '?' : '&').'action='.urlencode($removeaction).'&token='.newToken().'&file='.urlencode($relativepath);
  819. $out .= ($param ? '&'.$param : '');
  820. //$out.= '&modulepart='.$modulepart; // TODO obsolete ?
  821. //$out.= '&urlsource='.urlencode($urlsource); // TODO obsolete ?
  822. $out .= '">'.img_picto($langs->trans("Delete"), 'delete').'</a>';
  823. }
  824. if ($printer) {
  825. $out .= '<a class="marginleftonly reposition" href="'.$urlsource.(strpos($urlsource, '?') ? '&' : '?').'action=print_file&token='.newToken().'&printer='.urlencode($modulepart).'&file='.urlencode($relativepath);
  826. $out .= ($param ? '&'.$param : '');
  827. $out .= '">'.img_picto($langs->trans("PrintFile", $relativepath), 'printer.png').'</a>';
  828. }
  829. if ($morepicto) {
  830. $morepicto = preg_replace('/__FILENAMEURLENCODED__/', urlencode($relativepath), $morepicto);
  831. $out .= $morepicto;
  832. }
  833. $out .= '</td>';
  834. }
  835. if (is_object($hookmanager)) {
  836. $addcolumforpicto = ($delallowed || $printer || $morepicto);
  837. $colspan = (4 + ($addcolumforpicto ? 1 : 0));
  838. $colspanmore = 0;
  839. $parameters = array('colspan'=>($colspan + $colspanmore), 'socid'=>(isset($GLOBALS['socid']) ? $GLOBALS['socid'] : ''), 'id'=>(isset($GLOBALS['id']) ? $GLOBALS['id'] : ''), 'modulepart'=>$modulepart, 'relativepath'=>$relativepath);
  840. $res = $hookmanager->executeHooks('formBuilddocLineOptions', $parameters, $file);
  841. if (empty($res)) {
  842. $out .= $hookmanager->resPrint; // Complete line
  843. $out .= '</tr>';
  844. } else {
  845. $out = $hookmanager->resPrint; // Replace all $out
  846. }
  847. }
  848. }
  849. $this->numoffiles++;
  850. }
  851. // Loop on each link found
  852. if (is_array($link_list)) {
  853. $colspan = 2;
  854. foreach ($link_list as $file) {
  855. $out .= '<tr class="oddeven">';
  856. $out .= '<td colspan="'.$colspan.'" class="maxwidhtonsmartphone">';
  857. $out .= '<a data-ajax="false" href="'.$file->url.'" target="_blank" rel="noopener noreferrer">';
  858. $out .= $file->label;
  859. $out .= '</a>';
  860. $out .= '</td>';
  861. $out .= '<td class="right">';
  862. $out .= dol_print_date($file->datea, 'dayhour');
  863. $out .= '</td>';
  864. // for share link of files
  865. $out .= '<td></td>';
  866. if ($delallowed || $printer || $morepicto) {
  867. $out .= '<td></td>';
  868. }
  869. $out .= '</tr>'."\n";
  870. }
  871. $this->numoffiles++;
  872. }
  873. if (count($file_list) == 0 && count($link_list) == 0 && $headershown) {
  874. $out .= '<tr><td colspan="'.(3 + ($addcolumforpicto ? 1 : 0)).'"><span class="opacitymedium">'.$langs->trans("None").'</span></td></tr>'."\n";
  875. }
  876. }
  877. if ($headershown) {
  878. // Affiche pied du tableau
  879. $out .= "</table>\n";
  880. $out .= "</div>\n";
  881. if ($genallowed) {
  882. if (empty($noform)) {
  883. $out .= '</form>'."\n";
  884. }
  885. }
  886. }
  887. $out .= '<!-- End show_document -->'."\n";
  888. //return ($i?$i:$headershown);
  889. return $out;
  890. }
  891. /**
  892. * Show a Document icon with link(s)
  893. * You may want to call this into a div like this:
  894. * print '<div class="inline-block valignmiddle">'.$formfile->getDocumentsLink($element_doc, $filename, $filedir).'</div>';
  895. *
  896. * @param string $modulepart 'propal', 'facture', 'facture_fourn', ...
  897. * @param string $modulesubdir Sub-directory to scan (Example: '0/1/10', 'FA/DD/MM/YY/9999'). Use '' if file is not into subdir of module.
  898. * @param string $filedir Full path to directory to scan
  899. * @param string $filter Filter filenames on this regex string (Example: '\.pdf$')
  900. * @param string $morecss Add more css to the download picto
  901. * @param string $allfiles 0=Only generated docs, 1=All files
  902. * @return string Output string with HTML link of documents (might be empty string). This also fill the array ->infofiles
  903. */
  904. public function getDocumentsLink($modulepart, $modulesubdir, $filedir, $filter = '', $morecss = 'valignmiddle', $allfiles = 0)
  905. {
  906. global $conf, $langs;
  907. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  908. $out = '';
  909. $this->infofiles = array('nboffiles'=>0, 'extensions'=>array(), 'files'=>array());
  910. $entity = 1; // Without multicompany
  911. // Get object entity
  912. if (isModEnabled('multicompany')) {
  913. $regs = array();
  914. preg_match('/\/([0-9]+)\/[^\/]+\/'.preg_quote($modulesubdir, '/').'$/', $filedir, $regs);
  915. $entity = ((!empty($regs[1]) && $regs[1] > 1) ? $regs[1] : 1); // If entity id not found in $filedir this is entity 1 by default
  916. }
  917. // Get list of files starting with name of ref (Note: files with '^ref\.extension' are generated files, files with '^ref-...' are uploaded files)
  918. if ($allfiles || !empty($conf->global->MAIN_SHOW_ALL_FILES_ON_DOCUMENT_TOOLTIP)) {
  919. $filterforfilesearch = '^'.preg_quote(basename($modulesubdir), '/');
  920. } else {
  921. $filterforfilesearch = '^'.preg_quote(basename($modulesubdir), '/').'\.';
  922. }
  923. $file_list = dol_dir_list($filedir, 'files', 0, $filterforfilesearch, '\.meta$|\.png$'); // We also discard .meta and .png preview
  924. //var_dump($file_list);
  925. // For ajax treatment
  926. $out .= '<!-- html.formfile::getDocumentsLink -->'."\n";
  927. if (!empty($file_list)) {
  928. $out = '<dl class="dropdown inline-block">
  929. <dt><a data-ajax="false" href="#" onClick="return false;">'.img_picto('', 'listlight', '', 0, 0, 0, '', $morecss).'</a></dt>
  930. <dd><div class="multichoicedoc" style="position:absolute;left:100px;" ><ul class="ulselectedfields">';
  931. $tmpout = '';
  932. // Loop on each file found
  933. $found = 0;
  934. $i = 0;
  935. foreach ($file_list as $file) {
  936. $i++;
  937. if ($filter && !preg_match('/'.$filter.'/i', $file["name"])) {
  938. continue; // Discard this. It does not match provided filter.
  939. }
  940. $found++;
  941. // Define relative path for download link (depends on module)
  942. $relativepath = $file["name"]; // Cas general
  943. if ($modulesubdir) {
  944. $relativepath = $modulesubdir."/".$file["name"]; // Cas propal, facture...
  945. }
  946. // Autre cas
  947. if ($modulepart == 'donation') {
  948. $relativepath = get_exdir($modulesubdir, 2, 0, 0, null, 'donation').$file["name"];
  949. }
  950. if ($modulepart == 'export') {
  951. $relativepath = $file["name"];
  952. }
  953. $this->infofiles['nboffiles']++;
  954. $this->infofiles['files'][] = $file['fullname'];
  955. $ext = pathinfo($file["name"], PATHINFO_EXTENSION);
  956. if (empty($this->infofiles[$ext])) {
  957. $this->infofiles['extensions'][$ext] = 1;
  958. } else {
  959. $this->infofiles['extensions'][$ext]++;
  960. }
  961. // Preview
  962. if (!empty($conf->use_javascript_ajax) && ($conf->browser->layout != 'phone')) {
  963. $tmparray = getAdvancedPreviewUrl($modulepart, $relativepath, 1, '&entity='.$entity);
  964. if ($tmparray && $tmparray['url']) {
  965. $tmpout .= '<li><a href="'.$tmparray['url'].'"'.($tmparray['css'] ? ' class="'.$tmparray['css'].'"' : '').($tmparray['mime'] ? ' mime="'.$tmparray['mime'].'"' : '').($tmparray['target'] ? ' target="'.$tmparray['target'].'"' : '').'>';
  966. //$tmpout.= img_picto('','detail');
  967. $tmpout .= '<i class="fa fa-search-plus paddingright" style="color: gray"></i>';
  968. $tmpout .= $langs->trans("Preview").' '.$ext.'</a></li>';
  969. }
  970. }
  971. // Download
  972. $tmpout .= '<li class="nowrap"><a class="pictopreview nowrap" href="'.DOL_URL_ROOT.'/document.php?modulepart='.$modulepart.'&amp;entity='.$entity.'&amp;file='.urlencode($relativepath).'"';
  973. $mime = dol_mimetype($relativepath, '', 0);
  974. if (preg_match('/text/', $mime)) {
  975. $tmpout .= ' target="_blank" rel="noopener noreferrer"';
  976. }
  977. $tmpout .= '>';
  978. $tmpout .= img_mime($relativepath, $file["name"]);
  979. $tmpout .= $langs->trans("Download").' '.$ext;
  980. $tmpout .= '</a></li>'."\n";
  981. }
  982. $out .= $tmpout;
  983. $out .= '</ul></div></dd>
  984. </dl>';
  985. if (!$found) {
  986. $out = '';
  987. }
  988. } else {
  989. // TODO Add link to regenerate doc ?
  990. //$out.= '<div id="gen_pdf_'.$modulesubdir.'" class="linkobject hideobject">'.img_picto('', 'refresh').'</div>'."\n";
  991. }
  992. return $out;
  993. }
  994. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  995. /**
  996. * Show list of documents in $filearray (may be they are all in same directory but may not)
  997. * This also sync database if $upload_dir is defined.
  998. *
  999. * @param array $filearray Array of files loaded by dol_dir_list('files') function before calling this.
  1000. * @param Object $object Object on which document is linked to.
  1001. * @param string $modulepart Value for modulepart used by download or viewimage wrapper.
  1002. * @param string $param Parameters on sort links (param must start with &, example &aaa=bbb&ccc=ddd)
  1003. * @param int $forcedownload Force to open dialog box "Save As" when clicking on file.
  1004. * @param string $relativepath Relative path of docs (autodefined if not provided), relative to module dir, not to MAIN_DATA_ROOT.
  1005. * @param int $permonobject Permission on object (so permission to delete or crop document)
  1006. * @param int $useinecm Change output for use in ecm module:
  1007. * 0 or 6: Add a preview column. Show also a rename button. Show also a crop button for some values of $modulepart (must be supported into hard coded list in this function + photos_resize.php + restrictedArea + checkUserAccessToObject)
  1008. * 1: Add link to edit ECM entry
  1009. * 2: Add rename and crop link
  1010. * 4: Add a preview column
  1011. * 5: Add link to edit ECM entry and Add a preview column
  1012. * @param string $textifempty Text to show if filearray is empty ('NoFileFound' if not defined)
  1013. * @param int $maxlength Maximum length of file name shown.
  1014. * @param string $title Title before list. Use 'none' to disable title.
  1015. * @param string $url Full url to use for click links ('' = autodetect)
  1016. * @param int $showrelpart 0=Show only filename (default), 1=Show first level 1 dir
  1017. * @param int $permtoeditline Permission to edit document line (You must provide a value, -1 is deprecated and must not be used any more)
  1018. * @param string $upload_dir Full path directory so we can know dir relative to MAIN_DATA_ROOT. Fill this to complete file data with database indexes.
  1019. * @param string $sortfield Sort field ('name', 'size', 'position', ...)
  1020. * @param string $sortorder Sort order ('ASC' or 'DESC')
  1021. * @param int $disablemove 1=Disable move button, 0=Position move is possible.
  1022. * @param int $addfilterfields Add the line with filters
  1023. * @param int $disablecrop Disable crop feature on images (-1 = auto, prefer to set it explicitely to 0 or 1)
  1024. * @param string $moreattrondiv More attributes on the div for responsive. Example 'style="height:280px; overflow: auto;"'
  1025. * @return int <0 if KO, nb of files shown if OK
  1026. * @see list_of_autoecmfiles()
  1027. */
  1028. public function list_of_documents($filearray, $object, $modulepart, $param = '', $forcedownload = 0, $relativepath = '', $permonobject = 1, $useinecm = 0, $textifempty = '', $maxlength = 0, $title = '', $url = '', $showrelpart = 0, $permtoeditline = -1, $upload_dir = '', $sortfield = '', $sortorder = 'ASC', $disablemove = 1, $addfilterfields = 0, $disablecrop = -1, $moreattrondiv = '')
  1029. {
  1030. // phpcs:enable
  1031. global $user, $conf, $langs, $hookmanager, $form;
  1032. global $sortfield, $sortorder, $maxheightmini;
  1033. global $dolibarr_main_url_root;
  1034. if ($disablecrop == -1) {
  1035. $disablecrop = 1;
  1036. // Values here must be supported by the photos_resize.php page.
  1037. if (in_array($modulepart, array('bank', 'bom', 'expensereport', 'facture', 'facture_fournisseur', 'holiday', 'medias', 'member', 'mrp', 'project', 'product', 'produit', 'propal', 'service', 'societe', 'tax', 'tax-vat', 'ticket', 'user'))) {
  1038. $disablecrop = 0;
  1039. }
  1040. }
  1041. // Define relative path used to store the file
  1042. if (empty($relativepath)) {
  1043. $relativepath = (!empty($object->ref) ?dol_sanitizeFileName($object->ref) : '').'/';
  1044. if (!empty($object->element) && $object->element == 'invoice_supplier') {
  1045. $relativepath = get_exdir($object->id, 2, 0, 0, $object, 'invoice_supplier').$relativepath; // TODO Call using a defined value for $relativepath
  1046. }
  1047. if (!empty($object->element) && $object->element == 'project_task') {
  1048. $relativepath = 'Call_not_supported_._Call_function_using_a_defined_relative_path_.';
  1049. }
  1050. }
  1051. // For backward compatiblity, we detect file stored into an old path
  1052. if (getDolGlobalInt('PRODUCT_USE_OLD_PATH_FOR_PHOTO') && $filearray[0]['level1name'] == 'photos') {
  1053. $relativepath = preg_replace('/^.*\/produit\//', '', $filearray[0]['path']).'/';
  1054. }
  1055. // Defined relative dir to DOL_DATA_ROOT
  1056. $relativedir = '';
  1057. if ($upload_dir) {
  1058. $relativedir = preg_replace('/^'.preg_quote(DOL_DATA_ROOT, '/').'/', '', $upload_dir);
  1059. $relativedir = preg_replace('/^[\\/]/', '', $relativedir);
  1060. }
  1061. // For example here $upload_dir = '/pathtodocuments/commande/SO2001-123/'
  1062. // For example here $upload_dir = '/pathtodocuments/tax/vat/1'
  1063. $hookmanager->initHooks(array('formfile'));
  1064. $parameters = array(
  1065. 'filearray' => $filearray,
  1066. 'modulepart'=> $modulepart,
  1067. 'param' => $param,
  1068. 'forcedownload' => $forcedownload,
  1069. 'relativepath' => $relativepath, // relative filename to module dir
  1070. 'relativedir' => $relativedir, // relative dirname to DOL_DATA_ROOT
  1071. 'permtodelete' => $permonobject,
  1072. 'useinecm' => $useinecm,
  1073. 'textifempty' => $textifempty,
  1074. 'maxlength' => $maxlength,
  1075. 'title' => $title,
  1076. 'url' => $url
  1077. );
  1078. $reshook = $hookmanager->executeHooks('showFilesList', $parameters, $object);
  1079. if (!empty($reshook)) { // null or '' for bypass
  1080. return $reshook;
  1081. } else {
  1082. if (!is_object($form)) {
  1083. include_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php'; // The compoent may be included into ajax page that does not include the Form class
  1084. $form = new Form($this->db);
  1085. }
  1086. if (!preg_match('/&id=/', $param) && isset($object->id)) {
  1087. $param .= '&id='.$object->id;
  1088. }
  1089. $relativepathwihtoutslashend = preg_replace('/\/$/', '', $relativepath);
  1090. if ($relativepathwihtoutslashend) {
  1091. $param .= '&file='.urlencode($relativepathwihtoutslashend);
  1092. }
  1093. if ($permtoeditline < 0) { // Old behaviour for backward compatibility. New feature should call method with value 0 or 1
  1094. $permtoeditline = 0;
  1095. if (in_array($modulepart, array('product', 'produit', 'service'))) {
  1096. if ($user->rights->produit->creer && $object->type == Product::TYPE_PRODUCT) {
  1097. $permtoeditline = 1;
  1098. }
  1099. if ($user->rights->service->creer && $object->type == Product::TYPE_SERVICE) {
  1100. $permtoeditline = 1;
  1101. }
  1102. }
  1103. }
  1104. if (empty($conf->global->MAIN_UPLOAD_DOC)) {
  1105. $permtoeditline = 0;
  1106. $permonobject = 0;
  1107. }
  1108. // Show list of existing files
  1109. if ((empty($useinecm) || $useinecm == 6) && $title != 'none') {
  1110. print load_fiche_titre($title ? $title : $langs->trans("AttachedFiles"), '', 'file-upload', 0, '', 'table-list-of-attached-files');
  1111. }
  1112. if (empty($url)) {
  1113. $url = $_SERVER["PHP_SELF"];
  1114. }
  1115. print '<!-- html.formfile::list_of_documents -->'."\n";
  1116. if (GETPOST('action', 'aZ09') == 'editfile' && $permtoeditline) {
  1117. print '<form action="'.$_SERVER["PHP_SELF"].'?'.$param.'" method="POST">';
  1118. print '<input type="hidden" name="token" value="'.newToken().'">';
  1119. print '<input type="hidden" name="action" value="renamefile">';
  1120. print '<input type="hidden" name="id" value="'.$object->id.'">';
  1121. print '<input type="hidden" name="modulepart" value="'.$modulepart.'">';
  1122. }
  1123. print '<div class="div-table-responsive-no-min"'.($moreattrondiv ? ' '.$moreattrondiv : '').'>';
  1124. print '<table id="tablelines" class="centpercent liste noborder nobottom">'."\n";
  1125. if (!empty($addfilterfields)) {
  1126. print '<tr class="liste_titre nodrag nodrop">';
  1127. print '<td><input type="search_doc_ref" value="'.dol_escape_htmltag(GETPOST('search_doc_ref', 'alpha')).'"></td>';
  1128. print '<td></td>';
  1129. print '<td></td>';
  1130. if (empty($useinecm) || $useinecm == 4 || $useinecm == 5 || $useinecm == 6) {
  1131. print '<td></td>';
  1132. }
  1133. print '<td></td>';
  1134. print '<td></td>';
  1135. if (empty($disablemove) && count($filearray) > 1) {
  1136. print '<td></td>';
  1137. }
  1138. print "</tr>\n";
  1139. }
  1140. // Get list of files stored into database for same relative directory
  1141. if ($relativedir) {
  1142. completeFileArrayWithDatabaseInfo($filearray, $relativedir);
  1143. //var_dump($sortfield.' - '.$sortorder);
  1144. if ($sortfield && $sortorder) { // If $sortfield is for example 'position_name', we will sort on the property 'position_name' (that is concat of position+name)
  1145. $filearray = dol_sort_array($filearray, $sortfield, $sortorder);
  1146. }
  1147. }
  1148. print '<tr class="liste_titre nodrag nodrop">';
  1149. //print $url.' sortfield='.$sortfield.' sortorder='.$sortorder;
  1150. print_liste_field_titre('Documents2', $url, "name", "", $param, '', $sortfield, $sortorder, 'left ');
  1151. print_liste_field_titre('Size', $url, "size", "", $param, '', $sortfield, $sortorder, 'right ');
  1152. print_liste_field_titre('Date', $url, "date", "", $param, '', $sortfield, $sortorder, 'center ');
  1153. if (empty($useinecm) || $useinecm == 4 || $useinecm == 5 || $useinecm == 6) {
  1154. print_liste_field_titre('', $url, "", "", $param, '', $sortfield, $sortorder, 'center '); // Preview
  1155. }
  1156. // Shared or not - Hash of file
  1157. print_liste_field_titre('');
  1158. // Action button
  1159. print_liste_field_titre('');
  1160. if (empty($disablemove) && count($filearray) > 1) {
  1161. print_liste_field_titre('');
  1162. }
  1163. print "</tr>\n";
  1164. $nboffiles = count($filearray);
  1165. if ($nboffiles > 0) {
  1166. include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
  1167. }
  1168. $i = 0;
  1169. $nboflines = 0;
  1170. $lastrowid = 0;
  1171. foreach ($filearray as $key => $file) { // filearray must be only files here
  1172. if ($file['name'] != '.'
  1173. && $file['name'] != '..'
  1174. && !preg_match('/\.meta$/i', $file['name'])) {
  1175. if (array_key_exists('rowid', $filearray[$key]) && $filearray[$key]['rowid'] > 0) {
  1176. $lastrowid = $filearray[$key]['rowid'];
  1177. }
  1178. $filepath = $relativepath.$file['name'];
  1179. $editline = 0;
  1180. $nboflines++;
  1181. print '<!-- Line list_of_documents '.$key.' relativepath = '.$relativepath.' -->'."\n";
  1182. // Do we have entry into database ?
  1183. print '<!-- In database: position='.(array_key_exists('position', $filearray[$key]) ? $filearray[$key]['position'] : 0).' -->'."\n";
  1184. print '<tr class="oddeven" id="row-'.((array_key_exists('rowid', $filearray[$key]) && $filearray[$key]['rowid'] > 0) ? $filearray[$key]['rowid'] : 'AFTER'.$lastrowid.'POS'.($i + 1)).'">';
  1185. // File name
  1186. print '<td class="minwith200 tdoverflowmax500">';
  1187. // Show file name with link to download
  1188. //print "XX".$file['name']; //$file['name'] must be utf8
  1189. print '<a class="paddingright valignmiddle" href="'.DOL_URL_ROOT.'/document.php?modulepart='.$modulepart;
  1190. if ($forcedownload) {
  1191. print '&attachment=1';
  1192. }
  1193. if (!empty($object->entity)) {
  1194. print '&entity='.$object->entity;
  1195. }
  1196. print '&file='.urlencode($filepath);
  1197. print '">';
  1198. print img_mime($file['name'], $file['name'].' ('.dol_print_size($file['size'], 0, 0).')', 'inline-block valignmiddle paddingright');
  1199. if ($showrelpart == 1) {
  1200. print $relativepath;
  1201. }
  1202. //print dol_trunc($file['name'],$maxlength,'middle');
  1203. if (GETPOST('action', 'aZ09') == 'editfile' && $file['name'] == basename(GETPOST('urlfile', 'alpha'))) {
  1204. print '</a>';
  1205. $section_dir = dirname(GETPOST('urlfile', 'alpha'));
  1206. if (!preg_match('/\/$/', $section_dir)) {
  1207. $section_dir .= '/';
  1208. }
  1209. print '<input type="hidden" name="section_dir" value="'.$section_dir.'">';
  1210. print '<input type="hidden" name="renamefilefrom" value="'.dol_escape_htmltag($file['name']).'">';
  1211. print '<input type="text" name="renamefileto" class="quatrevingtpercent" value="'.dol_escape_htmltag($file['name']).'">';
  1212. $editline = 1;
  1213. } else {
  1214. $filenametoshow = preg_replace('/\.noexe$/', '', $file['name']);
  1215. print dol_escape_htmltag(dol_trunc($filenametoshow, 200));
  1216. print '</a>';
  1217. }
  1218. // Preview link
  1219. if (!$editline) {
  1220. print $this->showPreview($file, $modulepart, $filepath, 0, '&entity='.(!empty($object->entity) ? $object->entity : $conf->entity));
  1221. }
  1222. print "</td>\n";
  1223. // Size
  1224. $sizetoshow = dol_print_size($file['size'], 1, 1);
  1225. $sizetoshowbytes = dol_print_size($file['size'], 0, 1);
  1226. print '<td class="right nowraponall">';
  1227. if ($sizetoshow == $sizetoshowbytes) {
  1228. print $sizetoshow;
  1229. } else {
  1230. print $form->textwithpicto($sizetoshow, $sizetoshowbytes, -1);
  1231. }
  1232. print '</td>';
  1233. // Date
  1234. print '<td class="center nowraponall">'.dol_print_date($file['date'], "dayhour", "tzuser").'</td>';
  1235. // Preview
  1236. if (empty($useinecm) || $useinecm == 4 || $useinecm == 5 || $useinecm == 6) {
  1237. $fileinfo = pathinfo($file['name']);
  1238. print '<td class="center">';
  1239. if (image_format_supported($file['name']) >= 0) {
  1240. if ($useinecm == 5 || $useinecm == 6) {
  1241. $smallfile = getImageFileNameForSize($file['name'], ''); // There is no thumb for ECM module and Media filemanager, so we use true image. TODO Change this it is slow on image dir.
  1242. } else {
  1243. $smallfile = getImageFileNameForSize($file['name'], '_small'); // For new thumbs using same ext (in lower case however) than original
  1244. }
  1245. if (!dol_is_file($file['path'].'/'.$smallfile)) {
  1246. $smallfile = getImageFileNameForSize($file['name'], '_small', '.png'); // For backward compatibility of old thumbs that were created with filename in lower case and with .png extension
  1247. }
  1248. //print $file['path'].'/'.$smallfile.'<br>';
  1249. $urlforhref = getAdvancedPreviewUrl($modulepart, $relativepath.$fileinfo['filename'].'.'.strtolower($fileinfo['extension']), 1, '&entity='.(!empty($object->entity) ? $object->entity : $conf->entity));
  1250. if (empty($urlforhref)) {
  1251. $urlforhref = DOL_URL_ROOT.'/viewimage.php?modulepart='.$modulepart.'&entity='.(!empty($object->entity) ? $object->entity : $conf->entity).'&file='.urlencode($relativepath.$fileinfo['filename'].'.'.strtolower($fileinfo['extension']));
  1252. print '<a href="'.$urlforhref.'" class="aphoto" target="_blank" rel="noopener noreferrer">';
  1253. } else {
  1254. print '<a href="'.$urlforhref['url'].'" class="'.$urlforhref['css'].'" target="'.$urlforhref['target'].'" mime="'.$urlforhref['mime'].'">';
  1255. }
  1256. print '<img class="photo maxwidth200 shadow valignmiddle" height="'.(($useinecm == 4 || $useinecm == 5 || $useinecm == 6) ? '20' : $maxheightmini).'" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$modulepart.'&entity='.(!empty($object->entity) ? $object->entity : $conf->entity).'&file='.urlencode($relativepath.$smallfile).'" title="">';
  1257. print '</a>';
  1258. } else {
  1259. print '&nbsp;';
  1260. }
  1261. print '</td>';
  1262. }
  1263. // Shared or not - Hash of file
  1264. print '<td class="center">';
  1265. if ($relativedir && $filearray[$key]['rowid'] > 0) { // only if we are in a mode where a scan of dir were done and we have id of file in ECM table
  1266. if ($editline) {
  1267. print '<label for="idshareenabled'.$key.'">'.$langs->trans("FileSharedViaALink").'</label> ';
  1268. print '<input class="inline-block" type="checkbox" id="idshareenabled'.$key.'" name="shareenabled"'.($file['share'] ? ' checked="checked"' : '').' /> ';
  1269. } else {
  1270. if ($file['share']) {
  1271. // Define $urlwithroot
  1272. $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
  1273. $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  1274. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  1275. //print '<span class="opacitymedium">'.$langs->trans("Hash").' : '.$file['share'].'</span>';
  1276. $forcedownload = 0;
  1277. $paramlink = '';
  1278. if (!empty($file['share'])) {
  1279. $paramlink .= ($paramlink ? '&' : '').'hashp='.$file['share']; // Hash for public share
  1280. }
  1281. if ($forcedownload) {
  1282. $paramlink .= ($paramlink ? '&' : '').'attachment=1';
  1283. }
  1284. $fulllink = $urlwithroot.'/document.php'.($paramlink ? '?'.$paramlink : '');
  1285. print '<a href="'.$fulllink.'" target="_blank" rel="noopener">'.img_picto($langs->trans("FileSharedViaALink"), 'globe').'</a> ';
  1286. print '<input type="text" class="quatrevingtpercent minwidth200imp nopadding small" id="downloadlink'.$filearray[$key]['rowid'].'" name="downloadexternallink" title="'.dol_escape_htmltag($langs->trans("FileSharedViaALink")).'" value="'.dol_escape_htmltag($fulllink).'">';
  1287. } else {
  1288. //print '<span class="opacitymedium">'.$langs->trans("FileNotShared").'</span>';
  1289. }
  1290. }
  1291. }
  1292. print '</td>';
  1293. // Actions buttons (1 column or 2 if !disablemove)
  1294. if (!$editline) {
  1295. // Delete or view link
  1296. // ($param must start with &)
  1297. print '<td class="valignmiddle right actionbuttons nowraponall"><!-- action on files -->';
  1298. if ($useinecm == 1 || $useinecm == 5) { // ECM manual tree only
  1299. // $section is inside $param
  1300. $newparam = preg_replace('/&file=.*$/', '', $param); // We don't need param file=
  1301. $backtopage = DOL_URL_ROOT.'/ecm/index.php?&section_dir='.urlencode($relativepath).$newparam;
  1302. print '<a class="editfielda editfilelink" href="'.DOL_URL_ROOT.'/ecm/file_card.php?urlfile='.urlencode($file['name']).$param.'&backtopage='.urlencode($backtopage).'" rel="'.urlencode($file['name']).'">'.img_edit('default', 0, 'class="paddingrightonly"').'</a>';
  1303. }
  1304. if (empty($useinecm) || $useinecm == 2 || $useinecm == 6) { // 6=Media file manager
  1305. $newmodulepart = $modulepart;
  1306. if (in_array($modulepart, array('product', 'produit', 'service'))) {
  1307. $newmodulepart = 'produit|service';
  1308. }
  1309. if (!$disablecrop && image_format_supported($file['name']) > 0) {
  1310. if ($permtoeditline) {
  1311. // Link to resize
  1312. $moreparaminurl = '';
  1313. if (!empty($object->id) && $object->id > 0) {
  1314. $moreparaminurl .= '&id='.$object->id;
  1315. } elseif (GETPOST('website', 'alpha')) {
  1316. $moreparaminurl .= '&website='.GETPOST('website', 'alpha');
  1317. }
  1318. // Set the backtourl
  1319. if ($modulepart == 'medias' && !GETPOST('website')) {
  1320. $moreparaminurl .= '&backtourl='.urlencode(DOL_URL_ROOT.'/ecm/index_medias.php?file_manager=1&modulepart='.$modulepart.'&section_dir='.$relativepath);
  1321. }
  1322. //var_dump($moreparaminurl);
  1323. print '<a class="editfielda" href="'.DOL_URL_ROOT.'/core/photos_resize.php?modulepart='.urlencode($newmodulepart).$moreparaminurl.'&file='.urlencode($relativepath.$fileinfo['filename'].'.'.strtolower($fileinfo['extension'])).'" title="'.dol_escape_htmltag($langs->trans("ResizeOrCrop")).'">'.img_picto($langs->trans("ResizeOrCrop"), 'resize', 'class="paddingrightonly"').'</a>';
  1324. }
  1325. }
  1326. if ($permtoeditline) {
  1327. $paramsectiondir = (in_array($modulepart, array('medias', 'ecm')) ? '&section_dir='.urlencode($relativepath) : '');
  1328. print '<a class="editfielda reposition editfilelink" href="'.(($useinecm == 1 || $useinecm == 5) ? '#' : ($url.'?action=editfile&token='.newToken().'&urlfile='.urlencode($filepath).$paramsectiondir.$param)).'" rel="'.$filepath.'">'.img_edit('default', 0, 'class="paddingrightonly"').'</a>';
  1329. }
  1330. }
  1331. // Output link to delete file
  1332. if ($permonobject) {
  1333. $useajax = 1;
  1334. if (!empty($conf->dol_use_jmobile)) {
  1335. $useajax = 0;
  1336. }
  1337. if (empty($conf->use_javascript_ajax)) {
  1338. $useajax = 0;
  1339. }
  1340. if (!empty($conf->global->MAIN_ECM_DISABLE_JS)) {
  1341. $useajax = 0;
  1342. }
  1343. print '<a href="'.((($useinecm && $useinecm != 6) && $useajax) ? '#' : ($url.'?action=deletefile&token='.newToken().'&urlfile='.urlencode($filepath).$param)).'" class="reposition deletefilelink" rel="'.$filepath.'">'.img_delete().'</a>';
  1344. }
  1345. print "</td>";
  1346. if (empty($disablemove) && count($filearray) > 1) {
  1347. if ($nboffiles > 1 && $conf->browser->layout != 'phone') {
  1348. print '<td class="linecolmove tdlineupdown center">';
  1349. if ($i > 0) {
  1350. print '<a class="lineupdown" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=up&rowid='.$object->id.'">'.img_up('default', 0, 'imgupforline').'</a>';
  1351. }
  1352. if ($i < ($nboffiles - 1)) {
  1353. print '<a class="lineupdown" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=down&rowid='.$object->id.'">'.img_down('default', 0, 'imgdownforline').'</a>';
  1354. }
  1355. print '</td>';
  1356. } else {
  1357. print '<td'.(($conf->browser->layout != 'phone') ? ' class="linecolmove tdlineupdown center"' : ' class="linecolmove center"').'>';
  1358. print '</td>';
  1359. }
  1360. }
  1361. } else {
  1362. print '<td class="right">';
  1363. print '<input type="hidden" name="ecmfileid" value="'.$filearray[$key]['rowid'].'">';
  1364. print '<input type="submit" class="button button-save smallpaddingimp" name="renamefilesave" value="'.dol_escape_htmltag($langs->trans("Save")).'">';
  1365. print '<input type="submit" class="button button-cancel smallpaddingimp" name="cancel" value="'.dol_escape_htmltag($langs->trans("Cancel")).'">';
  1366. print '</td>';
  1367. if (empty($disablemove) && count($filearray) > 1) {
  1368. print '<td class="right"></td>';
  1369. }
  1370. }
  1371. print "</tr>\n";
  1372. $i++;
  1373. }
  1374. }
  1375. if ($nboffiles == 0) {
  1376. $colspan = '6';
  1377. if (empty($disablemove) && count($filearray) > 1) {
  1378. $colspan++; // 6 columns or 7
  1379. }
  1380. print '<tr class="oddeven"><td colspan="'.$colspan.'">';
  1381. if (empty($textifempty)) {
  1382. print '<span class="opacitymedium">'.$langs->trans("NoFileFound").'</span>';
  1383. } else {
  1384. print '<span class="opacitymedium">'.$textifempty.'</span>';
  1385. }
  1386. print '</td></tr>';
  1387. }
  1388. print "</table>";
  1389. print '</div>';
  1390. if ($nboflines > 1 && is_object($object)) {
  1391. if (!empty($conf->use_javascript_ajax) && $permtoeditline) {
  1392. $table_element_line = 'ecm_files';
  1393. include DOL_DOCUMENT_ROOT.'/core/tpl/ajaxrow.tpl.php';
  1394. }
  1395. }
  1396. print ajax_autoselect('downloadlink');
  1397. if (GETPOST('action', 'aZ09') == 'editfile' && $permtoeditline) {
  1398. print '</form>';
  1399. }
  1400. return $nboffiles;
  1401. }
  1402. }
  1403. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1404. /**
  1405. * Show list of documents in a directory of ECM module.
  1406. *
  1407. * @param string $upload_dir Directory that was scanned. This directory will contains files into subdirs REF/files
  1408. * @param array $filearray Array of files loaded by dol_dir_list function before calling this function
  1409. * @param string $modulepart Value for modulepart used by download wrapper. Value can be $object->table_name (that is 'myobject' or 'mymodule_myobject') or $object->element.'-'.$module (for compatibility purpose)
  1410. * @param string $param Parameters on sort links
  1411. * @param int $forcedownload Force to open dialog box "Save As" when clicking on file
  1412. * @param string $relativepath Relative path of docs (autodefined if not provided)
  1413. * @param int $permissiontodelete Permission to delete
  1414. * @param int $useinecm Change output for use in ecm module
  1415. * @param int $textifempty Text to show if filearray is empty
  1416. * @param int $maxlength Maximum length of file name shown
  1417. * @param string $url Full url to use for click links ('' = autodetect)
  1418. * @param int $addfilterfields Add line with filters
  1419. * @return int <0 if KO, nb of files shown if OK
  1420. * @see list_of_documents()
  1421. */
  1422. public function list_of_autoecmfiles($upload_dir, $filearray, $modulepart, $param, $forcedownload = 0, $relativepath = '', $permissiontodelete = 1, $useinecm = 0, $textifempty = '', $maxlength = 0, $url = '', $addfilterfields = 0)
  1423. {
  1424. // phpcs:enable
  1425. global $user, $conf, $langs, $hookmanager, $form;
  1426. global $sortfield, $sortorder;
  1427. global $search_doc_ref;
  1428. global $dolibarr_main_url_root;
  1429. dol_syslog(get_class($this).'::list_of_autoecmfiles upload_dir='.$upload_dir.' modulepart='.$modulepart);
  1430. // Show list of documents
  1431. if (empty($useinecm) || $useinecm == 6) {
  1432. print load_fiche_titre($langs->trans("AttachedFiles"));
  1433. }
  1434. if (empty($url)) {
  1435. $url = $_SERVER["PHP_SELF"];
  1436. }
  1437. if (!empty($addfilterfields)) {
  1438. print '<form action="'.$_SERVER['PHP_SELF'].'">';
  1439. print '<input type="hidden" name="token" value="'.newToken().'">';
  1440. print '<input type="hidden" name="module" value="'.$modulepart.'">';
  1441. }
  1442. print '<div class="div-table-responsive-no-min">';
  1443. print '<table width="100%" class="noborder">'."\n";
  1444. if (!empty($addfilterfields)) {
  1445. print '<tr class="liste_titre nodrag nodrop">';
  1446. print '<td class="liste_titre"></td>';
  1447. print '<td class="liste_titre"><input type="text" class="maxwidth100onsmartphone" name="search_doc_ref" value="'.dol_escape_htmltag($search_doc_ref).'"></td>';
  1448. print '<td class="liste_titre"></td>';
  1449. print '<td class="liste_titre"></td>';
  1450. // Action column
  1451. print '<td class="liste_titre right">';
  1452. $searchpicto = $form->showFilterButtons();
  1453. print $searchpicto;
  1454. print '</td>';
  1455. print "</tr>\n";
  1456. }
  1457. print '<tr class="liste_titre">';
  1458. $sortref = "fullname";
  1459. if ($modulepart == 'invoice_supplier') {
  1460. $sortref = 'level1name';
  1461. }
  1462. print_liste_field_titre("Ref", $url, $sortref, "", $param, '', $sortfield, $sortorder);
  1463. print_liste_field_titre("Documents2", $url, "name", "", $param, '', $sortfield, $sortorder);
  1464. print_liste_field_titre("Size", $url, "size", "", $param, '', $sortfield, $sortorder, 'right ');
  1465. print_liste_field_titre("Date", $url, "date", "", $param, '', $sortfield, $sortorder, 'center ');
  1466. print_liste_field_titre("Shared", $url, 'share', '', $param, '', $sortfield, $sortorder, 'right ');
  1467. print '</tr>'."\n";
  1468. // To show ref or specific information according to view to show (defined by $module)
  1469. $object_instance = null;
  1470. if ($modulepart == 'company') {
  1471. include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  1472. $object_instance = new Societe($this->db);
  1473. } elseif ($modulepart == 'invoice') {
  1474. include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  1475. $object_instance = new Facture($this->db);
  1476. } elseif ($modulepart == 'invoice_supplier') {
  1477. include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  1478. $object_instance = new FactureFournisseur($this->db);
  1479. } elseif ($modulepart == 'propal') {
  1480. include_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
  1481. $object_instance = new Propal($this->db);
  1482. } elseif ($modulepart == 'supplier_proposal') {
  1483. include_once DOL_DOCUMENT_ROOT.'/supplier_proposal/class/supplier_proposal.class.php';
  1484. $object_instance = new SupplierProposal($this->db);
  1485. } elseif ($modulepart == 'order') {
  1486. include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  1487. $object_instance = new Commande($this->db);
  1488. } elseif ($modulepart == 'order_supplier') {
  1489. include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
  1490. $object_instance = new CommandeFournisseur($this->db);
  1491. } elseif ($modulepart == 'contract') {
  1492. include_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
  1493. $object_instance = new Contrat($this->db);
  1494. } elseif ($modulepart == 'product') {
  1495. include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  1496. $object_instance = new Product($this->db);
  1497. } elseif ($modulepart == 'tax') {
  1498. include_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
  1499. $object_instance = new ChargeSociales($this->db);
  1500. } elseif ($modulepart == 'tax-vat') {
  1501. include_once DOL_DOCUMENT_ROOT.'/compta/tva/class/tva.class.php';
  1502. $object_instance = new Tva($this->db);
  1503. } elseif ($modulepart == 'salaries') {
  1504. include_once DOL_DOCUMENT_ROOT.'/salaries/class/salary.class.php';
  1505. $object_instance = new Salary($this->db);
  1506. } elseif ($modulepart == 'project') {
  1507. include_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  1508. $object_instance = new Project($this->db);
  1509. } elseif ($modulepart == 'project_task') {
  1510. include_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
  1511. $object_instance = new Task($this->db);
  1512. } elseif ($modulepart == 'fichinter') {
  1513. include_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
  1514. $object_instance = new Fichinter($this->db);
  1515. } elseif ($modulepart == 'user') {
  1516. include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  1517. $object_instance = new User($this->db);
  1518. } elseif ($modulepart == 'expensereport') {
  1519. include_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
  1520. $object_instance = new ExpenseReport($this->db);
  1521. } elseif ($modulepart == 'holiday') {
  1522. include_once DOL_DOCUMENT_ROOT.'/holiday/class/holiday.class.php';
  1523. $object_instance = new Holiday($this->db);
  1524. } elseif ($modulepart == 'recruitment-recruitmentcandidature') {
  1525. include_once DOL_DOCUMENT_ROOT.'/recruitment/class/recruitmentcandidature.class.php';
  1526. $object_instance = new RecruitmentCandidature($this->db);
  1527. } elseif ($modulepart == 'banque') {
  1528. include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  1529. $object_instance = new Account($this->db);
  1530. } elseif ($modulepart == 'chequereceipt') {
  1531. include_once DOL_DOCUMENT_ROOT.'/compta/paiement/cheque/class/remisecheque.class.php';
  1532. $object_instance = new RemiseCheque($this->db);
  1533. } elseif ($modulepart == 'mrp-mo') {
  1534. include_once DOL_DOCUMENT_ROOT.'/mrp/class/mo.class.php';
  1535. $object_instance = new Mo($this->db);
  1536. } else {
  1537. $parameters = array('modulepart'=>$modulepart);
  1538. $reshook = $hookmanager->executeHooks('addSectionECMAuto', $parameters);
  1539. if ($reshook > 0 && is_array($hookmanager->resArray) && count($hookmanager->resArray) > 0) {
  1540. if (array_key_exists('classpath', $hookmanager->resArray) && !empty($hookmanager->resArray['classpath'])) {
  1541. dol_include_once($hookmanager->resArray['classpath']);
  1542. if (array_key_exists('classname', $hookmanager->resArray) && !empty($hookmanager->resArray['classname'])) {
  1543. if (class_exists($hookmanager->resArray['classname'])) {
  1544. $tmpclassname = $hookmanager->resArray['classname'];
  1545. $object_instance = new $tmpclassname($this->db);
  1546. }
  1547. }
  1548. }
  1549. }
  1550. }
  1551. //var_dump($filearray);
  1552. //var_dump($object_instance);
  1553. // Get list of files stored into database for same relative directory
  1554. $relativepathfromroot = preg_replace('/'.preg_quote(DOL_DATA_ROOT.'/', '/').'/', '', $upload_dir);
  1555. if ($relativepathfromroot) {
  1556. completeFileArrayWithDatabaseInfo($filearray, $relativepathfromroot.'/%');
  1557. //var_dump($sortfield.' - '.$sortorder);
  1558. if ($sortfield && $sortorder) { // If $sortfield is for example 'position_name', we will sort on the property 'position_name' (that is concat of position+name)
  1559. $filearray = dol_sort_array($filearray, $sortfield, $sortorder, 1);
  1560. }
  1561. }
  1562. //var_dump($filearray);
  1563. foreach ($filearray as $key => $file) {
  1564. if (!is_dir($file['name'])
  1565. && $file['name'] != '.'
  1566. && $file['name'] != '..'
  1567. && $file['name'] != 'CVS'
  1568. && !preg_match('/\.meta$/i', $file['name'])) {
  1569. // Define relative path used to store the file
  1570. $relativefile = preg_replace('/'.preg_quote($upload_dir.'/', '/').'/', '', $file['fullname']);
  1571. $id = 0;
  1572. $ref = '';
  1573. // To show ref or specific information according to view to show (defined by $modulepart)
  1574. // $modulepart can be $object->table_name (that is 'mymodule_myobject') or $object->element.'-'.$module (for compatibility purpose)
  1575. $reg = array();
  1576. if ($modulepart == 'company' || $modulepart == 'tax' || $modulepart == 'tax-vat' || $modulepart == 'salaries') {
  1577. preg_match('/(\d+)\/[^\/]+$/', $relativefile, $reg);
  1578. $id = (isset($reg[1]) ? $reg[1] : '');
  1579. } elseif ($modulepart == 'invoice_supplier') {
  1580. preg_match('/([^\/]+)\/[^\/]+$/', $relativefile, $reg);
  1581. $ref = (isset($reg[1]) ? $reg[1] : '');
  1582. if (is_numeric($ref)) {
  1583. $id = $ref;
  1584. $ref = '';
  1585. }
  1586. } elseif ($modulepart == 'user') {
  1587. // $ref may be also id with old supplier invoices
  1588. preg_match('/(.*)\/[^\/]+$/', $relativefile, $reg);
  1589. $id = (isset($reg[1]) ? $reg[1] : '');
  1590. } elseif ($modulepart == 'project_task') {
  1591. // $ref of task is the sub-directory of the project
  1592. $reg = explode("/", $relativefile);
  1593. $ref = (isset($reg[1]) ? $reg[1] : '');
  1594. } elseif (in_array($modulepart, array(
  1595. 'invoice',
  1596. 'propal',
  1597. 'supplier_proposal',
  1598. 'order',
  1599. 'order_supplier',
  1600. 'contract',
  1601. 'product',
  1602. 'project',
  1603. 'project_task',
  1604. 'fichinter',
  1605. 'expensereport',
  1606. 'recruitment-recruitmentcandidature',
  1607. 'mrp-mo',
  1608. 'banque',
  1609. 'chequereceipt',
  1610. 'holiday'))) {
  1611. preg_match('/(.*)\/[^\/]+$/', $relativefile, $reg);
  1612. $ref = (isset($reg[1]) ? $reg[1] : '');
  1613. } else {
  1614. $parameters = array('modulepart'=>$modulepart, 'fileinfo'=>$file);
  1615. $reshook = $hookmanager->executeHooks('addSectionECMAuto', $parameters);
  1616. if ($reshook > 0 && is_array($hookmanager->resArray) && count($hookmanager->resArray) > 0) {
  1617. if (array_key_exists('ref', $hookmanager->resArray) && !empty($hookmanager->resArray['ref'])) {
  1618. $ref = $hookmanager->resArray['ref'];
  1619. }
  1620. if (array_key_exists('id', $hookmanager->resArray) && !empty($hookmanager->resArray['id'])) {
  1621. $id = $hookmanager->resArray['id'];
  1622. }
  1623. }
  1624. //print 'Error: Value for modulepart = '.$modulepart.' is not yet implemented in function list_of_autoecmfiles'."\n";
  1625. }
  1626. if (!$id && !$ref) {
  1627. continue;
  1628. }
  1629. $found = 0;
  1630. if (!empty($this->cache_objects[$modulepart.'_'.$id.'_'.$ref])) {
  1631. $found = 1;
  1632. } else {
  1633. //print 'Fetch '.$id." - ".$ref.' class='.get_class($object_instance).'<br>';
  1634. $result = 0;
  1635. if (is_object($object_instance)) {
  1636. $object_instance->id = 0;
  1637. $object_instance->ref = '';
  1638. if ($id) {
  1639. $result = $object_instance->fetch($id);
  1640. } else {
  1641. if (!($result = $object_instance->fetch('', $ref))) {
  1642. //fetchOneLike looks for objects with wildcards in its reference.
  1643. //It is useful for those masks who get underscores instead of their actual symbols (because the _ had replaced a forbiddn char)
  1644. //fetchOneLike requires some info in the object. If it doesn't have it, then 0 is returned
  1645. //that's why we look only into fetchOneLike when fetch returns 0
  1646. // TODO Remove this part ?
  1647. $result = $object_instance->fetchOneLike($ref);
  1648. }
  1649. }
  1650. }
  1651. if ($result > 0) { // Save object loaded into a cache
  1652. $found = 1;
  1653. $this->cache_objects[$modulepart.'_'.$id.'_'.$ref] = clone $object_instance;
  1654. }
  1655. if ($result == 0) {
  1656. $found = 1;
  1657. $this->cache_objects[$modulepart.'_'.$id.'_'.$ref] = 'notfound';
  1658. unset($filearray[$key]);
  1659. }
  1660. }
  1661. if ($found <= 0 || !is_object($this->cache_objects[$modulepart.'_'.$id.'_'.$ref])) {
  1662. continue; // We do not show orphelins files
  1663. }
  1664. print '<!-- Line list_of_autoecmfiles key='.$key.' -->'."\n";
  1665. print '<tr class="oddeven">';
  1666. print '<td>';
  1667. if ($found > 0 && is_object($this->cache_objects[$modulepart.'_'.$id.'_'.$ref])) {
  1668. $tmpobject = $this->cache_objects[$modulepart.'_'.$id.'_'.$ref];
  1669. //if (! in_array($tmpobject->element, array('expensereport'))) {
  1670. print $tmpobject->getNomUrl(1, 'document');
  1671. //} else {
  1672. // print $tmpobject->getNomUrl(1);
  1673. //}
  1674. } else {
  1675. print $langs->trans("ObjectDeleted", ($id ? $id : $ref));
  1676. }
  1677. //$modulesubdir=dol_sanitizeFileName($ref);
  1678. //$modulesubdir = dirname($relativefile);
  1679. //$filedir=$conf->$modulepart->dir_output . '/' . dol_sanitizeFileName($obj->ref);
  1680. //$filedir = $file['path'];
  1681. //$urlsource=$_SERVER['PHP_SELF'].'?id='.$obj->rowid;
  1682. //print $formfile->getDocumentsLink($modulepart, $filename, $filedir);
  1683. print '</td>';
  1684. // File
  1685. // Check if document source has external module part, if it the case use it for module part on document.php
  1686. print '<td>';
  1687. //print "XX".$file['name']; //$file['name'] must be utf8
  1688. print '<a href="'.DOL_URL_ROOT.'/document.php?modulepart='.urlencode($modulepart);
  1689. if ($forcedownload) {
  1690. print '&attachment=1';
  1691. }
  1692. print '&file='.urlencode($relativefile).'">';
  1693. print img_mime($file['name'], $file['name'].' ('.dol_print_size($file['size'], 0, 0).')');
  1694. print dol_trunc($file['name'], $maxlength, 'middle');
  1695. print '</a>';
  1696. //print $this->getDocumentsLink($modulepart, $modulesubdir, $filedir, '^'.preg_quote($file['name'],'/').'$');
  1697. print $this->showPreview($file, $modulepart, $file['relativename']);
  1698. print "</td>\n";
  1699. // Size
  1700. $sizetoshow = dol_print_size($file['size'], 1, 1);
  1701. $sizetoshowbytes = dol_print_size($file['size'], 0, 1);
  1702. print '<td class="right nowraponall">';
  1703. if ($sizetoshow == $sizetoshowbytes) {
  1704. print $sizetoshow;
  1705. } else {
  1706. print $form->textwithpicto($sizetoshow, $sizetoshowbytes, -1);
  1707. }
  1708. print '</td>';
  1709. // Date
  1710. print '<td class="center">'.dol_print_date($file['date'], "dayhour").'</td>';
  1711. // Share link
  1712. print '<td class="right">';
  1713. if (!empty($file['share'])) {
  1714. // Define $urlwithroot
  1715. $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
  1716. $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  1717. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  1718. //print '<span class="opacitymedium">'.$langs->trans("Hash").' : '.$file['share'].'</span>';
  1719. $forcedownload = 0;
  1720. $paramlink = '';
  1721. if (!empty($file['share'])) {
  1722. $paramlink .= ($paramlink ? '&' : '').'hashp='.$file['share']; // Hash for public share
  1723. }
  1724. if ($forcedownload) {
  1725. $paramlink .= ($paramlink ? '&' : '').'attachment=1';
  1726. }
  1727. $fulllink = $urlwithroot.'/document.php'.($paramlink ? '?'.$paramlink : '');
  1728. print img_picto($langs->trans("FileSharedViaALink"), 'globe').' ';
  1729. print '<input type="text" class="quatrevingtpercent width100 nopadding nopadding small" id="downloadlink" name="downloadexternallink" value="'.dol_escape_htmltag($fulllink).'">';
  1730. }
  1731. //if (!empty($useinecm) && $useinecm != 6) print '<a data-ajax="false" href="'.DOL_URL_ROOT.'/document.php?modulepart='.$modulepart;
  1732. //if ($forcedownload) print '&attachment=1';
  1733. //print '&file='.urlencode($relativefile).'">';
  1734. //print img_view().'</a> &nbsp; ';
  1735. //if ($permissiontodelete) print '<a href="'.$url.'?id='.$object->id.'&section='.$_REQUEST["section"].'&action=delete&token='.newToken().'&urlfile='.urlencode($file['name']).'">'.img_delete().'</a>';
  1736. //else print '&nbsp;';
  1737. print "</td>";
  1738. print "</tr>\n";
  1739. }
  1740. }
  1741. if (count($filearray) == 0) {
  1742. print '<tr class="oddeven"><td colspan="5">';
  1743. if (empty($textifempty)) {
  1744. print '<span class="opacitymedium">'.$langs->trans("NoFileFound").'</span>';
  1745. } else {
  1746. print '<span class="opacitymedium">'.$textifempty.'</span>';
  1747. }
  1748. print '</td></tr>';
  1749. }
  1750. print "</table>";
  1751. print '</div>';
  1752. if (!empty($addfilterfields)) {
  1753. print '</form>';
  1754. }
  1755. // Fin de zone
  1756. }
  1757. /**
  1758. * Show array with linked files
  1759. *
  1760. * @param Object $object Object
  1761. * @param int $permissiontodelete Deletion is allowed
  1762. * @param string $action Action
  1763. * @param string $selected ???
  1764. * @param string $param More param to add into URL
  1765. * @return int Number of links
  1766. */
  1767. public function listOfLinks($object, $permissiontodelete = 1, $action = null, $selected = null, $param = '')
  1768. {
  1769. global $user, $conf, $langs, $user;
  1770. global $sortfield, $sortorder;
  1771. $langs->load("link");
  1772. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  1773. $link = new Link($this->db);
  1774. $links = array();
  1775. if ($sortfield == "name") {
  1776. $sortfield = "label";
  1777. } elseif ($sortfield == "date") {
  1778. $sortfield = "datea";
  1779. } else {
  1780. $sortfield = null;
  1781. }
  1782. $res = $link->fetchAll($links, $object->element, $object->id, $sortfield, $sortorder);
  1783. $param .= (isset($object->id) ? '&id='.$object->id : '');
  1784. print '<!-- listOfLinks -->'."\n";
  1785. // Show list of associated links
  1786. print load_fiche_titre($langs->trans("LinkedFiles"), '', 'link', 0, '', 'table-list-of-links');
  1787. print '<form action="'.$_SERVER['PHP_SELF'].($param ? '?'.$param : '').'" method="POST">';
  1788. print '<input type="hidden" name="token" value="'.newToken().'">';
  1789. print '<table class="liste noborder nobottom centpercent">';
  1790. print '<tr class="liste_titre">';
  1791. print_liste_field_titre(
  1792. $langs->trans("Links"),
  1793. $_SERVER['PHP_SELF'],
  1794. "name",
  1795. "",
  1796. $param,
  1797. '',
  1798. $sortfield,
  1799. $sortorder,
  1800. ''
  1801. );
  1802. print_liste_field_titre(
  1803. "",
  1804. "",
  1805. "",
  1806. "",
  1807. "",
  1808. '',
  1809. '',
  1810. '',
  1811. 'right '
  1812. );
  1813. print_liste_field_titre(
  1814. $langs->trans("Date"),
  1815. $_SERVER['PHP_SELF'],
  1816. "date",
  1817. "",
  1818. $param,
  1819. '',
  1820. $sortfield,
  1821. $sortorder,
  1822. 'center '
  1823. );
  1824. print_liste_field_titre(
  1825. '',
  1826. $_SERVER['PHP_SELF'],
  1827. "",
  1828. "",
  1829. $param,
  1830. '',
  1831. '',
  1832. '',
  1833. 'center '
  1834. );
  1835. print_liste_field_titre('', '', '');
  1836. print '</tr>';
  1837. $nboflinks = count($links);
  1838. if ($nboflinks > 0) {
  1839. include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
  1840. }
  1841. foreach ($links as $link) {
  1842. print '<tr class="oddeven">';
  1843. //edit mode
  1844. if ($action == 'update' && $selected === $link->id) {
  1845. print '<td>';
  1846. print '<input type="hidden" name="id" value="'.$object->id.'">';
  1847. print '<input type="hidden" name="linkid" value="'.$link->id.'">';
  1848. print '<input type="hidden" name="action" value="confirm_updateline">';
  1849. print $langs->trans('Link').': <input type="text" name="link" value="'.$link->url.'">';
  1850. print '</td>';
  1851. print '<td>';
  1852. print $langs->trans('Label').': <input type="text" name="label" value="'.dol_escape_htmltag($link->label).'">';
  1853. print '</td>';
  1854. print '<td class="center">'.dol_print_date(dol_now(), "dayhour", "tzuser").'</td>';
  1855. print '<td class="right"></td>';
  1856. print '<td class="right">';
  1857. print '<input type="submit" class="button button-save" name="save" value="'.dol_escape_htmltag($langs->trans("Save")).'">';
  1858. print '<input type="submit" class="button button-cancel" name="cancel" value="'.dol_escape_htmltag($langs->trans("Cancel")).'">';
  1859. print '</td>';
  1860. } else {
  1861. print '<td>';
  1862. print img_picto('', 'globe').' ';
  1863. print '<a data-ajax="false" href="'.$link->url.'" target="_blank" rel="noopener noreferrer">';
  1864. print dol_escape_htmltag($link->label);
  1865. print '</a>';
  1866. print '</td>'."\n";
  1867. print '<td class="right"></td>';
  1868. print '<td class="center">'.dol_print_date($link->datea, "dayhour", "tzuser").'</td>';
  1869. print '<td class="center"></td>';
  1870. print '<td class="right">';
  1871. print '<a href="'.$_SERVER['PHP_SELF'].'?action=update&linkid='.$link->id.$param.'&token='.newToken().'" class="editfilelink editfielda reposition" >'.img_edit().'</a>'; // id= is included into $param
  1872. if ($permissiontodelete) {
  1873. print ' &nbsp; <a class="deletefilelink reposition" href="'.$_SERVER['PHP_SELF'].'?action=deletelink&token='.newToken().'&linkid='.((int) $link->id).$param.'">'.img_delete().'</a>'; // id= is included into $param
  1874. } else {
  1875. print '&nbsp;';
  1876. }
  1877. print '</td>';
  1878. }
  1879. print "</tr>\n";
  1880. }
  1881. if ($nboflinks == 0) {
  1882. print '<tr class="oddeven"><td colspan="5">';
  1883. print '<span class="opacitymedium">'.$langs->trans("NoLinkFound").'</span>';
  1884. print '</td></tr>';
  1885. }
  1886. print "</table>";
  1887. print '</form>';
  1888. return $nboflinks;
  1889. }
  1890. /**
  1891. * Show detail icon with link for preview
  1892. *
  1893. * @param array $file Array with data of file. Example: array('name'=>...)
  1894. * @param string $modulepart propal, facture, facture_fourn, ...
  1895. * @param string $relativepath Relative path of docs
  1896. * @param integer $ruleforpicto Rule for picto: 0=Use the generic preview picto, 1=Use the picto of mime type of file). Use a negative value to show a generic picto even if preview not available.
  1897. * @param string $param More param on http links
  1898. * @return string $out Output string with HTML
  1899. */
  1900. public function showPreview($file, $modulepart, $relativepath, $ruleforpicto = 0, $param = '')
  1901. {
  1902. global $langs, $conf;
  1903. $out = '';
  1904. if ($conf->browser->layout != 'phone' && !empty($conf->use_javascript_ajax)) {
  1905. $urladvancedpreview = getAdvancedPreviewUrl($modulepart, $relativepath, 1, $param); // Return if a file is qualified for preview.
  1906. if (count($urladvancedpreview)) {
  1907. $out .= '<a class="pictopreview '.$urladvancedpreview['css'].'" href="'.$urladvancedpreview['url'].'"'.(empty($urladvancedpreview['mime']) ? '' : ' mime="'.$urladvancedpreview['mime'].'"').' '.(empty($urladvancedpreview['target']) ? '' : ' target="'.$urladvancedpreview['target'].'"').'>';
  1908. //$out.= '<a class="pictopreview">';
  1909. if (empty($ruleforpicto)) {
  1910. //$out.= img_picto($langs->trans('Preview').' '.$file['name'], 'detail');
  1911. $out .= '<span class="fa fa-search-plus pictofixedwidth" style="color: gray"></span>';
  1912. } else {
  1913. $out .= img_mime($relativepath, $langs->trans('Preview').' '.$file['name'], 'pictofixedwidth');
  1914. }
  1915. $out .= '</a>';
  1916. } else {
  1917. if ($ruleforpicto < 0) {
  1918. $out .= img_picto('', 'generic', '', false, 0, 0, '', 'paddingright pictofixedwidth');
  1919. }
  1920. }
  1921. }
  1922. return $out;
  1923. }
  1924. }