add current dev version (WIP)
[GitHub/Stricted/Domain-Control-Panel.git] / lib / system / template / plugins / function.pages.php
1 <?php
2 use dns\system\DNS;
3
4 function smarty_function_pages($tagArgs, $tplObj) {
5 // needed params: controller, pageNo, pages
6 if (!isset($tagArgs['controller'])) throw new Exception("missing 'controller' argument in pages tag");
7 if (!isset($tagArgs['pageNo'])) {
8 if (($tagArgs['pageNo'] = $tplObj->smarty->getTemplateVars('pageNo')) === null) {
9 throw new Exception("missing 'pageNo' argument in pages tag");
10 }
11 }
12 if (!isset($tagArgs['pages'])) {
13 if (($tagArgs['pages'] = $tplObj->smarty->getTemplateVars('pages')) === null) {
14 throw new Exception("missing 'pages' argument in pages tag");
15 }
16 }
17
18 $html = '';
19
20 if ($tagArgs['pages'] > 1) {
21 $link = "index.php?".$tagArgs['controller'].(isset($tagArgs['id']) ? "/".$tagArgs['id'] : "");
22
23 if (!isset($tagArgs['pageNo'])) {
24 if (($tagArgs['pageNo'] = $tplObj->smarty->getTemplateVars('pageNo')) === null) {
25 $tagArgs['pageNo'] = 0;
26 }
27 }
28
29 // open div and ul
30 $html .= "<nav>\n<ul class='pagination'>\n";
31
32 // previous page
33 $html .= makePreviousLink($link, $tagArgs['pageNo']);
34
35 // first page
36 $html .= makeLink($link, 1, $tagArgs['pageNo'], $tagArgs['pages']);
37
38 // calculate page links
39 $maxLinks = 7;
40 $linksBeforePage = $tagArgs['pageNo'] - 2;
41 if ($linksBeforePage < 0) $linksBeforePage = 0;
42 $linksAfterPage = $tagArgs['pages'] - ($tagArgs['pageNo'] + 1);
43 if ($linksAfterPage < 0) $linksAfterPage = 0;
44 if ($tagArgs['pageNo'] > 1 && $tagArgs['pageNo'] < $tagArgs['pages']) {
45 $maxLinks--;
46 }
47
48 $half = $maxLinks / 2;
49 $left = $right = $tagArgs['pageNo'];
50 if ($left < 1) $left = 1;
51 if ($right < 1) $right = 1;
52 if ($right > $tagArgs['pages'] - 1) $right = $tagArgs['pages'] - 1;
53
54 if ($linksBeforePage >= $half) {
55 $left -= $half;
56 }
57 else {
58 $left -= $linksBeforePage;
59 $right += $half - $linksBeforePage;
60 }
61
62 if ($linksAfterPage >= $half) {
63 $right += $half;
64 }
65 else {
66 $right += $linksAfterPage;
67 $left -= $half - $linksAfterPage;
68 }
69
70 $right = intval(ceil($right));
71 $left = intval(ceil($left));
72 if ($left < 1) $left = 1;
73 if ($right > $tagArgs['pages']) $right = $tagArgs['pages'];
74
75 // left ... links
76 if ($left > 1) {
77 if ($left - 1 < 2) {
78 $html .= makeLink($link, 2, $tagArgs['pageNo'], $tagArgs['pages']);
79 }
80 else {
81 $html .= '<li class="button jumpTo"><a>&hellip;</a></li>'."\n";
82 }
83 }
84
85 // visible links
86 for ($i = $left + 1; $i < $right; $i++) {
87 $html .= makeLink($link, $i, $tagArgs['pageNo'], $tagArgs['pages']);
88 }
89
90 // right ... links
91 if ($right < $tagArgs['pages']) {
92 if ($tagArgs['pages'] - $right < 2) {
93 $html .= makeLink($link, $tagArgs['pages'] - 1, $tagArgs['pageNo'], $tagArgs['pages']);
94 }
95 else {
96 $html .= '<li class="button jumpTo"><a>&hellip;</a></li>'."\n";
97 }
98 }
99
100 // last page
101 $html .= makeLink($link, $tagArgs['pages'], $tagArgs['pageNo'], $tagArgs['pages']);
102
103 // next page
104 $html .= makeNextLink($link, $tagArgs['pageNo'], $tagArgs['pages']);
105
106 // close div and ul
107 $html .= "</ul></nav>\n";
108 }
109
110 // assign html output to template var
111 if (isset($tagArgs['assign'])) {
112 $tplObj->assign($tagArgs['assign'], $html);
113 }
114
115 return $html;
116 }
117
118 function insertPageNumber($link, $pageNo) {
119 $link = $link ."&pageNo=".$pageNo;
120 return $link;
121 }
122
123 function makeLink($link, $pageNo, $activePage, $pages) {
124 // first page
125 if ($activePage != $pageNo) {
126 return '<li><a href="'.insertPageNumber($link, $pageNo).'" class="ttips" title="'.DNS::getLanguageVariable('pagination.page', array('page' => $pageNo)).'">'.intval($pageNo).'</a></li>'."\n";
127 }
128 else {
129 return '<li class="active"><a>'.intval($pageNo).'</a></li>'."\n";
130 }
131 }
132
133 function makePreviousLink($link, $pageNo) {
134 if ($pageNo > 1) {
135 return '<li class="skip"><a href="'.insertPageNumber($link, $pageNo - 1).'" title="'.DNS::getLanguageVariable('pagination.previous').'" class="ttips"><span class="fa fa-angle-double-left"></span></a></li>'."\n";
136 }
137 else {
138 return '<li class="skip disabled"><span class="fa fa-angle-double-left disabled"></span></li>'."\n";
139 }
140 }
141
142
143 function makeNextLink($link, $pageNo, $pages) {
144 if ($pageNo && $pageNo < $pages) {
145 return '<li class="skip"><a href="'.insertPageNumber($link, $pageNo + 1).'" title="'.DNS::getLanguageVariable('pagination.next').'" class="ttips"><span class="fa fa-angle-double-right"></span></a></li>'."\n";
146 }
147 else {
148 return '<li class="skip disabled"><span class="fa fa-angle-double-right disabled"></span></li>'."\n";
149 }
150 }