html.formfile.class.php 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  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. private $db;
  34. public $error;
  35. public $numoffiles;
  36. public $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. Deprecated.
  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. //If there is no permission and the option to hide unauthorized actions is enabled, then nothing is printed
  81. if (!$perm && !empty($conf->global->MAIN_BUTTON_HIDE_UNAUTHORIZED)) {
  82. return 1;
  83. }
  84. $maxlength=$size;
  85. $out = "\n\n<!-- Start form attach new file -->\n";
  86. if (empty($title)) $title=$langs->trans("AttachANewFile");
  87. if ($title != 'none') $out.=load_fiche_titre($title, null, null);
  88. $out .= '<form name="'.$htmlname.'" id="'.$htmlname.'" action="'.$url.'" enctype="multipart/form-data" method="POST">';
  89. $out .= '<input type="hidden" id="'.$htmlname.'_section_dir" name="section_dir" value="">';
  90. $out .= '<input type="hidden" id="'.$htmlname.'_section_id" name="section_id" value="'.$sectionid.'">';
  91. $out .= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  92. $out .= '<table width="100%" class="nobordernopadding">';
  93. $out .= '<tr>';
  94. if (! empty($options)) $out .= '<td>'.$options.'</td>';
  95. $out .= '<td valign="middle">';
  96. $max=$conf->global->MAIN_UPLOAD_DOC; // En Kb
  97. $maxphp=@ini_get('upload_max_filesize'); // En inconnu
  98. if (preg_match('/k$/i',$maxphp)) $maxphp=$maxphp*1;
  99. if (preg_match('/m$/i',$maxphp)) $maxphp=$maxphp*1024;
  100. if (preg_match('/g$/i',$maxphp)) $maxphp=$maxphp*1024*1024;
  101. if (preg_match('/t$/i',$maxphp)) $maxphp=$maxphp*1024*1024*1024;
  102. // Now $max and $maxphp are in Kb
  103. if ($maxphp > 0) $max=min($max,$maxphp);
  104. if ($max > 0)
  105. {
  106. $out .= '<input type="hidden" name="max_file_size" value="'.($max*1024).'">';
  107. }
  108. $out .= '<input class="flat minwidth400" type="file"';
  109. $out .= ((! empty($conf->global->MAIN_DISABLE_MULTIPLE_FILEUPLOAD) || $conf->browser->layout != 'classic')?' name="userfile"':' name="userfile[]" multiple');
  110. $out .= (empty($conf->global->MAIN_UPLOAD_DOC) || empty($perm)?' disabled':'');
  111. $out .= '>';
  112. $out .= ' ';
  113. $out .= '<input type="submit" class="button" name="sendit" value="'.$langs->trans("Upload").'"';
  114. $out .= (empty($conf->global->MAIN_UPLOAD_DOC) || empty($perm)?' disabled':'');
  115. $out .= '>';
  116. if ($addcancel)
  117. {
  118. $out .= ' &nbsp; ';
  119. $out .= '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
  120. }
  121. if (! empty($conf->global->MAIN_UPLOAD_DOC))
  122. {
  123. if ($perm)
  124. {
  125. $langs->load('other');
  126. $out .= ' ';
  127. $out .= info_admin($langs->trans("ThisLimitIsDefinedInSetup",$max,$maxphp),1);
  128. }
  129. }
  130. else
  131. {
  132. $out .= ' ('.$langs->trans("UploadDisabled").')';
  133. }
  134. $out .= "</td></tr>";
  135. if ($savingdocmask)
  136. {
  137. $out .= '<tr>';
  138. if (! empty($options)) $out .= '<td>'.$options.'</td>';
  139. $out .= '<td valign="middle" class="nowrap">';
  140. $out .= '<input type="checkbox" checked class="savingdocmask" name="savingdocmask" value="'.dol_escape_js($savingdocmask).'"> '.$langs->trans("SaveUploadedFileWithMask", preg_replace('/__file__/',$langs->transnoentitiesnoconv("OriginFileName"),$savingdocmask), $langs->transnoentitiesnoconv("OriginFileName"));
  141. $out .= '</td>';
  142. $out .= '</tr>';
  143. }
  144. $out .= "</table>";
  145. $out .= '</form>';
  146. if (empty($sectionid)) $out .= '<br>';
  147. $out .= "\n<!-- End form attach new file -->\n";
  148. if ($linkfiles)
  149. {
  150. $out .= "\n<!-- Start form attach new link -->\n";
  151. $langs->load('link');
  152. $title = $langs->trans("LinkANewFile");
  153. $out .= load_fiche_titre($title, null, null);
  154. $out .= '<form name="'.$htmlname.'_link" id="'.$htmlname.'_link" action="'.$url.'" method="POST">';
  155. $out .= '<input type="hidden" id="'.$htmlname.'_link_section_dir" name="link_section_dir" value="">';
  156. $out .= '<input type="hidden" id="'.$htmlname.'_link_section_id" name="link_section_id" value="'.$sectionid.'">';
  157. $out .= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  158. $out .= '<div class="valignmiddle" >';
  159. $out .= '<div class="inline-block" style="padding-right: 10px;">';
  160. if (! empty($conf->global->OPTIMIZEFORTEXTBROWSER)) $out .= '<label for="link">'.$langs->trans("URLToLink") . ':</label> ';
  161. $out .= '<input type="text" name="link" class="flat minwidth400imp" id="link" placeholder="'.dol_escape_htmltag($langs->trans("URLToLink")).'">';
  162. $out .= '</div>';
  163. $out .= '<div class="inline-block" style="padding-right: 10px;">';
  164. if (! empty($conf->global->OPTIMIZEFORTEXTBROWSER)) $out .= '<label for="label">'.$langs->trans("Label") . ':</label> ';
  165. $out .= '<input type="text" class="flat" name="label" id="label" placeholder="'.dol_escape_htmltag($langs->trans("Label")).'">';
  166. $out .= '<input type="hidden" name="objecttype" value="' . $object->element . '">';
  167. $out .= '<input type="hidden" name="objectid" value="' . $object->id . '">';
  168. $out .= '</div>';
  169. $out .= '<div class="inline-block" style="padding-right: 10px;">';
  170. $out .= '<input type="submit" class="button" name="linkit" value="'.$langs->trans("ToLink").'"';
  171. $out .= (empty($conf->global->MAIN_UPLOAD_DOC) || empty($perm)?' disabled':'');
  172. $out .= '>';
  173. $out .= '</div>';
  174. $out .= '</div>';
  175. $out .= '<div class="clearboth"></div>';
  176. $out .= '</form><br>';
  177. $parameters = array('socid'=>(isset($GLOBALS['socid'])?$GLOBALS['socid']:''),'id'=>(isset($GLOBALS['id'])?$GLOBALS['id']:''), 'url'=>$url, 'perm'=>$perm);
  178. $res = $hookmanager->executeHooks('formattachOptions',$parameters,$object);
  179. $out .= "\n<!-- End form attach new file -->\n";
  180. }
  181. if (empty($res))
  182. {
  183. print '<div class="attacharea">';
  184. print $out;
  185. print '</div>';
  186. }
  187. print $hookmanager->resPrint;
  188. return 1;
  189. }
  190. }
  191. /**
  192. * Show the box with list of available documents for object
  193. *
  194. * @param string $modulepart propal, facture, facture_fourn, ...
  195. * @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.
  196. * @param string $filedir Directory to scan
  197. * @param string $urlsource Url of origin page (for return)
  198. * @param int $genallowed Generation is allowed (1/0 or array of formats)
  199. * @param int $delallowed Remove is allowed (1/0)
  200. * @param string $modelselected Model to preselect by default
  201. * @param integer $allowgenifempty Show warning if no model activated
  202. * @param integer $forcenomultilang Do not show language option (even if MAIN_MULTILANGS defined)
  203. * @param int $iconPDF Show only PDF icon with link (1/0)
  204. * @param int $notused Not used
  205. * @param integer $noform Do not output html form tags
  206. * @param string $param More param on http links
  207. * @param string $title Title to show on top of form
  208. * @param string $buttonlabel Label on submit button
  209. * @param string $codelang Default language code to use on lang combo box if multilang is enabled
  210. * @return int <0 if KO, number of shown files if OK
  211. * @deprecated Use print xxx->showdocuments() instead.
  212. */
  213. function show_documents($modulepart,$modulesubdir,$filedir,$urlsource,$genallowed,$delallowed=0,$modelselected='',$allowgenifempty=1,$forcenomultilang=0,$iconPDF=0,$notused=0,$noform=0,$param='',$title='',$buttonlabel='',$codelang='')
  214. {
  215. $this->numoffiles=0;
  216. print $this->showdocuments($modulepart,$modulesubdir,$filedir,$urlsource,$genallowed,$delallowed,$modelselected,$allowgenifempty,$forcenomultilang,$iconPDF,$notused,$noform,$param,$title,$buttonlabel,$codelang);
  217. return $this->numoffiles;
  218. }
  219. /**
  220. * Return a string to show the box with list of available documents for object.
  221. * This also set the property $this->numoffiles
  222. *
  223. * @param string $modulepart Module the files are related to ('propal', 'facture', 'facture_fourn', 'mymodule', 'mymodule_temp', ...)
  224. * @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.
  225. * @param string $filedir Directory to scan
  226. * @param string $urlsource Url of origin page (for return)
  227. * @param int $genallowed Generation is allowed (1/0 or array list of templates)
  228. * @param int $delallowed Remove is allowed (1/0)
  229. * @param string $modelselected Model to preselect by default
  230. * @param integer $allowgenifempty Allow generation even if list of template ($genallowed) is empty (show however a warning)
  231. * @param integer $forcenomultilang Do not show language option (even if MAIN_MULTILANGS defined)
  232. * @param int $iconPDF Deprecated, see getDocumentsLink
  233. * @param int $notused Not used
  234. * @param integer $noform Do not output html form tags
  235. * @param string $param More param on http links
  236. * @param string $title Title to show on top of form
  237. * @param string $buttonlabel Label on submit button
  238. * @param string $codelang Default language code to use on lang combo box if multilang is enabled
  239. * @param string $morepicto Add more HTML content into cell with picto
  240. * @param Object $object Object when method is called from an object card.
  241. * @return string Output string with HTML array of documents (might be empty string)
  242. */
  243. function showdocuments($modulepart,$modulesubdir,$filedir,$urlsource,$genallowed,$delallowed=0,$modelselected='',$allowgenifempty=1,$forcenomultilang=0,$iconPDF=0,$notused=0,$noform=0,$param='',$title='',$buttonlabel='',$codelang='',$morepicto='',$object=null)
  244. {
  245. // Deprecation warning
  246. if (0 !== $iconPDF) {
  247. dol_syslog(__METHOD__ . ": passing iconPDF parameter is deprecated", LOG_WARNING);
  248. }
  249. global $langs, $conf, $user, $hookmanager;
  250. global $form, $bc;
  251. if (! is_object($form)) $form=new Form($this->db);
  252. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  253. // For backward compatibility
  254. if (! empty($iconPDF)) {
  255. return $this->getDocumentsLink($modulepart, $modulesubdir, $filedir);
  256. }
  257. $printer=0;
  258. if (in_array($modulepart,array('facture','supplier_proposal','propal','proposal','order','commande','expedition', 'commande_fournisseur', 'expensereport'))) // The direct print feature is implemented only for such elements
  259. {
  260. $printer = (!empty($user->rights->printing->read) && !empty($conf->printing->enabled))?true:false;
  261. }
  262. $hookmanager->initHooks(array('formfile'));
  263. $forname='builddoc';
  264. $out='';
  265. $headershown=0;
  266. $showempty=0;
  267. $i=0;
  268. $out.= "\n".'<!-- Start show_document -->'."\n";
  269. //print 'filedir='.$filedir;
  270. if (preg_match('/massfilesarea_/', $modulepart))
  271. {
  272. $out.='<div id="show_files"><br></div>';
  273. $title=$langs->trans("MassFilesArea").' <a href="" id="togglemassfilesarea" ref="shown">('.$langs->trans("Hide").')</a>';
  274. $title.='<script type="text/javascript" language="javascript">
  275. jQuery(document).ready(function() {
  276. jQuery(\'#togglemassfilesarea\').click(function() {
  277. if (jQuery(\'#togglemassfilesarea\').attr(\'ref\') == "shown")
  278. {
  279. jQuery(\'#'.$modulepart.'_table\').hide();
  280. jQuery(\'#togglemassfilesarea\').attr("ref", "hidden");
  281. jQuery(\'#togglemassfilesarea\').text("('.dol_escape_js($langs->trans("Show")).')");
  282. }
  283. else
  284. {
  285. jQuery(\'#'.$modulepart.'_table\').show();
  286. jQuery(\'#togglemassfilesarea\').attr("ref","shown");
  287. jQuery(\'#togglemassfilesarea\').text("('.dol_escape_js($langs->trans("Hide")).')");
  288. }
  289. return false;
  290. });
  291. });
  292. </script>';
  293. }
  294. $titletoshow=$langs->trans("Documents");
  295. if (! empty($title)) $titletoshow=$title;
  296. // Show table
  297. if ($genallowed)
  298. {
  299. $modellist=array();
  300. if ($modulepart == 'company')
  301. {
  302. $showempty=1;
  303. if (is_array($genallowed)) $modellist=$genallowed;
  304. else
  305. {
  306. include_once DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php';
  307. $modellist=ModeleThirdPartyDoc::liste_modeles($this->db);
  308. }
  309. }
  310. else if ($modulepart == 'propal')
  311. {
  312. if (is_array($genallowed)) $modellist=$genallowed;
  313. else
  314. {
  315. include_once DOL_DOCUMENT_ROOT.'/core/modules/propale/modules_propale.php';
  316. $modellist=ModelePDFPropales::liste_modeles($this->db);
  317. }
  318. }
  319. else if ($modulepart == 'supplier_proposal')
  320. {
  321. if (is_array($genallowed)) $modellist=$genallowed;
  322. else
  323. {
  324. include_once DOL_DOCUMENT_ROOT.'/core/modules/supplier_proposal/modules_supplier_proposal.php';
  325. $modellist=ModelePDFSupplierProposal::liste_modeles($this->db);
  326. }
  327. }
  328. else if ($modulepart == 'commande')
  329. {
  330. if (is_array($genallowed)) $modellist=$genallowed;
  331. else
  332. {
  333. include_once DOL_DOCUMENT_ROOT.'/core/modules/commande/modules_commande.php';
  334. $modellist=ModelePDFCommandes::liste_modeles($this->db);
  335. }
  336. }
  337. elseif ($modulepart == 'expedition')
  338. {
  339. if (is_array($genallowed)) $modellist=$genallowed;
  340. else
  341. {
  342. include_once DOL_DOCUMENT_ROOT.'/core/modules/expedition/modules_expedition.php';
  343. $modellist=ModelePDFExpedition::liste_modeles($this->db);
  344. }
  345. }
  346. elseif ($modulepart == 'livraison')
  347. {
  348. if (is_array($genallowed)) $modellist=$genallowed;
  349. else
  350. {
  351. include_once DOL_DOCUMENT_ROOT.'/core/modules/livraison/modules_livraison.php';
  352. $modellist=ModelePDFDeliveryOrder::liste_modeles($this->db);
  353. }
  354. }
  355. else if ($modulepart == 'ficheinter')
  356. {
  357. if (is_array($genallowed)) $modellist=$genallowed;
  358. else
  359. {
  360. include_once DOL_DOCUMENT_ROOT.'/core/modules/fichinter/modules_fichinter.php';
  361. $modellist=ModelePDFFicheinter::liste_modeles($this->db);
  362. }
  363. }
  364. elseif ($modulepart == 'facture')
  365. {
  366. if (is_array($genallowed)) $modellist=$genallowed;
  367. else
  368. {
  369. include_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
  370. $modellist=ModelePDFFactures::liste_modeles($this->db);
  371. }
  372. }
  373. elseif ($modulepart == 'contract')
  374. {
  375. if (is_array($genallowed)) $modellist=$genallowed;
  376. else
  377. {
  378. include_once DOL_DOCUMENT_ROOT.'/core/modules/contract/modules_contract.php';
  379. $modellist=ModelePDFContract::liste_modeles($this->db);
  380. }
  381. }
  382. elseif ($modulepart == 'project')
  383. {
  384. if (is_array($genallowed)) $modellist=$genallowed;
  385. else
  386. {
  387. include_once DOL_DOCUMENT_ROOT.'/core/modules/project/modules_project.php';
  388. $modellist=ModelePDFProjects::liste_modeles($this->db);
  389. }
  390. }
  391. elseif ($modulepart == 'project_task')
  392. {
  393. if (is_array($genallowed)) $modellist=$genallowed;
  394. else
  395. {
  396. include_once DOL_DOCUMENT_ROOT.'/core/modules/project/task/modules_task.php';
  397. $modellist=ModelePDFTask::liste_modeles($this->db);
  398. }
  399. }
  400. elseif ($modulepart == 'product')
  401. {
  402. if (is_array($genallowed)) $modellist=$genallowed;
  403. else
  404. {
  405. include_once DOL_DOCUMENT_ROOT.'/core/modules/product/modules_product.class.php';
  406. $modellist=ModelePDFProduct::liste_modeles($this->db);
  407. }
  408. }
  409. elseif ($modulepart == 'export')
  410. {
  411. if (is_array($genallowed)) $modellist=$genallowed;
  412. else
  413. {
  414. include_once DOL_DOCUMENT_ROOT.'/core/modules/export/modules_export.php';
  415. $modellist=ModeleExports::liste_modeles($this->db);
  416. }
  417. }
  418. else if ($modulepart == 'commande_fournisseur' || $modulepart == 'supplier_order')
  419. {
  420. if (is_array($genallowed)) $modellist=$genallowed;
  421. else
  422. {
  423. include_once DOL_DOCUMENT_ROOT.'/core/modules/supplier_order/modules_commandefournisseur.php';
  424. $modellist=ModelePDFSuppliersOrders::liste_modeles($this->db);
  425. }
  426. }
  427. else if ($modulepart == 'facture_fournisseur' || $modulepart == 'supplier_invoice')
  428. {
  429. if (is_array($genallowed)) $modellist=$genallowed;
  430. else
  431. {
  432. include_once DOL_DOCUMENT_ROOT.'/core/modules/supplier_invoice/modules_facturefournisseur.php';
  433. $modellist=ModelePDFSuppliersInvoices::liste_modeles($this->db);
  434. }
  435. }
  436. else if ($modulepart == 'supplier_payment')
  437. {
  438. if (is_array($genallowed)) $modellist=$genallowed;
  439. else
  440. {
  441. include_once DOL_DOCUMENT_ROOT.'/core/modules/supplier_payment/modules_supplier_payment.php';
  442. $modellist=ModelePDFSuppliersPayments::liste_modeles($this->db);
  443. }
  444. }
  445. else if ($modulepart == 'remisecheque')
  446. {
  447. if (is_array($genallowed)) $modellist=$genallowed;
  448. else
  449. {
  450. include_once DOL_DOCUMENT_ROOT.'/core/modules/cheque/modules_chequereceipts.php';
  451. $modellist=ModeleChequeReceipts::liste_modeles($this->db);
  452. }
  453. }
  454. elseif ($modulepart == 'donation')
  455. {
  456. if (is_array($genallowed)) $modellist=$genallowed;
  457. else
  458. {
  459. include_once DOL_DOCUMENT_ROOT.'/core/modules/dons/modules_don.php';
  460. $modellist=ModeleDon::liste_modeles($this->db);
  461. }
  462. }
  463. elseif ($modulepart == 'member')
  464. {
  465. if (is_array($genallowed)) $modellist=$genallowed;
  466. else
  467. {
  468. include_once DOL_DOCUMENT_ROOT.'/core/modules/member/modules_cards.php';
  469. $modellist=ModelePDFCards::liste_modeles($this->db);
  470. }
  471. }
  472. elseif ($modulepart == 'agenda')
  473. {
  474. if (is_array($genallowed)) $modellist=$genallowed;
  475. else
  476. {
  477. include_once DOL_DOCUMENT_ROOT.'/core/modules/action/modules_action.php';
  478. $modellist=ModeleAction::liste_modeles($this->db);
  479. }
  480. }
  481. else if ($modulepart == 'expensereport')
  482. {
  483. if (is_array($genallowed)) $modellist=$genallowed;
  484. else
  485. {
  486. include_once DOL_DOCUMENT_ROOT.'/core/modules/expensereport/modules_expensereport.php';
  487. $modellist=ModeleExpenseReport::liste_modeles($this->db);
  488. }
  489. }
  490. else if ($modulepart == 'unpaid')
  491. {
  492. $modellist='';
  493. }
  494. elseif ($modulepart == 'user')
  495. {
  496. if (is_array($genallowed)) $modellist=$genallowed;
  497. else
  498. {
  499. include_once DOL_DOCUMENT_ROOT.'/core/modules/user/modules_user.class.php';
  500. $modellist=ModelePDFUser::liste_modeles($this->db);
  501. }
  502. }
  503. elseif ($modulepart == 'usergroup')
  504. {
  505. if (is_array($genallowed)) $modellist=$genallowed;
  506. else
  507. {
  508. include_once DOL_DOCUMENT_ROOT.'/core/modules/usergroup/modules_usergroup.class.php';
  509. $modellist=ModelePDFUserGroup::liste_modeles($this->db);
  510. }
  511. }
  512. else //if ($modulepart != 'agenda')
  513. {
  514. // For normalized standard modules
  515. $file=dol_buildpath('/core/modules/'.$modulepart.'/modules_'.$modulepart.'.php',0);
  516. if (file_exists($file))
  517. {
  518. $res=include_once $file;
  519. }
  520. // For normalized external modules
  521. else
  522. {
  523. $file=dol_buildpath('/'.$modulepart.'/core/modules/'.$modulepart.'/modules_'.$modulepart.'.php',0);
  524. $res=include_once $file;
  525. }
  526. $class='Modele'.ucfirst($modulepart);
  527. if (class_exists($class))
  528. {
  529. $modellist=call_user_func($class.'::liste_modeles',$this->db);
  530. }
  531. else
  532. {
  533. dol_print_error($this->db,'Bad value for modulepart');
  534. return -1;
  535. }
  536. }
  537. // Set headershown to avoit to have table opened a second time later
  538. $headershown=1;
  539. $buttonlabeltoshow=$buttonlabel;
  540. if (empty($buttonlabel)) $buttonlabel=$langs->trans('Generate');
  541. if ($conf->browser->layout == 'phone') $urlsource.='#'.$forname.'_form'; // So we switch to form after a generation
  542. if (empty($noform)) $out.= '<form action="'.$urlsource.(empty($conf->global->MAIN_JUMP_TAG)?'':'#builddoc').'" id="'.$forname.'_form" method="post">';
  543. $out.= '<input type="hidden" name="action" value="builddoc">';
  544. $out.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  545. $out.= load_fiche_titre($titletoshow, '', '');
  546. $out.= '<div class="div-table-responsive-no-min">';
  547. $out.= '<table class="liste formdoc noborder" summary="listofdocumentstable" width="100%">';
  548. $out.= '<tr class="liste_titre">';
  549. $addcolumforpicto=($delallowed || $printer || $morepicto);
  550. $out.= '<th align="center" colspan="'.(3+($addcolumforpicto?'2':'1')).'" class="formdoc liste_titre maxwidthonsmartphone">';
  551. // Model
  552. if (! empty($modellist))
  553. {
  554. $out.= '<span class="hideonsmartphone">'.$langs->trans('Model').' </span>';
  555. if (is_array($modellist) && count($modellist) == 1) // If there is only one element
  556. {
  557. $arraykeys=array_keys($modellist);
  558. $modelselected=$arraykeys[0];
  559. }
  560. $out.= $form->selectarray('model', $modellist, $modelselected, $showempty, 0, 0, '', 0, 0, 0, '', 'minwidth100');
  561. $out.= ajax_combobox('model');
  562. }
  563. else
  564. {
  565. $out.= '<div class="float">'.$langs->trans("Files").'</div>';
  566. }
  567. // Language code (if multilang)
  568. if (($allowgenifempty || (is_array($modellist) && count($modellist) > 0)) && $conf->global->MAIN_MULTILANGS && ! $forcenomultilang && (! empty($modellist) || $showempty))
  569. {
  570. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
  571. $formadmin=new FormAdmin($this->db);
  572. $defaultlang=$codelang?$codelang:$langs->getDefaultLang();
  573. $morecss='maxwidth150';
  574. if (! empty($conf->browser->phone)) $morecss='maxwidth100';
  575. $out.= $formadmin->select_language($defaultlang, 'lang_id', 0, 0, 0, 0, 0, $morecss);
  576. }
  577. else
  578. {
  579. $out.= '&nbsp;';
  580. }
  581. // Button
  582. $genbutton = '<input class="button buttongen" id="'.$forname.'_generatebutton" name="'.$forname.'_generatebutton"';
  583. $genbutton.= ' type="submit" value="'.$buttonlabel.'"';
  584. if (! $allowgenifempty && ! is_array($modellist) && empty($modellist)) $genbutton.= ' disabled';
  585. $genbutton.= '>';
  586. if ($allowgenifempty && ! is_array($modellist) && empty($modellist) && empty($conf->dol_no_mouse_hover) && $modulepart != 'unpaid')
  587. {
  588. $langs->load("errors");
  589. $genbutton.= ' '.img_warning($langs->transnoentitiesnoconv("WarningNoDocumentModelActivated"));
  590. }
  591. if (! $allowgenifempty && ! is_array($modellist) && empty($modellist) && empty($conf->dol_no_mouse_hover) && $modulepart != 'unpaid') $genbutton='';
  592. if (empty($modellist) && ! $showempty && $modulepart != 'unpaid') $genbutton='';
  593. $out.= $genbutton;
  594. $out.= '</th>';
  595. if (!empty($hookmanager->hooks['formfile']))
  596. {
  597. foreach($hookmanager->hooks['formfile'] as $module)
  598. {
  599. if (method_exists($module, 'formBuilddocLineOptions')) $out .= '<th></th>';
  600. }
  601. }
  602. $out.= '</tr>';
  603. // Execute hooks
  604. $parameters=array('socid'=>(isset($GLOBALS['socid'])?$GLOBALS['socid']:''),'id'=>(isset($GLOBALS['id'])?$GLOBALS['id']:''),'modulepart'=>$modulepart);
  605. if (is_object($hookmanager))
  606. {
  607. $reshook = $hookmanager->executeHooks('formBuilddocOptions',$parameters,$GLOBALS['object']);
  608. $out.= $hookmanager->resPrint;
  609. }
  610. }
  611. // Get list of files
  612. if (! empty($filedir))
  613. {
  614. $file_list=dol_dir_list($filedir,'files',0,'','(\.meta|_preview.*.*\.png)$','date',SORT_DESC);
  615. $link_list = array();
  616. if (is_object($object))
  617. {
  618. require_once DOL_DOCUMENT_ROOT . '/core/class/link.class.php';
  619. $link = new Link($this->db);
  620. $sortfield = $sortorder = null;
  621. $res = $link->fetchAll($link_list, $object->element, $object->id, $sortfield, $sortorder);
  622. }
  623. $out.= '<!-- html.formfile::showdocuments -->'."\n";
  624. // Show title of array if not already shown
  625. if ((! empty($file_list) || ! empty($link_list) || preg_match('/^massfilesarea/', $modulepart)) && ! $headershown)
  626. {
  627. $headershown=1;
  628. $out.= '<div class="titre">'.$titletoshow.'</div>'."\n";
  629. $out.= '<table class="noborder" summary="listofdocumentstable" id="'.$modulepart.'_table" width="100%">'."\n";
  630. }
  631. // Loop on each file found
  632. if (is_array($file_list))
  633. {
  634. foreach($file_list as $file)
  635. {
  636. // Define relative path for download link (depends on module)
  637. $relativepath=$file["name"]; // Cas general
  638. if ($modulesubdir) $relativepath=$modulesubdir."/".$file["name"]; // Cas propal, facture...
  639. if ($modulepart == 'export') $relativepath = $file["name"]; // Other case
  640. $out.= '<tr class="oddeven">';
  641. $documenturl = DOL_URL_ROOT.'/document.php';
  642. if (isset($conf->global->DOL_URL_ROOT_DOCUMENT_PHP)) $documenturl=$conf->global->DOL_URL_ROOT_DOCUMENT_PHP;
  643. // Show file name with link to download
  644. $out.= '<td class="tdoverflowmax300">';
  645. $tmp = $this->showPreview($file,$modulepart,$relativepath,0,$param);
  646. $out.= ($tmp?$tmp.' ':'');
  647. $out.= '<a href="'.$documenturl.'?modulepart='.$modulepart.'&amp;file='.urlencode($relativepath).($param?'&'.$param:'').'"';
  648. $mime=dol_mimetype($relativepath,'',0);
  649. if (preg_match('/text/',$mime)) $out.= ' target="_blank"';
  650. $out.= ' target="_blank">';
  651. $out.= img_mime($file["name"],$langs->trans("File").': '.$file["name"]).' '.$file["name"];
  652. $out.= '</a>'."\n";
  653. $out.= '</td>';
  654. // Show file size
  655. $size=(! empty($file['size'])?$file['size']:dol_filesize($filedir."/".$file["name"]));
  656. $out.= '<td align="right" class="nowrap">'.dol_print_size($size).'</td>';
  657. // Show file date
  658. $date=(! empty($file['date'])?$file['date']:dol_filemtime($filedir."/".$file["name"]));
  659. $out.= '<td align="right" class="nowrap">'.dol_print_date($date, 'dayhour', 'tzuser').'</td>';
  660. if ($delallowed || $printer || $morepicto)
  661. {
  662. $out.= '<td align="right">';
  663. if ($delallowed)
  664. {
  665. $out.= '<a href="'.$urlsource.(strpos($urlsource,'?')?'&amp;':'?').'action=remove_file&amp;file='.urlencode($relativepath);
  666. $out.= ($param?'&amp;'.$param:'');
  667. //$out.= '&modulepart='.$modulepart; // TODO obsolete ?
  668. //$out.= '&urlsource='.urlencode($urlsource); // TODO obsolete ?
  669. $out.= '">'.img_picto($langs->trans("Delete"), 'delete.png').'</a>';
  670. //$out.='</td>';
  671. }
  672. if ($printer)
  673. {
  674. //$out.= '<td align="right">';
  675. $out.= '&nbsp;<a href="'.$urlsource.(strpos($urlsource,'?')?'&amp;':'?').'action=print_file&amp;printer='.$modulepart.'&amp;file='.urlencode($relativepath);
  676. $out.= ($param?'&amp;'.$param:'');
  677. $out.= '">'.img_picto($langs->trans("PrintFile", $relativepath),'printer.png').'</a>';
  678. }
  679. if ($morepicto)
  680. {
  681. $morepicto=preg_replace('/__FILENAMEURLENCODED__/',urlencode($relativepath),$morepicto);
  682. $out.=$morepicto;
  683. }
  684. $out.='</td>';
  685. }
  686. if (is_object($hookmanager))
  687. {
  688. $parameters=array('socid'=>(isset($GLOBALS['socid'])?$GLOBALS['socid']:''),'id'=>(isset($GLOBALS['id'])?$GLOBALS['id']:''),'modulepart'=>$modulepart,'relativepath'=>$relativepath);
  689. $res = $hookmanager->executeHooks('formBuilddocLineOptions',$parameters,$file);
  690. if (empty($res))
  691. {
  692. $out .= $hookmanager->resPrint; // Complete line
  693. $out.= '</tr>';
  694. }
  695. else $out = $hookmanager->resPrint; // Replace line
  696. }
  697. }
  698. $this->numoffiles++;
  699. }
  700. // Loop on each file found
  701. if (is_array($link_list))
  702. {
  703. $colspan=2;
  704. foreach($link_list as $file)
  705. {
  706. $out.='<tr class="oddeven">';
  707. $out.='<td colspan="'.$colspan.'" class="maxwidhtonsmartphone">';
  708. $out.='<a data-ajax="false" href="' . $link->url . '" target="_blank">';
  709. $out.=$file->label;
  710. $out.='</a>';
  711. $out.='</td>';
  712. $out.='<td align="right">';
  713. $out.=dol_print_date($file->datea,'dayhour');
  714. $out.='</td>';
  715. if ($delallowed || $printer || $morepicto) $out.='<td></td>';
  716. $out.='</tr>';
  717. }
  718. $this->numoffiles++;
  719. }
  720. if (count($file_list) == 0 && count($link_list) == 0 && $headershown)
  721. {
  722. $out.='<tr class="oddeven"><td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  723. }
  724. }
  725. if ($headershown)
  726. {
  727. // Affiche pied du tableau
  728. $out.= "</table>\n";
  729. $out.= "</div>\n";
  730. if ($genallowed)
  731. {
  732. if (empty($noform)) $out.= '</form>'."\n";
  733. }
  734. }
  735. $out.= '<!-- End show_document -->'."\n";
  736. //return ($i?$i:$headershown);
  737. return $out;
  738. }
  739. /**
  740. * Show a Document icon with link(s)
  741. * You may want to call this into a div like this:
  742. * print '<div class="inline-block valignmiddle">'.$formfile->getDocumentsLink($element_doc, $filename, $filedir).'</div>';
  743. *
  744. * @param string $modulepart propal, facture, facture_fourn, ...
  745. * @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.
  746. * @param string $filedir Full path to directory to scan
  747. * @param string $filter Filter filenames on this regex string (Example: '\.pdf$')
  748. * @return string Output string with HTML link of documents (might be empty string). This also fill the array ->infofiles
  749. */
  750. function getDocumentsLink($modulepart, $modulesubdir, $filedir, $filter='')
  751. {
  752. global $conf, $langs;
  753. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  754. $out='';
  755. $this->infofiles=array('nboffiles'=>0,'extensions'=>array(),'files'=>array());
  756. //if (! empty($conf->dol_use_jmobile)) return '';
  757. $file_list=dol_dir_list($filedir, 'files', 0, preg_quote(basename($modulesubdir),'/').'[^\-]+', '\.meta$|\.png$'); // Get list of files starting with name of ref (but not followed by "-" to discard uploaded files)
  758. // For ajax treatment
  759. $out.= '<!-- html.formfile::getDocumentsLink -->'."\n";
  760. if (! empty($file_list))
  761. {
  762. $out='<dl class="dropdown inline-block">
  763. <dt><a data-ajax="false" href="#" onClick="return false;">'.img_picto('', 'listlight').'</a></dt>
  764. <dd><div class="multichoicedoc" style="position:absolute;left:100px;" ><ul class="ulselectedfields" style="display: none;">';
  765. $tmpout='';
  766. // Loop on each file found
  767. foreach($file_list as $file)
  768. {
  769. $i++;
  770. if ($filter && ! preg_match('/'.$filter.'/i', $file["name"])) continue; // Discard this. It does not match provided filter.
  771. // Define relative path for download link (depends on module)
  772. $relativepath=$file["name"]; // Cas general
  773. if ($modulesubdir) $relativepath=$modulesubdir."/".$file["name"]; // Cas propal, facture...
  774. // Autre cas
  775. if ($modulepart == 'donation') {
  776. $relativepath = get_exdir($modulesubdir,2,0,0,null,'donation').$file["name"];
  777. }
  778. if ($modulepart == 'export') {
  779. $relativepath = $file["name"];
  780. }
  781. $this->infofiles['nboffiles']++;
  782. $this->infofiles['files'][]=$file['fullname'];
  783. $ext=pathinfo($file["name"], PATHINFO_EXTENSION);
  784. if (empty($this->infofiles[$ext])) $this->infofiles['extensions'][$ext]=1;
  785. else $this->infofiles['extensions'][$ext]++;
  786. // Preview
  787. if (! empty($conf->use_javascript_ajax) && ($conf->browser->layout != 'phone'))
  788. {
  789. $tmparray = getAdvancedPreviewUrl($modulepart, $relativepath, 1, '&entity='.$entity);
  790. if ($tmparray && $tmparray['url']) $tmpout.= '<li><a href="'.$tmparray['url'].'"'.($tmparray['css']?' class="'.$tmparray['css'].'"':'').($tmparray['mime']?' mime="'.$tmparray['mime'].'"':'').($tmparray['target']?' target="'.$tmparray['target'].'"':'').'>'.img_picto('','detail').' '.$langs->trans("Preview").' '.$ext.'</a></li>';
  791. }
  792. // Download
  793. $tmpout.= '<li><a class="pictopreview" href="'.DOL_URL_ROOT . '/document.php?modulepart='.$modulepart.'&amp;file='.urlencode($relativepath).'"';
  794. $mime=dol_mimetype($relativepath,'',0);
  795. if (preg_match('/text/',$mime)) $tmpout.= ' target="_blank"';
  796. $tmpout.= '>';
  797. $tmpout.=img_mime($relativepath, $file["name"]).' ';
  798. $tmpout.= $langs->trans("Download").' '.$ext;
  799. $tmpout.= '</a></li>'."\n";
  800. }
  801. $out.=$tmpout;
  802. $out.='</ul></div></dd>
  803. </dl>';
  804. }
  805. else
  806. {
  807. // TODO Add link to regenerate doc ?
  808. //$out.= '<div id="gen_pdf_'.$modulesubdir.'" class="linkobject hideobject">'.img_picto('', 'refresh').'</div>'."\n";
  809. }
  810. return $out;
  811. }
  812. /**
  813. * Show list of documents in $filearray (may be they are all in same directory but may not)
  814. *
  815. * @param array $filearray Array of files loaded by dol_dir_list('files') function before calling this.
  816. * @param Object $object Object on which document is linked to.
  817. * @param string $modulepart Value for modulepart used by download or viewimage wrapper.
  818. * @param string $param Parameters on sort links (param must start with &, example &aaa=bbb&ccc=ddd)
  819. * @param int $forcedownload Force to open dialog box "Save As" when clicking on file.
  820. * @param string $relativepath Relative path of docs (autodefined if not provided), relative to module dir, not to MAIN_DATA_ROOT.
  821. * @param int $permonobject Permission on object (so permission to delete or crop document)
  822. * @param int $useinecm Change output for use in ecm module
  823. * @param string $textifempty Text to show if filearray is empty ('NoFileFound' if not defined)
  824. * @param int $maxlength Maximum length of file name shown.
  825. * @param string $title Title before list
  826. * @param string $url Full url to use for click links ('' = autodetect)
  827. * @param int $showrelpart 0=Show only filename (default), 1=Show first level 1 dir
  828. * @param int $permtoeditline Permission to edit document line (You must provide a value, -1 is deprecated and must not be used any more)
  829. * @param string $upload_dir Full path directory so we can know dir relative to MAIN_DATA_ROOT. Fill this if you want to complete file data with database indexes.
  830. * @param string $sortfield Sort field ('name', 'size', 'position', ...)
  831. * @param string $sortorder Sort order ('ASC' or 'DESC')
  832. * @param int $disablemove 1=Disable move button, 0=Position move is possible.
  833. * @return int <0 if KO, nb of files shown if OK
  834. */
  835. function list_of_documents($filearray,$object,$modulepart,$param='',$forcedownload=0,$relativepath='',$permonobject=1,$useinecm=0,$textifempty='',$maxlength=0,$title='',$url='', $showrelpart=0, $permtoeditline=-1,$upload_dir='',$sortfield='',$sortorder='ASC', $disablemove=1)
  836. {
  837. global $user, $conf, $langs, $hookmanager;
  838. global $bc,$bcdd;
  839. global $sortfield, $sortorder, $maxheightmini;
  840. // Define relative path used to store the file
  841. if (empty($relativepath))
  842. {
  843. $relativepath=(! empty($object->ref)?dol_sanitizeFileName($object->ref):'').'/';
  844. 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
  845. if ($object->element == 'project_task') $relativepath='Call_not_supported_._Call_function_using_a_defined_relative_path_.';
  846. }
  847. // For backward compatiblity, we detect file is stored into an old path
  848. if (! empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO) && $file['level1name'] == 'photos')
  849. {
  850. $relativepath=preg_replace('/^.*\/produit\//','',$file['path']).'/';
  851. }
  852. // Defined relative dir to DOL_DATA_ROOT
  853. $relativedir = '';
  854. if ($upload_dir)
  855. {
  856. $relativedir = preg_replace('/^'.preg_quote(DOL_DATA_ROOT,'/').'/', '', $upload_dir);
  857. $relativedir = preg_replace('/^[\\/]/','',$relativedir);
  858. }
  859. $hookmanager->initHooks(array('formfile'));
  860. $parameters=array(
  861. 'filearray' => $filearray,
  862. 'modulepart'=> $modulepart,
  863. 'param' => $param,
  864. 'forcedownload' => $forcedownload,
  865. 'relativepath' => $relativepath, // relative filename to module dir
  866. 'relativedir' => $relativedir, // relative dirname to DOL_DATA_ROOT
  867. 'permtodelete' => $permonobject,
  868. 'useinecm' => $useinecm,
  869. 'textifempty' => $textifempty,
  870. 'maxlength' => $maxlength,
  871. 'title' => $title,
  872. 'url' => $url
  873. );
  874. $reshook=$hookmanager->executeHooks('showFilesList', $parameters, $object);
  875. if (isset($reshook) && $reshook != '') // null or '' for bypass
  876. {
  877. return $reshook;
  878. }
  879. else
  880. {
  881. $param = (isset($object->id)?'&id='.$object->id:'').$param;
  882. if ($permtoeditline < 0) // Old behaviour for backward compatibility. New feature should call method with value 0 or 1
  883. {
  884. $permtoeditline=0;
  885. if (in_array($modulepart, array('product','produit','service')))
  886. {
  887. if ($user->rights->produit->creer && $object->type == Product::TYPE_PRODUCT) $permtoeditline=1;
  888. if ($user->rights->service->creer && $object->type == Product::TYPE_SERVICE) $permtoeditline=1;
  889. }
  890. }
  891. if (empty($conf->global->MAIN_UPLOAD_DOC))
  892. {
  893. $permtoeditline=0;
  894. $permonobject=0;
  895. }
  896. // Show list of existing files
  897. if (empty($useinecm)) print load_fiche_titre($title?$title:$langs->trans("AttachedFiles"));
  898. if (empty($url)) $url=$_SERVER["PHP_SELF"];
  899. print '<!-- html.formfile::list_of_documents -->'."\n";
  900. if (GETPOST('action','aZ09') == 'editfile' && $permtoeditline)
  901. {
  902. print '<form action="'.$_SERVER["PHP_SELF"].'?'.$param.'" method="POST">';
  903. print '<input type="hidden" name="action" value="renamefile">';
  904. print '<input type="hidden" name="id" value="'.$object->id.'">';
  905. print '<input type="hidden" name="modulepart" value="'.$modulepart.'">';
  906. }
  907. print '<div class="div-table-responsive-no-min">';
  908. print '<table width="100%" id="tablelines" class="'.($useinecm?'liste noborder':'liste').'">'."\n";
  909. print '<tr class="liste_titre nodrag nodrop">';
  910. print_liste_field_titre($langs->trans("Documents2"),$url,"name","",$param,'align="left"',$sortfield,$sortorder);
  911. print_liste_field_titre($langs->trans("Size"),$url,"size","",$param,'align="right"',$sortfield,$sortorder);
  912. print_liste_field_titre($langs->trans("Date"),$url,"date","",$param,'align="center"',$sortfield,$sortorder);
  913. if (empty($useinecm)) print_liste_field_titre('',$url,"","",$param,'align="center"');
  914. print_liste_field_titre('');
  915. if (! $disablemove) print_liste_field_titre('');
  916. print "</tr>\n";
  917. // Get list of files stored into database for same relative directory
  918. if ($relativedir)
  919. {
  920. $filearrayindatabase = dol_dir_list_in_database($relativedir, '', null, 'name', SORT_ASC);
  921. //var_dump($filearray);
  922. //var_dump($filearrayindatabase);
  923. // Complete filearray with properties found into $filearrayindatabase
  924. foreach($filearray as $key => $val)
  925. {
  926. $found=0;
  927. // Search if it exists into $filearrayindatabase
  928. foreach($filearrayindatabase as $key2 => $val2)
  929. {
  930. if ($filearrayindatabase[$key2]['name'] == $filearray[$key]['name'])
  931. {
  932. $filearray[$key]['position_name']=($filearrayindatabase[$key2]['position']?$filearrayindatabase[$key2]['position']:'0').'_'.$filearrayindatabase[$key2]['name'];
  933. $filearray[$key]['position']=$filearrayindatabase[$key2]['position'];
  934. $filearray[$key]['cover']=$filearrayindatabase[$key2]['cover'];
  935. $filearray[$key]['acl']=$filearrayindatabase[$key2]['acl'];
  936. $filearray[$key]['rowid']=$filearrayindatabase[$key2]['rowid'];
  937. $filearray[$key]['label']=$filearrayindatabase[$key2]['label'];
  938. $found=1;
  939. break;
  940. }
  941. }
  942. if (! $found) // This happen in transition towerd version 6, or if files were added manually into os dir.
  943. {
  944. $filearray[$key]['position']='999999'; // File not indexed are at end. So if we add a file, it will not replace an existing position
  945. $filearray[$key]['cover']=0;
  946. $filearray[$key]['acl']='';
  947. $rel_filename = preg_replace('/^'.preg_quote(DOL_DATA_ROOT,'/').'/', '', $filearray[$key]['fullname']);
  948. if (! preg_match('/(\/temp\/|\/thumbs|\.meta$)/', $rel_filetorenameafter)) // If not a tmp file
  949. {
  950. dol_syslog("list_of_documents We found a file called '".$filearray[$key]['name']."' not indexed into database. We add it");
  951. include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
  952. $ecmfile=new EcmFiles($this->db);
  953. // Add entry into database
  954. $filename = basename($rel_filename);
  955. $rel_dir = dirname($rel_filename);
  956. $rel_dir = preg_replace('/[\\/]$/', '', $rel_dir);
  957. $rel_dir = preg_replace('/^[\\/]/', '', $rel_dir);
  958. $ecmfile->filepath = $rel_dir;
  959. $ecmfile->filename = $filename;
  960. $ecmfile->label = md5_file(dol_osencode($filearray[$key]['fullname'])); // $destfile is a full path to file
  961. $ecmfile->fullpath_orig = $filearray[$key]['fullname'];
  962. $ecmfile->gen_or_uploaded = 'unknown';
  963. $ecmfile->description = ''; // indexed content
  964. $ecmfile->keyword = ''; // keyword content
  965. $result = $ecmfile->create($user);
  966. if ($result < 0)
  967. {
  968. setEventMessages($ecmfile->error, $ecmfile->errors, 'warnings');
  969. }
  970. else
  971. {
  972. $filearray[$key]['rowid']=$result;
  973. }
  974. }
  975. else
  976. {
  977. $filearray[$key]['rowid']=0; // Should not happened
  978. }
  979. }
  980. }
  981. /*var_dump($filearray);
  982. var_dump($sortfield);
  983. var_dump($sortorder);*/
  984. if ($sortfield && $sortorder)
  985. {
  986. $filearray=dol_sort_array($filearray, $sortfield, $sortorder);
  987. }
  988. //var_dump($filearray);
  989. }
  990. $nboffiles=count($filearray);
  991. if ($nboffiles > 0) include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
  992. $var=true; $i=0; $nboflines = 0; $lastrowid=0;
  993. foreach($filearray as $key => $file) // filearray must be only files here
  994. {
  995. if ($file['name'] != '.'
  996. && $file['name'] != '..'
  997. && ! preg_match('/\.meta$/i',$file['name']))
  998. {
  999. if ($filearray[$key]['rowid'] > 0) $lastrowid = $filearray[$key]['rowid'];
  1000. $editline=0;
  1001. $nboflines++;
  1002. print '<!-- Line list_of_documents '.$key.' relativepath = '.$relativepath.' -->'."\n";
  1003. // Do we have entry into database ?
  1004. print '<!-- In database: position='.$filearray[$key]['position'].' -->'."\n";
  1005. print '<tr id="row-'.($filearray[$key]['rowid']>0?$filearray[$key]['rowid']:'-AFTER'.$lastrowid.'POS'.($i+1)).'" '.$bcdd[$var].'>';
  1006. print '<td class="tdoverflowmax300">';
  1007. $filepath=$relativepath.$file['name'];
  1008. if (! $editline) print $this->showPreview($file,$modulepart,$filepath);
  1009. //print "XX".$file['name']; //$file['name'] must be utf8
  1010. print '<a class="paddingleft" href="'.DOL_URL_ROOT.'/document.php?modulepart='.$modulepart;
  1011. if ($forcedownload) print '&attachment=1';
  1012. if (! empty($object->entity)) print '&entity='.$object->entity;
  1013. print '&file='.urlencode($filepath);
  1014. print '">';
  1015. print img_mime($file['name'],$file['name'].' ('.dol_print_size($file['size'],0,0).')').' ';
  1016. if ($showrelpart == 1) print $relativepath;
  1017. //print dol_trunc($file['name'],$maxlength,'middle');
  1018. if (GETPOST('action','aZ09') == 'editfile' && $file['name'] == basename(GETPOST('urlfile')))
  1019. {
  1020. print '</a>';
  1021. print '<input type="hidden" name="renamefilefrom" value="'.dol_escape_htmltag($file['name']).'">';
  1022. print '<input type="text" name="renamefileto" class="quatrevingtpercent" value="'.dol_escape_htmltag($file['name']).'">';
  1023. $editline=1;
  1024. }
  1025. else
  1026. {
  1027. print $file['name'];
  1028. print '</a>';
  1029. }
  1030. print "</td>\n";
  1031. print '<td align="right" width="80px">'.dol_print_size($file['size'],1,1).'</td>';
  1032. print '<td align="center" width="130px">'.dol_print_date($file['date'],"dayhour","tzuser").'</td>';
  1033. // Preview
  1034. if (empty($useinecm))
  1035. {
  1036. $fileinfo = pathinfo($file['name']);
  1037. print '<td align="center">';
  1038. if (image_format_supported($file['name']) > 0)
  1039. {
  1040. $minifile=getImageFileNameForSize($file['name'], '_mini'); // For new thumbs using same ext (in lower case howerver) than original
  1041. if (! dol_is_file($file['path'].'/'.$minifile)) $minifile=getImageFileNameForSize($file['name'], '_mini', '.png'); // For backward compatibility of old thumbs that were created with filename in lower case and with .png extension
  1042. //print $file['path'].'/'.$minifile.'<br>';
  1043. $urlforhref=getAdvancedPreviewUrl($modulepart, $relativepath.$fileinfo['filename'].'.'.strtolower($fileinfo['extension']), 0, '&entity='.(!empty($object->entity)?$object->entity:$conf->entity));
  1044. if (empty($urlforhref)) $urlforhref=DOL_URL_ROOT.'/viewimage.php?modulepart='.$modulepart.'&entity='.(!empty($object->entity)?$object->entity:$conf->entity).'&file='.urlencode($relativepath.$fileinfo['filename'].'.'.strtolower($fileinfo['extension']));
  1045. print '<a href="'.$urlforhref.'" class="aphoto" target="_blank">';
  1046. print '<img border="0" height="'.$maxheightmini.'" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$modulepart.'&entity='.(!empty($object->entity)?$object->entity:$conf->entity).'&file='.urlencode($relativepath.$minifile).'" title="">';
  1047. print '</a>';
  1048. }
  1049. else print '&nbsp;';
  1050. print '</td>';
  1051. }
  1052. if (! $editline)
  1053. {
  1054. // Delete or view link
  1055. // ($param must start with &)
  1056. print '<td class="valignmiddle right"><!-- action on files -->';
  1057. if ($useinecm)
  1058. {
  1059. print '<a href="'.DOL_URL_ROOT.'/ecm/docfile.php?urlfile='.urlencode($file['name']).$param.'" class="editfilelink" rel="'.urlencode($file['name']).'">'.img_view('default', 0, 'class="paddingrightonly"').'</a>';
  1060. }
  1061. else
  1062. {
  1063. $newmodulepart=$modulepart;
  1064. if (in_array($modulepart, array('product','produit','service'))) $newmodulepart='produit|service';
  1065. $disablecrop=1;
  1066. if (in_array($modulepart, array('product','produit','service','expensereport','holiday','project','user'))) $disablecrop=0;
  1067. if (! $disablecrop && image_format_supported($file['name']) > 0)
  1068. {
  1069. if ($permtoeditline)
  1070. {
  1071. // Link to resize
  1072. 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','class="paddingrightonly"',1).'</a>';
  1073. }
  1074. }
  1075. if ($permtoeditline)
  1076. {
  1077. print '<a href="'.(($useinecm && $useajax)?'#':$url.'?action=editfile&urlfile='.urlencode($filepath).$param).'" class="editfilelink" rel="'.$filepath.'">'.img_edit('default',0,'class="paddingrightonly"').'</a>';
  1078. }
  1079. }
  1080. if ($permonobject)
  1081. {
  1082. /*
  1083. if ($file['level1name'] <> $object->id)
  1084. $filepath=$file['level1name'].'/'.$file['name'];
  1085. else
  1086. $filepath=$file['name'];
  1087. */
  1088. $useajax=1;
  1089. if (! empty($conf->dol_use_jmobile)) $useajax=0;
  1090. if (empty($conf->use_javascript_ajax)) $useajax=0;
  1091. if (! empty($conf->global->MAIN_ECM_DISABLE_JS)) $useajax=0;
  1092. print '<a href="'.(($useinecm && $useajax)?'#':$url.'?action=delete&urlfile='.urlencode($filepath).$param).'" class="deletefilelink" rel="'.$filepath.'">'.img_delete().'</a>';
  1093. }
  1094. print "</td>";
  1095. if (empty($disablemove))
  1096. {
  1097. if ($nboffiles > 1 && empty($conf->browser->phone)) {
  1098. print '<td align="center" class="linecolmove tdlineupdown">';
  1099. if ($i > 0) {
  1100. print '<a class="lineupdown" href="'.$_SERVER["PHP_SELF"].'?id='.$this->id.'&amp;action=up&amp;rowid='.$line->id.'">'.img_up('default',0,'imgupforline').'</a>';
  1101. }
  1102. if ($i < $nboffiles-1) {
  1103. print '<a class="lineupdown" href="'.$_SERVER["PHP_SELF"].'?id='.$this->id.'&amp;action=down&amp;rowid='.$line->id.'">'.img_down('default',0,'imgdownforline').'</a>';
  1104. }
  1105. print '</td>';
  1106. }
  1107. else {
  1108. print '<td align="center"'.((empty($conf->browser->phone) && empty($disablemove)) ?' class="linecolmove tdlineupdown"':' class="linecolmove"').'>';
  1109. print '</td>';
  1110. }
  1111. }
  1112. }
  1113. else
  1114. {
  1115. print '<td class="right">';
  1116. print '<input type="submit" class="button" name="renamefilesave" value="'.dol_escape_htmltag($langs->trans("Save")).'">';
  1117. print '<input type="submit" class="button" name="cancel" value="'.dol_escape_htmltag($langs->trans("Cancel")).'">';
  1118. print '</td>';
  1119. if (empty($disablemove)) print '<td class="right"></td>';
  1120. }
  1121. print "</tr>\n";
  1122. $i++;
  1123. }
  1124. }
  1125. if ($nboffiles == 0)
  1126. {
  1127. $colspan=(empty($useinecm)?'5':'5');
  1128. if (empty($disablemove)) $colspan++;
  1129. print '<tr '.$bc[false].'><td colspan="'.$colspan.'" class="opacitymedium">';
  1130. if (empty($textifempty)) print $langs->trans("NoFileFound");
  1131. else print $textifempty;
  1132. print '</td></tr>';
  1133. }
  1134. print "</table>";
  1135. print '</div>';
  1136. if (! $editline && $nboflines > 1) {
  1137. if (! empty($conf->use_javascript_ajax) && $permtoeditline) {
  1138. $table_element_line = 'ecm_files';
  1139. include DOL_DOCUMENT_ROOT . '/core/tpl/ajaxrow.tpl.php';
  1140. }
  1141. }
  1142. if (GETPOST('action','aZ09') == 'editfile' && $permtoeditline)
  1143. {
  1144. print '</form>';
  1145. }
  1146. return $nboffiles;
  1147. }
  1148. }
  1149. /**
  1150. * Show list of documents in a directory
  1151. *
  1152. * @param string $upload_dir Directory that was scanned
  1153. * @param array $filearray Array of files loaded by dol_dir_list function before calling this function
  1154. * @param string $modulepart Value for modulepart used by download wrapper
  1155. * @param string $param Parameters on sort links
  1156. * @param int $forcedownload Force to open dialog box "Save As" when clicking on file
  1157. * @param string $relativepath Relative path of docs (autodefined if not provided)
  1158. * @param int $permtodelete Permission to delete
  1159. * @param int $useinecm Change output for use in ecm module
  1160. * @param int $textifempty Text to show if filearray is empty
  1161. * @param int $maxlength Maximum length of file name shown
  1162. * @param string $url Full url to use for click links ('' = autodetect)
  1163. * @return int <0 if KO, nb of files shown if OK
  1164. */
  1165. function list_of_autoecmfiles($upload_dir,$filearray,$modulepart,$param,$forcedownload=0,$relativepath='',$permtodelete=1,$useinecm=0,$textifempty='',$maxlength=0,$url='')
  1166. {
  1167. global $user, $conf, $langs;
  1168. global $bc;
  1169. global $sortfield, $sortorder;
  1170. dol_syslog(get_class($this).'::list_of_autoecmfiles upload_dir='.$upload_dir.' modulepart='.$modulepart);
  1171. // Show list of documents
  1172. if (empty($useinecm)) print load_fiche_titre($langs->trans("AttachedFiles"));
  1173. if (empty($url)) $url=$_SERVER["PHP_SELF"];
  1174. print '<table width="100%" class="noborder">'."\n";
  1175. print '<tr class="liste_titre">';
  1176. $sortref="fullname";
  1177. if ($modulepart == 'invoice_supplier') $sortref='level1name';
  1178. print_liste_field_titre($langs->trans("Ref"),$url,$sortref,"",$param,'align="left"',$sortfield,$sortorder);
  1179. print_liste_field_titre($langs->trans("Documents2"),$url,"name","",$param,'align="left"',$sortfield,$sortorder);
  1180. print_liste_field_titre($langs->trans("Size"),$url,"size","",$param,'align="right"',$sortfield,$sortorder);
  1181. print_liste_field_titre($langs->trans("Date"),$url,"date","",$param,'align="center"',$sortfield,$sortorder);
  1182. print_liste_field_titre('','','');
  1183. print '</tr>'."\n";
  1184. // To show ref or specific information according to view to show (defined by $module)
  1185. if ($modulepart == 'company')
  1186. {
  1187. include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  1188. $object_instance=new Societe($this->db);
  1189. }
  1190. else if ($modulepart == 'invoice')
  1191. {
  1192. include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  1193. $object_instance=new Facture($this->db);
  1194. }
  1195. else if ($modulepart == 'invoice_supplier')
  1196. {
  1197. include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  1198. $object_instance=new FactureFournisseur($this->db);
  1199. }
  1200. else if ($modulepart == 'propal')
  1201. {
  1202. include_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
  1203. $object_instance=new Propal($this->db);
  1204. }
  1205. else if ($modulepart == 'supplier_proposal')
  1206. {
  1207. include_once DOL_DOCUMENT_ROOT.'/supplier_proposal/class/supplier_proposal.class.php';
  1208. $object_instance=new SupplierProposal($this->db);
  1209. }
  1210. else if ($modulepart == 'order')
  1211. {
  1212. include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  1213. $object_instance=new Commande($this->db);
  1214. }
  1215. else if ($modulepart == 'order_supplier')
  1216. {
  1217. include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
  1218. $object_instance=new CommandeFournisseur($this->db);
  1219. }
  1220. else if ($modulepart == 'contract')
  1221. {
  1222. include_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
  1223. $object_instance=new Contrat($this->db);
  1224. }
  1225. else if ($modulepart == 'product')
  1226. {
  1227. include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  1228. $object_instance=new Product($this->db);
  1229. }
  1230. else if ($modulepart == 'tax')
  1231. {
  1232. include_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
  1233. $object_instance=new ChargeSociales($this->db);
  1234. }
  1235. else if ($modulepart == 'project')
  1236. {
  1237. include_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  1238. $object_instance=new Project($this->db);
  1239. }
  1240. else if ($modulepart == 'fichinter')
  1241. {
  1242. include_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
  1243. $object_instance=new Fichinter($this->db);
  1244. }
  1245. else if ($modulepart == 'user')
  1246. {
  1247. include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  1248. $object_instance=new User($this->db);
  1249. }
  1250. else if ($modulepart == 'expensereport')
  1251. {
  1252. include_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
  1253. $object_instance=new ExpenseReport($this->db);
  1254. }
  1255. $var=true;
  1256. foreach($filearray as $key => $file)
  1257. {
  1258. if (!is_dir($file['name'])
  1259. && $file['name'] != '.'
  1260. && $file['name'] != '..'
  1261. && $file['name'] != 'CVS'
  1262. && ! preg_match('/\.meta$/i',$file['name']))
  1263. {
  1264. // Define relative path used to store the file
  1265. $relativefile=preg_replace('/'.preg_quote($upload_dir.'/','/').'/','',$file['fullname']);
  1266. //var_dump($file);
  1267. $id=0; $ref=''; $label='';
  1268. // To show ref or specific information according to view to show (defined by $module)
  1269. if ($modulepart == 'company') { preg_match('/(\d+)\/[^\/]+$/',$relativefile,$reg); $id=(isset($reg[1])?$reg[1]:''); }
  1270. if ($modulepart == 'invoice') { preg_match('/(.*)\/[^\/]+$/',$relativefile,$reg); $ref=(isset($reg[1])?$reg[1]:''); }
  1271. 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
  1272. if ($modulepart == 'propal') { preg_match('/(.*)\/[^\/]+$/',$relativefile,$reg); $ref=(isset($reg[1])?$reg[1]:''); }
  1273. if ($modulepart == 'supplier_proposal') { preg_match('/(.*)\/[^\/]+$/',$relativefile,$reg); $ref=(isset($reg[1])?$reg[1]:''); }
  1274. if ($modulepart == 'order') { preg_match('/(.*)\/[^\/]+$/',$relativefile,$reg); $ref=(isset($reg[1])?$reg[1]:''); }
  1275. if ($modulepart == 'order_supplier') { preg_match('/(.*)\/[^\/]+$/',$relativefile,$reg); $ref=(isset($reg[1])?$reg[1]:''); }
  1276. if ($modulepart == 'contract') { preg_match('/(.*)\/[^\/]+$/',$relativefile,$reg); $ref=(isset($reg[1])?$reg[1]:''); }
  1277. if ($modulepart == 'product') { preg_match('/(.*)\/[^\/]+$/',$relativefile,$reg); $ref=(isset($reg[1])?$reg[1]:''); }
  1278. if ($modulepart == 'tax') { preg_match('/(\d+)\/[^\/]+$/',$relativefile,$reg); $id=(isset($reg[1])?$reg[1]:''); }
  1279. if ($modulepart == 'project') { preg_match('/(.*)\/[^\/]+$/',$relativefile,$reg); $ref=(isset($reg[1])?$reg[1]:'');}
  1280. if ($modulepart == 'fichinter') { preg_match('/(.*)\/[^\/]+$/',$relativefile,$reg); $ref=(isset($reg[1])?$reg[1]:'');}
  1281. if ($modulepart == 'user') { preg_match('/(.*)\/[^\/]+$/',$relativefile,$reg); $id=(isset($reg[1])?$reg[1]:'');}
  1282. if ($modulepart == 'expensereport') { preg_match('/(.*)\/[^\/]+$/',$relativefile,$reg); $id=(isset($reg[1])?$reg[1]:'');}
  1283. if (! $id && ! $ref) continue;
  1284. $found=0;
  1285. if (! empty($this->cache_objects[$modulepart.'_'.$id.'_'.$ref]))
  1286. {
  1287. $found=1;
  1288. }
  1289. else
  1290. {
  1291. //print 'Fetch '.$id." - ".$ref.'<br>';
  1292. if ($id) {
  1293. $result = $object_instance->fetch($id);
  1294. } else {
  1295. //fetchOneLike looks for objects with wildcards in its reference.
  1296. //It is useful for those masks who get underscores instead of their actual symbols
  1297. //fetchOneLike requires some info in the object. If it doesn't have it, then 0 is returned
  1298. //that's why we look only look fetchOneLike when fetch returns 0
  1299. if (!$result = $object_instance->fetch('', $ref)) {
  1300. $result = $object_instance->fetchOneLike($ref);
  1301. }
  1302. }
  1303. if ($result > 0) { // Save object into a cache
  1304. $found=1; $this->cache_objects[$modulepart.'_'.$id.'_'.$ref] = clone $object_instance;
  1305. }
  1306. if ($result == 0) { $found=1; $this->cache_objects[$modulepart.'_'.$id.'_'.$ref]='notfound'; unset($filearray[$key]); }
  1307. }
  1308. if (! $found > 0 || ! is_object($this->cache_objects[$modulepart.'_'.$id.'_'.$ref])) continue; // We do not show orphelins files
  1309. print '<!-- Line list_of_autoecmfiles '.$key.' -->'."\n";
  1310. print '<tr class="oddeven">';
  1311. print '<td>';
  1312. if ($found > 0 && is_object($this->cache_objects[$modulepart.'_'.$id.'_'.$ref])) print $this->cache_objects[$modulepart.'_'.$id.'_'.$ref]->getNomUrl(1,'document');
  1313. else print $langs->trans("ObjectDeleted",($id?$id:$ref));
  1314. $filename=dol_sanitizeFileName($ref);
  1315. //$filedir=$conf->$modulepart->dir_output . '/' . dol_sanitizeFileName($obj->ref);
  1316. $filedir=$file['path'];
  1317. //$urlsource=$_SERVER['PHP_SELF'].'?id='.$obj->rowid;
  1318. //print $formfile->getDocumentsLink($modulepart, $filename, $filedir);
  1319. print '</td>';
  1320. print '<td>';
  1321. //print "XX".$file['name']; //$file['name'] must be utf8
  1322. print '<a data-ajax="false" href="'.DOL_URL_ROOT.'/document.php?modulepart='.$modulepart;
  1323. if ($forcedownload) print '&attachment=1';
  1324. print '&file='.urlencode($relativefile).'">';
  1325. print img_mime($file['name'],$file['name'].' ('.dol_print_size($file['size'],0,0).')').' ';
  1326. print dol_trunc($file['name'],$maxlength,'middle');
  1327. print '</a>';
  1328. print $this->getDocumentsLink($modulepart, $filename, $filedir);
  1329. print "</td>\n";
  1330. print '<td align="right">'.dol_print_size($file['size'],1,1).'</td>';
  1331. print '<td align="center">'.dol_print_date($file['date'],"dayhour").'</td>';
  1332. print '<td align="right">';
  1333. //if (! empty($useinecm)) print '<a data-ajax="false" href="'.DOL_URL_ROOT.'/document.php?modulepart='.$modulepart;
  1334. //if ($forcedownload) print '&attachment=1';
  1335. //print '&file='.urlencode($relativefile).'">';
  1336. //print img_view().'</a> &nbsp; ';
  1337. //if ($permtodelete) print '<a href="'.$url.'?id='.$object->id.'&section='.$_REQUEST["section"].'&action=delete&urlfile='.urlencode($file['name']).'">'.img_delete().'</a>';
  1338. //else print '&nbsp;';
  1339. print "</td></tr>\n";
  1340. }
  1341. }
  1342. if (count($filearray) == 0)
  1343. {
  1344. print '<tr '.$bc[false].'><td colspan="4">';
  1345. if (empty($textifempty)) print $langs->trans("NoFileFound");
  1346. else print $textifempty;
  1347. print '</td></tr>';
  1348. }
  1349. print "</table>";
  1350. // Fin de zone
  1351. }
  1352. /**
  1353. * Show form to upload a new file with jquery fileupload.
  1354. * This form use the fileupload.php file.
  1355. *
  1356. * @param Object $object Object to use
  1357. * @return void
  1358. */
  1359. private function _formAjaxFileUpload($object)
  1360. {
  1361. global $langs;
  1362. // PHP post_max_size
  1363. $post_max_size = ini_get('post_max_size');
  1364. $mul_post_max_size = substr($post_max_size, -1);
  1365. $mul_post_max_size = ($mul_post_max_size == 'M' ? 1048576 : ($mul_post_max_size == 'K' ? 1024 : ($mul_post_max_size == 'G' ? 1073741824 : 1)));
  1366. $post_max_size = $mul_post_max_size * (int) $post_max_size;
  1367. // PHP upload_max_filesize
  1368. $upload_max_filesize = ini_get('upload_max_filesize');
  1369. $mul_upload_max_filesize = substr($upload_max_filesize, -1);
  1370. $mul_upload_max_filesize = ($mul_upload_max_filesize == 'M' ? 1048576 : ($mul_upload_max_filesize == 'K' ? 1024 : ($mul_upload_max_filesize == 'G' ? 1073741824 : 1)));
  1371. $upload_max_filesize = $mul_upload_max_filesize * (int) $upload_max_filesize;
  1372. // Max file size
  1373. $max_file_size = (($post_max_size < $upload_max_filesize) ? $post_max_size : $upload_max_filesize);
  1374. // Include main
  1375. include DOL_DOCUMENT_ROOT.'/core/tpl/ajax/fileupload_main.tpl.php';
  1376. // Include template
  1377. include DOL_DOCUMENT_ROOT.'/core/tpl/ajax/fileupload_view.tpl.php';
  1378. }
  1379. /**
  1380. * Show array with linked files
  1381. *
  1382. * @param Object $object Object
  1383. * @param int $permtodelete Deletion is allowed
  1384. * @param string $action Action
  1385. * @param string $selected ???
  1386. * @param string $param More param to add into URL
  1387. * @return int Number of links
  1388. */
  1389. public function listOfLinks($object, $permtodelete=1, $action=null, $selected=null, $param='')
  1390. {
  1391. global $user, $conf, $langs, $user;
  1392. global $bc;
  1393. global $sortfield, $sortorder;
  1394. $langs->load("link");
  1395. require_once DOL_DOCUMENT_ROOT . '/core/class/link.class.php';
  1396. $link = new Link($this->db);
  1397. $links = array();
  1398. if ($sortfield == "name") {
  1399. $sortfield = "label";
  1400. } elseif ($sortfield == "date") {
  1401. $sortfield = "datea";
  1402. } else {
  1403. $sortfield = null;
  1404. }
  1405. $res = $link->fetchAll($links, $object->element, $object->id, $sortfield, $sortorder);
  1406. $param .= (isset($object->id)?'&id=' . $object->id : '');
  1407. // Show list of associated links
  1408. print load_fiche_titre($langs->trans("LinkedFiles"));
  1409. print '<form action="' . $_SERVER['PHP_SELF'] . ($param?'?'.$param:'') . '" method="POST">';
  1410. print '<table width="100%" class="liste">';
  1411. print '<tr class="liste_titre">';
  1412. print_liste_field_titre(
  1413. $langs->trans("Links"),
  1414. $_SERVER['PHP_SELF'],
  1415. "name",
  1416. "",
  1417. $param,
  1418. 'align="left"',
  1419. $sortfield,
  1420. $sortorder
  1421. );
  1422. print_liste_field_titre(
  1423. "",
  1424. "",
  1425. "",
  1426. "",
  1427. "",
  1428. 'align="right"'
  1429. );
  1430. print_liste_field_titre(
  1431. $langs->trans("Date"),
  1432. $_SERVER['PHP_SELF'],
  1433. "date",
  1434. "",
  1435. $param,
  1436. 'align="center"',
  1437. $sortfield,
  1438. $sortorder
  1439. );
  1440. print_liste_field_titre(
  1441. '',
  1442. $_SERVER['PHP_SELF'],
  1443. "",
  1444. "",
  1445. $param,
  1446. 'align="center"'
  1447. );
  1448. print_liste_field_titre('','','');
  1449. print '</tr>';
  1450. $nboflinks = count($links);
  1451. if ($nboflinks > 0) include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
  1452. $var = true;
  1453. foreach ($links as $link)
  1454. {
  1455. $var =! $var;
  1456. print '<tr ' . $bc[$var] . '>';
  1457. //edit mode
  1458. if ($action == 'update' && $selected === $link->id)
  1459. {
  1460. print '<td>';
  1461. print '<input type="hidden" name="id" value="' . $object->id . '">';
  1462. print '<input type="hidden" name="linkid" value="' . $link->id . '">';
  1463. print '<input type="hidden" name="action" value="confirm_updateline">';
  1464. print $langs->trans('Link') . ': <input type="text" name="link" value="' . $link->url . '">';
  1465. print '</td>';
  1466. print '<td>';
  1467. print $langs->trans('Label') . ': <input type="text" name="label" value="' . $link->label . '">';
  1468. print '</td>';
  1469. print '<td align="center">' . dol_print_date(dol_now(), "dayhour", "tzuser") . '</td>';
  1470. print '<td align="right"></td>';
  1471. print '<td align="right">';
  1472. print '<input type="submit" name="save" class="button" value="' . dol_escape_htmltag($langs->trans('Save')) . '">';
  1473. print '<input type="submit" name="cancel" class="button" value="' . dol_escape_htmltag($langs->trans('Cancel')) . '">';
  1474. print '</td>';
  1475. }
  1476. else
  1477. {
  1478. print '<td>';
  1479. print img_picto('', 'object_globe').' ';
  1480. print '<a data-ajax="false" href="' . $link->url . '" target="_blank">';
  1481. print $link->label;
  1482. print '</a>';
  1483. print '</td>'."\n";
  1484. print '<td align="right"></td>';
  1485. print '<td align="center">' . dol_print_date($link->datea, "dayhour", "tzuser") . '</td>';
  1486. print '<td align="center"></td>';
  1487. print '<td align="right">';
  1488. print '<a href="' . $_SERVER['PHP_SELF'] . '?action=update&linkid=' . $link->id . $param . '" class="editfilelink" >' . img_edit() . '</a>'; // id= is included into $param
  1489. if ($permtodelete) {
  1490. print ' &nbsp; <a href="'. $_SERVER['PHP_SELF'] .'?action=delete&linkid=' . $link->id . $param . '" class="deletefilelink">' . img_delete() . '</a>'; // id= is included into $param
  1491. } else {
  1492. print '&nbsp;';
  1493. }
  1494. print '</td>';
  1495. }
  1496. print "</tr>\n";
  1497. }
  1498. if ($nboflinks == 0)
  1499. {
  1500. print '<tr ' . $bc[false] . '><td colspan="5" class="opacitymedium">';
  1501. print $langs->trans("NoLinkFound");
  1502. print '</td></tr>';
  1503. }
  1504. print "</table>";
  1505. print '</form>';
  1506. return $nboflinks;
  1507. }
  1508. /**
  1509. * Show detail icon with link for preview
  1510. *
  1511. * @param array $file File
  1512. * @param string $modulepart propal, facture, facture_fourn, ...
  1513. * @param string $relativepath Relative path of docs
  1514. * @param string $ruleforpicto Rule for picto: 0=Preview picto, 1=Use picto of mime type of file)
  1515. * @param string $param More param on http links
  1516. * @return string $out Output string with HTML
  1517. */
  1518. public function showPreview($file, $modulepart, $relativepath, $ruleforpicto=0, $param='')
  1519. {
  1520. global $langs, $conf;
  1521. $out='';
  1522. if ($conf->browser->layout != 'phone' && ! empty($conf->use_javascript_ajax))
  1523. {
  1524. $urladvancedpreview=getAdvancedPreviewUrl($modulepart, $relativepath, 1, $param); // Return if a file is qualified for preview.
  1525. if (count($urladvancedpreview))
  1526. {
  1527. $out.= '<a class="pictopreview '.$urladvancedpreview['css'].'" href="'.$urladvancedpreview['url'].'"'.(empty($urladvancedpreview['mime'])?'':' mime="'.$urladvancedpreview['mime'].'"').' '.(empty($urladvancedpreview['target'])?'':' target="'.$urladvancedpreview['target'].'"').'>';
  1528. //$out.= '<a class="pictopreview">';
  1529. if (empty($ruleforpicto)) $out.= img_picto($langs->trans('Preview').' '.$file['name'], 'detail');
  1530. else $out.= img_mime($relativepath, $langs->trans('Preview').' '.$file['name']);
  1531. $out.= '</a>';
  1532. }
  1533. }
  1534. return $out;
  1535. }
  1536. }