Skip to content

Commit

Permalink
Evarisk#1003 [Documents] fix: add pagination on documents list
Browse files Browse the repository at this point in the history
  • Loading branch information
theodaviddd committed Jul 23, 2024
1 parent cf886e7 commit 017cf47
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
21 changes: 21 additions & 0 deletions js/modules/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ window.saturne.document.event = function() {
$(document).on('click', '#builddoc_generatebutton', window.saturne.document.displayLoader);
$(document).on('click', '.pdf-generation', window.saturne.document.displayLoader);
$(document).on('click', '.download-template', window.saturne.document.autoDownloadTemplate);
$(document).on( 'keydown', '#change_pagination', window.saturne.document.changePagination );
};

/**
Expand Down Expand Up @@ -103,3 +104,23 @@ window.saturne.document.autoDownloadTemplate = function() {
error: function () {}
});
};


window.saturne.document.changePagination = function (event) {
if (event.keyCode === 13) {
event.preventDefault();

var input = event.target;
var pageNumber = $('#page_number').val();
var pageValue = parseInt(input.value) <= parseInt(pageNumber) ? input.value : pageNumber;
var currentUrl = new URL(window.location.href);

if (currentUrl.searchParams.has('page')) {
currentUrl.searchParams.set('page', pageValue);
} else {
currentUrl.searchParams.append('page', pageValue);
}

window.location.replace(currentUrl.toString());
}
}
2 changes: 1 addition & 1 deletion js/saturne.min.js

Large diffs are not rendered by default.

35 changes: 32 additions & 3 deletions lib/documents.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,22 @@ 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);
}
}
}

$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 '';
Expand Down Expand Up @@ -234,6 +249,20 @@ function saturne_show_documents(string $modulepart, $modulesubdir, $filedir, str
$genbutton = '';
}
$out .= $genbutton;
$querySeparator = (strpos($_SERVER['REQUEST_URI'], '?') === false) ? '?' : '&';

$out .= '<div class="pagination">';
if($page > 1) {
$out .= '<a href="'. $_SERVER['PHP_SELF'] . '?page=' . ($page - 1) .'&sortfield=' . $sortfield . '&sortorder=' . $sortorder . '#builddoc_form"><i class="fas fa-chevron-left"></i></a>';
$out .= '&nbsp;&nbsp;&nbsp;&nbsp;';
}
$out .= '<input hidden id="page_number" value="' . $pageNumber . '">';
$out .= '<input name="change_pagination" id="change_pagination" class="maxwidth25" value="' . $page . '" name="page">';
$out .= ' / ';
$out .= '<a href="'. $_SERVER['PHP_SELF'] . '?page=' . $pageNumber .'&sortfield=' . $sortfield . '&sortorder=' . $sortorder . '#builddoc_form">' . $pageNumber . '</a>';
$out .= '&nbsp;&nbsp;&nbsp;&nbsp;';
$out .= '<a href="'. $_SERVER['PHP_SELF'] . '?page=' . ($page + 1) .'&sortfield=' . $sortfield . '&sortorder=' . $sortorder . '#builddoc_form"><i class="fas fa-chevron-right"></i></a>';
$out .= '</div>';
} else {
$out .= '<div class="float">' . $langs->trans('Files') . '</div>';
}
Expand Down Expand Up @@ -297,7 +326,6 @@ function saturne_show_documents(string $modulepart, $modulesubdir, $filedir, str
$out .= '<div class="div-table-responsive-no-min">';
$out .= '<table class="noborder centpercent" id="' . $modulepart . '_table">' . "\n";
}
$fileList = dol_sort_array($fileList, $sortfield, $sortorder);

// Loop on each file found
if (is_array($fileList)) {
Expand Down Expand Up @@ -442,9 +470,10 @@ function saturne_show_documents(string $modulepart, $modulesubdir, $filedir, str
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 = '<th class="'. $morehtml .'">';
if ($sortable) {
$out .= '<a href="'. $_SERVER['PHP_SELF'] . '?sortfield='. (strtolower($name)) .'&sortorder=' . ($sortorder == 'desc' ? 'asc' : 'desc') .'#builddoc_form">';
$out .= '<a href="'. $_SERVER['PHP_SELF'] . $querySeparator . 'sortfield='. (strtolower($name)) .'&sortorder=' . ($sortorder == 'desc' ? 'asc' : 'desc') .'#builddoc_form">';
$out .= ($sortfield == strtolower($name) ? '<u>' : '');
}
$out .= $langs->trans($name);
Expand Down

0 comments on commit 017cf47

Please sign in to comment.