Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1003 [Documents] fix: enhance document list #1021

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

133 changes: 130 additions & 3 deletions lib/documents.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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 '';
Expand Down Expand Up @@ -215,6 +264,19 @@ function saturne_show_documents(string $modulepart, $modulesubdir, $filedir, str
$genbutton = '';
}
$out .= $genbutton;

$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 All @@ -240,6 +302,25 @@ function saturne_show_documents(string $modulepart, $modulesubdir, $filedir, str
$out .= '<td></td>';
}
$out .= '</tr>';
$out .= '<tr class="liste_titre">';
$out .= get_document_title_search('text', 'Name');
$out .= '<td></td>';
$out .= get_document_title_search('date', 'Date', 'right');
$out .= '<td></td>';
$out .= '<td class="right ">';
$out .= '<i class="saturne-search-button fas fa-search" style="cursor: pointer;"></i>';
$out .= '&nbsp;&nbsp;&nbsp;';
$out .= '<i class="saturne-cancel-button fas fa-times" style="cursor: pointer;"></i>';
$out .= '</td>';

$out .= '</tr>';
$out .= '<tr class="liste_titre">';
$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 .= '</tr>';

// Execute hooks
$parameters = ['colspan' => ($colspan + $colspanmore), 'socid' => ($GLOBALS['socid'] ?? ''), 'id' => ($GLOBALS['id'] ?? ''), 'modulepart' => $modulepart];
Expand All @@ -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);
}

Expand Down Expand Up @@ -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 = '<th class="'. $morehtml .'">';
if ($sortable) {
$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);
if ($sortable) {
$out .= ' ' . ($sortfield == strtolower($name) ? ($sortorder == 'asc' ? '<i class="fas fa-chevron-up"></i>' : '<i class="fas fa-chevron-down"></i>') : '');
$out .= ($sortfield == strtolower($name) ? '</u>' : '');
$out .= '</a>';
}
$out .='</th>';

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 = '<td class="'. $morehtml .'">';
$out .= '<input class="saturne-search" id="search_' . strtolower($name) . '" type="'. $type .'" name="search_' . strtolower($name) . '" value="' . GETPOST('search_' . strtolower($name)) . '">';
$out .= '</td>';

return $out;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

un espace entre le return

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+psr12

}

/**
* Exclude index.php files from list of models for document generation
*
Expand Down