PageRenderTime 40ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 1ms

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

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