fbf50a6153662f6189f3c42c47ee33a597099eba.svn-base 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <?php namespace Maze\Page;
  2. use Maze\Http\Input;
  3. use Maze\Config\Load as Config;
  4. use Maze\Routing\Uri;
  5. use Maze\Http\Url;
  6. class Main
  7. {
  8. /**
  9. * PAGE
  10. *
  11. * @var string
  12. */
  13. const PAGE = 'page/';
  14. /**
  15. * total
  16. *
  17. * @var int
  18. */
  19. public $total;
  20. /**
  21. * num
  22. *
  23. * @var int
  24. */
  25. public $num;
  26. /**
  27. * maxpage
  28. *
  29. * @var int
  30. */
  31. public $maxpage;
  32. /**
  33. * page
  34. *
  35. * @var int
  36. */
  37. public $page;
  38. /**
  39. * prev
  40. *
  41. * @var int
  42. */
  43. public $prev;
  44. /**
  45. * next
  46. *
  47. * @var int
  48. */
  49. public $next;
  50. /**
  51. * current
  52. *
  53. * @var int
  54. */
  55. public $current;
  56. /**
  57. * template
  58. *
  59. * @var string
  60. */
  61. public $template;
  62. /**
  63. * html
  64. *
  65. * @var string
  66. */
  67. public $html;
  68. /**
  69. * link
  70. *
  71. * @var string
  72. */
  73. public $link;
  74. /**
  75. * ext
  76. *
  77. * @var string
  78. */
  79. public $ext = '';
  80. /**
  81. * instance
  82. *
  83. * @var string
  84. */
  85. static protected $instance;
  86. /**
  87. * getInstance
  88. *
  89. * @return Maze\Plad\Page;
  90. */
  91. static public function getInstance($key)
  92. {
  93. if(empty(self::$instance[$key]))
  94. {
  95. self::$instance[$key] = new self();
  96. }
  97. return self::$instance[$key];
  98. }
  99. /**
  100. * getPage
  101. *
  102. * @return Maze\Plad\Page;
  103. */
  104. static public function getPage($key, $total = false)
  105. {
  106. if(is_numeric($total) && $total > 0)
  107. {
  108. self::getInstance($key)->total($total);
  109. self::getInstance($key)->offset(1);
  110. self::getInstance($key)->template($key);
  111. self::getInstance($key)->link();
  112. }
  113. $result = self::getInstance($key)->handle();
  114. if($total == 'NEXT')
  115. {
  116. if(self::getInstance($key)->next)
  117. {
  118. $result = self::getInstance($key)->href(self::getInstance($key)->next);
  119. }
  120. else
  121. {
  122. $result = '';
  123. }
  124. }
  125. return $result;
  126. }
  127. /**
  128. * getTotal
  129. *
  130. * @return Maze\Plad\Page;
  131. */
  132. static public function getTotal($key)
  133. {
  134. return self::getInstance($key)->total();
  135. }
  136. /**
  137. * __construct
  138. *
  139. * @return mixd
  140. */
  141. private function __construct()
  142. {
  143. $this->maxpage = 10;
  144. }
  145. /**
  146. * getCurrent
  147. *
  148. * @return int
  149. */
  150. public function current($name = 'page')
  151. {
  152. $this->current = Input::get($name, 1);
  153. return $this->current;
  154. }
  155. /**
  156. * getPage
  157. *
  158. * @return int
  159. */
  160. public function getAllPage()
  161. {
  162. if(!$this->total)
  163. {
  164. return 0;
  165. }
  166. $this->page = ceil($this->total / $this->num);
  167. return $this->page;
  168. }
  169. /**
  170. * offset
  171. *
  172. * @return int
  173. */
  174. public function offset($num)
  175. {
  176. $this->num = $num;
  177. $offset = $this->num * ($this->current()-1);
  178. return $offset;
  179. }
  180. /**
  181. * total
  182. *
  183. * @return mixd
  184. */
  185. public function total($total = false)
  186. {
  187. if(!$total)
  188. {
  189. return $this->total ? $this->total : 0;
  190. }
  191. $this->total = $total;
  192. }
  193. /**
  194. * maxpage
  195. *
  196. * @return mixd
  197. */
  198. public function maxpage($maxpage)
  199. {
  200. $this->maxpage = $maxpage;
  201. }
  202. /**
  203. * link
  204. *
  205. * @return mixd
  206. */
  207. public function link($link = '')
  208. {
  209. if(!$link) $link = Uri::$url;
  210. if(strpos($link, 'page=') !== false)
  211. {
  212. $link = preg_replace('/[?|&]page=(\d+)/i', '', $link);
  213. }
  214. # search
  215. if(strpos($link, 'search_') === false)
  216. {
  217. $search = Input::prefix('search_');
  218. if($search)
  219. {
  220. $link .= '&' . http_build_query($search);
  221. }
  222. }
  223. $this->link = $link;
  224. return $this->link;
  225. }
  226. /**
  227. * template
  228. *
  229. * @return mixd
  230. */
  231. public function template($template)
  232. {
  233. $this->template = $template;
  234. }
  235. /**
  236. * handle
  237. *
  238. * @return mixd
  239. */
  240. public function handle()
  241. {
  242. if($this->total < 1)
  243. {
  244. return '';
  245. }
  246. # total page
  247. $this->getAllPage();
  248. //$this->page = ceil($this->total / $this->num);
  249. # current page
  250. if($this->page < $this->current)
  251. {
  252. $this->current = $this->page;
  253. }
  254. if($this->page <= 1)
  255. {
  256. return '';
  257. }
  258. if($this->total > $this->num)
  259. {
  260. if($this->current > 1)
  261. {
  262. $this->prev = $this->current-1;
  263. }
  264. if($this->current < $this->page)
  265. {
  266. $this->next = $this->current+1;
  267. }
  268. if($this->page <= $this->maxpage)
  269. {
  270. $this->start = 1;
  271. $this->end = $this->page;
  272. }
  273. else
  274. {
  275. $page = intval($this->maxpage/2);
  276. if($this->current < $page)
  277. {
  278. $this->start = 1;
  279. }
  280. elseif($this->current <= ($this->page - $this->maxpage))
  281. {
  282. $this->start = $this->current - $page;
  283. }
  284. elseif($this->current > $this->page - $this->maxpage && $this->current <= $this->page - $page)
  285. {
  286. $this->start = $this->current - $page;
  287. }
  288. elseif($this->current > $this->page - $page)
  289. {
  290. $this->start = $this->page - $this->maxpage + 1;
  291. }
  292. $this->end = $this->start + $this->maxpage - 1;
  293. if($this->start < 1)
  294. {
  295. $this->end = $this->current + 1 - $this->start;
  296. $this->start = 1;
  297. if(($this->end - $this->start) < $this->maxpage)
  298. {
  299. $this->end = $this->maxpage;
  300. }
  301. }
  302. elseif($this->end > $this->page)
  303. {
  304. $this->start = $this->page-$this->maxpage+1;
  305. $this->end = $this->page;
  306. }
  307. }
  308. }
  309. $file = MAZE_PROJECT_PATH . self::PAGE . $this->template . '.php';
  310. if(is_file($file))
  311. {
  312. $page = $this;
  313. include($file);
  314. return $page->get();
  315. }
  316. return '';
  317. }
  318. /**
  319. * get
  320. *
  321. * @return string
  322. */
  323. public function get()
  324. {
  325. return $this->html;
  326. }
  327. /**
  328. * html
  329. *
  330. * @return string
  331. */
  332. 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 = '')
  333. {
  334. if($ext)
  335. {
  336. $this->ext = $ext;
  337. }
  338. $html = '';
  339. if(empty($current[2]))
  340. {
  341. $current[2] = '';
  342. }
  343. if($start && $this->current > 1)
  344. {
  345. $html .= $this->set($child, $start[1], 1, $start[0], $current[2]);
  346. }
  347. if($this->prev)
  348. {
  349. # prev
  350. $html .= $this->set($child, $prev[1], $this->prev, $prev[0], $current[2]);
  351. }
  352. elseif(isset($prev[2]))
  353. {
  354. $html .= $this->set($child, $prev[1], $this->current, $prev[0], $current[2]);
  355. }
  356. if($current[1])
  357. {
  358. $i = $this->start;
  359. for($i; $i <= $this->end; $i++)
  360. {
  361. $class = $current[0];
  362. if($i == $this->current)
  363. {
  364. if(isset($current[3]) && $current[3] == true)
  365. {
  366. $class = $current[1];
  367. }
  368. else
  369. {
  370. if($class) $class .= ' ';
  371. $class .= $current[1];
  372. }
  373. }
  374. $html .= $this->set($child, $class, $i, $i, $current[2]);
  375. }
  376. }
  377. if($this->next)
  378. {
  379. # next
  380. $html .= $this->set($child, $next[1], $this->next, $next[0], $current[2]);
  381. }
  382. elseif(isset($next[2]))
  383. {
  384. $html .= $this->set($child, $next[1], $this->end, $next[0], $current[2]);
  385. }
  386. if($end && $this->current < $this->page)
  387. {
  388. $html .= $this->set($child, $end[1], $this->page, $end[0], $current[2]);
  389. }
  390. if($jump)
  391. {
  392. $click = 'onclick="var link=\''.$this->href('{1}').'\';location.href=link.replace(\'{1}\', document.getElementById(\'maze_page\').value)"';
  393. $html .= str_replace('{click}', $click, $jump);
  394. }
  395. $this->html = $this->tag($parent, $html);
  396. }
  397. /**
  398. * set
  399. *
  400. * @return string
  401. */
  402. public function set($child, $class, $num, $name, $type = '')
  403. {
  404. if($type == 'parent')
  405. {
  406. $child[1] = 'class="'.$class.'"';
  407. $class = '';
  408. }
  409. return $this->tag($child, $this->tag(array('a', $this->attr($class, $this->href($num))), $name));
  410. }
  411. /**
  412. * tag
  413. *
  414. * @return string
  415. */
  416. public function tag($tag, $content)
  417. {
  418. if(!$tag)
  419. {
  420. return $content;
  421. }
  422. $attr = '';
  423. if(is_array($tag))
  424. {
  425. $temp = $tag;unset($tag);
  426. $tag = $temp[0];
  427. $attr = $temp[1];
  428. }
  429. return '<' . $tag . ' ' . $attr . '>' . $content . '</' . $tag . '>';
  430. }
  431. /**
  432. * href
  433. *
  434. * @return string
  435. */
  436. public function href($page)
  437. {
  438. return Url::get($this->link . '&page=' . $page) . $this->ext;
  439. }
  440. /**
  441. * attr
  442. *
  443. * @return string
  444. */
  445. public function attr($class, $href)
  446. {
  447. return ' class="'.$class.'" href="'.$href.'" ';
  448. }
  449. }