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 eaea335
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 4 deletions.
99 changes: 99 additions & 0 deletions js/modules/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ 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 );
$(document).on( 'keydown', '.saturne-search', window.saturne.document.saturneSearch );
$(document).on( 'click', '.saturne-search-button', window.saturne.document.saturneSearch );
$(document).on( 'click', '.saturne-cancel-button', window.saturne.document.saturneCancelSearch );

};

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

/**
* Manage documents list pagination
*
* @memberof Saturne_Framework_Document
*
* @since 1.6.0
* @version 1.6.0
*
* @return {void}
*/
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());
}
}

/**
* Manage search on documents list
*
* @memberof Saturne_Framework_Document
*
* @since 1.6.0
* @version 1.6.0
*
* @return {void}
*/
window.saturne.document.saturneSearch = function (event) {
if (event.keyCode === 13 || $(this).hasClass('saturne-search-button')) {
event.preventDefault();

var currentUrl = new URL(window.location.href);

let name = $('#search_name').val();
let date = $('#search_date').val();

if (name === '' && date === '') {
return;
}
if (name.length > 0) {
if (currentUrl.searchParams.has('search_name')) {
currentUrl.searchParams.set('search_name', name);
} else {
currentUrl.searchParams.append('search_name', name);
}
}
if (date.length > 0) {
if (currentUrl.searchParams.has('search_date')) {
currentUrl.searchParams.set('search_date', date);
} else {
currentUrl.searchParams.append('search_date', date);
}
}
window.location.replace(currentUrl.toString());

}
}

/**
* Cancel search on documents list
*
* @memberof Saturne_Framework_Document
*
* @since 1.6.0
* @version 1.6.0
*
* @return {void}
*/
window.saturne.document.saturneCancelSearch = function (event) {
event.preventDefault();

var currentUrl = new URL(window.location.href);

if (currentUrl.searchParams.has('search_name')) {
currentUrl.searchParams.delete('search_name');
}
if (currentUrl.searchParams.has('search_date')) {
currentUrl.searchParams.delete('search_date');
}
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 eaea335

Please sign in to comment.