Skip to content

Commit

Permalink
Moved pagination layout into its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
ignacionelson committed Oct 2, 2022
1 parent d596fee commit 8f257b5
Show file tree
Hide file tree
Showing 18 changed files with 461 additions and 429 deletions.
7 changes: 3 additions & 4 deletions actions-log.php
Expand Up @@ -271,10 +271,9 @@

echo $table->render();

/**
* PAGINATION
*/
echo $table->pagination([
// PAGINATION
$pagination = new \ProjectSend\Classes\PaginationLayout;
echo $pagination->make([
'link' => 'actions-log.php',
'current' => $pagination_page,
'item_count' => $count_for_pagination,
Expand Down
18 changes: 9 additions & 9 deletions assets/src/js/parts/main.js
Expand Up @@ -109,15 +109,15 @@
$('.footable').footable().find('> tbody > tr:not(.footable-row-detail):nth-child(even)').addClass('odd');
}

/** Pagination */
$(".go_to_page").on("click", "button", function() {
var _page = $('.go_to_page #page_number').data('link');
var _page_no = parseInt($('.go_to_page #page_number').val());
if (typeof _page_no == 'number'){
_page = _page.replace('_pgn_', _page_no);
}
window.location.href = _page;
});
// /** Pagination */
// $(".go_to_page").on("click", "button", function() {
// var _page = $('.go_to_page #page_number').data('link');
// var _page_no = parseInt($('.go_to_page #page_number').val());
// if (typeof _page_no == 'number'){
// _page = _page.replace('_pgn_', _page_no);
// }
// window.location.href = _page;
// });

/** Password generator */
var hdl = new Jen(true);
Expand Down
2 changes: 1 addition & 1 deletion assets/src/scss/main.scss
Expand Up @@ -909,7 +909,7 @@ li .required.password_toggle {
}

.go_to_page #page_number {
width:4em !important;
width:3.5rem !important;
text-align:center;
}

Expand Down
3 changes: 2 additions & 1 deletion categories.php
Expand Up @@ -306,7 +306,8 @@ function format_category_row($arranged)
echo $table->render();

// PAGINATION
echo $table->pagination([
$pagination = new \ProjectSend\Classes\PaginationLayout;
echo $pagination->make([
'link' => basename($_SERVER['SCRIPT_FILENAME']),
'current' => $params['page'],
'item_count' => $get_categories['count'],
Expand Down
3 changes: 2 additions & 1 deletion clients-membership-requests.php
Expand Up @@ -355,7 +355,8 @@
echo $table->render();

// PAGINATION
echo $table->pagination([
$pagination = new \ProjectSend\Classes\PaginationLayout;
echo $pagination->make([
'link' => $this_page,
'current' => $pagination_page,
'item_count' => $count_for_pagination,
Expand Down
3 changes: 2 additions & 1 deletion clients-requests.php
Expand Up @@ -407,7 +407,8 @@
echo $table->render();

// PAGINATION
echo $table->pagination([
$pagination = new \ProjectSend\Classes\PaginationLayout;
echo $pagination->make([
'link' => $this_page,
'current' => $pagination_page,
'item_count' => $count_for_pagination,
Expand Down
3 changes: 2 additions & 1 deletion clients.php
Expand Up @@ -386,7 +386,8 @@
echo $table->render();

// PAGINATION
echo $table->pagination([
$pagination = new \ProjectSend\Classes\PaginationLayout;
echo $pagination->make([
'link' => 'clients.php',
'current' => $pagination_page,
'item_count' => $count_for_pagination,
Expand Down
5 changes: 3 additions & 2 deletions cron-log.php
Expand Up @@ -214,8 +214,9 @@
echo $table->render();

// PAGINATION
echo $table->pagination([
'link' => 'cron-log.php',
$pagination = new \ProjectSend\Classes\PaginationLayout;
echo $pagination->make([
'link' => 'cron-log.php',
'current' => $pagination_page,
'item_count' => $count_for_pagination,
]);
Expand Down
5 changes: 3 additions & 2 deletions custom-assets.php
Expand Up @@ -297,8 +297,9 @@
echo $table->render();

// PAGINATION
echo $table->pagination([
'link' => 'custom-assets.php',
$pagination = new \ProjectSend\Classes\PaginationLayout;
echo $pagination->make([
'link' => 'custom-assets.php',
'current' => $pagination_page,
'item_count' => $count_for_pagination,
]);
Expand Down
5 changes: 3 additions & 2 deletions download-information.php
Expand Up @@ -206,8 +206,9 @@
echo $table->render();

// PAGINATION
echo $table->pagination([
'link' => 'download-information.php',
$pagination = new \ProjectSend\Classes\PaginationLayout;
echo $pagination->make([
'link' => 'download-information.php',
'current' => $pagination_page,
'item_count' => $count_for_pagination,
]);
Expand Down
5 changes: 2 additions & 3 deletions footer.php
@@ -1,6 +1,5 @@
</div> <!-- container-fluid -->

<?php render_footer_text(); ?>
<?php render_footer_text(); ?>
</div> <!-- main-content -->
</div> <!-- container-fluid -->
</main>
<?php
Expand Down
3 changes: 2 additions & 1 deletion groups.php
Expand Up @@ -321,7 +321,8 @@
echo $table->render();

// PAGINATION
echo $table->pagination([
$pagination = new \ProjectSend\Classes\PaginationLayout;
echo $pagination->make([
'link' => 'groups.php',
'current' => $pagination_page,
'item_count' => $count_for_pagination,
Expand Down
152 changes: 152 additions & 0 deletions includes/Classes/PaginationLayout.php
@@ -0,0 +1,152 @@
<?php
namespace ProjectSend\Classes;

class PaginationLayout {
private $template;

public function __construct()
{
$this->template = '
<div class="pagination_wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-12 col-md-6 text-center">
<nav aria-label="' . __('Results navigation', 'cftp_admin') . '">
<ul class="pagination">
{pages}
</ul>
</nav>
</div>
<div class="col-12 col-md-6 text-end">
<div class="go_to_page">
<form method="get" action="{goto_action}" class="row row-cols-lg-auto g-3 align-items-center">
<label class="control-label hidden-xs hidden-sm">' . __('Go to page:', 'cftp_admin') . '</label>
<div class="col-12">
<input type="text" class="form-control" name="page" id="page_number" value="{current_page}" />
</div>
<div class="col-12">
<button type="submit" class="form-control"><i aria-hidden="true" class="fa fa-check"></i></button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
';
}

private function constructPaginationLink($link, $page = 1)
{
$params['page'] = $page;

/**
* List of parameters to ignore when building the pagination links.
* TODO: change it so it ignores all but 'search' instead? must check
* if there are other parameters that need to be saved.
*/
$ignore_current_params = array(
'page',
'categories_actions',
'action',
'do_action',
'batch',
);

if (!empty($_GET)) {
foreach ($_GET as $param => $value) {
if (!in_array($param, $ignore_current_params)) {
$params[$param] = $value;
}
}
}
$this->query = http_build_query($params);

return BASE_URI . $link . '?' . $this->query;
}

public function make($parameters)
{
if (!is_numeric($parameters['current'])) {
$parameters['current'] = 1;
} else {
$parameters['current'] = (int)$parameters['current'];
}

$layout = '';

$pages = 0;
$items_per_page = get_option('pagination_results_per_page');
if (!empty($parameters['items_per_page']) && is_numeric($parameters['items_per_page'])) {
$items_per_page = $parameters['items_per_page'];
}
if (!empty($parameters['item_count']) && is_numeric($parameters['item_count'])) {
$pages = ceil($parameters['item_count'] / $items_per_page);
}

if ($pages > 1) {
$layout_pages = '';
// First and previous
if ($parameters['current'] > 1) {
$layout_pages .= '<li class="page-item">
<a class="page-link" href="' . $this->constructPaginationLink($parameters['link']) . '" data-page="first"><span aria-hidden="true">&laquo;</span></a>
</li>
<li class="page-item">
<a class="page-link" href="' . $this->constructPaginationLink($parameters['link'], $parameters['current'] - 1) . '" data-page="prev">&lsaquo;</a>
</li>';
}

/** Pages */
$already_spaced = false;
for ($i = 1; $i <= $pages; $i++) {
if (
($i < $parameters['current'] - 3 || $i > $parameters['current'] + 3) &&
($i != 1 && $i != $pages)
) {
if ($already_spaced == false) {
$layout_pages .= '<li class="page-item disabled"><a class="page-link" href="#">...</a></li>';
$already_spaced = true;
}
continue;
}
if ($parameters['current'] == $i) {
$layout_pages .= '<li class="page-item active"><a class="page-link" href="#">' . $i . '</a></li>';
} else {
$layout_pages .= '<li class="page-item"><a class="page-link" href="' . $this->constructPaginationLink($parameters['link'], $i) . '">' . $i . '</a></li>';
}

if ($i > $parameters['current']) {
$already_spaced = false;
}
}

/** Next and last */
if ($parameters['current'] != $pages) {
$layout_pages .= '<li class="page-item">
<a class="page-link" href="' . $this->constructPaginationLink($parameters['link'], $parameters['current'] + 1) . '" data-page="next">&rsaquo;</a>
</li>
<li class="page-item">
<a class="page-link" href="' . $this->constructPaginationLink($parameters['link'], $pages) . '" data-page="last"><span aria-hidden="true">&raquo;</span></a>
</li>';
}

$goto_action = $this->removePageUrlParam($this->constructPaginationLink($parameters['link']));

$layout = str_replace(
['{pages}', '{goto_action}', '{current_page}'],
[$layout_pages, $goto_action, $parameters['current']],
$this->template
);
}

return $layout;
}

private function removePageUrlParam($url)
{
$url = preg_replace('/(&|\?)'.preg_quote('page').'=[^&]*$/', '', $url);
$url = preg_replace('/(&|\?)'.preg_quote('page').'=[^&]*&/', '$1', $url);

return $url;
}
}

0 comments on commit 8f257b5

Please sign in to comment.