0) { self::getInstance($key)->total($total); self::getInstance($key)->offset(1); self::getInstance($key)->template($key); self::getInstance($key)->link(); } $result = self::getInstance($key)->handle(); if($total == 'NEXT') { if(self::getInstance($key)->next) { $result = self::getInstance($key)->href(self::getInstance($key)->next); } else { $result = ''; } } return $result; } /** * getTotal * * @return Maze\Plad\Page; */ static public function getTotal($key) { return self::getInstance($key)->total(); } /** * __construct * * @return mixd */ private function __construct() { $this->maxpage = 10; } /** * current * * @return int */ public function current($name = 'page') { $this->current = Input::get($name, 1); return $this->current; } /** * offset * * @return int */ public function offset($num) { $this->num = $num; $offset = $this->num * ($this->current()-1); return $offset; } /** * total * * @return mixd */ public function total($total = false) { if(!$total) { return $this->total ? $this->total : 0; } $this->total = $total; } /** * maxpage * * @return mixd */ public function maxpage($maxpage) { $this->maxpage = $maxpage; } /** * link * * @return mixd */ public function link($link = '') { if(!$link) $link = Uri::$url; if(strpos($link, 'page=') !== false) { $link = preg_replace('/[?|&]page=(\d+)/i', '', $link); } # search if(strpos($link, 'search_') === false) { $search = Input::prefix('search_'); if($search) { $link .= '&' . http_build_query($search); } } $this->link = $link; } /** * template * * @return mixd */ public function template($template) { $this->template = $template; } /** * handle * * @return mixd */ public function handle() { if($this->total < 1) { return ''; } # total page $this->page = ceil($this->total / $this->num); # current page if($this->page < $this->current) { $this->current = $this->page; } if($this->page <= 1) { return ''; } if($this->total > $this->num) { if($this->current > 1) { $this->prev = $this->current-1; } if($this->current < $this->page) { $this->next = $this->current+1; } if($this->page <= $this->maxpage) { $this->start = 1; $this->end = $this->page; } else { $page = intval($this->maxpage/2); if($this->current < $page) { $this->start = 1; } elseif($this->current <= ($this->page - $this->maxpage)) { $this->start = $this->current - $page; } elseif($this->current > $this->page - $this->maxpage && $this->current <= $this->page - $page) { $this->start = $this->current - $page; } elseif($this->current > $this->page - $page) { $this->start = $this->page - $this->maxpage + 1; } $this->end = $this->start + $this->maxpage - 1; if($this->start < 1) { $this->end = $this->current + 1 - $this->start; $this->start = 1; if(($this->end - $this->start) < $this->maxpage) { $this->end = $this->maxpage; } } elseif($this->end > $this->page) { $this->start = $this->page-$this->maxpage+1; $this->end = $this->page; } } } $file = MAZE_PROJECT_PATH . self::PAGE . $this->template . '.php'; if(is_file($file)) { $page = $this; include($file); return $page->get(); } return ''; } /** * get * * @return string */ public function get() { return $this->html; } /** * html * * @return string */ public function html($parent, $child, $prev = array('prev', 'prev'), $next = array('next', 'next'), $current = array('page','current', '', false), $start = false, $end = false, $jump = false, $ext = '') { if($ext) { $this->ext = $ext; } $html = ''; if(empty($current[2])) { $current[2] = ''; } if($start && $this->current > 1) { $html .= $this->set($child, $start[1], 1, $start[0], $current[2]); } if($this->prev) { # prev $html .= $this->set($child, $prev[1], $this->prev, $prev[0], $current[2]); } elseif(isset($prev[2])) { $html .= $this->set($child, $prev[1], $this->current, $prev[0], $current[2]); } if($current[1]) { $i = $this->start; for($i; $i <= $this->end; $i++) { $class = $current[0]; if($i == $this->current) { if(isset($current[3]) && $current[3] == true) { $class = $current[1]; } else { if($class) $class .= ' '; $class .= $current[1]; } } $html .= $this->set($child, $class, $i, $i, $current[2]); } } if($this->next) { # next $html .= $this->set($child, $next[1], $this->next, $next[0], $current[2]); } elseif(isset($next[2])) { $html .= $this->set($child, $next[1], $this->end, $next[0], $current[2]); } if($end && $this->current < $this->end) { $html .= $this->set($child, $end[1], $this->end, $end[0], $current[2]); } if($jump) { $click = 'onclick="var link=\''.$this->href('{1}').'\';location.href=link.replace(\'{1}\', document.getElementById(\'maze_page\').value)"'; $html .= str_replace('{click}', $click, $jump); } $this->html = $this->tag($parent, $html); } /** * set * * @return string */ public function set($child, $class, $num, $name, $type = '') { if($type == 'parent') { $child[1] = 'class="'.$class.'"'; $class = ''; } return $this->tag($child, $this->tag(array('a', $this->attr($class, $this->href($num))), $name)); } /** * tag * * @return string */ public function tag($tag, $content) { if(!$tag) { return $content; } $attr = ''; if(is_array($tag)) { $temp = $tag;unset($tag); $tag = $temp[0]; $attr = $temp[1]; } return '<' . $tag . ' ' . $attr . '>' . $content . ''; } /** * href * * @return string */ public function href($page) { return Url::get($this->link . '&page=' . $page) . $this->ext; } /** * attr * * @return string */ public function attr($class, $href) { return ' class="'.$class.'" href="'.$href.'" '; } }