add pagination
authorStricted <info@stricted.de>
Sun, 15 Feb 2015 04:09:00 +0000 (05:09 +0100)
committerStricted <info@stricted.de>
Sun, 15 Feb 2015 04:09:00 +0000 (05:09 +0100)
lib/api/smarty/plugins/function.pages.php [new file with mode: 0644]
lib/page/IndexPage.class.php
lib/page/RecordListPage.class.php
templates/default/index.tpl
templates/default/recordList.tpl

diff --git a/lib/api/smarty/plugins/function.pages.php b/lib/api/smarty/plugins/function.pages.php
new file mode 100644 (file)
index 0000000..741b4b6
--- /dev/null
@@ -0,0 +1,149 @@
+<?php
+
+function smarty_function_pages($tagArgs, $tplObj) {
+       // needed params: controller, pageNo, pages
+       if (!isset($tagArgs['controller'])) throw new Exception("missing 'controller' argument in pages tag");
+       if (!isset($tagArgs['pageNo'])) {
+               if (($tagArgs['pageNo'] = $tplObj->smarty->getTemplateVars('pageNo')) === null) {
+                       throw new Exception("missing 'pageNo' argument in pages tag");
+               }
+       }
+       if (!isset($tagArgs['pages'])) {
+               if (($tagArgs['pages'] = $tplObj->smarty->getTemplateVars('pages')) === null) {
+                       throw new Exception("missing 'pages' argument in pages tag");
+               }
+       }
+       
+       $html = '';
+       
+       if ($tagArgs['pages'] > 1) {
+               $link = "index.php?page=".$tagArgs['controller'].(isset($tagArgs['id']) ? "&id=".$tagArgs['id'] : "");
+               
+               if (!isset($tagArgs['pageNo'])) {
+                       if (($tagArgs['pageNo'] = $tplObj->smarty->getTemplateVars('pageNo')) === null) {
+                               $tagArgs['pageNo'] = 0;
+                       }
+               }
+               
+               // open div and ul
+               $html .= "<nav>\n<ul class='pagination'>\n";
+               
+               // previous page
+               $html .= makePreviousLink($link, $tagArgs['pageNo']);
+               
+               // first page
+               $html .= makeLink($link, 1, $tagArgs['pageNo'], $tagArgs['pages']);
+               
+               // calculate page links
+               $maxLinks = 7;
+               $linksBeforePage = $tagArgs['pageNo'] - 2;
+               if ($linksBeforePage < 0) $linksBeforePage = 0;
+               $linksAfterPage = $tagArgs['pages'] - ($tagArgs['pageNo'] + 1);
+               if ($linksAfterPage < 0) $linksAfterPage = 0;
+               if ($tagArgs['pageNo'] > 1 && $tagArgs['pageNo'] < $tagArgs['pages']) {
+                       $maxLinks--;
+               }
+               
+               $half = $maxLinks / 2;
+               $left = $right = $tagArgs['pageNo'];
+               if ($left < 1) $left = 1;
+               if ($right < 1) $right = 1;
+               if ($right > $tagArgs['pages'] - 1) $right = $tagArgs['pages'] - 1;
+               
+               if ($linksBeforePage >= $half) {
+                       $left -= $half;
+               }
+               else {
+                       $left -= $linksBeforePage;
+                       $right += $half - $linksBeforePage;
+               }
+               
+               if ($linksAfterPage >= $half) {
+                       $right += $half;
+               }
+               else {
+                       $right += $linksAfterPage;
+                       $left -= $half - $linksAfterPage;
+               }
+               
+               $right = intval(ceil($right));
+               $left = intval(ceil($left));
+               if ($left < 1) $left = 1;
+               if ($right > $tagArgs['pages']) $right = $tagArgs['pages'];
+               
+               // left ... links
+               if ($left > 1) {
+                       if ($left - 1 < 2) {
+                               $html .= makeLink($link, 2, $tagArgs['pageNo'], $tagArgs['pages']);
+                       }
+                       else {
+                               $html .= '<li class="button jumpTo"><a title="Gehe zu Seite" class="ttips">&hellip;</a></li>'."\n";
+                       }
+               }
+               
+               // visible links
+               for ($i = $left + 1; $i < $right; $i++) {
+                       $html .= makeLink($link, $i, $tagArgs['pageNo'], $tagArgs['pages']);
+               }
+               
+               // right ... links
+               if ($right < $tagArgs['pages']) {
+                       if ($tagArgs['pages'] - $right < 2) {
+                               $html .= makeLink($link, $tagArgs['pages'] - 1, $tagArgs['pageNo'], $tagArgs['pages']);
+                       }
+                       else {
+                               $html .= '<li class="button jumpTo"><a>&hellip;</a></li>'."\n";
+                       }
+               }
+               
+               // last page
+               $html .= makeLink($link, $tagArgs['pages'], $tagArgs['pageNo'], $tagArgs['pages']);
+               
+               // next page
+               $html .= makeNextLink($link, $tagArgs['pageNo'], $tagArgs['pages']);
+               
+               // close div and ul
+               $html .= "</ul></nav>\n";
+       }
+       
+       // assign html output to template var
+       if (isset($tagArgs['assign'])) {
+               $tplObj->assign($tagArgs['assign'], $html);
+       }
+       
+       return $html;
+}
+
+function insertPageNumber($link, $pageNo) {
+       $link = $link ."&pageNo=".$pageNo;
+       return $link;
+}
+
+function makeLink($link, $pageNo, $activePage, $pages) {
+       // first page
+       if ($activePage != $pageNo) {
+               return '<li><a href="'.insertPageNumber($link, $pageNo).'" title="Seite '.$pageNo.'">'.intval($pageNo).'</a></li>'."\n";
+       }
+       else {
+               return '<li class="active"><a>'.intval($pageNo).'</a></li>'."\n";
+       }
+}
+
+function makePreviousLink($link, $pageNo) {
+       if ($pageNo > 1) {
+               return '<li class="skip"><a href="'.insertPageNumber($link, $pageNo - 1).'" title="Vorherige Seite" class="ttips"><span class="fa fa-angle-double-left"></span></a></li>'."\n";
+       }
+       else {
+               return '<li class="skip disabled"><span class="fa fa-angle-double-left disabled"></span></li>'."\n";
+       }
+}
+
+
+function makeNextLink($link, $pageNo, $pages) {
+       if ($pageNo && $pageNo < $pages) {
+               return '<li class="skip"><a href="'.insertPageNumber($link, $pageNo + 1).'" title="Nächste Seite" class="ttips"><span class="fa fa-angle-double-right"></span></a></li>'."\n";
+       }
+       else {
+               return '<li class="skip disabled"><span class="fa fa-angle-double-right disabled"></span></li>'."\n";
+       }
+}
\ No newline at end of file
index f141fd02a2c762e6bdbdefba7562474870231ea0..56a59874eb700aa369a461bbcb723c584c211874 100644 (file)
@@ -16,8 +16,43 @@ class IndexPage extends AbstractPage {
                $domains = array();
                $soaIDs = User::getAccessibleDomains();
                $idna = new idna_convert();
+               
+               $sortField = "id";
+               $sortOrder = "ASC";
+               $sqlOrderBy = "";
+               $validSortFields = array('id', 'origin', 'serial');
+               
+               if (isset($_GET['sortField'])) {
+                       if (in_array($_GET['sortField'], $validSortFields)) {
+                               $sortField = $_GET['sortField'];
+                       }
+               }
+               
+               if (isset($_GET['sortOrder'])) {
+                       if ($_GET['sortOrder'] == "ASC" || $_GET['sortOrder'] == "DESC") {
+                               $sortOrder = $_GET['sortOrder'];
+                       }
+               }
+               
+               if (!empty($sortField) && !empty($sortField)) {
+                       $sqlOrderBy = $sortField." ".$sortOrder;
+               }
+               
+               $pageNo = 1;
+               if (isset($_GET['pageNo']) && !empty($_GET['pageNo'])) {
+                       $pageNo = intval($_GET['pageNo']);
+               }
+               
+               $itemsPerPage = 20;
+               $pages = 0;
+               
+               $sqlLimit = $itemsPerPage;
+               $sqlOffset = ($pageNo - 1) * $itemsPerPage;
+               $pages = intval(ceil(count($soaIDs) / $itemsPerPage));
+               
+               
                if (count($soaIDs) > 0) {
-                       $sql = "SELECT * FROM dns_soa WHERE id IN (".str_repeat('?, ', count($soaIDs) - 1). "?)";
+                       $sql = "SELECT * FROM dns_soa WHERE id IN (".str_repeat('?, ', count($soaIDs) - 1). "?)".(!empty($sqlOrderBy) ? " ORDER BY ".$sqlOrderBy : '')." LIMIT " . $sqlLimit . " OFFSET " . $sqlOffset;
                        $res = DNS::getDB()->query($sql, $soaIDs);
                        while ($row = DNS::getDB()->fetch_array($res)) {
                                $sql2 = "SELECT count(*) as count FROM dns_rr WHERE zone = ?";
@@ -29,6 +64,12 @@ class IndexPage extends AbstractPage {
                        }
                }
                
-               DNS::getTPL()->assign(array("domains" => $domains));
+               DNS::getTPL()->assign(array(
+                       'domains' => $domains,
+                       'pageNo' => $pageNo,
+                       'pages' => $pages,
+                       'sortField' => $sortField,
+                       'sortOrder' => $sortOrder
+               ));
        }
 }
