Browse Source

move html from paginate functions to template

pull/695/head
Fabrixxm 8 years ago
parent
commit
faa510befc
3 changed files with 111 additions and 72 deletions
  1. +5
    -1
      boot.php
  2. +93
    -71
      include/text.php
  3. +13
    -0
      view/templates/paginate.tpl

+ 5
- 1
boot.php View File

@ -635,7 +635,11 @@ if(! class_exists('App')) {
function set_pager_itemspage($n) {
$this->pager['itemspage'] = ((intval($n) > 0) ? intval($n) : 0);
$this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
}
function set_pager_page($n) {
$this->pager['page'] = $n;
$this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
}
function init_pagehead() {


+ 93
- 71
include/text.php View File

@ -27,7 +27,11 @@ function replace_macros($s,$r) {
$a = get_app();
$t = $a->template_engine();
$output = $t->replace_macros($s,$r);
try {
$output = $t->replace_macros($s,$r);
} catch (Exception $e) {
echo "<pre><b>".__function__."</b>: ".$e->getMessage()."</pre>"; killme();
}
$a->save_timestamp($stamp1, "rendering");
@ -260,75 +264,106 @@ function hex2bin($s) {
}}
if(! function_exists('paginate')) {
if(! function_exists('paginate_data')) {
/**
* Automatic pagination.
* Automatica pagination data.
*
* To use, get the count of total items.
* Then call $a->set_pager_total($number_items);
* Optionally call $a->set_pager_itemspage($n) to the number of items to display on each page
* Then call paginate($a) after the end of the display loop to insert the pager block on the page
* (assuming there are enough items to paginate).
* When using with SQL, the setting LIMIT %d, %d => $a->pager['start'],$a->pager['itemspage']
* will limit the results to the correct items for the current page.
* The actual page handling is then accomplished at the application layer.
*
* @param App $a App instance
* @return string html for pagination #FIXME remove html
* @param int $count [optional] item count (used with alt pager)
* @return Array data for pagination template
*/
function paginate(&$a) {
$o = '';
function paginate_data(&$a, $count=null) {
$stripped = preg_replace('/(&page=[0-9]*)/','',$a->query_string);
// $stripped = preg_replace('/&zrl=(.*?)([\?&]|$)/ism','',$stripped);
$stripped = str_replace('q=','',$stripped);
$stripped = trim($stripped,'/');
$pagenum = $a->pager['page'];
$url = $a->get_baseurl() . '/' . $stripped;
if($a->pager['total'] > $a->pager['itemspage']) {
$o .= '<div class="pager">';
if($a->pager['page'] != 1)
$o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
$data = array();
function _l(&$d, $name, $url, $text, $class="") {
$d[$name] = array('url'=>$url, 'text'=>$text, 'class'=>$class);
}
if (!is_null($count)){
// alt pager
if($a->pager['page']>1)
_l($data, "prev", $url.'&page='.($a->pager['page'] - 1), t('newer'));
if($count>0)
_l($data, "next", $url.'&page='.($a->pager['page'] - 1), t('older'));
} else {
// full pager
if($a->pager['total'] > $a->pager['itemspage']) {
if($a->pager['page'] != 1)
_l($data, "prev", $url.'&page='.($a->pager['page'] - 1), t('prev'));
$o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
_l($data, "first", $url."&page=1", t('first'));
$numpages = $a->pager['total'] / $a->pager['itemspage'];
$numpages = $a->pager['total'] / $a->pager['itemspage'];
$numstart = 1;
$numstop = $numpages;
$numstop = $numpages;
if($numpages > 14) {
$numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
$numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
}
for($i = $numstart; $i <= $numstop; $i++){
if($i == $a->pager['page'])
$o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
else
$o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
$o .= '</span> ';
}
if($numpages > 14) {
$numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
$numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
}
if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
if($i == $a->pager['page'])
$o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
else
$o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
$o .= '</span> ';
}
$pages = array();
$lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
$o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
for($i = $numstart; $i <= $numstop; $i++){
if($i == $a->pager['page'])
_l($pages, $i, "#", $i, "current");
else
_l($pages, $i, $url."&page=$i", $i, "n");
}
if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
if($i == $a->pager['page'])
_l($pages, $i, "#", $i, "current");
else
_l($pages, $i, $url."&page=$i", $i, "n");
}
$data['pages'] = $pages;
if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
$o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
$o .= '</div>'."\r\n";
$lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
_l($data, "last", $url."&page=$lastpage", t('last'));
if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
_l($data, "next", $url."&page=".($a->pager['page'] + 1), t('next'));
}
}
return $o;
return $data;
}}
if(! function_exists('paginate')) {
/**
* Automatic pagination.
*
* To use, get the count of total items.
* Then call $a->set_pager_total($number_items);
* Optionally call $a->set_pager_itemspage($n) to the number of items to display on each page
* Then call paginate($a) after the end of the display loop to insert the pager block on the page
* (assuming there are enough items to paginate).
* When using with SQL, the setting LIMIT %d, %d => $a->pager['start'],$a->pager['itemspage']
* will limit the results to the correct items for the current page.
* The actual page handling is then accomplished at the application layer.
*
* @param App $a App instance
* @return string html for pagination #FIXME remove html
*/
function paginate(&$a) {
$data = paginate_data($a);
$tpl = get_markup_template("paginate.tpl");
return replace_macros($tpl, array("pager" => $data));
}}
if(! function_exists('alt_pager')) {
@ -339,27 +374,11 @@ if(! function_exists('alt_pager')) {
* @return string html for pagination #FIXME remove html
*/
function alt_pager(&$a, $i) {
$o = '';
$stripped = preg_replace('/(&page=[0-9]*)/','',$a->query_string);
$stripped = str_replace('q=','',$stripped);
$stripped = trim($stripped,'/');
$pagenum = $a->pager['page'];
$url = $a->get_baseurl() . '/' . $stripped;
$o .= '<div class="pager">';
if($a->pager['page']>1)
$o .= "<a href=\"$url"."&page=".($a->pager['page'] - 1).'" class="pager_newer">' . t('newer') . '</a>';
if($i>0) {
if($a->pager['page']>1)
$o .= "&nbsp;-&nbsp;";
$o .= "<a href=\"$url"."&page=".($a->pager['page'] + 1).'" class="pager_older">' . t('older') . '</a>';
}
$o .= '</div>'."\r\n";
return $o;
$data = paginate_data($a, $i);
$tpl = get_markup_template("paginate.tpl");
return replace_macros($tpl, array('pager' => $data));
}}
@ -564,8 +583,11 @@ function get_markup_template($s, $root = '') {
$a = get_app();
$t = $a->template_engine();
$template = $t->get_template_file($s, $root);
try {
$template = $t->get_template_file($s, $root);
} catch (Exception $e) {
echo "<pre><b>".__function__."</b>: ".$e->getMessage()."</pre>"; killme();
}
$a->save_timestamp($stamp1, "file");


+ 13
- 0
view/templates/paginate.tpl View File

@ -0,0 +1,13 @@
<div class="pager">
{{if $pager}}
{{ if $pager.prev }}<span class="pager_prev {{ $pager.prev.class }}"><a href="{{ $pager.prev.url }}">{{ $pager.prev.text }}</a></span>{{ /if }}
{{ if $pager.first }}<span class="pager_first $pager.first.class"><a href="{{ $pager.first.url }}">{{ $pager.first.text }}</a></span>{{ /if }}
{{ foreach $pager.pages as $p }}<span class="pager_{{ $p.class }}"><a href="{{ $p.url }}">{{ $p.text }}</a></span>{{ /foreach }}
{{ if $pager.last }}<span class="pager_last {{ $pager.last.class }}"><a href="{{ $pager.last.url }}">{{ $pager.last.text }}</a></span>{{ /if }}
{{ if $pager.next }}<span class="pager_next {{ $pager.next.class }}"><a href="{{ $pager.next.url }}">{{ $pager.next.text }}</a></span>{{ /if }}
{{/if}}
</div>

Loading…
Cancel
Save