PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/core/class/html.formfile.class.php

https://github.com/asterix14/dolibarr
PHP | 845 lines | 600 code | 74 blank | 171 comment | 156 complexity | d23a01a9c7b8630aa28a79f7f281f535 MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (c) 2008-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2010-2011 Regis Houssin <regis@dolibarr.fr>
  4. * Copyright (c) 2010 Juanjo Menent <jmenent@2byte.es>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/core/class/html.formfile.class.php
  21. * \ingroup core
  22. * \brief File of class to offer components to list and upload files
  23. */
  24. /**
  25. * \class FormFile
  26. * \brief Class to offer components to list and upload files
  27. */
  28. class FormFile
  29. {
  30. var $db;
  31. var $error;
  32. var $numoffiles;
  33. /**
  34. * Constructor
  35. *
  36. * @param DoliDB $DB Database handler
  37. */
  38. function FormFile($db)
  39. {
  40. $this->db = $db;
  41. $this->numoffiles=0;
  42. return 1;
  43. }
  44. /**
  45. * Show form to upload a new file
  46. *
  47. * @param url Url
  48. * @param title Title zone (Title or '' or 'none')
  49. * @param addcancel 1=Add 'Cancel' button
  50. * @param sectionid If upload must be done inside a particular ECM section
  51. * @param perm Value of permission to allow upload
  52. * @param size Length of input file area
  53. * @return int <0 ij KO, >0 if OK
  54. */
  55. function form_attach_new_file($url, $title='', $addcancel=0, $sectionid=0, $perm=1, $size=50)
  56. {
  57. global $conf,$langs;
  58. $maxlength=$size;
  59. print "\n\n<!-- Start form attach new file -->\n";
  60. if (empty($title)) $title=$langs->trans("AttachANewFile");
  61. if ($title != 'none') print_titre($title);
  62. print '<form name="userfile" action="'.$url.'" enctype="multipart/form-data" method="POST">';
  63. print '<input type="hidden" name="section" value="'.$sectionid.'">';
  64. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  65. print '<table width="100%" class="nobordernopadding">';
  66. print '<tr><td width="50%" valign="top">';
  67. $max=$conf->global->MAIN_UPLOAD_DOC; // En Kb
  68. $maxphp=@ini_get('upload_max_filesize'); // En inconnu
  69. if (preg_match('/m$/i',$maxphp)) $maxphp=$maxphp*1024;
  70. if (preg_match('/k$/i',$maxphp)) $maxphp=$maxphp;
  71. // Now $max and $maxphp are in Kb
  72. if ($maxphp > 0) $max=min($max,$maxphp);
  73. if ($max > 0)
  74. {
  75. print '<input type="hidden" name="max_file_size" value="'.($max*1024).'">';
  76. }
  77. print '<input class="flat" type="file" name="userfile" size="'.$maxlength.'"';
  78. print (empty($conf->global->MAIN_UPLOAD_DOC) || empty($perm)?' disabled="disabled"':'');
  79. print '>';
  80. print ' &nbsp; ';
  81. print '<input type="submit" class="button" name="sendit" value="'.$langs->trans("Upload").'"';
  82. print (empty($conf->global->MAIN_UPLOAD_DOC) || empty($perm)?' disabled="disabled"':'');
  83. print '>';
  84. if ($addcancel)
  85. {
  86. print ' &nbsp; ';
  87. print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
  88. }
  89. if (! empty($conf->global->MAIN_UPLOAD_DOC))
  90. {
  91. if ($perm)
  92. {
  93. print ' ('.$langs->trans("MaxSize").': '.$max.' '.$langs->trans("Kb");
  94. print ' '.info_admin($langs->trans("ThisLimitIsDefinedInSetup",$max,$maxphp),1);
  95. print ')';
  96. }
  97. }
  98. else
  99. {
  100. print ' ('.$langs->trans("UploadDisabled").')';
  101. }
  102. print "</td></tr>";
  103. print "</table>";
  104. print '</form>';
  105. if (empty($sectionid)) print '<br>';
  106. print "\n<!-- End form attach new file -->\n\n";
  107. return 1;
  108. }
  109. /**
  110. * Show the box with list of available documents for object
  111. *
  112. * @param string $modulepart propal, facture, facture_fourn, ...
  113. * @param string $filename Sub-directory to scan (Example: '0/1/10', 'FA/DD/MM/YY/9999'). Use '' if $filedir is already complete)
  114. * @param string $filedir Directory to scan
  115. * @param string $urlsource Url of origin page (for return)
  116. * @param int $genallowed Generation is allowed (1/0 or array of formats)
  117. * @param int $delallowed Remove is allowed (1/0)
  118. * @param string $modelselected Model to preselect by default
  119. * @param string $allowgenifempty Show warning if no model activated
  120. * @param string $forcenomultilang Do not show language option (even if MAIN_MULTILANGS defined)
  121. * @param int $iconPDF Show only PDF icon with link (1/0)
  122. * @param int $maxfilenamelength Max length for filename shown
  123. * @param string $noform Do not output html form tags
  124. * @param string $param More param on http links
  125. * @param string $title Title to show on top of form
  126. * @param string $buttonlabel Label on submit button
  127. * @param string $codelang Default language code to use on lang combo box if multilang is enabled
  128. * @param HookManager $hookmanager Object hookmanager with instance of external modules hook classes
  129. * @return int <0 if KO, number of shown files if OK
  130. */
  131. function show_documents($modulepart,$filename,$filedir,$urlsource,$genallowed,$delallowed=0,$modelselected='',$allowgenifempty=1,$forcenomultilang=0,$iconPDF=0,$maxfilenamelength=28,$noform=0,$param='',$title='',$buttonlabel='',$codelang='',$hookmanager=false)
  132. {
  133. $this->numoffiles=0;
  134. print $this->showdocuments($modulepart,$filename,$filedir,$urlsource,$genallowed,$delallowed,$modelselected,$allowgenifempty,$forcenomultilang,$iconPDF,$maxfilenamelength,$noform,$param,$title,$buttonlabel,$codelang,$hookmanager);
  135. return $this->numoffiles;
  136. }
  137. /**
  138. * Return a string to show the box with list of available documents for object.
  139. * This also set the property $this->numoffiles
  140. *
  141. * @param string $modulepart propal, facture, facture_fourn, ...
  142. * @param string $filename Sub-directory to scan (Example: '0/1/10', 'FA/DD/MM/YY/9999'). Use '' if $filedir is already complete)
  143. * @param string $filedir Directory to scan
  144. * @param string $urlsource Url of origin page (for return)
  145. * @param int $genallowed Generation is allowed (1/0 or array of formats)
  146. * @param int $delallowed Remove is allowed (1/0)
  147. * @param string $modelselected Model to preselect by default
  148. * @param string $allowgenifempty Show warning if no model activated
  149. * @param string $forcenomultilang Do not show language option (even if MAIN_MULTILANGS defined)
  150. * @param int $iconPDF Show only PDF icon with link (1/0)
  151. * @param int $maxfilenamelength Max length for filename shown
  152. * @param string $noform Do not output html form tags
  153. * @param string $param More param on http links
  154. * @param string $title Title to show on top of form
  155. * @param string $buttonlabel Label on submit button
  156. * @param string $codelang Default language code to use on lang combo box if multilang is enabled
  157. * @param HookManager $hookmanager Object hookmanager with instance of external modules hook classes
  158. * @return string Output string with HTML array of documents (might be empty string)
  159. */
  160. function showdocuments($modulepart,$filename,$filedir,$urlsource,$genallowed,$delallowed=0,$modelselected='',$allowgenifempty=1,$forcenomultilang=0,$iconPDF=0,$maxfilenamelength=28,$noform=0,$param='',$title='',$buttonlabel='',$codelang='',$hookmanager=false)
  161. {
  162. // filedir = conf->...dir_ouput."/".get_exdir(id)
  163. include_once(DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php');
  164. global $langs,$bc,$conf;
  165. $forname='builddoc';
  166. $out='';
  167. $var=true;
  168. // Clean paramaters
  169. if ($iconPDF == 1)
  170. {
  171. $genallowed = '';
  172. $delallowed = 0;
  173. $modelselected = '';
  174. $forcenomultilang=0;
  175. }
  176. //$filename = dol_sanitizeFileName($filename); //Must be sanitized before calling show_documents
  177. $headershown=0;
  178. $showempty=0;
  179. $i=0;
  180. $titletoshow=$langs->trans("Documents");
  181. if (! empty($title)) $titletoshow=$title;
  182. $out.= "\n".'<!-- Start show_document -->'."\n";
  183. //print 'filedir='.$filedir;
  184. // Affiche en-tete tableau
  185. if ($genallowed)
  186. {
  187. $modellist=array();
  188. if ($modulepart == 'company')
  189. {
  190. $showempty=1;
  191. if (is_array($genallowed)) $modellist=$genallowed;
  192. else
  193. {
  194. include_once(DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php');
  195. $modellist=ModeleThirdPartyDoc::liste_modeles($this->db);
  196. }
  197. }
  198. else if ($modulepart == 'propal')
  199. {
  200. if (is_array($genallowed)) $modellist=$genallowed;
  201. else
  202. {
  203. include_once(DOL_DOCUMENT_ROOT.'/core/modules/propale/modules_propale.php');
  204. $modellist=ModelePDFPropales::liste_modeles($this->db);
  205. }
  206. }
  207. else if ($modulepart == 'commande')
  208. {
  209. if (is_array($genallowed)) $modellist=$genallowed;
  210. else
  211. {
  212. include_once(DOL_DOCUMENT_ROOT.'/core/modules/commande/modules_commande.php');
  213. $modellist=ModelePDFCommandes::liste_modeles($this->db);
  214. }
  215. }
  216. elseif ($modulepart == 'expedition')
  217. {
  218. if (is_array($genallowed)) $modellist=$genallowed;
  219. else
  220. {
  221. include_once(DOL_DOCUMENT_ROOT.'/core/modules/expedition/pdf/ModelePdfExpedition.class.php');
  222. $modellist=ModelePDFExpedition::liste_modeles($this->db);
  223. }
  224. }
  225. elseif ($modulepart == 'livraison')
  226. {
  227. if (is_array($genallowed)) $modellist=$genallowed;
  228. else
  229. {
  230. include_once(DOL_DOCUMENT_ROOT.'/core/modules/livraison/modules_livraison.php');
  231. $modellist=ModelePDFDeliveryOrder::liste_modeles($this->db);
  232. }
  233. }
  234. else if ($modulepart == 'ficheinter')
  235. {
  236. if (is_array($genallowed)) $modellist=$genallowed;
  237. else
  238. {
  239. include_once(DOL_DOCUMENT_ROOT.'/core/modules/fichinter/modules_fichinter.php');
  240. $modellist=ModelePDFFicheinter::liste_modeles($this->db);
  241. }
  242. }
  243. elseif ($modulepart == 'facture')
  244. {
  245. if (is_array($genallowed)) $modellist=$genallowed;
  246. else
  247. {
  248. include_once(DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php');
  249. $modellist=ModelePDFFactures::liste_modeles($this->db);
  250. }
  251. }
  252. elseif ($modulepart == 'project')
  253. {
  254. if (is_array($genallowed)) $modellist=$genallowed;
  255. else
  256. {
  257. include_once(DOL_DOCUMENT_ROOT.'/core/modules/project/modules_project.php');
  258. $modellist=ModelePDFProjects::liste_modeles($this->db);
  259. }
  260. }
  261. elseif ($modulepart == 'export')
  262. {
  263. if (is_array($genallowed)) $modellist=$genallowed;
  264. else
  265. {
  266. include_once(DOL_DOCUMENT_ROOT.'/core/modules/export/modules_export.php');
  267. $modellist=ModeleExports::liste_modeles($this->db);
  268. }
  269. }
  270. else if ($modulepart == 'commande_fournisseur')
  271. {
  272. if (is_array($genallowed)) $modellist=$genallowed;
  273. else
  274. {
  275. include_once(DOL_DOCUMENT_ROOT.'/core/modules/supplier_order/modules_commandefournisseur.php');
  276. $modellist=ModelePDFSuppliersOrders::liste_modeles($this->db);
  277. }
  278. }
  279. else if ($modulepart == 'facture_fournisseur')
  280. {
  281. if (is_array($genallowed)) $modellist=$genallowed;
  282. else
  283. {
  284. include_once(DOL_DOCUMENT_ROOT.'/core/modules/supplier_invoice/modules_facturefournisseur.php');
  285. $modellist=ModelePDFSuppliersInvoices::liste_modeles($this->db);
  286. }
  287. }
  288. else if ($modulepart == 'remisecheque')
  289. {
  290. if (is_array($genallowed)) $modellist=$genallowed;
  291. else
  292. {
  293. include_once(DOL_DOCUMENT_ROOT.'/core/modules/cheque/pdf/modules_chequereceipts.php');
  294. $modellist=ModeleChequeReceipts::liste_modeles($this->db);
  295. }
  296. }
  297. elseif ($modulepart == 'donation')
  298. {
  299. if (is_array($genallowed)) $modellist=$genallowed;
  300. else
  301. {
  302. include_once(DOL_DOCUMENT_ROOT.'/core/modules/dons/modules_don.php');
  303. $modellist=ModeleDon::liste_modeles($this->db);
  304. }
  305. }
  306. else if ($modulepart == 'unpaid')
  307. {
  308. $modellist='';
  309. }
  310. else
  311. {
  312. // Generic feature, for external modules
  313. $file=dol_buildpath('/core/modules/'.$modulepart.'/modules_'.$modulepart.'.php',0);
  314. if (file_exists($file))
  315. {
  316. $res=include_once($file);
  317. }
  318. $class='Modele'.ucfirst($modulepart);
  319. if (class_exists($class))
  320. {
  321. $modellist=call_user_func($class.'::liste_modeles',$this->db);
  322. }
  323. else
  324. {
  325. dol_print_error($this->db,'Bad value for modulepart');
  326. return -1;
  327. }
  328. }
  329. $headershown=1;
  330. $form = new Form($db);
  331. $buttonlabeltoshow=$buttonlabel;
  332. if (empty($buttonlabel)) $buttonlabel=$langs->trans('Generate');
  333. if (empty($noform)) $out.= '<form action="'.$urlsource.(empty($conf->global->MAIN_JUMP_TAG)?'':'#builddoc').'" name="'.$forname.'" id="'.$forname.'_form" method="post">';
  334. $out.= '<input type="hidden" name="action" value="builddoc">';
  335. $out.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  336. $out.= '<div class="titre">'.$titletoshow.'</div>';
  337. $out.= '<table class="border formdoc" summary="listofdocumentstable" width="100%">';
  338. $out.= '<tr class="liste_titre">';
  339. // Model
  340. if (! empty($modellist))
  341. {
  342. $out.= '<th align="center" class="formdoc liste_titre">';
  343. $out.= $langs->trans('Model').' ';
  344. if (is_array($modellist) && count($modellist) == 1) // If there is only one element
  345. {
  346. $arraykeys=array_keys($modellist);
  347. $modelselected=$arraykeys[0];
  348. }
  349. $out.= $form->selectarray('model',$modellist,$modelselected,$showempty,0,0);
  350. $out.= '</th>';
  351. }
  352. else
  353. {
  354. $out.= '<th align="left" class="formdoc liste_titre">';
  355. $out.= $langs->trans("Files");
  356. $out.= '</th>';
  357. }
  358. // Language code (if multilang)
  359. $out.= '<th align="center" class="formdoc liste_titre">';
  360. if (($allowgenifempty || (is_array($modellist) && count($modellist) > 0)) && $conf->global->MAIN_MULTILANGS && ! $forcenomultilang)
  361. {
  362. include_once(DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php');
  363. $formadmin=new FormAdmin($this->db);
  364. $defaultlang=$codelang?$codelang:$langs->getDefaultLang();
  365. $out.= $formadmin->select_language($defaultlang);
  366. }
  367. else
  368. {
  369. $out.= '&nbsp;';
  370. }
  371. $out.= '</th>';
  372. // Button
  373. $out.= '<th align="center" colspan="'.($delallowed?'2':'1').'" class="formdocbutton liste_titre">';
  374. $out.= '<input class="button" id="'.$forname.'_generatebutton"';
  375. $out.= ' type="submit" value="'.$buttonlabel.'"';
  376. if (! $allowgenifempty && ! is_array($modellist) && empty($modellist)) $out.= ' disabled="disabled"';
  377. $out.= '>';
  378. if ($allowgenifempty && ! is_array($modellist) && empty($modellist) && $modulepart != 'unpaid')
  379. {
  380. $langs->load("errors");
  381. $out.= ' '.img_warning($langs->transnoentitiesnoconv("WarningNoDocumentModelActivated"));
  382. }
  383. $out.= '</th>';
  384. $out.= '</tr>';
  385. // Execute hooks
  386. $parameters=array('socid'=>$GLOBALS['socid'],'id'=>$GLOBALS['id'],'modulepart'=>$modulepart);
  387. if (is_object($hookmanager)) $out.= $hookmanager->executeHooks('formBuilddocOptions',$parameters);
  388. }
  389. // Get list of files
  390. if ($filedir)
  391. {
  392. $png = '';
  393. $filter = '';
  394. if ($iconPDF==1)
  395. {
  396. $png = '\.png$';
  397. $filter = $filename.'.pdf';
  398. }
  399. $file_list=dol_dir_list($filedir,'files',0,$filter,'\.meta$'.($png?'|'.$png:''),'date',SORT_DESC);
  400. // Affiche en-tete tableau si non deja affiche
  401. if (! empty($file_list) && ! $headershown && ! $iconPDF)
  402. {
  403. $headershown=1;
  404. $out.= '<div class="titre">'.$titletoshow.'</div>';
  405. $out.= '<table class="border" summary="listofdocumentstable" width="100%">';
  406. }
  407. else if (empty($file_list) && ! empty($iconPDF))
  408. {
  409. // For ajax treatment
  410. $out.= '<div id="gen_pdf_'.$filename.'" class="linkobject hideobject">'.img_picto('', 'refresh').'</div>'."\n";
  411. }
  412. // Loop on each file found
  413. foreach($file_list as $file)
  414. {
  415. $var=!$var;
  416. // Define relative path for download link (depends on module)
  417. $relativepath=$file["name"]; // Cas general
  418. if ($filename) $relativepath=$filename."/".$file["name"]; // Cas propal, facture...
  419. // Autre cas
  420. if ($modulepart == 'donation') { $relativepath = get_exdir($filename,2).$file["name"]; }
  421. if ($modulepart == 'export') { $relativepath = $file["name"]; }
  422. if (! $iconPDF) $out.= "<tr ".$bc[$var].">";
  423. // Show file name with link to download
  424. if (! $iconPDF) $out.= '<td nowrap="nowrap">';
  425. $out.= '<a href="'.DOL_URL_ROOT . '/document.php?modulepart='.$modulepart.'&amp;file='.urlencode($relativepath).'"';
  426. $mime=dol_mimetype($relativepath,'',0);
  427. if (preg_match('/text/',$mime)) $out.= ' target="_blank"';
  428. $out.= '>';
  429. if (! $iconPDF)
  430. {
  431. $out.= img_mime($file["name"],$langs->trans("File").': '.$file["name"]).' '.dol_trunc($file["name"],$maxfilenamelength);
  432. }
  433. else
  434. {
  435. $out.= img_pdf($file["name"],2);
  436. }
  437. $out.= '</a>'."\n";
  438. if (! $iconPDF)
  439. {
  440. $out.= '</td>';
  441. // Show file size
  442. $out.= '<td align="right" nowrap="nowrap">'.dol_print_size(dol_filesize($filedir."/".$file["name"])).'</td>';
  443. // Show file date
  444. $out.= '<td align="right" nowrap="nowrap">'.dol_print_date(dol_filemtime($filedir."/".$file["name"]),'dayhour').'</td>';
  445. }
  446. if ($delallowed)
  447. {
  448. $out.= '<td align="right"><a href="'.DOL_URL_ROOT.'/document.php?action=remove_file&amp;modulepart='.$modulepart.'&amp;file='.urlencode($relativepath);
  449. $out.= ($param?'&amp;'.$param:'');
  450. $out.= '&amp;urlsource='.urlencode($urlsource);
  451. $out.= '">'.img_delete().'</a></td>';
  452. }
  453. if (! $iconPDF) $out.= '</tr>';
  454. $this->numoffiles++;
  455. }
  456. }
  457. if ($headershown)
  458. {
  459. // Affiche pied du tableau
  460. $out.= "</table>\n";
  461. if ($genallowed)
  462. {
  463. if (empty($noform)) $out.= '</form>'."\n";
  464. }
  465. }
  466. $out.= '<!-- End show_document -->'."\n";
  467. //return ($i?$i:$headershown);
  468. return $out;
  469. }
  470. /**
  471. * Show list of documents in a directory
  472. *
  473. * @param filearray Array of files loaded by dol_dir_list('files') function before calling this
  474. * @param object Object on which document is linked to
  475. * @param modulepart Value for modulepart used by download or viewimage wrapper
  476. * @param param Parameters on sort links
  477. * @param forcedownload Force to open dialog box "Save As" when clicking on file
  478. * @param relativepath Relative path of docs (autodefined if not provided)
  479. * @param permtodelete Permission to delete
  480. * @param useinecm Change output for use in ecm module
  481. * @param textifempty Text to show if filearray is empty
  482. * @param maxlength Maximum length of file name shown
  483. * @return int <0 if KO, nb of files shown if OK
  484. */
  485. function list_of_documents($filearray,$object,$modulepart,$param,$forcedownload=0,$relativepath='',$permtodelete=1,$useinecm=0,$textifempty='',$maxlength=0)
  486. {
  487. global $user, $conf, $langs;
  488. global $bc;
  489. global $sortfield, $sortorder;
  490. // Show list of existing files
  491. if (empty($useinecm)) print_titre($langs->trans("AttachedFiles"));
  492. //else { $bc[true]=''; $bc[false]=''; };
  493. $url=$_SERVER["PHP_SELF"];
  494. print '<table width="100%" class="'.($useinecm?'nobordernopadding':'liste').'">';
  495. print '<tr class="liste_titre">';
  496. print_liste_field_titre($langs->trans("Documents2"),$_SERVER["PHP_SELF"],"name","",$param,'align="left"',$sortfield,$sortorder);
  497. print_liste_field_titre($langs->trans("Size"),$_SERVER["PHP_SELF"],"size","",$param,'align="right"',$sortfield,$sortorder);
  498. print_liste_field_titre($langs->trans("Date"),$_SERVER["PHP_SELF"],"date","",$param,'align="center"',$sortfield,$sortorder);
  499. if (empty($useinecm)) print_liste_field_titre('',$_SERVER["PHP_SELF"],"","",$param,'align="center"');
  500. print_liste_field_titre('','','');
  501. print '</tr>';
  502. $nboffiles=count($filearray);
  503. if ($nboffiles > 0) include_once(DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php');
  504. $var=true;
  505. foreach($filearray as $key => $file) // filearray must be only files here
  506. {
  507. if ($file['name'] != '.'
  508. && $file['name'] != '..'
  509. && $file['name'] != 'CVS'
  510. && ! preg_match('/\.meta$/i',$file['name']))
  511. {
  512. // Define relative path used to store the file
  513. if (! $relativepath) $relativepath=dol_sanitizeFileName($object->ref).'/';
  514. $var=!$var;
  515. print '<tr '.$bc[$var].'>';
  516. print '<td>';
  517. //print "XX".$file['name']; //$file['name'] must be utf8
  518. print '<a href="'.DOL_URL_ROOT.'/document.php?modulepart='.$modulepart;
  519. if ($forcedownload) print '&attachment=1';
  520. print '&file='.urlencode($relativepath.$file['name']).'">';
  521. print img_mime($file['name'],$file['name'].' ('.dol_print_size($file['size'],0,0).')').' ';
  522. print dol_trunc($file['name'],$maxlength,'middle');
  523. print '</a>';
  524. print "</td>\n";
  525. print '<td align="right">'.dol_print_size($file['size'],1,1).'</td>';
  526. print '<td align="center">'.dol_print_date($file['date'],"dayhour").'</td>';
  527. // Preview
  528. if (empty($useinecm))
  529. {
  530. print '<td align="center">';
  531. $tmp=explode('.',$file['name']);
  532. $minifile=$tmp[0].'_mini.'.$tmp[1];
  533. if (image_format_supported($file['name']) > 0) print '<img border="0" height="'.$maxheightmini.'" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$modulepart.'&file='.urlencode($relativepath.'thumbs/'.$minifile).'" title="">';
  534. else print '&nbsp;';
  535. print '</td>';
  536. }
  537. // Delete or view link
  538. print '<td align="right">';
  539. if (! empty($useinecm)) print '<a href="'.DOL_URL_ROOT.'/ecm/docfile.php?urlfile='.urlencode($file['name']).$param.'">'.img_view().'</a> &nbsp; ';
  540. if ($permtodelete) print '<a href="'.$url.'?id='.$object->id.'&action=delete&urlfile='.urlencode($file['name']).$param.'">'.img_delete().'</a>';
  541. else print '&nbsp;';
  542. print "</td>";
  543. print "</tr>\n";
  544. }
  545. }
  546. if ($nboffiles == 0)
  547. {
  548. print '<tr '.$bc[$var].'><td colspan="'.(empty($useinecm)?'5':'4').'">';
  549. if (empty($textifempty)) print $langs->trans("NoFileFound");
  550. else print $textifempty;
  551. print '</td></tr>';
  552. }
  553. print "</table>";
  554. // Fin de zone
  555. }
  556. /**
  557. * Show list of documents in a directory
  558. * @param upload_dir Directory that was scanned
  559. * @param filearray Array of files loaded by dol_dir_list function before calling this function
  560. * @param modulepart Value for modulepart used by download wrapper
  561. * @param param Parameters on sort links
  562. * @param forcedownload Force to open dialog box "Save As" when clicking on file
  563. * @param relativepath Relative path of docs (autodefined if not provided)
  564. * @param permtodelete Permission to delete
  565. * @param useinecm Change output for use in ecm module
  566. * @param textifempty Text to show if filearray is empty
  567. * @param maxlength Maximum length of file name shown
  568. * @return int <0 if KO, nb of files shown if OK
  569. */
  570. function list_of_autoecmfiles($upload_dir,$filearray,$modulepart,$param,$forcedownload=0,$relativepath='',$permtodelete=1,$useinecm=0,$textifempty='',$maxlength=0)
  571. {
  572. global $user, $conf, $langs;
  573. global $bc;
  574. global $sortfield, $sortorder;
  575. // Affiche liste des documents existant
  576. if (empty($useinecm)) print_titre($langs->trans("AttachedFiles"));
  577. //else { $bc[true]=''; $bc[false]=''; };
  578. $url=$_SERVER["PHP_SELF"];
  579. print '<table width="100%" class="nobordernopadding">';
  580. print '<tr class="liste_titre">';
  581. print_liste_field_titre($langs->trans("Ref"),$_SERVER["PHP_SELF"],"","",$param,'align="left"',$sortfield,$sortorder);
  582. print_liste_field_titre($langs->trans("Documents2"),$_SERVER["PHP_SELF"],"name","",$param,'align="left"',$sortfield,$sortorder);
  583. print_liste_field_titre($langs->trans("Size"),$_SERVER["PHP_SELF"],"size","",$param,'align="right"',$sortfield,$sortorder);
  584. print_liste_field_titre($langs->trans("Date"),$_SERVER["PHP_SELF"],"date","",$param,'align="center"',$sortfield,$sortorder);
  585. print_liste_field_titre('','','');
  586. print '</tr>';
  587. if ($modulepart == 'invoice')
  588. {
  589. include_once(DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php');
  590. $object_static=new Facture($this->db);
  591. }
  592. if ($modulepart == 'invoice_supplier')
  593. {
  594. include_once(DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php');
  595. $object_static=new FactureFournisseur($this->db);
  596. }
  597. $var=true;
  598. foreach($filearray as $key => $file)
  599. {
  600. if (!is_dir($file['name'])
  601. && $file['name'] != '.'
  602. && $file['name'] != '..'
  603. && $file['name'] != 'CVS'
  604. && ! preg_match('/\.meta$/i',$file['name']))
  605. {
  606. // Define relative path used to store the file
  607. $relativefile=preg_replace('/'.preg_quote($upload_dir.'/','/').'/','',$file['fullname']);
  608. //print 'eeee'.$relativefile;
  609. //var_dump($file);
  610. $var=!$var;
  611. print '<tr '.$bc[$var].'>';
  612. print '<td>';
  613. $id='';$ref='';
  614. if ($modulepart == 'invoice')
  615. {
  616. preg_match('/(.*)\/[^\/]+$/',$relativefile,$reg);
  617. $ref=$reg[1];
  618. $object_static->fetch('',$ref);
  619. //print $relativefile.'rr'.$id;
  620. print $object_static->getNomUrl(1,'document');
  621. }
  622. if ($modulepart == 'invoice_supplier')
  623. {
  624. preg_match('/(\d+)\/[^\/]+$/',$relativefile,$reg);
  625. $id=$reg[1];
  626. $object_static->fetch($id);
  627. //print $relativefile.'rr'.$id;
  628. print $object_static->getNomUrl(1,'document');
  629. }
  630. print '</td>';
  631. print '<td>';
  632. //print "XX".$file['name']; //$file['name'] must be utf8
  633. print '<a href="'.DOL_URL_ROOT.'/document.php?modulepart='.$modulepart;
  634. if ($forcedownload) print '&attachment=1';
  635. print '&file='.urlencode($relativefile).'">';
  636. print img_mime($file['name'],$file['name'].' ('.dol_print_size($file['size'],0,0).')').' ';
  637. print dol_trunc($file['name'],$maxlength,'middle');
  638. print '</a>';
  639. print "</td>\n";
  640. print '<td align="right">'.dol_print_size($file['size'],1,1).'</td>';
  641. print '<td align="center">'.dol_print_date($file['date'],"dayhour").'</td>';
  642. print '<td align="right">';
  643. if (! empty($useinecm)) print '<a href="'.DOL_URL_ROOT.'/document.php?modulepart='.$modulepart;
  644. if ($forcedownload) print '&attachment=1';
  645. print '&file='.urlencode($relativefile).'">';
  646. print img_view().'</a> &nbsp; ';
  647. //if ($permtodelete) print '<a href="'.$url.'?id='.$object->id.'&section='.$_REQUEST["section"].'&action=delete&urlfile='.urlencode($file['name']).'">'.img_delete().'</a>';
  648. //else print '&nbsp;';
  649. print "</td></tr>\n";
  650. }
  651. }
  652. if (count($filearray) == 0)
  653. {
  654. print '<tr '.$bc[$var].'><td colspan="4">';
  655. if (empty($textifempty)) print $langs->trans("NoFileFound");
  656. else print $textifempty;
  657. print '</td></tr>';
  658. }
  659. print "</table>";
  660. // Fin de zone
  661. }
  662. /**
  663. * Show form to upload a new file with jquery fileupload
  664. */
  665. function form_ajaxfileupload($object)
  666. {
  667. global $langs;
  668. // PHP post_max_size
  669. $post_max_size = ini_get('post_max_size');
  670. $mul_post_max_size = substr($post_max_size, -1);
  671. $mul_post_max_size = ($mul_post_max_size == 'M' ? 1048576 : ($mul_post_max_size == 'K' ? 1024 : ($mul_post_max_size == 'G' ? 1073741824 : 1)));
  672. $post_max_size = $mul_post_max_size * (int) $post_max_size;
  673. // PHP upload_max_filesize
  674. $upload_max_filesize = ini_get('upload_max_filesize');
  675. $mul_upload_max_filesize = substr($upload_max_filesize, -1);
  676. $mul_upload_max_filesize = ($mul_upload_max_filesize == 'M' ? 1048576 : ($mul_upload_max_filesize == 'K' ? 1024 : ($mul_upload_max_filesize == 'G' ? 1073741824 : 1)));
  677. $upload_max_filesize = $mul_upload_max_filesize * (int) $upload_max_filesize;
  678. // Max file size
  679. $max_file_size = (($post_max_size < $upload_max_filesize) ? $post_max_size : $upload_max_filesize);
  680. print '<script type="text/javascript">
  681. $(function () {
  682. \'use strict\';
  683. var max_file_size = \''.$max_file_size.'\';
  684. // Initialize the jQuery File Upload widget:
  685. $("#fileupload").fileupload({
  686. maxFileSize: max_file_size,
  687. done: function (e, data) {
  688. $.ajax(data).success(function () {
  689. location.href=\''.$_SERVER["PHP_SELF"].'?'.$_SERVER["QUERY_STRING"].'\';
  690. });
  691. },
  692. destroy: function (e, data) {
  693. var that = $(this).data("fileupload");
  694. if ( confirm("Delete this file ?") == true ) {
  695. if (data.url) {
  696. $.ajax(data).success(function () {
  697. that._adjustMaxNumberOfFiles(1);
  698. $(this).fadeOut(function () {
  699. $(this).remove();
  700. });
  701. });
  702. } else {
  703. data.context.fadeOut(function () {
  704. $(this).remove();
  705. });
  706. }
  707. }
  708. }
  709. });
  710. // Load existing files:
  711. // TODO do not delete
  712. if (1 == 2) {
  713. $.getJSON($("#fileupload form").prop("action"), { fk_element: "'.$object->id.'", element: "'.$object->element.'"}, function (files) {
  714. var fu = $("#fileupload").data("fileupload");
  715. fu._adjustMaxNumberOfFiles(-files.length);
  716. fu._renderDownload(files)
  717. .appendTo($("#fileupload .files"))
  718. .fadeIn(function () {
  719. // Fix for IE7 and lower:
  720. $(this).show();
  721. });
  722. });
  723. }
  724. // Open download dialogs via iframes,
  725. // to prevent aborting current uploads:
  726. $("#fileupload .files a:not([target^=_blank])").live("click", function (e) {
  727. e.preventDefault();
  728. $(\'<iframe style="display:none;"></iframe>\')
  729. .prop("src", this.href)
  730. .appendTo("body");
  731. });
  732. });
  733. </script>';
  734. print '<div id="fileupload">';
  735. print '<form action="'.DOL_URL_ROOT.'/core/ajax/fileupload.php" method="POST" enctype="multipart/form-data">';
  736. print '<input type="hidden" name="fk_element" value="'.$object->id.'">';
  737. print '<input type="hidden" name="element" value="'.$object->element.'">';
  738. print '<div class="fileupload-buttonbar">';
  739. print '<input type="hidden" name="protocol" value="http">';
  740. print '<label class="fileinput-button">';
  741. print '<span>'.$langs->trans('AddFiles').'</span>';
  742. print '<input type="file" name="files[]" multiple>';
  743. print '</label>';
  744. print '<button type="submit" class="start">'.$langs->trans('StartUpload').'</button>';
  745. print '<button type="reset" class="cancel">'.$langs->trans('CancelUpload').'</button>';
  746. print '</div></form>';
  747. print '</div><!-- end div fileupload -->';
  748. print '<div id="fileupload-view">';
  749. print '<div class="fileupload-content">';
  750. print '<table width="100%" class="files">';
  751. /*print '<tr>';
  752. print '<td>'.$langs->trans("Documents2").'</td>';
  753. print '<td>'.$langs->trans("Preview").'</td>';
  754. print '<td align="right">'.$langs->trans("Size").'</td>';
  755. print '<td colspan="3"></td>';
  756. print '</tr>';*/
  757. print '</table>';
  758. // We remove this because there is already individual bars.
  759. //print '<div class="fileupload-progressbar"></div>';
  760. print '</div><!-- end div fileupload-content -->';
  761. print '</div><!-- end div fileupload-view -->';
  762. // Include template
  763. include(DOL_DOCUMENT_ROOT.'/core/tpl/ajaxfileupload.tpl.php');
  764. }
  765. }
  766. ?>