html.formfile.class.php 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  1. <?php
  2. /* Copyright (C) 2008-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2010-2014 Regis Houssin <regis.houssin@capnetworks.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 Ferran Marcet <fmarcet@2byte.es>
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. /**
  24. * \file htdocs/core/class/html.formfile.class.php
  25. * \ingroup core
  26. * \brief File of class to offer components to list and upload files
  27. */
  28. /**
  29. * Class to offer components to list and upload files
  30. */
  31. class FormFile
  32. {
  33. var $db;
  34. var $error;
  35. var $numoffiles;
  36. var $infofiles; // Used to return informations by function getDocumentsLink
  37. /**
  38. * Constructor
  39. *
  40. * @param DoliDB $db Database handler
  41. */
  42. function __construct($db)
  43. {
  44. $this->db = $db;
  45. $this->numoffiles=0;
  46. return 1;
  47. }
  48. /**
  49. * Show form to upload a new file
  50. *
  51. * @param string $url Url
  52. * @param string $title Title zone (Title or '' or 'none')
  53. * @param int $addcancel 1=Add 'Cancel' button
  54. * @param int $sectionid If upload must be done inside a particular ECM section
  55. * @param int $perm Value of permission to allow upload
  56. * @param int $size Length of input file area
  57. * @param Object $object Object to use (when attachment is done on an element)
  58. * @param string $options Add an option column
  59. * @param integer $useajax Use fileupload ajax (0=never, 1=if enabled, 2=always whatever is option). 2 should never be used.
  60. * @param string $savingdocmask Mask to use to define output filename. For example 'XXXXX-__YYYYMMDD__-__file__'
  61. * @param integer $linkfiles 1=Also add form to link files, 0=Do not show form to link files
  62. * @param string $htmlname Name and id of HTML form
  63. * @return int <0 if KO, >0 if OK
  64. */
  65. function form_attach_new_file($url, $title='', $addcancel=0, $sectionid=0, $perm=1, $size=50, $object='', $options='', $useajax=1, $savingdocmask='', $linkfiles=1, $htmlname='formuserfile')
  66. {
  67. global $conf,$langs, $hookmanager;
  68. $hookmanager->initHooks(array('formfile'));
  69. if (! empty($conf->browser->layout) && $conf->browser->layout != 'classic') $useajax=0;
  70. if ((! empty($conf->global->MAIN_USE_JQUERY_FILEUPLOAD) && $useajax) || ($useajax==2))
  71. {
  72. // TODO: Check this works with 2 forms on same page
  73. // TODO: Check this works with GED module, otherwise, force useajax to 0
  74. // TODO: This does not support option savingdocmask
  75. // TODO: This break feature to upload links too
  76. return $this->_formAjaxFileUpload($object);
  77. }
  78. else
  79. {
  80. $maxlength=$size;
  81. $out = "\n\n<!-- Start form attach new file -->\n";
  82. if (empty($title)) $title=$langs->trans("AttachANewFile");
  83. if ($title != 'none') $out.=load_fiche_titre($title, null, null);
  84. $out .= '<form name="'.$htmlname.'" id="'.$htmlname.'" action="'.$url.'" enctype="multipart/form-data" method="POST">';
  85. $out .= '<input type="hidden" id="'.$htmlname.'_section_dir" name="section_dir" value="">';
  86. $out .= '<input type="hidden" id="'.$htmlname.'_section_id" name="section_id" value="'.$sectionid.'">';
  87. $out .= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  88. $out .= '<table width="100%" class="nobordernopadding">';
  89. $out .= '<tr>';
  90. if (! empty($options)) $out .= '<td>'.$options.'</td>';
  91. $out .= '<td valign="middle" class="nowrap">';
  92. $max=$conf->global->MAIN_UPLOAD_DOC; // En Kb
  93. $maxphp=@ini_get('upload_max_filesize'); // En inconnu
  94. if (preg_match('/k$/i',$maxphp)) $maxphp=$maxphp*1;
  95. if (preg_match('/m$/i',$maxphp)) $maxphp=$maxphp*1024;
  96. if (preg_match('/g$/i',$maxphp)) $maxphp=$maxphp*1024*1024;
  97. if (preg_match('/t$/i',$maxphp)) $maxphp=$maxphp*1024*1024*1024;
  98. // Now $max and $maxphp are in Kb
  99. if ($maxphp > 0) $max=min($max,$maxphp);
  100. if ($max > 0)
  101. {
  102. $out .= '<input type="hidden" name="max_file_size" value="'.($max*1024).'">';
  103. }
  104. $out .= '<input class="flat" type="file" name="userfile" size="'.$maxlength.'"';
  105. $out .= (empty($conf->global->MAIN_UPLOAD_DOC) || empty($perm)?' disabled':'');
  106. $out .= '>';
  107. $out .= '&nbsp;';
  108. $out .= '<input type="submit" class="button" name="sendit" value="'.$langs->trans("Upload").'"';
  109. $out .= (empty($conf->global->MAIN_UPLOAD_DOC) || empty($perm)?' disabled':'');
  110. $out .= '>';
  111. if ($addcancel)
  112. {
  113. $out .= ' &nbsp; ';
  114. $out .= '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
  115. }
  116. if (! empty($conf->global->MAIN_UPLOAD_DOC))
  117. {
  118. if ($perm)
  119. {
  120. $langs->load('other');
  121. //$out .= ' ('.$langs->trans("MaxSize").': '.$max.' '.$langs->trans("Kb");
  122. $out .= ' ';
  123. $out.=info_admin($langs->trans("ThisLimitIsDefinedInSetup",$max,$maxphp),1);
  124. //$out .= ')';
  125. }
  126. }
  127. else
  128. {
  129. $out .= ' ('.$langs->trans("UploadDisabled").')';
  130. }
  131. $out .= "</td></tr>";
  132. if ($savingdocmask)
  133. {
  134. $out .= '<tr>';
  135. if (! empty($options)) $out .= '<td>'.$options.'</td>';
  136. $out .= '<td valign="middle" class="nowrap">';
  137. $out .= '<input type="checkbox" checked name="savingdocmask" value="'.dol_escape_js($savingdocmask).'"> '.$langs->trans("SaveUploadedFileWithMask", preg_replace('/__file__/',$langs->transnoentitiesnoconv("OriginFileName"),$savingdocmask), $langs->transnoentitiesnoconv("OriginFileName"));
  138. $out .= '</td>';
  139. $out .= '</tr>';
  140. }
  141. $out .= "</table>";
  142. $out .= '</form>';
  143. if (empty($sectionid)) $out .= '<br>';
  144. $out .= "\n<!-- End form attach new file -->\n";
  145. if ($linkfiles)
  146. {
  147. $out .= "\n<!-- Start form attach new link -->\n";
  148. $langs->load('link');
  149. $title = $langs->trans("LinkANewFile");
  150. $out .= load_fiche_titre($title, null, null);
  151. $out .= '<form name="'.$htmlname.'_link" id="'.$htmlname.'_link" action="'.$url.'" method="POST">';
  152. $out .= '<input type="hidden" id="'.$htmlname.'_link_section_dir" name="link_section_dir" value="">';
  153. $out .= '<input type="hidden" id="'.$htmlname.'_link_section_id" name="link_section_id" value="'.$sectionid.'">';
  154. $out .= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  155. $out .= '<div class="valignmiddle" >';
  156. $out .= '<div class="inline-block" style="padding-right: 10px;">';
  157. if (! empty($conf->global->OPTIMIZEFORTEXTBROWSER)) $out .= '<label for="link">'.$langs->trans("URLToLink") . ':</label> ';
  158. $out .= '<input type="text" name="link" size="'.$maxlength.'" id="link" placeholder="'.dol_escape_htmltag($langs->trans("URLToLink")).'">';
  159. $out .= '</div>';
  160. $out .= '<div class="inline-block" style="padding-right: 10px;">';
  161. if (! empty($conf->global->OPTIMIZEFORTEXTBROWSER)) $out .= '<label for="label">'.$langs->trans("Label") . ':</label> ';
  162. $out .= '<input type="text" name="label" id="label" placeholder="'.dol_escape_htmltag($langs->trans("Label")).'">';
  163. $out .= '<input type="hidden" name="objecttype" value="' . $object->element . '">';
  164. $out .= '<input type="hidden" name="objectid" value="' . $object->id . '">';
  165. $out .= '</div>';
  166. $out .= '<div class="inline-block" style="padding-right: 10px;">';
  167. $out .= '<input type="submit" class="button" name="linkit" value="'.$langs->trans("ToLink").'"';
  168. $out .= (empty($conf->global->MAIN_UPLOAD_DOC) || empty($perm)?' disabled':'');
  169. $out .= '>';
  170. $out .= '</div>';
  171. $out .= '</div>';
  172. $out .= '<div class="clearboth"></div>';
  173. $out .= '</form><br>';
  174. $parameters = array('socid'=>(isset($GLOBALS['socid'])?$GLOBALS['socid']:''),'id'=>(isset($GLOBALS['id'])?$GLOBALS['id']:''), 'url'=>$url, 'perm'=>$perm);
  175. $res = $hookmanager->executeHooks('formattachOptions',$parameters,$object);
  176. $out .= "\n<!-- End form attach new file -->\n";
  177. }
  178. if (empty($res))
  179. {
  180. print '<div class="attacharea">';
  181. print $out;
  182. print '</div>';
  183. }
  184. print $hookmanager->resPrint;
  185. return 1;
  186. }
  187. }
  188. /**
  189. * Show the box with list of available documents for object
  190. *
  191. * @param string $modulepart propal, facture, facture_fourn, ...
  192. * @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.
  193. * @param string $filedir Directory to scan
  194. * @param string $urlsource Url of origin page (for return)
  195. * @param int $genallowed Generation is allowed (1/0 or array of formats)
  196. * @param int $delallowed Remove is allowed (1/0)
  197. * @param string $modelselected Model to preselect by default
  198. * @param integer $allowgenifempty Show warning if no model activated
  199. * @param integer $forcenomultilang Do not show language option (even if MAIN_MULTILANGS defined)
  200. * @param int $iconPDF Show only PDF icon with link (1/0)
  201. * @param int $maxfilenamelength Max length for filename shown
  202. * @param integer $noform Do not output html form tags
  203. * @param string $param More param on http links
  204. * @param string $title Title to show on top of form
  205. * @param string $buttonlabel Label on submit button
  206. * @param string $codelang Default language code to use on lang combo box if multilang is enabled
  207. * @return int <0 if KO, number of shown files if OK
  208. * @deprecated Use print xxx->showdocuments() instead.
  209. */
  210. function show_documents($modulepart,$modulesubdir,$filedir,$urlsource,$genallowed,$delallowed=0,$modelselected='',$allowgenifempty=1,$forcenomultilang=0,$iconPDF=0,$maxfilenamelength=28,$noform=0,$param='',$title='',$buttonlabel='',$codelang='')
  211. {
  212. $this->numoffiles=0;
  213. print $this->showdocuments($modulepart,$modulesubdir,$filedir,$urlsource,$genallowed,$delallowed,$modelselected,$allowgenifempty,$forcenomultilang,$iconPDF,$maxfilenamelength,$noform,$param,$title,$buttonlabel,$codelang);
  214. return $this->numoffiles;
  215. }
  216. /**
  217. * Return a string to show the box with list of available documents for object.
  218. * This also set the property $this->numoffiles
  219. *
  220. * @param string $modulepart Module the files are related to ('propal', 'facture', 'facture_fourn', 'mymodule', 'mymodule_temp', ...)
  221. * @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 subdir of module.
  222. * @param string $filedir Directory to scan
  223. * @param string $urlsource Url of origin page (for return)
  224. * @param int $genallowed Generation is allowed (1/0 or array list of templates)
  225. * @param int $delallowed Remove is allowed (1/0)
  226. * @param string $modelselected Model to preselect by default
  227. * @param integer $allowgenifempty Allow generation even if list of template ($genallowed) is empty (show however a warning)
  228. * @param integer $forcenomultilang Do not show language option (even if MAIN_MULTILANGS defined)
  229. * @param int $iconPDF Deprecated, see getDocumentsLink
  230. * @param int $maxfilenamelength Max length for filename shown
  231. * @param integer $noform Do not output html form tags
  232. * @param string $param More param on http links
  233. * @param string $title Title to show on top of form
  234. * @param string $buttonlabel Label on submit button
  235. * @param string $codelang Default language code to use on lang combo box if multilang is enabled
  236. * @param string $morepicto Add more HTML content into cell with picto
  237. * @return string Output string with HTML array of documents (might be empty string)
  238. */
  239. function showdocuments($modulepart,$modulesubdir,$filedir,$urlsource,$genallowed,$delallowed=0,$modelselected='',$allowgenifempty=1,$forcenomultilang=0,$iconPDF=0,$maxfilenamelength=28,$noform=0,$param='',$title='',$buttonlabel='',$codelang='',$morepicto='')
  240. {
  241. // Deprecation warning
  242. if (0 !== $iconPDF) {
  243. dol_syslog(__METHOD__ . ": passing iconPDF parameter is deprecated", LOG_WARNING);
  244. }
  245. global $langs, $conf, $user, $hookmanager;
  246. global $form, $bc;
  247. if (! is_object($form)) $form=new Form($this->db);
  248. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  249. // For backward compatibility
  250. if (! empty($iconPDF)) {
  251. return $this->getDocumentsLink($modulepart, $modulesubdir, $filedir);
  252. }
  253. $printer=0;
  254. if (in_array($modulepart,array('facture','supplier_proposal','propal','proposal','order','commande','expedition', 'commande_fournisseur'))) // The direct print feature is implemented only for such elements
  255. {
  256. $printer = (!empty($user->rights->printing->read) && !empty($conf->printing->enabled))?true:false;
  257. }
  258. $hookmanager->initHooks(array('formfile'));
  259. $forname='builddoc';
  260. $out='';
  261. $var=true;
  262. $headershown=0;
  263. $showempty=0;
  264. $i=0;
  265. $titletoshow=$langs->trans("Documents");
  266. if (! empty($title)) $titletoshow=$title;
  267. $out.= "\n".'<!-- Start show_document -->'."\n";
  268. //print 'filedir='.$filedir;
  269. // Show table
  270. if ($genallowed)
  271. {
  272. $modellist=array();
  273. if ($modulepart == 'company')
  274. {
  275. $showempty=1;
  276. if (is_array($genallowed)) $modellist=$genallowed;
  277. else
  278. {
  279. include_once DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php';
  280. $modellist=ModeleThirdPartyDoc::liste_modeles($this->db);
  281. }
  282. }
  283. else if ($modulepart == 'propal')
  284. {
  285. if (is_array($genallowed)) $modellist=$genallowed;
  286. else
  287. {
  288. include_once DOL_DOCUMENT_ROOT.'/core/modules/propale/modules_propale.php';
  289. $modellist=ModelePDFPropales::liste_modeles($this->db);
  290. }
  291. }
  292. else if ($modulepart == 'supplier_proposal')
  293. {
  294. if (is_array($genallowed)) $modellist=$genallowed;
  295. else
  296. {
  297. include_once DOL_DOCUMENT_ROOT.'/core/modules/supplier_proposal/modules_supplier_proposal.php';
  298. $modellist=ModelePDFSupplierProposal::liste_modeles($this->db);
  299. }
  300. }
  301. else if ($modulepart == 'commande')
  302. {
  303. if (is_array($genallowed)) $modellist=$genallowed;
  304. else
  305. {
  306. include_once DOL_DOCUMENT_ROOT.'/core/modules/commande/modules_commande.php';
  307. $modellist=ModelePDFCommandes::liste_modeles($this->db);
  308. }
  309. }
  310. elseif ($modulepart == 'expedition')
  311. {
  312. if (is_array($genallowed)) $modellist=$genallowed;
  313. else
  314. {
  315. include_once DOL_DOCUMENT_ROOT.'/core/modules/expedition/modules_expedition.php';
  316. $modellist=ModelePDFExpedition::liste_modeles($this->db);
  317. }
  318. }
  319. elseif ($modulepart == 'livraison')
  320. {
  321. if (is_array($genallowed)) $modellist=$genallowed;
  322. else
  323. {
  324. include_once DOL_DOCUMENT_ROOT.'/core/modules/livraison/modules_livraison.php';
  325. $modellist=ModelePDFDeliveryOrder::liste_modeles($this->db);
  326. }
  327. }
  328. else if ($modulepart == 'ficheinter')
  329. {
  330. if (is_array($genallowed)) $modellist=$genallowed;
  331. else
  332. {
  333. include_once DOL_DOCUMENT_ROOT.'/core/modules/fichinter/modules_fichinter.php';
  334. $modellist=ModelePDFFicheinter::liste_modeles($this->db);
  335. }
  336. }
  337. elseif ($modulepart == 'facture')
  338. {
  339. if (is_array($genallowed)) $modellist=$genallowed;
  340. else
  341. {
  342. include_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
  343. $modellist=ModelePDFFactures::liste_modeles($this->db);
  344. }
  345. }
  346. elseif ($modulepart == 'contract')
  347. {
  348. if (is_array($genallowed)) $modellist=$genallowed;
  349. else
  350. {
  351. include_once DOL_DOCUMENT_ROOT.'/core/modules/contract/modules_contract.php';
  352. $modellist=ModelePDFContract::liste_modeles($this->db);
  353. }
  354. }
  355. elseif ($modulepart == 'project')
  356. {
  357. if (is_array($genallowed)) $modellist=$genallowed;
  358. else
  359. {
  360. include_once DOL_DOCUMENT_ROOT.'/core/modules/project/modules_project.php';
  361. $modellist=ModelePDFProjects::liste_modeles($this->db);
  362. }
  363. }
  364. elseif ($modulepart == 'project_task')
  365. {
  366. if (is_array($genallowed)) $modellist=$genallowed;
  367. else
  368. {
  369. include_once DOL_DOCUMENT_ROOT.'/core/modules/project/task/modules_task.php';
  370. $modellist=ModelePDFTask::liste_modeles($this->db);
  371. }
  372. }
  373. elseif ($modulepart == 'export')
  374. {
  375. if (is_array($genallowed)) $modellist=$genallowed;
  376. else
  377. {
  378. include_once DOL_DOCUMENT_ROOT.'/core/modules/export/modules_export.php';
  379. $modellist=ModeleExports::liste_modeles($this->db);
  380. }
  381. }
  382. else if ($modulepart == 'commande_fournisseur')
  383. {
  384. if (is_array($genallowed)) $modellist=$genallowed;
  385. else
  386. {
  387. include_once DOL_DOCUMENT_ROOT.'/core/modules/supplier_order/modules_commandefournisseur.php';
  388. $modellist=ModelePDFSuppliersOrders::liste_modeles($this->db);
  389. }
  390. }
  391. else if ($modulepart == 'facture_fournisseur')
  392. {
  393. if (is_array($genallowed)) $modellist=$genallowed;
  394. else
  395. {
  396. include_once DOL_DOCUMENT_ROOT.'/core/modules/supplier_invoice/modules_facturefournisseur.php';
  397. $modellist=ModelePDFSuppliersInvoices::liste_modeles($this->db);
  398. }
  399. }
  400. else if ($modulepart == 'remisecheque')
  401. {
  402. if (is_array($genallowed)) $modellist=$genallowed;
  403. else
  404. {
  405. include_once DOL_DOCUMENT_ROOT.'/core/modules/cheque/modules_chequereceipts.php';
  406. $modellist=ModeleChequeReceipts::liste_modeles($this->db);
  407. }
  408. }
  409. elseif ($modulepart == 'donation')
  410. {
  411. if (is_array($genallowed)) $modellist=$genallowed;
  412. else
  413. {
  414. include_once DOL_DOCUMENT_ROOT.'/core/modules/dons/modules_don.php';
  415. $modellist=ModeleDon::liste_modeles($this->db);
  416. }
  417. }
  418. elseif ($modulepart == 'agenda')
  419. {
  420. if (is_array($genallowed)) $modellist=$genallowed;
  421. else
  422. {
  423. include_once DOL_DOCUMENT_ROOT.'/core/modules/action/modules_action.php';
  424. $modellist=ModeleAction::liste_modeles($this->db);
  425. }
  426. }
  427. else if ($modulepart == 'unpaid')
  428. {
  429. $modellist='';
  430. }
  431. else //if ($modulepart != 'agenda')
  432. {
  433. // For normalized standard modules
  434. $file=dol_buildpath('/core/modules/'.$modulepart.'/modules_'.$modulepart.'.php',0);
  435. if (file_exists($file))
  436. {
  437. $res=include_once $file;
  438. }
  439. // For normalized external modules
  440. else
  441. {
  442. $file=dol_buildpath('/'.$modulepart.'/core/modules/'.$modulepart.'/modules_'.$modulepart.'.php',0);
  443. $res=include_once $file;
  444. }
  445. $class='Modele'.ucfirst($modulepart);
  446. if (class_exists($class))
  447. {
  448. $modellist=call_user_func($class.'::liste_modeles',$this->db);
  449. }
  450. else
  451. {
  452. dol_print_error($this->db,'Bad value for modulepart');
  453. return -1;
  454. }
  455. }
  456. $headershown=1;
  457. $buttonlabeltoshow=$buttonlabel;
  458. if (empty($buttonlabel)) $buttonlabel=$langs->trans('Generate');
  459. if (empty($noform)) $out.= '<form action="'.$urlsource.(empty($conf->global->MAIN_JUMP_TAG)?'':'#builddoc').'" name="'.$forname.'" id="'.$forname.'_form" method="post">';
  460. $out.= '<input type="hidden" name="action" value="builddoc">';
  461. $out.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  462. $out.= load_fiche_titre($titletoshow, '', '');
  463. $out.= '<table class="liste formdoc noborder" summary="listofdocumentstable" width="100%">';
  464. $out.= '<tr class="liste_titre">';
  465. $addcolumforpicto=($delallowed || $printer || $morepicto);
  466. $out.= '<th align="center" colspan="'.(3+($addcolumforpicto?'2':'1')).'" class="formdoc liste_titre maxwidthonsmartphone">';
  467. // Model
  468. if (! empty($modellist))
  469. {
  470. $out.= '<span class="hideonsmartphone">'.$langs->trans('Model').' </span>';
  471. if (is_array($modellist) && count($modellist) == 1) // If there is only one element
  472. {
  473. $arraykeys=array_keys($modellist);
  474. $modelselected=$arraykeys[0];
  475. }
  476. $out.= $form->selectarray('model', $modellist, $modelselected, $showempty, 0, 0, '', 0, 0, 0, '', 'minwidth100');
  477. $out.= ajax_combobox('model');
  478. }
  479. else
  480. {
  481. $out.= '<div class="float">'.$langs->trans("Files").'</div>';
  482. }
  483. // Language code (if multilang)
  484. if (($allowgenifempty || (is_array($modellist) && count($modellist) > 0)) && $conf->global->MAIN_MULTILANGS && ! $forcenomultilang && (! empty($modellist) || $showempty))
  485. {
  486. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
  487. $formadmin=new FormAdmin($this->db);
  488. $defaultlang=$codelang?$codelang:$langs->getDefaultLang();
  489. $morecss='maxwidth150';
  490. if (! empty($conf->browser->phone)) $morecss='maxwidth100';
  491. $out.= $formadmin->select_language($defaultlang, 'lang_id', 0, 0, 0, 0, 0, $morecss);
  492. }
  493. else
  494. {
  495. $out.= '&nbsp;';
  496. }
  497. // Button
  498. $genbutton = '<input class="button buttongen" id="'.$forname.'_generatebutton" name="'.$forname.'_generatebutton"';
  499. $genbutton.= ' type="submit" value="'.$buttonlabel.'"';
  500. if (! $allowgenifempty && ! is_array($modellist) && empty($modellist)) $genbutton.= ' disabled';
  501. $genbutton.= '>';
  502. if ($allowgenifempty && ! is_array($modellist) && empty($modellist) && empty($conf->dol_no_mouse_hover) && $modulepart != 'unpaid')
  503. {
  504. $langs->load("errors");
  505. $genbutton.= ' '.img_warning($langs->transnoentitiesnoconv("WarningNoDocumentModelActivated"));
  506. }
  507. if (! $allowgenifempty && ! is_array($modellist) && empty($modellist) && empty($conf->dol_no_mouse_hover) && $modulepart != 'unpaid') $genbutton='';
  508. if (empty($modellist) && ! $showempty && $modulepart != 'unpaid') $genbutton='';
  509. $out.= $genbutton;
  510. $out.= '</th>';
  511. if (!empty($hookmanager->hooks['formfile']))
  512. {
  513. foreach($hookmanager->hooks['formfile'] as $module)
  514. {
  515. if (method_exists($module, 'formBuilddocLineOptions')) $out .= '<th></th>';
  516. }
  517. }
  518. $out.= '</tr>';
  519. // Execute hooks
  520. $parameters=array('socid'=>(isset($GLOBALS['socid'])?$GLOBALS['socid']:''),'id'=>(isset($GLOBALS['id'])?$GLOBALS['id']:''),'modulepart'=>$modulepart);
  521. if (is_object($hookmanager))
  522. {
  523. $reshook = $hookmanager->executeHooks('formBuilddocOptions',$parameters,$GLOBALS['object']);
  524. $out.= $hookmanager->resPrint;
  525. }
  526. }
  527. // Get list of files
  528. if (! empty($filedir))
  529. {
  530. $file_list=dol_dir_list($filedir,'files',0,'','(\.meta|_preview\.png)$','date',SORT_DESC);
  531. // Show title of array if not already shown
  532. if ((! empty($file_list) || preg_match('/^massfilesarea/', $modulepart)) && ! $headershown)
  533. {
  534. $headershown=1;
  535. $out.= '<div class="titre">'.$titletoshow.'</div>';
  536. $out.= '<table class="border" summary="listofdocumentstable" width="100%">';
  537. }
  538. // Loop on each file found
  539. if (is_array($file_list))
  540. {
  541. foreach($file_list as $file)
  542. {
  543. $var=!$var;
  544. // Define relative path for download link (depends on module)
  545. $relativepath=$file["name"]; // Cas general
  546. if ($modulesubdir) $relativepath=$modulesubdir."/".$file["name"]; // Cas propal, facture...
  547. if ($modulepart == 'export') $relativepath = $file["name"]; // Other case
  548. $out.= "<tr ".$bc[$var].">";
  549. $documenturl = DOL_URL_ROOT.'/document.php';
  550. if (isset($conf->global->DOL_URL_ROOT_DOCUMENT_PHP)) $documenturl=$conf->global->DOL_URL_ROOT_DOCUMENT_PHP;
  551. // Show file name with link to download
  552. $out.= '<td class="nowrap">';
  553. $out.= '<a data-ajax="false" href="'.$documenturl.'?modulepart='.$modulepart.'&amp;file='.urlencode($relativepath).'"';
  554. $mime=dol_mimetype($relativepath,'',0);
  555. if (preg_match('/text/',$mime)) $out.= ' target="_blank"';
  556. $out.= ' target="_blank">';
  557. $out.= img_mime($file["name"],$langs->trans("File").': '.$file["name"]).' '.dol_trunc($file["name"],$maxfilenamelength);
  558. $out.= '</a>'."\n";
  559. $out.= '</td>';
  560. // Show file size
  561. $size=(! empty($file['size'])?$file['size']:dol_filesize($filedir."/".$file["name"]));
  562. $out.= '<td align="right" class="nowrap">'.dol_print_size($size).'</td>';
  563. // Show file date
  564. $date=(! empty($file['date'])?$file['date']:dol_filemtime($filedir."/".$file["name"]));
  565. $out.= '<td align="right" class="nowrap">'.dol_print_date($date, 'dayhour', 'tzuser').'</td>';
  566. if ($delallowed || $printer || $morepicto)
  567. {
  568. $out.= '<td align="right">';
  569. if ($delallowed)
  570. {
  571. $out.= '<a href="'.$urlsource.(strpos($urlsource,'?')?'&amp;':'?').'action=remove_file&amp;file='.urlencode($relativepath);
  572. $out.= ($param?'&amp;'.$param:'');
  573. //$out.= '&modulepart='.$modulepart; // TODO obsolete ?
  574. //$out.= '&urlsource='.urlencode($urlsource); // TODO obsolete ?
  575. $out.= '">'.img_picto($langs->trans("Delete"), 'delete.png').'</a>';
  576. //$out.='</td>';
  577. }
  578. if ($printer)
  579. {
  580. //$out.= '<td align="right">';
  581. $out.= '&nbsp;<a href="'.$urlsource.(strpos($urlsource,'?')?'&amp;':'?').'action=print_file&amp;printer='.$modulepart.'&amp;file='.urlencode($relativepath);
  582. $out.= ($param?'&amp;'.$param:'');
  583. $out.= '">'.img_picto($langs->trans("PrintFile", $relativepath),'printer.png').'</a>';
  584. }
  585. if ($morepicto)
  586. {
  587. $morepicto=preg_replace('/__FILENAMEURLENCODED__/',urlencode($relativepath),$morepicto);
  588. $out.=$morepicto;
  589. }
  590. $out.='</td>';
  591. }
  592. if (is_object($hookmanager))
  593. {
  594. $parameters=array('socid'=>(isset($GLOBALS['socid'])?$GLOBALS['socid']:''),'id'=>(isset($GLOBALS['id'])?$GLOBALS['id']:''),'modulepart'=>$modulepart,'relativepath'=>$relativepath);
  595. $res = $hookmanager->executeHooks('formBuilddocLineOptions',$parameters,$file);
  596. if (empty($res))
  597. {
  598. $out .= $hookmanager->resPrint; // Complete line
  599. $out.= '</tr>';
  600. }
  601. else $out = $hookmanager->resPrint; // Replace line
  602. }
  603. }
  604. if (count($file_list) == 0 && $headershown)
  605. {
  606. $out.='<tr '.$bc[0].'><td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  607. }
  608. $this->numoffiles++;
  609. }
  610. }
  611. if ($headershown)
  612. {
  613. // Affiche pied du tableau
  614. $out.= "</table>\n";
  615. if ($genallowed)
  616. {
  617. if (empty($noform)) $out.= '</form>'."\n";
  618. }
  619. }
  620. $out.= '<!-- End show_document -->'."\n";
  621. //return ($i?$i:$headershown);
  622. return $out;
  623. }
  624. /**
  625. * Show only Document icon with link
  626. *
  627. * @param string $modulepart propal, facture, facture_fourn, ...
  628. * @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.
  629. * @param string $filedir Full path to directory to scan
  630. * @param string $filter Filter filenames on this regex string (Example: '\.pdf$')
  631. * @return string Output string with HTML link of documents (might be empty string). This also fill the array ->infofiles
  632. */
  633. function getDocumentsLink($modulepart, $modulesubdir, $filedir, $filter='')
  634. {
  635. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  636. $out='';
  637. $this->infofiles=array('nboffiles'=>0,'extensions'=>array(),'files'=>array());
  638. $file_list=dol_dir_list($filedir, 'files', 0, preg_quote(basename($modulesubdir),'/').'[^\-]+', '\.meta$|\.png$'); // Get list of files starting with name fo ref (but not followed by "-" to discard uploaded files)
  639. // For ajax treatment
  640. $out.= '<div id="gen_pdf_'.$modulesubdir.'" class="linkobject hideobject">'.img_picto('', 'refresh').'</div>'."\n";
  641. if (! empty($file_list))
  642. {
  643. // Loop on each file found
  644. foreach($file_list as $file)
  645. {
  646. if ($filter && ! preg_match('/'.$filter.'/i', $file["name"])) continue; // Discard this. It does not match provided filter.
  647. // Define relative path for download link (depends on module)
  648. $relativepath=$file["name"]; // Cas general
  649. if ($modulesubdir) $relativepath=$modulesubdir."/".$file["name"]; // Cas propal, facture...
  650. // Autre cas
  651. if ($modulepart == 'donation') {
  652. $relativepath = get_exdir($modulesubdir,2,0,0,null,'donation').$file["name"];
  653. }
  654. if ($modulepart == 'export') {
  655. $relativepath = $file["name"];
  656. }
  657. // Show file name with link to download
  658. $out.= '<a data-ajax="false" href="'.DOL_URL_ROOT . '/document.php?modulepart='.$modulepart.'&amp;file='.urlencode($relativepath).'"';
  659. $mime=dol_mimetype($relativepath,'',0);
  660. if (preg_match('/text/',$mime)) $out.= ' target="_blank"';
  661. $out.= '>';
  662. $out.= img_mime($relativepath, $file["name"]);
  663. $out.= '</a>'."\n";
  664. $this->infofiles['nboffiles']++;
  665. $this->infofiles['files'][]=$file['fullname'];
  666. $ext=pathinfo($file["name"], PATHINFO_EXTENSION);
  667. if (empty($this->infofiles[$ext])) $this->infofiles['extensions'][$ext]=1;
  668. else $this->infofiles['extensions'][$ext]++;
  669. }
  670. }
  671. return $out;
  672. }
  673. /**
  674. * Show list of documents in a directory
  675. *
  676. * @param array $filearray Array of files loaded by dol_dir_list('files') function before calling this
  677. * @param Object $object Object on which document is linked to
  678. * @param string $modulepart Value for modulepart used by download or viewimage wrapper
  679. * @param string $param Parameters on sort links (param must start with &, example &aaa=bbb&ccc=ddd)
  680. * @param int $forcedownload Force to open dialog box "Save As" when clicking on file
  681. * @param string $relativepath Relative path of docs (autodefined if not provided)
  682. * @param int $permtodelete Permission to delete
  683. * @param int $useinecm Change output for use in ecm module
  684. * @param string $textifempty Text to show if filearray is empty ('NoFileFound' if not defined)
  685. * @param int $maxlength Maximum length of file name shown
  686. * @param string $title Title before list
  687. * @param string $url Full url to use for click links ('' = autodetect)
  688. * @param int $showrelpart 0=Show only filename (default), 1=Show first level 1 dir
  689. * @return int <0 if KO, nb of files shown if OK
  690. */
  691. function list_of_documents($filearray,$object,$modulepart,$param='',$forcedownload=0,$relativepath='',$permtodelete=1,$useinecm=0,$textifempty='',$maxlength=0,$title='',$url='', $showrelpart=0)
  692. {
  693. global $user, $conf, $langs, $hookmanager;
  694. global $bc;
  695. global $sortfield, $sortorder, $maxheightmini;
  696. $hookmanager->initHooks(array('formfile'));
  697. $parameters=array(
  698. 'filearray' => $filearray,
  699. 'modulepart'=> $modulepart,
  700. 'param' => $param,
  701. 'forcedownload' => $forcedownload,
  702. 'relativepath' => $relativepath,
  703. 'permtodelete' => $permtodelete,
  704. 'useinecm' => $useinecm,
  705. 'textifempty' => $textifempty,
  706. 'maxlength' => $maxlength,
  707. 'title' => $title,
  708. 'url' => $url
  709. );
  710. $reshook=$hookmanager->executeHooks('showFilesList', $parameters, $object);
  711. if (isset($reshook) && $reshook != '') // null or '' for bypass
  712. {
  713. return $reshook;
  714. }
  715. else
  716. {
  717. $param = (isset($object->id)?'&id='.$object->id:'').$param;
  718. // Show list of existing files
  719. if (empty($useinecm)) print load_fiche_titre($title?$title:$langs->trans("AttachedFiles"));
  720. if (empty($url)) $url=$_SERVER["PHP_SELF"];
  721. print '<!-- html.formfile::list_of_documents -->'."\n";
  722. print '<table width="100%" class="'.($useinecm?'nobordernopadding':'liste').'">';
  723. print '<tr class="liste_titre">';
  724. print_liste_field_titre($langs->trans("Documents2"),$url,"name","",$param,'align="left"',$sortfield,$sortorder);
  725. print_liste_field_titre($langs->trans("Size"),$url,"size","",$param,'align="right"',$sortfield,$sortorder);
  726. print_liste_field_titre($langs->trans("Date"),$url,"date","",$param,'align="center"',$sortfield,$sortorder);
  727. if (empty($useinecm)) print_liste_field_titre('',$url,"","",$param,'align="center"');
  728. print_liste_field_titre('');
  729. print "</tr>\n";
  730. $nboffiles=count($filearray);
  731. if ($nboffiles > 0) include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
  732. $var=true;
  733. foreach($filearray as $key => $file) // filearray must be only files here
  734. {
  735. if ($file['name'] != '.'
  736. && $file['name'] != '..'
  737. && ! preg_match('/\.meta$/i',$file['name']))
  738. {
  739. // Define relative path used to store the file
  740. if (empty($relativepath))
  741. {
  742. $relativepath=(! empty($object->ref)?dol_sanitizeFileName($object->ref):'').'/';
  743. if ($object->element == 'invoice_supplier') $relativepath=get_exdir($object->id,2,0,0,$object,'invoice_supplier').$relativepath; // TODO Call using a defined value for $relativepath
  744. if ($object->element == 'member') $relativepath=get_exdir($object->id,2,0,0,$object,'member').$relativepath; // TODO Call using a defined value for $relativepath
  745. if ($object->element == 'project_task') $relativepath='Call_not_supported_._Call_function_using_a_defined_relative_path_.';
  746. }
  747. // For backward compatiblity, we detect file is stored into an old path
  748. if (! empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO) && $file['level1name'] == 'photos')
  749. {
  750. $relativepath=preg_replace('/^.*\/produit\//','',$file['path']).'/';
  751. }
  752. $var=!$var;
  753. print '<tr '.$bc[$var].'>';
  754. print '<td class="tdoverflow">';
  755. //print "XX".$file['name']; //$file['name'] must be utf8
  756. print '<a data-ajax="false" href="'.DOL_URL_ROOT.'/document.php?modulepart='.$modulepart;
  757. if ($forcedownload) print '&attachment=1';
  758. if (! empty($object->entity)) print '&entity='.$object->entity;
  759. $filepath=$relativepath.$file['name'];
  760. /* Restore old code: When file is at level 2+, full relative path (and not only level1) must be into url
  761. if ($file['level1name'] <> $object->id)
  762. $filepath=$object->id.'/'.$file['level1name'].'/'.$file['name'];
  763. else
  764. $filepath=$object->id.'/'.$file['name'];
  765. */
  766. print '&file='.urlencode($filepath);
  767. print '">';
  768. print img_mime($file['name'],$file['name'].' ('.dol_print_size($file['size'],0,0).')').' ';
  769. if ($showrelpart == 1) print $relativepath;
  770. //print dol_trunc($file['name'],$maxlength,'middle');
  771. print $file['name'];
  772. print '</a>';
  773. print "</td>\n";
  774. print '<td align="right" width="80px">'.dol_print_size($file['size'],1,1).'</td>';
  775. print '<td align="center" width="130px">'.dol_print_date($file['date'],"dayhour","tzuser").'</td>';
  776. // Preview
  777. if (empty($useinecm))
  778. {
  779. $fileinfo = pathinfo($file['name']);
  780. print '<td align="center">';
  781. if (image_format_supported($file['name']) > 0)
  782. {
  783. $minifile=getImageFileNameForSize($file['name'], '_mini', '.png'); // Thumbs are created with filename in lower case and with .png extension
  784. //print $relativepath.'<br>';
  785. //print $file['path'].'/'.$minifile.'<br>';
  786. if (! dol_is_file($file['path'].'/'.$minifile)) $minifile=getImageFileNameForSize($file['name'], '_mini', '.'.$fileinfo['extension']); // For old thumbs
  787. //print $file['path'].'/'.$minifile.'<br>';
  788. print '<a href="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$modulepart.'&file='.urlencode($relativepath.$fileinfo['filename'].'.'.strtolower($fileinfo['extension'])).'" class="aphoto" target="_blank">';
  789. print '<img border="0" height="'.$maxheightmini.'" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$modulepart.'&file='.urlencode($relativepath.$minifile).'" title="">';
  790. print '</a>';
  791. }
  792. else print '&nbsp;';
  793. print '</td>';
  794. }
  795. // Delete or view link
  796. // ($param must start with &)
  797. print '<td class="valignmiddle right" width="50px">';
  798. if ($useinecm) print '<a href="'.DOL_URL_ROOT.'/ecm/docfile.php?urlfile='.urlencode($file['name']).$param.'" class="editfilelink" rel="'.urlencode($file['name']).'">'.img_view().'</a> &nbsp; ';
  799. else
  800. {
  801. if (image_format_supported($file['name']) > 0)
  802. {
  803. $permtoedit=0;
  804. $newmodulepart=$modulepart;
  805. if ($modulepart == 'product' || $modulepart == 'produit' || $modulepart == 'service')
  806. {
  807. if ($user->rights->produit->creer && $object->type == Product::TYPE_PRODUCT) $permtoedit=1;
  808. if ($user->rights->service->creer && $object->type == Product::TYPE_SERVICE) $permtoedit=1;
  809. $newmodulepart='produit|service';
  810. }
  811. /* TODO Not yet working
  812. if ($modulepart == 'holiday')
  813. {
  814. if ($user->rights->holiday->write_all) $permtoedit=1;
  815. }
  816. */
  817. if (empty($conf->global->MAIN_UPLOAD_DOC)) $permtoedit=0;
  818. if ($permtoedit)
  819. {
  820. // Link to resize
  821. print '<a href="'.DOL_URL_ROOT.'/core/photos_resize.php?modulepart='.urlencode($newmodulepart).'&id='.$object->id.'&file='.urlencode($relativepath.$fileinfo['filename'].'.'.strtolower($fileinfo['extension'])).'" title="'.dol_escape_htmltag($langs->trans("Resize")).'">'.img_picto($langs->trans("Resize"),DOL_URL_ROOT.'/theme/common/transform-crop-and-resize','',1).'</a> &nbsp; ';
  822. }
  823. }
  824. }
  825. if ($permtodelete)
  826. {
  827. /*
  828. if ($file['level1name'] <> $object->id)
  829. $filepath=$file['level1name'].'/'.$file['name'];
  830. else
  831. $filepath=$file['name'];
  832. */
  833. $useajax=1;
  834. if (! empty($conf->dol_use_jmobile)) $useajax=0;
  835. if (empty($conf->use_javascript_ajax)) $useajax=0;
  836. if (! empty($conf->global->MAIN_ECM_DISABLE_JS)) $useajax=0;
  837. print '<a href="'.(($useinecm && $useajax)?'#':$url.'?action=delete&urlfile='.urlencode($filepath).$param).'" class="deletefilelink" rel="'.$filepath.'">'.img_delete().'</a>';
  838. }
  839. else print '&nbsp;';
  840. print "</td>";
  841. print "</tr>\n";
  842. }
  843. }
  844. if ($nboffiles == 0)
  845. {
  846. print '<tr '.$bc[false].'><td colspan="'.(empty($useinecm)?'5':'4').'" class="opacitymedium">';
  847. if (empty($textifempty)) print $langs->trans("NoFileFound");
  848. else print $textifempty;
  849. print '</td></tr>';
  850. }
  851. print "</table>";
  852. return $nboffiles;
  853. }
  854. }
  855. /**
  856. * Show list of documents in a directory
  857. *
  858. * @param string $upload_dir Directory that was scanned
  859. * @param array $filearray Array of files loaded by dol_dir_list function before calling this function
  860. * @param string $modulepart Value for modulepart used by download wrapper
  861. * @param string $param Parameters on sort links
  862. * @param int $forcedownload Force to open dialog box "Save As" when clicking on file
  863. * @param string $relativepath Relative path of docs (autodefined if not provided)
  864. * @param int $permtodelete Permission to delete
  865. * @param int $useinecm Change output for use in ecm module
  866. * @param int $textifempty Text to show if filearray is empty
  867. * @param int $maxlength Maximum length of file name shown
  868. * @param string $url Full url to use for click links ('' = autodetect)
  869. * @return int <0 if KO, nb of files shown if OK
  870. */
  871. function list_of_autoecmfiles($upload_dir,$filearray,$modulepart,$param,$forcedownload=0,$relativepath='',$permtodelete=1,$useinecm=0,$textifempty='',$maxlength=0,$url='')
  872. {
  873. global $user, $conf, $langs;
  874. global $bc;
  875. global $sortfield, $sortorder;
  876. dol_syslog(get_class($this).'::list_of_autoecmfiles upload_dir='.$upload_dir.' modulepart='.$modulepart);
  877. // Show list of documents
  878. if (empty($useinecm)) print load_fiche_titre($langs->trans("AttachedFiles"));
  879. if (empty($url)) $url=$_SERVER["PHP_SELF"];
  880. print '<table width="100%" class="nobordernopadding">';
  881. print '<tr class="liste_titre">';
  882. $sortref="fullname";
  883. if ($modulepart == 'invoice_supplier') $sortref='level1name';
  884. print_liste_field_titre($langs->trans("Ref"),$url,$sortref,"",$param,'align="left"',$sortfield,$sortorder);
  885. print_liste_field_titre($langs->trans("Documents2"),$url,"name","",$param,'align="left"',$sortfield,$sortorder);
  886. print_liste_field_titre($langs->trans("Size"),$url,"size","",$param,'align="right"',$sortfield,$sortorder);
  887. print_liste_field_titre($langs->trans("Date"),$url,"date","",$param,'align="center"',$sortfield,$sortorder);
  888. print_liste_field_titre('','','');
  889. print '</tr>';
  890. // To show ref or specific information according to view to show (defined by $module)
  891. if ($modulepart == 'company')
  892. {
  893. include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  894. $object_instance=new Societe($this->db);
  895. }
  896. else if ($modulepart == 'invoice')
  897. {
  898. include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  899. $object_instance=new Facture($this->db);
  900. }
  901. else if ($modulepart == 'invoice_supplier')
  902. {
  903. include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  904. $object_instance=new FactureFournisseur($this->db);
  905. }
  906. else if ($modulepart == 'propal')
  907. {
  908. include_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
  909. $object_instance=new Propal($this->db);
  910. }
  911. else if ($modulepart == 'supplier_proposal')
  912. {
  913. include_once DOL_DOCUMENT_ROOT.'/supplier_proposal/class/supplier_proposal.class.php';
  914. $object_instance=new SupplierProposal($this->db);
  915. }
  916. else if ($modulepart == 'order')
  917. {
  918. include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  919. $object_instance=new Commande($this->db);
  920. }
  921. else if ($modulepart == 'order_supplier')
  922. {
  923. include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
  924. $object_instance=new CommandeFournisseur($this->db);
  925. }
  926. else if ($modulepart == 'contract')
  927. {
  928. include_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
  929. $object_instance=new Contrat($this->db);
  930. }
  931. else if ($modulepart == 'product')
  932. {
  933. include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  934. $object_instance=new Product($this->db);
  935. }
  936. else if ($modulepart == 'tax')
  937. {
  938. include_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
  939. $object_instance=new ChargeSociales($this->db);
  940. }
  941. else if ($modulepart == 'project')
  942. {
  943. include_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  944. $object_instance=new Project($this->db);
  945. }
  946. else if ($modulepart == 'fichinter')
  947. {
  948. include_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
  949. $object_instance=new Fichinter($this->db);
  950. }
  951. else if ($modulepart == 'user')
  952. {
  953. include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  954. $object_instance=new User($this->db);
  955. }
  956. $var=true;
  957. foreach($filearray as $key => $file)
  958. {
  959. if (!is_dir($file['name'])
  960. && $file['name'] != '.'
  961. && $file['name'] != '..'
  962. && $file['name'] != 'CVS'
  963. && ! preg_match('/\.meta$/i',$file['name']))
  964. {
  965. // Define relative path used to store the file
  966. $relativefile=preg_replace('/'.preg_quote($upload_dir.'/','/').'/','',$file['fullname']);
  967. //var_dump($file);
  968. $id=0; $ref=''; $label='';
  969. // To show ref or specific information according to view to show (defined by $module)
  970. if ($modulepart == 'company') { preg_match('/(\d+)\/[^\/]+$/',$relativefile,$reg); $id=(isset($reg[1])?$reg[1]:''); }
  971. if ($modulepart == 'invoice') { preg_match('/(.*)\/[^\/]+$/',$relativefile,$reg); $ref=(isset($reg[1])?$reg[1]:''); }
  972. if ($modulepart == 'invoice_supplier') { preg_match('/([^\/]+)\/[^\/]+$/',$relativefile,$reg); $ref=(isset($reg[1])?$reg[1]:''); if (is_numeric($ref)) { $id=$ref; $ref=''; } } // $ref may be also id with old supplier invoices
  973. if ($modulepart == 'propal') { preg_match('/(.*)\/[^\/]+$/',$relativefile,$reg); $ref=(isset($reg[1])?$reg[1]:''); }
  974. if ($modulepart == 'supplier_proposal') { preg_match('/(.*)\/[^\/]+$/',$relativefile,$reg); $ref=(isset($reg[1])?$reg[1]:''); }
  975. if ($modulepart == 'order') { preg_match('/(.*)\/[^\/]+$/',$relativefile,$reg); $ref=(isset($reg[1])?$reg[1]:''); }
  976. if ($modulepart == 'order_supplier') { preg_match('/(.*)\/[^\/]+$/',$relativefile,$reg); $ref=(isset($reg[1])?$reg[1]:''); }
  977. if ($modulepart == 'contract') { preg_match('/(.*)\/[^\/]+$/',$relativefile,$reg); $ref=(isset($reg[1])?$reg[1]:''); }
  978. if ($modulepart == 'product') { preg_match('/(.*)\/[^\/]+$/',$relativefile,$reg); $ref=(isset($reg[1])?$reg[1]:''); }
  979. if ($modulepart == 'tax') { preg_match('/(\d+)\/[^\/]+$/',$relativefile,$reg); $id=(isset($reg[1])?$reg[1]:''); }
  980. if ($modulepart == 'project') { preg_match('/(.*)\/[^\/]+$/',$relativefile,$reg); $ref=(isset($reg[1])?$reg[1]:'');}
  981. if ($modulepart == 'fichinter') { preg_match('/(.*)\/[^\/]+$/',$relativefile,$reg); $ref=(isset($reg[1])?$reg[1]:'');}
  982. if ($modulepart == 'user') { preg_match('/(.*)\/[^\/]+$/',$relativefile,$reg); $id=(isset($reg[1])?$reg[1]:'');}
  983. if (! $id && ! $ref) continue;
  984. $found=0;
  985. if (! empty($this->cache_objects[$modulepart.'_'.$id.'_'.$ref]))
  986. {
  987. $found=1;
  988. }
  989. else
  990. {
  991. //print 'Fetch '.$id." - ".$ref.'<br>';
  992. if ($id) {
  993. $result = $object_instance->fetch($id);
  994. } else {
  995. //fetchOneLike looks for objects with wildcards in its reference.
  996. //It is useful for those masks who get underscores instead of their actual symbols
  997. //fetchOneLike requires some info in the object. If it doesn't have it, then 0 is returned
  998. //that's why we look only look fetchOneLike when fetch returns 0
  999. if (!$result = $object_instance->fetch('', $ref)) {
  1000. $result = $object_instance->fetchOneLike($ref);
  1001. }
  1002. }
  1003. if ($result > 0) { // Save object into a cache
  1004. $found=1; $this->cache_objects[$modulepart.'_'.$id.'_'.$ref] = clone $object_instance;
  1005. }
  1006. if ($result == 0) { $found=1; $this->cache_objects[$modulepart.'_'.$id.'_'.$ref]='notfound'; unset($filearray[$key]); }
  1007. }
  1008. if (! $found > 0 || ! is_object($this->cache_objects[$modulepart.'_'.$id.'_'.$ref])) continue; // We do not show orphelins files
  1009. $var=!$var;
  1010. print '<tr '.$bc[$var].'>';
  1011. print '<td>';
  1012. if ($found > 0 && is_object($this->cache_objects[$modulepart.'_'.$id.'_'.$ref])) print $this->cache_objects[$modulepart.'_'.$id.'_'.$ref]->getNomUrl(1,'document');
  1013. else print $langs->trans("ObjectDeleted",($id?$id:$ref));
  1014. print '</td>';
  1015. print '<td>';
  1016. //print "XX".$file['name']; //$file['name'] must be utf8
  1017. print '<a data-ajax="false" href="'.DOL_URL_ROOT.'/document.php?modulepart='.$modulepart;
  1018. if ($forcedownload) print '&attachment=1';
  1019. print '&file='.urlencode($relativefile).'">';
  1020. print img_mime($file['name'],$file['name'].' ('.dol_print_size($file['size'],0,0).')').' ';
  1021. print dol_trunc($file['name'],$maxlength,'middle');
  1022. print '</a>';
  1023. print "</td>\n";
  1024. print '<td align="right">'.dol_print_size($file['size'],1,1).'</td>';
  1025. print '<td align="center">'.dol_print_date($file['date'],"dayhour").'</td>';
  1026. print '<td align="right">';
  1027. if (! empty($useinecm)) print '<a data-ajax="false" href="'.DOL_URL_ROOT.'/document.php?modulepart='.$modulepart;
  1028. if ($forcedownload) print '&attachment=1';
  1029. print '&file='.urlencode($relativefile).'">';
  1030. print img_view().'</a> &nbsp; ';
  1031. //if ($permtodelete) print '<a href="'.$url.'?id='.$object->id.'&section='.$_REQUEST["section"].'&action=delete&urlfile='.urlencode($file['name']).'">'.img_delete().'</a>';
  1032. //else print '&nbsp;';
  1033. print "</td></tr>\n";
  1034. }
  1035. }
  1036. if (count($filearray) == 0)
  1037. {
  1038. print '<tr '.$bc[false].'><td colspan="4">';
  1039. if (empty($textifempty)) print $langs->trans("NoFileFound");
  1040. else print $textifempty;
  1041. print '</td></tr>';
  1042. }
  1043. print "</table>";
  1044. // Fin de zone
  1045. }
  1046. /**
  1047. * Show form to upload a new file with jquery fileupload.
  1048. * This form use the fileupload.php file.
  1049. *
  1050. * @param Object $object Object to use
  1051. * @return void
  1052. */
  1053. private function _formAjaxFileUpload($object)
  1054. {
  1055. global $langs;
  1056. // PHP post_max_size
  1057. $post_max_size = ini_get('post_max_size');
  1058. $mul_post_max_size = substr($post_max_size, -1);
  1059. $mul_post_max_size = ($mul_post_max_size == 'M' ? 1048576 : ($mul_post_max_size == 'K' ? 1024 : ($mul_post_max_size == 'G' ? 1073741824 : 1)));
  1060. $post_max_size = $mul_post_max_size * (int) $post_max_size;
  1061. // PHP upload_max_filesize
  1062. $upload_max_filesize = ini_get('upload_max_filesize');
  1063. $mul_upload_max_filesize = substr($upload_max_filesize, -1);
  1064. $mul_upload_max_filesize = ($mul_upload_max_filesize == 'M' ? 1048576 : ($mul_upload_max_filesize == 'K' ? 1024 : ($mul_upload_max_filesize == 'G' ? 1073741824 : 1)));
  1065. $upload_max_filesize = $mul_upload_max_filesize * (int) $upload_max_filesize;
  1066. // Max file size
  1067. $max_file_size = (($post_max_size < $upload_max_filesize) ? $post_max_size : $upload_max_filesize);
  1068. // Include main
  1069. include DOL_DOCUMENT_ROOT.'/core/tpl/ajax/fileupload_main.tpl.php';
  1070. // Include template
  1071. include DOL_DOCUMENT_ROOT.'/core/tpl/ajax/fileupload_view.tpl.php';
  1072. }
  1073. /**
  1074. * Show array with linked files
  1075. *
  1076. * @param Object $object Object
  1077. * @param int $permtodelete Deletion is allowed
  1078. * @param string $action Action
  1079. * @param string $selected ???
  1080. * @param string $param More param to add into URL
  1081. * @return int Number of links
  1082. */
  1083. public function listOfLinks($object, $permtodelete=1, $action=null, $selected=null, $param='')
  1084. {
  1085. global $user, $conf, $langs, $user;
  1086. global $bc;
  1087. global $sortfield, $sortorder;
  1088. $langs->load("link");
  1089. require_once DOL_DOCUMENT_ROOT . '/core/class/link.class.php';
  1090. $link = new Link($this->db);
  1091. $links = array();
  1092. if ($sortfield == "name") {
  1093. $sortfield = "label";
  1094. } elseif ($sortfield == "date") {
  1095. $sortfield = "datea";
  1096. } else {
  1097. $sortfield = null;
  1098. }
  1099. $res = $link->fetchAll($links, $object->element, $object->id, $sortfield, $sortorder);
  1100. $param .= (isset($object->id)?'&id=' . $object->id : '');
  1101. // Show list of associated links
  1102. print load_fiche_titre($langs->trans("LinkedFiles"));
  1103. print '<form action="' . $_SERVER['PHP_SELF'] . ($param?'?'.$param:'') . '" method="POST">';
  1104. print '<table width="100%" class="liste">';
  1105. print '<tr class="liste_titre">';
  1106. print_liste_field_titre(
  1107. $langs->trans("Links"),
  1108. $_SERVER['PHP_SELF'],
  1109. "name",
  1110. "",
  1111. $param,
  1112. 'align="left"',
  1113. $sortfield,
  1114. $sortorder
  1115. );
  1116. print_liste_field_titre(
  1117. "",
  1118. "",
  1119. "",
  1120. "",
  1121. "",
  1122. 'align="right"'
  1123. );
  1124. print_liste_field_titre(
  1125. $langs->trans("Date"),
  1126. $_SERVER['PHP_SELF'],
  1127. "date",
  1128. "",
  1129. $param,
  1130. 'align="center"',
  1131. $sortfield,
  1132. $sortorder
  1133. );
  1134. print_liste_field_titre(
  1135. '',
  1136. $_SERVER['PHP_SELF'],
  1137. "",
  1138. "",
  1139. $param,
  1140. 'align="center"'
  1141. );
  1142. print_liste_field_titre('','','');
  1143. print '</tr>';
  1144. $nboflinks = count($links);
  1145. if ($nboflinks > 0) include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
  1146. $var = true;
  1147. foreach ($links as $link)
  1148. {
  1149. $var =! $var;
  1150. print '<tr ' . $bc[$var] . '>';
  1151. //edit mode
  1152. if ($action == 'update' && $selected === $link->id)
  1153. {
  1154. print '<td>';
  1155. print '<input type="hidden" name="id" value="' . $object->id . '">';
  1156. print '<input type="hidden" name="linkid" value="' . $link->id . '">';
  1157. print '<input type="hidden" name="action" value="confirm_updateline">';
  1158. print $langs->trans('Link') . ': <input type="text" name="link" value="' . $link->url . '">';
  1159. print '</td>';
  1160. print '<td>';
  1161. print $langs->trans('Label') . ': <input type="text" name="label" value="' . $link->label . '">';
  1162. print '</td>';
  1163. print '<td align="center">' . dol_print_date(dol_now(), "dayhour", "tzuser") . '</td>';
  1164. print '<td align="right"></td>';
  1165. print '<td align="right">';
  1166. print '<input type="submit" name="save" class="button" value="' . dol_escape_htmltag($langs->trans('Save')) . '">';
  1167. print '<input type="submit" name="cancel" class="button" value="' . dol_escape_htmltag($langs->trans('Cancel')) . '">';
  1168. print '</td>';
  1169. }
  1170. else
  1171. {
  1172. print '<td>';
  1173. print '<a data-ajax="false" href="' . $link->url . '" target="_blank">';
  1174. print $link->label;
  1175. print '</a>';
  1176. print '</td>'."\n";
  1177. print '<td align="right"></td>';
  1178. print '<td align="center">' . dol_print_date($link->datea, "dayhour", "tzuser") . '</td>';
  1179. print '<td align="center"></td>';
  1180. print '<td align="right">';
  1181. print '<a href="' . $_SERVER['PHP_SELF'] . '?action=update&linkid=' . $link->id . $param . '" class="editfilelink" >' . img_edit() . '</a>'; // id= is included into $param
  1182. if ($permtodelete) {
  1183. print ' &nbsp; <a href="'. $_SERVER['PHP_SELF'] .'?action=delete&linkid=' . $link->id . $param . '" class="deletefilelink">' . img_delete() . '</a>'; // id= is included into $param
  1184. } else {
  1185. print '&nbsp;';
  1186. }
  1187. print '</td>';
  1188. }
  1189. print "</tr>\n";
  1190. }
  1191. if ($nboflinks == 0)
  1192. {
  1193. print '<tr ' . $bc[false] . '><td colspan="5" class="opacitymedium">';
  1194. print $langs->trans("NoLinkFound");
  1195. print '</td></tr>';
  1196. }
  1197. print "</table>";
  1198. print '</form>';
  1199. return $nboflinks;
  1200. }
  1201. }