40ad0b78ff8a5635c736166edf360de9d0bbb509.svn-base 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. * current
  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. * offset
  157. *
  158. * @return int
  159. */
  160. public function offset($num)
  161. {
  162. $this->num = $num;
  163. $offset = $this->num * ($this->current()-1);
  164. return $offset;
  165. }
  166. /**
  167. * total
  168. *
  169. * @return mixd
  170. */
  171. public function total($total = false)
  172. {
  173. if(!$total)
  174. {
  175. return $this->total ? $this->total : 0;
  176. }
  177. $this->total = $total;
  178. }
  179. /**
  180. * maxpage
  181. *
  182. * @return mixd
  183. */
  184. public function maxpage($maxpage)
  185. {
  186. $this->maxpage = $maxpage;
  187. }
  188. /**
  189. * link
  190. *
  191. * @return mixd
  192. */
  193. public function link($link = '')
  194. {
  195. if(!$link) $link = Uri::$url;
  196. if(strpos($link, 'page=') !== false)
  197. {
  198. $link = preg_replace('/[?|&]page=(\d+)/i', '', $link);
  199. }
  200. # search
  201. if(strpos($link, 'search_') === false)
  202. {
  203. $search = Input::prefix('search_');
  204. if($search)
  205. {
  206. $link .= '&' . http_build_query($search);
  207. }
  208. }
  209. $this->link = $link;
  210. }
  211. /**
  212. * template
  213. *
  214. * @return mixd
  215. */
  216. public function template($template)
  217. {
  218. $this->template = $template;
  219. }
  220. /**
  221. * handle
  222. *
  223. * @return mixd
  224. */
  225. public function handle()
  226. {
  227. if($this->total < 1)
  228. {
  229. return '';
  230. }
  231. # total page
  232. $this->page = ceil($this->total / $this->num);
  233. # current page
  234. if($this->page < $this->current)
  235. {
  236. $this->current = $this->page;
  237. }
  238. if($this->page <= 1)
  239. {
  240. return '';
  241. }
  242. if($this->total > $this->num)
  243. {
  244. if($this->current > 1)
  245. {
  246. $this->prev = $this->current-1;
  247. }
  248. if($this->current < $this->page)
  249. {
  250. $this->next = $this->current+1;
  251. }
  252. if($this->page <= $this->maxpage)
  253. {
  254. $this->start = 1;
  255. $this->end = $this->page;
  256. }
  257. else
  258. {
  259. $page = intval($this->maxpage/2);
  260. if($this->current < $page)
  261. {
  262. $this->start = 1;
  263. }
  264. elseif($this->current <= ($this->page - $this->maxpage))
  265. {
  266. $this->start = $this->current - $page;
  267. }
  268. elseif($this->current > $this->page - $this->maxpage && $this->current <= $this->page - $page)
  269. {
  270. $this->start = $this->current - $page;
  271. }
  272. elseif($this->current > $this->page - $page)
  273. {
  274. $this->start = $this->page - $this->maxpage + 1;
  275. }
  276. $this->end = $this->start + $this->maxpage - 1;
  277. if($this->start < 1)
  278. {
  279. $this->end = $this->current + 1 - $this->start;
  280. $this->start = 1;
  281. if(($this->end - $this->start) < $this->maxpage)
  282. {
  283. $this->end = $this->maxpage;
  284. }
  285. }
  286. elseif($this->end > $this->page)
  287. {
  288. $this->start = $this->page-$this->maxpage+1;
  289. $this->end = $this->page;
  290. }
  291. }
  292. }
  293. $file = MAZE_PROJECT_PATH . self::PAGE . $this->template . '.php';
  294. if(is_file($file))
  295. {
  296. $page = $this;
  297. include($file);
  298. return $page->get();
  299. }
  300. return '';
  301. }
  302. /**
  303. * get
  304. *
  305. * @return string
  306. */
  307. public function get()
  308. {
  309. return $this->html;
  310. }
  311. /**
  312. * html
  313. *
  314. * @return string
  315. */
  316. 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 = '')
  317. {
  318. if($ext)
  319. {
  320. $this->ext = $ext;
  321. }
  322. $html = '';
  323. if(empty($current[2]))
  324. {
  325. $current[2] = '';
  326. }
  327. if($start && $this->current > 1)
  328. {
  329. $html .= $this->set($child, $start[1], 1, $start[0], $current[2]);
  330. }
  331. if($this->prev)
  332. {
  333. # prev
  334. $html .= $this->set($child, $prev[1], $this->prev, $prev[0], $current[2]);
  335. }
  336. elseif(isset($prev[2]))
  337. {
  338. $html .= $this->set($child, $prev[1], $this->current, $prev[0], $current[2]);
  339. }
  340. if($current[1])
  341. {
  342. $i = $this->start;
  343. for($i; $i <= $this->end; $i++)
  344. {
  345. $class = $current[0];
  346. if($i == $this->current)
  347. {
  348. if(isset($current[3]) && $current[3] == true)
  349. {
  350. $class = $current[1];
  351. }
  352. else
  353. {
  354. if($class) $class .= ' ';
  355. $class .= $current[1];
  356. }
  357. }
  358. $html .= $this->set($child, $class, $i, $i, $current[2]);
  359. }
  360. }
  361. if($this->next)
  362. {
  363. # next
  364. $html .= $this->set($child, $next[1], $this->next, $next[0], $current[2]);
  365. }
  366. elseif(isset($next[2]))
  367. {
  368. $html .= $this->set($child, $next[1], $this->end, $next[0], $current[2]);
  369. }
  370. if($end && $this->current < $this->end)
  371. {
  372. $html .= $this->set($child, $end[1], $this->end, $end[0], $current[2]);
  373. }
  374. if($jump)
  375. {
  376. $click = 'onclick="var link=\''.$this->href('{1}').'\';location.href=link.replace(\'{1}\', document.getElementById(\'maze_page\').value)"';
  377. $html .= str_replace('{click}', $click, $jump);
  378. }
  379. $this->html = $this->tag($parent, $html);
  380. }
  381. /**
  382. * set
  383. *
  384. * @return string
  385. */
  386. public function set($child, $class, $num, $name, $type = '')
  387. {
  388. if($type == 'parent')
  389. {
  390. $child[1] = 'class="'.$class.'"';
  391. $class = '';
  392. }
  393. return $this->tag($child, $this->tag(array('a', $this->attr($class, $this->href($num))), $name));
  394. }
  395. /**
  396. * tag
  397. *
  398. * @return string
  399. */
  400. public function tag($tag, $content)
  401. {
  402. if(!$tag)
  403. {
  404. return $content;
  405. }
  406. $attr = '';
  407. if(is_array($tag))
  408. {
  409. $temp = $tag;unset($tag);
  410. $tag = $temp[0];
  411. $attr = $temp[1];
  412. }
  413. return '<' . $tag . ' ' . $attr . '>' . $content . '</' . $tag . '>';
  414. }
  415. /**
  416. * href
  417. *
  418. * @return string
  419. */
  420. public function href($page)
  421. {
  422. return Url::get($this->link . '&page=' . $page) . $this->ext;
  423. }
  424. /**
  425. * attr
  426. *
  427. * @return string
  428. */
  429. public function attr($class, $href)
  430. {
  431. return ' class="'.$class.'" href="'.$href.'" ';
  432. }
  433. }