From faa510befca7b5f2f990960987931d7b23b92c32 Mon Sep 17 00:00:00 2001 From: Fabrixxm Date: Thu, 13 Jun 2013 08:12:15 -0400 Subject: [PATCH] move html from paginate functions to template --- boot.php | 6 +- include/text.php | 170 ++++++++++++++++++++---------------- view/templates/paginate.tpl | 13 +++ 3 files changed, 114 insertions(+), 75 deletions(-) create mode 100644 view/templates/paginate.tpl diff --git a/boot.php b/boot.php index bed6caa572..0f3f7f67a4 100644 --- a/boot.php +++ b/boot.php @@ -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() { diff --git a/include/text.php b/include/text.php index 9f8114d926..264dd76a5e 100644 --- a/include/text.php +++ b/include/text.php @@ -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 "
".__function__.": ".$e->getMessage()."
"; killme(); + } $a->save_timestamp($stamp1, "rendering"); @@ -260,6 +264,84 @@ function hex2bin($s) { }} +if(! function_exists('paginate_data')) { +/** + * Automatica pagination data. + * + * @param App $a App instance + * @param int $count [optional] item count (used with alt pager) + * @return Array data for pagination template + */ +function paginate_data(&$a, $count=null) { + $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; + + + $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')); + + _l($data, "first", $url."&page=1", t('first')); + + + $numpages = $a->pager['total'] / $a->pager['itemspage']; + + $numstart = 1; + $numstop = $numpages; + + if($numpages > 14) { + $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1); + $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14)); + } + + $pages = array(); + + 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; + + $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 $data; + +}} + if(! function_exists('paginate')) { /** * Automatic pagination. @@ -277,58 +359,11 @@ if(! function_exists('paginate')) { * @return string html for pagination #FIXME remove html */ function paginate(&$a) { - $o = ''; - $stripped = preg_replace('/(&page=[0-9]*)/','',$a->query_string); + + $data = paginate_data($a); + $tpl = get_markup_template("paginate.tpl"); + return replace_macros($tpl, array("pager" => $data)); -// $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 .= '
'; - if($a->pager['page'] != 1) - $o .= ''."pager['page'] - 1).'">' . t('prev') . ' '; - - $o .= "" . t('first') . " "; - - $numpages = $a->pager['total'] / $a->pager['itemspage']; - - $numstart = 1; - $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 .= ''.(($i < 10) ? ' '.$i : $i); - else - $o .= "".(($i < 10) ? ' '.$i : $i).""; - $o .= ' '; - } - - if(($a->pager['total'] % $a->pager['itemspage']) != 0) { - if($i == $a->pager['page']) - $o .= ''.(($i < 10) ? ' '.$i : $i); - else - $o .= "".(($i < 10) ? ' '.$i : $i).""; - $o .= ' '; - } - - $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages); - $o .= "" . t('last') . " "; - - if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0) - $o .= ''."pager['page'] + 1).'">' . t('next') . ''; - $o .= '
'."\r\n"; - } - return $o; }} 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 .= '
'; - - if($a->pager['page']>1) - $o .= "pager['page'] - 1).'" class="pager_newer">' . t('newer') . ''; - if($i>0) { - if($a->pager['page']>1) - $o .= " - "; - $o .= "pager['page'] + 1).'" class="pager_older">' . t('older') . ''; - } - - - $o .= '
'."\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 "
".__function__.": ".$e->getMessage()."
"; killme(); + } $a->save_timestamp($stamp1, "file"); diff --git a/view/templates/paginate.tpl b/view/templates/paginate.tpl new file mode 100644 index 0000000000..68dafc4e9e --- /dev/null +++ b/view/templates/paginate.tpl @@ -0,0 +1,13 @@ +
+ {{if $pager}} + {{ if $pager.prev }}{{ $pager.prev.text }}{{ /if }} + + {{ if $pager.first }}{{ $pager.first.text }}{{ /if }} + + {{ foreach $pager.pages as $p }}{{ $p.text }}{{ /foreach }} + + {{ if $pager.last }}{{ $pager.last.text }}{{ /if }} + + {{ if $pager.next }}{{ $pager.next.text }}{{ /if }} + {{/if}} +
\ No newline at end of file