123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502 |
- <?php namespace Maze\Page;
- use Maze\Http\Input;
- use Maze\Config\Load as Config;
- use Maze\Routing\Uri;
- use Maze\Http\Url;
- class Main
- {
- /**
- * PAGE
- *
- * @var string
- */
- const PAGE = 'page/';
-
- /**
- * total
- *
- * @var int
- */
- public $total;
- /**
- * num
- *
- * @var int
- */
- public $num;
- /**
- * maxpage
- *
- * @var int
- */
- public $maxpage;
- /**
- * page
- *
- * @var int
- */
- public $page;
- /**
- * prev
- *
- * @var int
- */
- public $prev;
- /**
- * next
- *
- * @var int
- */
- public $next;
- /**
- * current
- *
- * @var int
- */
- public $current;
- /**
- * template
- *
- * @var string
- */
- public $template;
- /**
- * html
- *
- * @var string
- */
- public $html;
-
- /**
- * link
- *
- * @var string
- */
- public $link;
-
- /**
- * ext
- *
- * @var string
- */
- public $ext = '';
- /**
- * instance
- *
- * @var string
- */
- static protected $instance;
- /**
- * getInstance
- *
- * @return Maze\Plad\Page;
- */
- static public function getInstance($key)
- {
- if(empty(self::$instance[$key]))
- {
- self::$instance[$key] = new self();
- }
- return self::$instance[$key];
- }
- /**
- * getPage
- *
- * @return Maze\Plad\Page;
- */
- static public function getPage($key, $total = false)
- {
- if(is_numeric($total) && $total > 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 . '</' . $tag . '>';
- }
- /**
- * 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.'" ';
- }
- }
|