From 767f07d02e1b6466f0574517426a38c70625c933 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 11 Nov 2020 22:47:06 +0100 Subject: [PATCH] Debug function to show list of files in ECM module --- htdocs/core/class/html.formfile.class.php | 54 ++++++++++++++++++----- htdocs/core/lib/files.lib.php | 23 ++++++---- 2 files changed, 58 insertions(+), 19 deletions(-) diff --git a/htdocs/core/class/html.formfile.class.php b/htdocs/core/class/html.formfile.class.php index c41c61be536..0381a031a7d 100644 --- a/htdocs/core/class/html.formfile.class.php +++ b/htdocs/core/class/html.formfile.class.php @@ -1035,10 +1035,9 @@ class FormFile public 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, $addfilterfields = 0, $disablecrop = -1) { // phpcs:enable - global $user, $conf, $langs, $hookmanager; + global $user, $conf, $langs, $hookmanager, $form; global $sortfield, $sortorder, $maxheightmini; global $dolibarr_main_url_root; - global $form; if ($disablecrop == -1) { @@ -1407,9 +1406,9 @@ class FormFile // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** - * Show list of documents in a directory + * Show list of documents in a directory of ECM module. * - * @param string $upload_dir Directory that was scanned + * @param string $upload_dir Directory that was scanned. This directory will contains files into subdirs REF/files * @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 @@ -1427,9 +1426,10 @@ class FormFile public function list_of_autoecmfiles($upload_dir, $filearray, $modulepart, $param, $forcedownload = 0, $relativepath = '', $permissiontodelete = 1, $useinecm = 0, $textifempty = '', $maxlength = 0, $url = '', $addfilterfields = 0) { // phpcs:enable - global $user, $conf, $langs, $form; + global $user, $conf, $langs, $hookmanager, $form; global $sortfield, $sortorder; global $search_doc_ref; + global $dolibarr_main_url_root; dol_syslog(get_class($this).'::list_of_autoecmfiles upload_dir='.$upload_dir.' modulepart='.$modulepart); @@ -1547,6 +1547,23 @@ class FormFile $object_instance = new Mo($this->db); } + //var_dump($filearray); + + // Get list of files stored into database for same relative directory + $relativepathfromroot = preg_replace('/'.preg_quote(DOL_DATA_ROOT.'/', '/').'/', '', $upload_dir); + if ($relativepathfromroot) + { + completeFileArrayWithDatabaseInfo($filearray, $relativepathfromroot.'/%'); + + //var_dump($sortfield.' - '.$sortorder); + if ($sortfield && $sortorder) // If $sortfield is for example 'position_name', we will sort on the property 'position_name' (that is concat of position+name) + { + $filearray = dol_sort_array($filearray, $sortfield, $sortorder); + } + } + + //var_dump($filearray); + foreach ($filearray as $key => $file) { if (!is_dir($file['name']) @@ -1558,7 +1575,7 @@ class FormFile // Define relative path used to store the file $relativefile = preg_replace('/'.preg_quote($upload_dir.'/', '/').'/', '', $file['fullname']); - $id = 0; $ref = ''; $label = ''; + $id = 0; $ref = ''; // To show ref or specific information according to view to show (defined by $module) $reg = array(); @@ -1590,7 +1607,7 @@ class FormFile { $found = 1; } else { - //print 'Fetch '.$id." - ".$ref.'
'; + //print 'Fetch '.$id." - ".$ref.' class='.get_class($object_instance).'
'; if ($id) { $result = $object_instance->fetch($id); @@ -1619,13 +1636,12 @@ class FormFile else print $langs->trans("ObjectDeleted", ($id ? $id : $ref)); //$modulesubdir=dol_sanitizeFileName($ref); - $modulesubdir = dirname($relativefile); + //$modulesubdir = dirname($relativefile); //$filedir=$conf->$modulepart->dir_output . '/' . dol_sanitizeFileName($obj->ref); - $filedir = $file['path']; + //$filedir = $file['path']; //$urlsource=$_SERVER['PHP_SELF'].'?id='.$obj->rowid; //print $formfile->getDocumentsLink($modulepart, $filename, $filedir); - print ''; // File @@ -1639,12 +1655,30 @@ class FormFile print ''; //print $this->getDocumentsLink($modulepart, $modulesubdir, $filedir, '^'.preg_quote($file['name'],'/').'$'); + print $this->showPreview($file, $modulepart, $file['relativename']); print "\n"; print ''.dol_print_size($file['size'], 1, 1).''; print ''.dol_print_date($file['date'], "dayhour").''; print ''; + if ($file['share']) { + // Define $urlwithroot + $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root)); + $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file + //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current + + //print ''.$langs->trans("Hash").' : '.$file['share'].''; + $forcedownload = 0; + $paramlink = ''; + if (!empty($file['share'])) $paramlink .= ($paramlink ? '&' : '').'hashp='.$file['share']; // Hash for public share + if ($forcedownload) $paramlink .= ($paramlink ? '&' : '').'attachment=1'; + + $fulllink = $urlwithroot.'/document.php'.($paramlink ? '?'.$paramlink : ''); + + print img_picto($langs->trans("FileSharedViaALink"), 'globe').' '; + print ''; + } //if (! empty($useinecm) && $useinecm != 6) print ''; diff --git a/htdocs/core/lib/files.lib.php b/htdocs/core/lib/files.lib.php index dd74ce70a64..0be1cfbc3bc 100644 --- a/htdocs/core/lib/files.lib.php +++ b/htdocs/core/lib/files.lib.php @@ -228,8 +228,12 @@ function dol_dir_list_in_database($path, $filter = "", $excludefilter = null, $s $sql .= " date_c, tms as date_m, fk_user_c, fk_user_m, acl, position, share"; if ($mode) $sql .= ", description"; $sql .= " FROM ".MAIN_DB_PREFIX."ecm_files"; - $sql .= " WHERE filepath = '".$db->escape($path)."'"; - $sql .= " AND entity = ".$conf->entity; + $sql .= " WHERE entity = ".$conf->entity; + if (preg_match('/%$/', $path)) { + $sql .= " AND filepath LIKE '".$db->escape($path)."'"; + } else { + $sql .= " AND filepath = '".$db->escape($path)."'"; + } $resql = $db->query($sql); if ($resql) @@ -242,6 +246,7 @@ function dol_dir_list_in_database($path, $filter = "", $excludefilter = null, $s $obj = $db->fetch_object($resql); if ($obj) { + $reg = array(); preg_match('/([^\/]+)\/[^\/]+$/', DOL_DATA_ROOT.'/'.$obj->filepath.'/'.$obj->filename, $reg); $level1name = (isset($reg[1]) ? $reg[1] : ''); $file_list[] = array( @@ -289,7 +294,7 @@ function dol_dir_list_in_database($path, $filter = "", $excludefilter = null, $s * Complete $filearray with data from database. * This will call doldir_list_indatabase to complate filearray. * - * @param array $filearray Array of files get using dol_dir_list + * @param array $filearray Array of files obtained using dol_dir_list * @param string $relativedir Relative dir from DOL_DATA_ROOT * @return void */ @@ -315,19 +320,19 @@ function completeFileArrayWithDatabaseInfo(&$filearray, $relativedir) } } - //var_dump($filearray); - //var_dump($filearrayindatabase); + /*var_dump($relativedir); + var_dump($filearray); + var_dump($filearrayindatabase);*/ // Complete filearray with properties found into $filearrayindatabase foreach ($filearray as $key => $val) { $tmpfilename = preg_replace('/\.noexe$/', '', $filearray[$key]['name']); - $found = 0; // Search if it exists into $filearrayindatabase foreach ($filearrayindatabase as $key2 => $val2) { - if ($filearrayindatabase[$key2]['name'] == $tmpfilename) + if (($filearrayindatabase[$key2]['path'] == $filearray[$key]['path']) && ($filearrayindatabase[$key2]['name'] == $tmpfilename)) { $filearray[$key]['position_name'] = ($filearrayindatabase[$key2]['position'] ? $filearrayindatabase[$key2]['position'] : '0').'_'.$filearrayindatabase[$key2]['name']; $filearray[$key]['position'] = $filearrayindatabase[$key2]['position']; @@ -348,6 +353,7 @@ function completeFileArrayWithDatabaseInfo(&$filearray, $relativedir) $filearray[$key]['acl'] = ''; $rel_filename = preg_replace('/^'.preg_quote(DOL_DATA_ROOT, '/').'/', '', $filearray[$key]['fullname']); + if (!preg_match('/([\\/]temp[\\/]|[\\/]thumbs|\.meta$)/', $rel_filename)) // If not a tmp file { dol_syslog("list_of_documents We found a file called '".$filearray[$key]['name']."' not indexed into database. We add it"); @@ -379,8 +385,7 @@ function completeFileArrayWithDatabaseInfo(&$filearray, $relativedir) } } } - - /*var_dump($filearray);*/ + //var_dump($filearray); var_dump($relativedir.' - tmpfilename='.$tmpfilename.' - found='.$found); }