/**
  *	Show list of documents in a directory
  *
  *  @param	string	$upload_dir         Directory that was scanned
  *  @param  array	$filearray          Array of files loaded by dol_dir_list function before calling this function
  *  @param  string	$modulepart         Value for modulepart used by download wrapper
  *  @param  string	$param              Parameters on sort links
  *  @param  int		$forcedownload      Force to open dialog box "Save As" when clicking on file
  *  @param  string	$relativepath       Relative path of docs (autodefined if not provided)
  *  @param  int		$permtodelete       Permission to delete
  *  @param  int		$useinecm           Change output for use in ecm module
  *  @param  int		$textifempty        Text to show if filearray is empty
  *  @param  int		$maxlength          Maximum length of file name shown
  *  @param	string $url				Full url to use for click links ('' = autodetect)
  *  @return int                 		<0 if KO, nb of files shown if OK
  */
 function list_of_autoecmfiles($upload_dir, $filearray, $modulepart, $param, $forcedownload = 0, $relativepath = '', $permtodelete = 1, $useinecm = 0, $textifempty = '', $maxlength = 0, $url = '')
 {
     global $user, $conf, $langs;
     global $bc;
     global $sortfield, $sortorder;
     dol_syslog(get_class($this) . '::list_of_autoecmfiles upload_dir=' . $upload_dir . ' modulepart=' . $modulepart);
     // Show list of documents
     if (empty($useinecm)) {
         print load_fiche_titre($langs->trans("AttachedFiles"));
     }
     if (empty($url)) {
         $url = $_SERVER["PHP_SELF"];
     }
     print '<table width="100%" class="nobordernopadding">';
     print '<tr class="liste_titre">';
     $sortref = "fullname";
     if ($modulepart == 'invoice_supplier') {
         $sortref = 'level1name';
     }
     print_liste_field_titre($langs->trans("Ref"), $url, $sortref, "", $param, 'align="left"', $sortfield, $sortorder);
     print_liste_field_titre($langs->trans("Documents2"), $url, "name", "", $param, 'align="left"', $sortfield, $sortorder);
     print_liste_field_titre($langs->trans("Size"), $url, "size", "", $param, 'align="right"', $sortfield, $sortorder);
     print_liste_field_titre($langs->trans("Date"), $url, "date", "", $param, 'align="center"', $sortfield, $sortorder);
     print_liste_field_titre('', '', '');
     print '</tr>';
     // To show ref or specific information according to view to show (defined by $module)
     if ($modulepart == 'company') {
         include_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
         $object_instance = new Societe($this->db);
     } else {
         if ($modulepart == 'invoice') {
             include_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php';
             $object_instance = new Facture($this->db);
         } else {
             if ($modulepart == 'invoice_supplier') {
                 include_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php';
                 $object_instance = new FactureFournisseur($this->db);
             } else {
                 if ($modulepart == 'propal') {
                     include_once DOL_DOCUMENT_ROOT . '/comm/propal/class/propal.class.php';
                     $object_instance = new Propal($this->db);
                 } else {
                     if ($modulepart == 'supplier_proposal') {
                         include_once DOL_DOCUMENT_ROOT . '/supplier_proposal/class/supplier_proposal.class.php';
                         $object_instance = new SupplierProposal($this->db);
                     } else {
                         if ($modulepart == 'order') {
                             include_once DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php';
                             $object_instance = new Commande($this->db);
                         } else {
                             if ($modulepart == 'order_supplier') {
                                 include_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.commande.class.php';
                                 $object_instance = new CommandeFournisseur($this->db);
                             } else {
                                 if ($modulepart == 'contract') {
                                     include_once DOL_DOCUMENT_ROOT . '/contrat/class/contrat.class.php';
                                     $object_instance = new Contrat($this->db);
                                 } else {
                                     if ($modulepart == 'product') {
                                         include_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
                                         $object_instance = new Product($this->db);
                                     } else {
                                         if ($modulepart == 'tax') {
                                             include_once DOL_DOCUMENT_ROOT . '/compta/sociales/class/chargesociales.class.php';
                                             $object_instance = new ChargeSociales($this->db);
                                         } else {
                                             if ($modulepart == 'project') {
                                                 include_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php';
                                                 $object_instance = new Project($this->db);
                                             } else {
                                                 if ($modulepart == 'fichinter') {
                                                     include_once DOL_DOCUMENT_ROOT . '/fichinter/class/fichinter.class.php';
                                                     $object_instance = new Fichinter($this->db);
                                                 } else {
                                                     if ($modulepart == 'user') {
                                                         include_once DOL_DOCUMENT_ROOT . '/user/class/user.class.php';
                                                         $object_instance = new User($this->db);
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     $var = true;
     foreach ($filearray as $key => $file) {
         if (!is_dir($file['name']) && $file['name'] != '.' && $file['name'] != '..' && $file['name'] != 'CVS' && !preg_match('/\\.meta$/i', $file['name'])) {
             // Define relative path used to store the file
             $relativefile = preg_replace('/' . preg_quote($upload_dir . '/', '/') . '/', '', $file['fullname']);
             //var_dump($file);
             $id = 0;
             $ref = '';
             $label = '';
             // To show ref or specific information according to view to show (defined by $module)
             if ($modulepart == 'company') {
                 preg_match('/(\\d+)\\/[^\\/]+$/', $relativefile, $reg);
                 $id = isset($reg[1]) ? $reg[1] : '';
             }
             if ($modulepart == 'invoice') {
                 preg_match('/(.*)\\/[^\\/]+$/', $relativefile, $reg);
                 $ref = isset($reg[1]) ? $reg[1] : '';
             }
             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
             if ($modulepart == 'propal') {
                 preg_match('/(.*)\\/[^\\/]+$/', $relativefile, $reg);
                 $ref = isset($reg[1]) ? $reg[1] : '';
             }
             if ($modulepart == 'supplier_proposal') {
                 preg_match('/(.*)\\/[^\\/]+$/', $relativefile, $reg);
                 $ref = isset($reg[1]) ? $reg[1] : '';
             }
             if ($modulepart == 'order') {
                 preg_match('/(.*)\\/[^\\/]+$/', $relativefile, $reg);
                 $ref = isset($reg[1]) ? $reg[1] : '';
             }
             if ($modulepart == 'order_supplier') {
                 preg_match('/(.*)\\/[^\\/]+$/', $relativefile, $reg);
                 $ref = isset($reg[1]) ? $reg[1] : '';
             }
             if ($modulepart == 'contract') {
                 preg_match('/(.*)\\/[^\\/]+$/', $relativefile, $reg);
                 $ref = isset($reg[1]) ? $reg[1] : '';
             }
             if ($modulepart == 'product') {
                 preg_match('/(.*)\\/[^\\/]+$/', $relativefile, $reg);
                 $ref = isset($reg[1]) ? $reg[1] : '';
             }
             if ($modulepart == 'tax') {
                 preg_match('/(\\d+)\\/[^\\/]+$/', $relativefile, $reg);
                 $id = isset($reg[1]) ? $reg[1] : '';
             }
             if ($modulepart == 'project') {
                 preg_match('/(.*)\\/[^\\/]+$/', $relativefile, $reg);
                 $ref = isset($reg[1]) ? $reg[1] : '';
             }
             if ($modulepart == 'fichinter') {
                 preg_match('/(.*)\\/[^\\/]+$/', $relativefile, $reg);
                 $ref = isset($reg[1]) ? $reg[1] : '';
             }
             if ($modulepart == 'user') {
                 preg_match('/(.*)\\/[^\\/]+$/', $relativefile, $reg);
                 $id = isset($reg[1]) ? $reg[1] : '';
             }
             if (!$id && !$ref) {
                 continue;
             }
             $found = 0;
             if (!empty($this->cache_objects[$modulepart . '_' . $id . '_' . $ref])) {
                 $found = 1;
             } else {
                 //print 'Fetch '.$id." - ".$ref.'<br>';
                 if ($id) {
                     $result = $object_instance->fetch($id);
                 } else {
                     //fetchOneLike looks for objects with wildcards in its reference.
                     //It is useful for those masks who get underscores instead of their actual symbols
                     //fetchOneLike requires some info in the object. If it doesn't have it, then 0 is returned
                     //that's why we look only look fetchOneLike when fetch returns 0
                     if (!($result = $object_instance->fetch('', $ref))) {
                         $result = $object_instance->fetchOneLike($ref);
                     }
                 }
                 if ($result > 0) {
                     // Save object into a cache
                     $found = 1;
                     $this->cache_objects[$modulepart . '_' . $id . '_' . $ref] = clone $object_instance;
                 }
                 if ($result == 0) {
                     $found = 1;
                     $this->cache_objects[$modulepart . '_' . $id . '_' . $ref] = 'notfound';
                     unset($filearray[$key]);
                 }
             }
             if (!$found > 0 || !is_object($this->cache_objects[$modulepart . '_' . $id . '_' . $ref])) {
                 continue;
             }
             // We do not show orphelins files
             $var = !$var;
             print '<tr ' . $bc[$var] . '>';
             print '<td>';
             if ($found > 0 && is_object($this->cache_objects[$modulepart . '_' . $id . '_' . $ref])) {
                 print $this->cache_objects[$modulepart . '_' . $id . '_' . $ref]->getNomUrl(1, 'document');
             } else {
                 print $langs->trans("ObjectDeleted", $id ? $id : $ref);
             }
             print '</td>';
             print '<td>';
             //print "XX".$file['name']; //$file['name'] must be utf8
             print '<a data-ajax="false" href="' . DOL_URL_ROOT . '/document.php?modulepart=' . $modulepart;
             if ($forcedownload) {
                 print '&attachment=1';
             }
             print '&file=' . urlencode($relativefile) . '">';
             print img_mime($file['name'], $file['name'] . ' (' . dol_print_size($file['size'], 0, 0) . ')') . ' ';
             print dol_trunc($file['name'], $maxlength, 'middle');
             print '</a>';
             print "</td>\n";
             print '<td align="right">' . dol_print_size($file['size'], 1, 1) . '</td>';
             print '<td align="center">' . dol_print_date($file['date'], "dayhour") . '</td>';
             print '<td align="right">';
             if (!empty($useinecm)) {
                 print '<a data-ajax="false" href="' . DOL_URL_ROOT . '/document.php?modulepart=' . $modulepart;
             }
             if ($forcedownload) {
                 print '&attachment=1';
             }
             print '&file=' . urlencode($relativefile) . '">';
             print img_view() . '</a> &nbsp; ';
             //if ($permtodelete) print '<a href="'.$url.'?id='.$object->id.'&section='.$_REQUEST["section"].'&action=delete&urlfile='.urlencode($file['name']).'">'.img_delete().'</a>';
             //else print '&nbsp;';
             print "</td></tr>\n";
         }
     }
     if (count($filearray) == 0) {
         print '<tr ' . $bc[false] . '><td colspan="4">';
         if (empty($textifempty)) {
             print $langs->trans("NoFileFound");
         } else {
             print $textifempty;
         }
         print '</td></tr>';
     }
     print "</table>";
     // Fin de zone
 }