parseInt(e.max))&&(e.value=e.max)},window.saturne.utils.checkMoreParams=function(e){e&&e.removeAttr&&$(e.removeAttr.element).removeAttr(e.removeAttr.value)},window.saturne.utils.toggleSetting=function(e,t){var n=$(this),a=window.saturne.toolbox.getQuerySeparator(document.URL),o=window.saturne.toolbox.getToken(),i=n.hasClass("fa-toggle-off")?1:0;let r=n.data("update-targets")?.split(",")||[];window.saturne.loader.display(n),$.ajax({url:""+document.URL+a+`action=${e}&token=`+o,type:"POST",processData:!1,data:JSON.stringify({[t]:i}),contentType:!1,success:function(n){r.forEach(e=>{var t=$(n).find(e);t.length&&$(e).replaceWith(t)})},error:function(){}})};
\ No newline at end of file
diff --git a/lib/documents.lib.php b/lib/documents.lib.php
index c16df38b..92b1b6bd 100644
--- a/lib/documents.lib.php
+++ b/lib/documents.lib.php
@@ -46,9 +46,11 @@
* @param string $removeaction (optional) The action to remove a file
* @param int $active (optional) To show gen button disabled
* @param string $tooltiptext (optional) Tooltip text when gen button disabled
+ * @param string $sortfield (optional) Allows to sort the list of files by a field
+ * @param string $sortorder (optional) Allows to sort the list of files with a specific order
* @return string Output string with HTML array of documents (might be empty string)
*/
-function saturne_show_documents(string $modulepart, $modulesubdir, $filedir, string $urlsource, $genallowed, int $delallowed = 0, string $modelselected = '', int $allowgenifempty = 1, int $forcenomultilang = 0, int $notused = 0, int $noform = 0, string $param = '', string $title = '', string $buttonlabel = '', string $codelang = '', string $morepicto = '', $object = null, int $hideifempty = 0, string $removeaction = 'remove_file', int $active = 1, string $tooltiptext = ''): string
+function saturne_show_documents(string $modulepart, $modulesubdir, $filedir, string $urlsource, $genallowed, int $delallowed = 0, string $modelselected = '', int $allowgenifempty = 1, int $forcenomultilang = 0, int $notused = 0, int $noform = 0, string $param = '', string $title = '', string $buttonlabel = '', string $codelang = '', string $morepicto = '', $object = null, int $hideifempty = 0, string $removeaction = 'remove_file', int $active = 1, string $tooltiptext = '', string $sortfield = '', string $sortorder = ''): string
{
global $conf, $db, $form, $hookmanager, $langs;
@@ -63,6 +65,23 @@ function saturne_show_documents(string $modulepart, $modulesubdir, $filedir, str
$param .= ($param ? '&' : '') . 'entity=' . (!empty($object->entity) ? $object->entity : $conf->entity);
}
+ if (empty($sortfield)) {
+ if (GETPOST('sortfield')) {
+ $sortfield = GETPOST('sortfield');
+ } else {
+ $sortfield = 'name';
+ }
+ }
+
+
+ if (empty($sortorder)) {
+ if (GETPOST('sortorder')) {
+ $sortorder = GETPOST('sortorder');
+ } else {
+ $sortorder = 'desc';
+ }
+ }
+
$hookmanager->initHooks(['formfile']);
// Get list of files
@@ -75,7 +94,37 @@ function saturne_show_documents(string $modulepart, $modulesubdir, $filedir, str
} else {
$fileList = dol_dir_list($filedir, 'files', 0, '(\.jpg|\.jpeg|\.png|\.odt|\.zip|\.pdf)', '', 'date', SORT_DESC, 1);
}
- }
+ }
+
+ if (GETPOST('search_name')) {
+ $fileList = array_filter($fileList, function($file) {
+ return strpos($file['name'], GETPOST('search_name')) !== false;
+ });
+ }
+
+ if (GETPOST('search_date')) {
+ $search_date = GETPOST('search_date');
+ $fileList = array_filter($fileList, function($file) use ($search_date) {
+ $file_date = date('Y-m-d', $file['date']);
+ return $file_date === $search_date;
+ });
+ }
+
+
+ $fileList = dol_sort_array($fileList, $sortfield, $sortorder);
+
+ $page = GETPOST('page', 'int') ?: 1;
+ $filePerPage = 20;
+ $fileListLength = 0;
+ if (is_array($fileList) && !empty($fileList)) {
+ $fileListLength = count($fileList);
+ }
+
+ if ($fileListLength > $filePerPage) {
+ $fileList = array_slice($fileList, ($page - 1 ) * $filePerPage, $filePerPage);
+ }
+
+ $pageNumber = ceil($fileListLength / $filePerPage);
if ($hideifempty && empty($fileList)) {
return '';
@@ -215,6 +264,19 @@ function saturne_show_documents(string $modulepart, $modulesubdir, $filedir, str
$genbutton = '';
}
$out .= $genbutton;
+
+ $out .= '';
} else {
$out .= '' . $langs->trans('Files') . '
';
}
@@ -240,6 +302,25 @@ function saturne_show_documents(string $modulepart, $modulesubdir, $filedir, str
$out .= ' | ';
}
$out .= '';
+ $out .= '';
+ $out .= get_document_title_search('text', 'Name');
+ $out .= ' | ';
+ $out .= get_document_title_search('date', 'Date', 'right');
+ $out .= ' | ';
+ $out .= '';
+ $out .= '';
+ $out .= ' ';
+ $out .= '';
+ $out .= ' | ';
+
+ $out .= '
';
+ $out .= '';
+ $out .= get_document_title_field($sortfield, $sortorder, 'Name');
+ $out .= get_document_title_field($sortfield, $sortorder, 'Size', true, 'right');
+ $out .= get_document_title_field($sortfield, $sortorder, 'Date', true, 'right');
+ $out .= get_document_title_field($sortfield, $sortorder, 'PDF', false, 'right');
+ $out .= get_document_title_field($sortfield, $sortorder, 'Action', false, 'right');
+ $out .= '
';
// Execute hooks
$parameters = ['colspan' => ($colspan + $colspanmore), 'socid' => ($GLOBALS['socid'] ?? ''), 'id' => ($GLOBALS['id'] ?? ''), 'modulepart' => $modulepart];
@@ -258,7 +339,6 @@ function saturne_show_documents(string $modulepart, $modulesubdir, $filedir, str
if (is_object($object) && $object->id > 0) {
require_once DOL_DOCUMENT_ROOT . '/core/class/link.class.php';
$link = new Link($db);
- $sortfield = $sortorder = null;
$link->fetchAll($link_list, $object->element, $object->id, $sortfield, $sortorder);
}
@@ -412,6 +492,53 @@ function saturne_show_documents(string $modulepart, $modulesubdir, $filedir, str
return $out;
}
+/**
+ * Get document title field
+ *
+ * @param string $sortfield
+ * @param string $sortorder
+ * @param string $name
+ * @param bool $sortable
+ * @param string $morehtml
+ * @return string
+ */
+function get_document_title_field(string $sortfield, string $sortorder, string $name, bool $sortable = true, string $morehtml = ''): string {
+ global $langs;
+
+ $querySeparator = (strpos($_SERVER['PHP_SELF'], '?') === false) ? '?' : '&';
+ $out = '';
+ if ($sortable) {
+ $out .= '';
+ $out .= ($sortfield == strtolower($name) ? '' : '');
+ }
+ $out .= $langs->trans($name);
+ if ($sortable) {
+ $out .= ' ' . ($sortfield == strtolower($name) ? ($sortorder == 'asc' ? '' : '') : '');
+ $out .= ($sortfield == strtolower($name) ? '' : '');
+ $out .= '';
+ }
+ $out .=' | ';
+
+ return $out;
+}
+
+/**
+ * Get document title search
+ *
+ * @param string $type
+ * @param string $name
+ * @param string $morehtml
+ * @return string
+ */
+function get_document_title_search(string $type, string $name, string $morehtml = ''): string
+{
+ $out = '';
+ $out .= '';
+ $out .= ' | ';
+
+ return $out;
+}
+
/**
* Exclude index.php files from list of models for document generation
*