Skip to content

Commit

Permalink
#13 [Pagination] add: pagination function for media gallery
Browse files Browse the repository at this point in the history
  • Loading branch information
Théo David committed Jan 2, 2023
1 parent 5ca03fa commit 6bad399
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 16 deletions.
117 changes: 108 additions & 9 deletions core/tpl/medias_gallery_modal.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,79 @@
}
}

if ( ! $error && $subaction == "pagination") {
$data = json_decode(file_get_contents('php://input'), true);

$offset = $data['offset'];
$pagesCounter = $data['pagesCounter'];

$pagesDisplayed = [];

if ($pagesCounter < 6) {
for ($i = 1; $i <= $pagesCounter; $i++) {
$page_array[] = $pagesCounter;
}
} else {
for ($i = 1; $i <= 3; $i++) {
$page_array[] = $i;
}
if($offset > 5 && $offset < ($pagesCounter - 4)) {
if ($offset != 6) {
$page_array[] = '...';
}
$page_array[] = $offset - 2;
$page_array[] = $offset - 1;
$page_array[] = $offset;
$page_array[] = $offset + 1;
$page_array[] = $offset + 2;
if ($offset != $pagesCounter - 5) {
$page_array[] = '...';
}
} else {
if ($offset == 3) {
$page_array[] = $offset + 1;
$page_array[] = $offset + 2;
$page_array[] = '...';
} else if ($offset == 4) {
$page_array[] = $offset;
$page_array[] = '...';
} else if ($offset == 5) {
$page_array[] = $offset - 1;
$page_array[] = $offset;
$page_array[] = $offset + 1;
$page_array[] = $offset + 2;
$page_array[] = '...';
} else if ($offset == ($pagesCounter - 4)) {
$page_array[] = '...';
$page_array[] = $offset - 2;
$page_array[] = $offset - 1;
$page_array[] = $offset;
$page_array[] = $offset + 1;
} else if ($offset == ($pagesCounter - 3)) {
$page_array[] = '...';
$page_array[] = $offset - 2;
$page_array[] = $offset - 1;
$page_array[] = $offset;
} else if ($offset == ($pagesCounter - 2)) {
$page_array[] = '...';
$page_array[] = $offset - 2;
$page_array[] = $offset - 1;
} else {
$page_array[] = '...';
}
}

for ($i = 0; $i < 3; $i++) {
$last_pages[] = $pagesCounter - $i;
}
asort($last_pages);
$page_array = array_merge($page_array, $last_pages);
}

$new_pagination = 1;

}

if (is_array($submit_file_error_text)) {
print '<input class="error-medias" value="'. htmlspecialchars(json_encode($submit_file_error_text)) .'">';
}
Expand Down Expand Up @@ -184,24 +257,50 @@
<div class="wpeo-gridlayout grid-5 grid-gap-3 grid-margin-2 ecm-photo-list ecm-photo-list">
<?php
$relativepath = $module . '/medias/thumbs';
print saturne_show_medias($module, 'ecm', $conf->ecm->multidir_output[$conf->entity] . '/'. $module .'/medias', ($conf->browser->layout == 'phone' ? 'mini' : 'small'), 80, 80, (!empty(GETPOST('offset')) ? GETPOST('offset') : 0));
print saturne_show_medias($module, 'ecm', $conf->ecm->multidir_output[$conf->entity] . '/'. $module .'/medias', ($conf->browser->layout == 'phone' ? 'mini' : 'small'), 80, 80, (!empty($offset) ? $offset : 0));
?>
</div>
</div>
</div>
<!-- Modal-Footer -->
<div class="modal-footer">
<?php $filearray = dol_dir_list($conf->ecm->multidir_output[$conf->entity] . '/'. $module .'/medias/', "files", 0, '', '(\.meta|_preview.*\.png)$', 'date', SORT_DESC);
$allMedias = count($filearray); ?>
<?php
$filearray = dol_dir_list($conf->ecm->multidir_output[$conf->entity] . '/'. $module .'/medias/', "files", 0, '', '(\.meta|_preview.*\.png)$', 'date', SORT_DESC);
$moduleImageNumberPerPageConf = strtoupper($module) . '_DISPLAY_NUMBER_MEDIA_GALLERY';
$allMediasNumber = count($filearray);
$pagesCounter = $conf->global->$moduleImageNumberPerPageConf ? ceil($allMediasNumber/($conf->global->$moduleImageNumberPerPageConf ?: 1)) - 1 : 1;
?>
<ul class="wpeo-pagination">
<?php
$moduleImageNumberPerPageConf = strtoupper($module) . '_DISPLAY_NUMBER_MEDIA_GALLERY';
for ($i = 1; $i <= 1 + $allMedias/($conf->global->$moduleImageNumberPerPageConf ?: 1 ); $i++) : ?>
<li class="pagination-element <?php echo ($i == 1 ? 'pagination-current' : '') ?>">
<a class="selected-page" value="<?php echo $i - 1; ?>"><?php echo $i; ?></a>
</li>
<?php endfor; ?>
print '<input hidden id="pagesCounter" value="'. ($pagesCounter) .'">';
print '<input hidden id="containerToRefresh" value="media_gallery">';
if (!is_array($page_array) || empty($page_array)) {
$page_array = [];
if ($pagesCounter < 6) {
for ($i = 1; $i <= $pagesCounter; $i++) {
$page_array[] = $pagesCounter;
}
$page_array[] = '...';
} else {
for ($i = 1; $i <= 3; $i++) {
$page_array[] = $i;
}
$page_array[] = '...';
for ($i = 0; $i < 3; $i++) {
$last_pages[] = $pagesCounter - $i;
}
asort($last_pages);
$page_array = array_merge($page_array, $last_pages);
}
}
foreach ($page_array as $pageNumber) {
print '<li class="pagination-element ' . ($pageNumber == $offset ? 'pagination-current' : ($pageNumber == 1 && !$offset ? 'pagination-current' : '')) . '">';
print '<a class="select-page" value="' . ($pageNumber) . '">' . $pageNumber . '</a>';
print '</li>';
}
?>
</ul>
<div class="test-pagination" id="demo"></div>
<div class="save-photo wpeo-button button-blue button-disable" value="">
<input class="from-type" value="" type="hidden"/>
<input class="from-subtype" value="" type="hidden"/>
Expand Down
23 changes: 17 additions & 6 deletions js/saturne.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ window.saturne.mediaGallery.event = function() {
$( document ).on( 'click', '.media-gallery-unlink', window.saturne.mediaGallery.unlinkFile );
$( document ).on( 'click', '.media-gallery-favorite', window.saturne.mediaGallery.addToFavorite );
$( document ).on( 'change', '.fast-upload', window.saturne.mediaGallery.fastUpload );
$( document ).on( 'click', '.selected-page', window.saturne.mediaGallery.selectPage );
$( document ).on( 'click', '.select-page', window.saturne.mediaGallery.selectPage );
}