index 5593074919c3888a3b549b2e567cb73e1b25216a..c16f383ad2676be2ecb3d70ca299773c2ea7b1bc 100644 (file)
@@ -24,6 +24,44 @@ class RecordListPage extends AbstractPage {
                
                $idna = new idna_convert();
                
+               $sql = "SELECT count(*) as count FROM dns_rr WHERE zone = ?";
+               $res = DNS::getDB()->query($sql, array($_GET['id']));
+               $row = DNS::getDB()->fetch_array($res);
+               $count = $row['count'];
+               
+               $sortField = "type";
+               $sortOrder = "ASC";
+               $sqlOrderBy = "";
+               $validSortFields = array('id', 'name', 'type', 'ttl', 'data');
+               
+               if (isset($_GET['sortField'])) {
+                       if (in_array($_GET['sortField'], $validSortFields)) {
+                               $sortField = $_GET['sortField'];
+                       }
+               }
+               
+               if (isset($_GET['sortOrder'])) {
+                       if ($_GET['sortOrder'] == "ASC" || $_GET['sortOrder'] == "DESC") {
+                               $sortOrder = $_GET['sortOrder'];
+                       }
+               }
+               
+               if (!empty($sortField) && !empty($sortField)) {
+                       $sqlOrderBy = $sortField." ".$sortOrder;
+               }
+               
+               $pageNo = 1;
+               if (isset($_GET['pageNo']) && !empty($_GET['pageNo'])) {
+                       $pageNo = intval($_GET['pageNo']);
+               }
+               
+               $itemsPerPage = 20;
+               $pages = 0;
+               
+               $sqlLimit = $itemsPerPage;
+               $sqlOffset = ($pageNo - 1) * $itemsPerPage;
+               $pages = intval(ceil($count / $itemsPerPage));
+               
                $sql = "SELECT * FROM dns_soa WHERE id = ?";
                $res = DNS::getDB()->query($sql, array($_GET['id']));
                $soa = DNS::getDB()->fetch_array($res);
@@ -32,7 +70,7 @@ class RecordListPage extends AbstractPage {
                
                $records = array();
                
-               $sql = "SELECT * FROM dns_rr WHERE zone = ?";
+               $sql = "SELECT * FROM dns_rr WHERE zone = ?".(!empty($sqlOrderBy) ? " ORDER BY ".$sqlOrderBy : '')." LIMIT " . $sqlLimit . " OFFSET " . $sqlOffset;
                $res = DNS::getDB()->query($sql, array($_GET['id']));
                while ($row = DNS::getDB()->fetch_array($res)) {
                        $row['name'] = $idna->decode($row['name']);
@@ -48,7 +86,7 @@ class RecordListPage extends AbstractPage {
                        }
                        else {
                                if ($row['type'] == "TLSA" || $row['type'] == "DS") {
-                                       $row['data'] = $$row['data'];
+                                       $row['data'] = $row['data'];
                                }
                                else {
                                        $row['data'] = $idna->decode($row['data']);
@@ -57,6 +95,13 @@ class RecordListPage extends AbstractPage {
                        $records[] = $row;
                }
                
-               DNS::getTPL()->assign(array("records" => $records, "soa" => $soa));
+               DNS::getTPL()->assign(array(
+                       'records' => $records,
+                       'soa' => $soa,
+                       'pageNo' => $pageNo,
+                       'pages' => $pages,
+                       'sortField' => $sortField,
+                       'sortOrder' => $sortOrder
+               ));
        }
 }
index 31f64a6c83b53824e589a31ccdc2cee6e3356a20..69ca6bb92792860e69e6d18feab53d929cf94f9e 100644 (file)
                </div>
        </div>
 </div>
-
 <div class="row">
        <div class="col-lg-12">
                <div class="panel panel-default">
                        <div class="panel-heading">Domains <span class="badge badge-black">{$domains|count}</span></div>
                        <div class="panel-body">
+                               {pages controller='DomainList'}
                                <div class="dataTable_wrapper">
-                                       <table class="table table-bordered table-hover radius table-striped dataTable" data-dontsort="4">
+                                       <table class="table table-bordered table-hover radius table-striped dataTable">
                                                <thead>
                                                        <tr>
-                                                               <th>ID</th>
-                                                               <th>Name</th>
-                                                               <th>Serial</th>
+                                                               <th><a class="sorting{if $sortField == 'id'}_{$sortOrder|strtolower}{/if}" href="index.php?page=DomainList&pageNo={$pageNo}&sortField=id&sortOrder={if $sortField == 'id' && $sortOrder == 'ASC'}DESC{else}ASC{/if}">ID</a></th>
+                                                               <th><a class="sorting{if $sortField == 'origin'}_{$sortOrder|strtolower}{/if}" href="index.php?page=DomainList&pageNo={$pageNo}&sortField=origin&sortOrder={if $sortField == 'origin' && $sortOrder == 'ASC'}DESC{else}ASC{/if}">Name</a></th>
+                                                               <th><a class="sorting{if $sortField == 'serial'}_{$sortOrder|strtolower}{/if}" href="index.php?page=DomainList&pageNo={$pageNo}&sortField=serial&sortOrder={if $sortField == 'serial' && $sortOrder == 'ASC'}DESC{else}ASC{/if}">Serial</a></th>
                                                                <th>Records</th>
                                                                <th>Manage</th>
                                                        </tr>
@@ -48,6 +48,7 @@
                                                </tbody>
                                        </table>
                                </div>
+                               {pages controller='DomainList'}
                        </div>
                </div>
        </div>
index 2804ce16c789a7e24a9fb5ebf9cbf060388b4694..7d8f1cb4ea8311e059688bc3d60c30358433f762 100644 (file)
                <div class="panel panel-default">
                        <div class="panel-heading">Records <span class="badge badge-black">{$records|count}</span></div>
                        <div class="panel-body">
+                               {pages controller='RecordList' id=$soa['id']}
                                <div class="dataTable_wrapper">
-                                       <table class="table table-bordered table-hover radius table-striped dataTable" data-dontsort="6" data-sort="asc,3">
+                                       <table class="table table-bordered table-hover radius table-striped dataTable">
                                                <thead>
                                                        <tr>
-                                                               <th>ID</th>
-                                                               <th>Host</th>
-                                                               <th>TTL</th>
-                                                               <th>Type</th>
+                                                               <th><a class="sorting{if $sortField == 'id'}_{$sortOrder|strtolower}{/if}" href="index.php?page=RecordList&id={$soa['id']}&pageNo={$pageNo}&sortField=id&sortOrder={if $sortField == 'id' && $sortOrder == 'ASC'}DESC{else}ASC{/if}">ID</th>
+                                                               <th><a class="sorting{if $sortField == 'name'}_{$sortOrder|strtolower}{/if}" href="index.php?page=RecordList&id={$soa['id']}&pageNo={$pageNo}&sortField=name&sortOrder={if $sortField == 'name' && $sortOrder == 'ASC'}DESC{else}ASC{/if}">Host</th>
+                                                               <th><a class="sorting{if $sortField == 'ttl'}_{$sortOrder|strtolower}{/if}" href="index.php?page=RecordList&id={$soa['id']}&pageNo={$pageNo}&sortField=ttl&sortOrder={if $sortField == 'ttl' && $sortOrder == 'ASC'}DESC{else}ASC{/if}">TTL</th>
+                                                               <th><a class="sorting{if $sortField == 'type'}_{$sortOrder|strtolower}{/if}" href="index.php?page=RecordList&id={$soa['id']}&pageNo={$pageNo}&sortField=type&sortOrder={if $sortField == 'type' && $sortOrder == 'ASC'}DESC{else}ASC{/if}">Type</th>
                                                                <th>Prio</th>
-                                                               <th>Data</th>
+                                                               <th><a class="sorting{if $sortField == 'data'}_{$sortOrder|strtolower}{/if}" href="index.php?page=RecordList&id={$soa['id']}&pageNo={$pageNo}&sortField=data&sortOrder={if $sortField == 'data' && $sortOrder == 'ASC'}DESC{else}ASC{/if}">Data</th>
                                                                <th>Manage</th>
                                                        </tr>
                                                </thead>
@@ -55,6 +56,7 @@
                                                </tbody>
                                        </table>
                                </div>
+                               {pages controller='RecordList' id=$soa['id']}
                        </div>
                </div>
        </div>