Permalink
Browse files

Add pagination to subscription overview

  • Loading branch information...
0bitus committed Aug 4, 2017
1 parent d86b7b1 commit f4beb546591e451ccd8110cce4e370af4f2f2fa8
@@ -12,14 +12,15 @@ class DBconn {
private $db, $result, $stmt;
private $params = array();
private $types = "";
public $num_rows;
public $error;
public function __construct($db_host = null, $db_user = null, $bd_pass = null, $db_name = null, $flag = null, $ca = null) {
$db_host = empty($db_host) ? DB_host : $db_host;
$db_user = empty($db_user) ? DB_user : $db_user;
$bd_pass = empty($bd_pass) ? DB_pass : $bd_pass;
$db_name = empty($db_name) ? DB_name : $db_name;
$this->db = new mysqli();
$this->db = new mysqli();
if (!empty($ca)) {
$this->db->ssl_set(NULL, NULL, $ca, NULL, NULL);
}
@@ -51,13 +52,17 @@ public function commit() {
$this->db->query("COMMIT");
}
public function query($sql) {
public function query($sql, $flush = TRUE) {
$this->stmt = $this->db->stmt_init();
if ($this->stmt->prepare($sql)) {
if (!empty($this->types)) {
call_user_func_array(array($this->stmt, 'bind_param'), array_merge(array($this->types), $this->params));
}
return $this->finalExecute();
if ($flush) {
return $this->finalExecute();
} else {
return $this->execute();
}
}
$this->exception($this->stmt->error);
return FALSE;
@@ -67,6 +72,7 @@ public function execute() {
$execute = $this->stmt->execute();
if (!$execute)
$this->exception($this->stmt->error);
$this->num_rows = $this->stmt->num_rows;
return $execute;
}
@@ -0,0 +1,87 @@
<?php
/**
* Pagination
*
* @author Alexander Rein <a.rein@be-clever-ag.de>, beclever werbeagentur AG <support@be-clever-ag.de>
* @copyright (c) 2017, Alexander Rein
* @license http://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License
*/
class Pagination {
private $_resuilt;
private $_limit;
private $_page;
private $_query;
private $_total;
public function __construct($result) {
$this->_resuilt = $result;
$this->_total = count($this->_resuilt);
}
public function getPart($limit = 100, $page = 1) {
$this->_limit = $limit;
$this->_page = $page;
if ($this->_limit == 'all') {
$part = $this->_resuilt;
} else {
$temp = array_chunk($this->_resuilt, $limit);
if (isset($temp[$page - 1]) || array_key_exists($page - 1, $temp)) {
$part = $temp[$page - 1];
} else {
$part = $page < 1 ? $temp[0] : end($temp);
}
}
$result = new stdClass();
$result->page = $this->_page;
$result->limit = $this->_limit;
$result->total = $this->_total;
$result->part = $part;
return $result;
}
public function createLinks($links = 7, $list_class = "pagination") {
if ($this->_limit == 'all' || $this->_total <= $this->_limit) {
return '';
}
$last = ceil($this->_total / $this->_limit);
$start = ( ( $this->_page - $links ) > 0 ) ? $this->_page - $links : 1;
$end = ( ( $this->_page + $links ) < $last ) ? $this->_page + $links : $last;
$html = '<ul class="' . $list_class . '">';
$class = ( $this->_page == 1 ) ? "disabled" : "";
$html .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . ( $this->_page - 1 ) . '">&laquo;</a></li>';
if ($start > 1) {
$html .= '<li><a href="?limit=' . $this->_limit . '&page=1">1</a></li>';
$html .= '<li class="disabled"><span>...</span></li>';
}
for ($i = $start; $i <= $end; $i++) {
$class = ( $this->_page == $i ) ? "active" : "";
$html .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . $i . '">' . $i . '</a></li>';
}
if ($end < $last) {
$html .= '<li class="disabled"><span>...</span></li>';
$html .= '<li><a href="?limit=' . $this->_limit . '&page=' . $last . '">' . $last . '</a></li>';
}
$class = ( $this->_page == $last ) ? "disabled" : "";
$html .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . ( $this->_page + 1 ) . '">&raquo;</a></li>';
$html .= '</ul>';
return $html;
}
}
@@ -269,6 +269,7 @@
$LOCAL["subscription"]["dialog"]["extend"]["label_selected"] = "Selected subscriptions";
$LOCAL["subscription"]["dialog"]["extend"]["label_keys"] = "Insert the same number of keys (one per line) as selected";
$LOCAL["subscription"]["dialog"]["extend"]["label_termsconditions"] = "I accept the terms and conditions for <a href='https://samba.plus/tcsambaplus.pdf' target='_blank'>SAMBA+ <span class='ui-icon ui-icon-extlink'></span></a><br/>(German: Ich akzeptiere die AGB f&uuml;r <a href='https://samba.plus/agbsambaplus.pdf' target='_blank'>SAMBA+ <span class='ui-icon ui-icon-extlink'></span></a>)";
$LOCAL["subscription"]["dialog"]["extend"]["selected_info"] = "subscriptions selected.";
$LOCAL["subscription"]["dialog"]["extend"]["submit"] = "Extend subscriptions";
$LOCAL["subscription"]["dialog"]["setpass"]["title"] = "Set password for subscription";
$LOCAL["subscription"]["dialog"]["setpass"]["label_pass"] = "Password";
@@ -13,4 +13,7 @@ h2 {color: @colorPrimary}
h3 {color: @colorSecondary}
h4 {color: @colorPrimary}
nav #navigation li a {border-left: 1px solid lighten(@colorPrimary, 20%); border-right: 1px solid lighten(@colorPrimary, 20%)}
nav #navigation li a {border-left: 1px solid lighten(@colorPrimary, 20%); border-right: 1px solid lighten(@colorPrimary, 20%)}
.pagination li a {border-color: @colorPrimary; color: @colorPrimary;}
.pagination li.active a {background-color: @colorPrimary; color: #FFF; border-color: @colorPrimary}
@@ -137,12 +137,13 @@ $(function () {
event.preventDefault();
var data = {};
var href = $(this).is('a') ? $(this).attr("href") : $(this).attr("data-href");
var callback = window[$(this).attr("data-callback")];
data["source"] = $($(this).attr("data-source")).serialize();
data["action"] = $(this).attr("data-action");
if (typeof $(this).attr("title") !== "undefined") {
$($(this).attr("data-target")).dialog("option", "title", $(this).attr("title"));
}
initEditDialog($(this).attr("data-target"), href, data);
initEditDialog($(this).attr("data-target"), href, data, "html", callback);
});
$("#accordion").accordion({
@@ -159,6 +160,14 @@ $(function () {
init_button();
});
function subInfo(){
$(".dialog#extendsubs").each(function () {
$(".ui-dialog-buttonpane .dialog-sub-info", $(this).parent()).remove();
var count = $("select#source option",this).length;
$(".ui-dialog-buttonpane", $(this).parent()).prepend("<div class='dialog-sub-info'>" + count + " " + LOCAL["subscription"]["dialog"]["extend"]["selected_info"] + "</div>");
});
}
function message(msg, append) {
console.log(msg);
append = (typeof append === "undefined") ? "body" : append;
@@ -177,7 +186,7 @@ function message(msg, append) {
}
// Handle dynamic forms loaded via ajax
function initEditDialog(target, requestURL, data, type) {
function initEditDialog(target, requestURL, data, type, callback) {
type = (typeof type === "undefined") ? "html" : type;
var form = $(target);
$.ajax({
@@ -227,6 +236,9 @@ function initEditDialog(target, requestURL, data, type) {
}
}
if (typeof callback === "function") {
callback();
}
}
});
}
@@ -32,6 +32,14 @@ nav #navigation li.disabled a {opacity: 0.5; cursor: default}
.separate-left {margin-left: -1px; border-left: 1px solid #dfdfdf}
.separate-right {margin-right: -1px; border-right: 1px solid #dfdfdf}
.pagination {list-style-type: none; text-align: center; padding: 3px 5px 7px; margin: 0; background-color: #E0E0E0; border: 2px solid #EEE}
.pagination li {display: inline;}
.pagination li a {display: inline-block; padding: 2px 10px; margin: 0 2px; border: 1px solid; border-radius: 3px; background-color: #FFF}
.pagination li span {display: inline-block; padding: 6px 4px 0;}
.pagination li.disabled a {border-color: #bbb; color: #bbb; background-color: transparent; cursor: default;}
.pagination li.disabled a:hover {text-decoration: none;}
.pagination:after {content: ""; display: block; clear: both;}
input.text {display: inline-block; vertical-align: middle; padding: 0.4em 0.8em;}
.dialog fieldset {padding:0; border:0; margin-top:25px;}
.dialog input, .dialog label {display: block;}
@@ -7,10 +7,11 @@
<li><a href="#tabs-expired">{$LOCAL.subscription.head.expired_subscription}</a></li>
<button class="button_key btn-dialog" value="-1" title="{$LOCAL.subscription.button.set_global_password}" data-target="#setpass">{$LOCAL.subscription.button.set_global_password}</button>
<button class="button_add btn-dialog" type="button" data-target="#addkeys">{$LOCAL.subscription.button.add_subscription}</button>
<button class="button_add ajax" type="button" data-href="process/subscription.php" data-action="getform" data-source=".checksub" data-target="#extendsubs">{$LOCAL.subscription.button.extend_subscription}</button>
<button class="button_add ajax" type="button" data-href="process/subscription.php" data-action="getform" data-source=".checksub" data-callback="subInfo" data-target="#extendsubs">{$LOCAL.subscription.button.extend_subscription}</button>
</ul>
<div id="tabs-current">
{if !empty($currSub)}
{$currPagination}
<form method="post" action="process/subscription.php" class="ajax">
<input type="hidden" name="action" value="edit" />
<table class="list ui-widget ui-widget-content">
@@ -45,10 +46,12 @@
</tbody>
</table>
</form>
{$currPagination}
{/if}
</div>
<div id="tabs-expired">
{if !empty($expSub)}
{$expPagination}
<table class="list ui-widget ui-widget-content">
<thead>
<tr class="ui-widget-header">
@@ -73,6 +76,7 @@
{/foreach}
</tbody>
</table>
{$expPagination}
{/if}
</div>
</div>
@@ -1,4 +1,5 @@
<?php
/**
* @author Alexander Rein <a.rein@be-clever-ag.de>, beclever werbeagentur AG <support@be-clever-ag.de>
* @copyright (c) 2016, Alexander Rein
@@ -10,13 +11,16 @@
require_once($PATH_classes . "/vendor/autoload.php");
require_once($PATH_classes . '/class.Subscription.php');
require_once($PATH_classes . "/class.Customize.php");
require_once($PATH_classes . '/class.Pagination.php');
require_once($PATH_l10n . "/locallang.php");
$subscription = new Subscription($_SESSION["user"]["uID"]);
$allSub = $subscription->get_subscription();
$limit = !empty($_GET['limit']) ? $_GET['limit'] : 500;
$page = !empty($_GET['page']) ? $_GET['page'] : 1;
$allSub = $subscription->get_subscription("");
$smarty = new Smarty;
if ($allSub === FALSE)
$_SESSION['ERROR'] = $subscription->error_msg;
else {
@@ -29,10 +33,18 @@
$expSub[] = $sub;
}
}
}
$smarty->assign("currSub", $currSub);
$smarty->assign("expSub", $expSub);
$currPaginator = new Pagination($currSub);
$currSubPart = $currPaginator->getPart($limit, $page);
$expPaginator = new Pagination($expSub);
$expSubPart = $expPaginator->getPart($limit, $page);
$smarty->assign("currPagination", $currPaginator->createLinks());
$smarty->assign("currSub", $currSubPart->part);
$smarty->assign("expPagination", $expPaginator->createLinks());
$smarty->assign("expSub", $expSubPart->part);
}
$msg_error = isset($_SESSION['ERROR']) ? $_SESSION['ERROR'] : "";
$msg_success = isset($_SESSION['SUCCESS']) ? $_SESSION['SUCCESS'] : "";
@@ -51,4 +63,4 @@
if (!empty($_COOKIE))
$smarty->assign("COOKIE", $_COOKIE);
$smarty->display('layout/template/subscription.tpl');
$smarty->display('layout/template/subscription.tpl');

0 comments on commit f4beb54

Please sign in to comment.