/**
Expand Down Expand Up @@ -594,24 +594,35 @@ window.saturne.mediaGallery.fastUpload = function( typeFrom ) {
* @return {void}
*/
window.saturne.mediaGallery.selectPage = function( event ) {

let token = $('.fiche').find('input[name="token"]').val();

let pagesCounter = $(this).closest('.wpeo-pagination').find('#pagesCounter').val()
let containerToRefresh = $(this).closest('.wpeo-pagination').find('#containerToRefresh').val()

let offset = $(this).attr('value');

let mediaGallery = $('#' + containerToRefresh);
let querySeparator = '?';

$(this).closest('.wpeo-pagination').find('.pagination-element').removeClass('pagination-current');
$(this).closest('.pagination-element').addClass('pagination-current');

let elementParent = $('.modal-container').find('.ecm-photo-list-content');
let querySeparator = '?';
document.URL.match(/\?/) ? querySeparator = '&' : 1
let token = $('.fiche').find('input[name="token"]').val();
window.saturne.loader.display($('#media_gallery').find('.modal-content'));

$.ajax({
url: document.URL + querySeparator + "token=" + token + "&offset=" + offset,
url: document.URL + querySeparator + "subaction=pagination" + "&token=" + token,
type: "POST",
data: JSON.stringify({
offset: offset,
pagesCounter: pagesCounter
}),
processData: false,
contentType: false,
success: function ( resp ) {
$('.wpeo-loader').removeClass('wpeo-loader')
elementParent.html($(resp).find('.ecm-photo-list-content'));
mediaGallery.html($(resp).find('#' + containerToRefresh).children());
},
error: function ( ) {
}
Expand Down
2 changes: 1 addition & 1 deletion lib/saturne_functions.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function saturne_show_medias($module, $modulepart = 'ecm', $sdir, $size = 0, $ma
$filearray = dol_sort_array($filearray, $sortfield, $sortorder);
}
$moduleImageNumberPerPageConf = strtoupper($module) . '_DISPLAY_NUMBER_MEDIA_GALLERY';
for ($i = 0 + ($offset * $conf->global->$moduleImageNumberPerPageConf); $i < $conf->global->$moduleImageNumberPerPageConf + ($offset * $conf->global->$moduleImageNumberPerPageConf); $i++) {
for ($i = ($offset * $conf->global->$moduleImageNumberPerPageConf); $i < ($conf->global->$moduleImageNumberPerPageConf + ($offset * $conf->global->$moduleImageNumberPerPageConf)); $i++) {
$file = $filearray[$i]['name'];

if (image_format_supported($file) >= 0) {
Expand Down

0 comments on commit 6bad399

Please sign in to comment.