<?php
/**
 * Made By Frody: ffrody@gmail.com (H.S.Choi)
 *
 * 본인 이외에 복제/사용 금지
 */

class Pagenation {
  public function __construct(){}
  public function get_pagenation($total_rows, $rows_per_page, $show_pages, $page_num=1){ //전체 데이터수, 1페이지당 열수, Pagenation 갯수, 현재 페이지
    $pages = new stdClass;
    $pages->total_rows = $total_rows;
    $last_page = ceil($total_rows / $rows_per_page);
    $page_num = (int) $page_num;
    if ($page_num < 1) {
      $page_num = 1;
    }
    $upto = ($page_num - 1) * $rows_per_page;
    if( $upto < 0 ){
      $upto = 0;
    }
    $pages->limit = array($upto, $rows_per_page);
    $pages->current = $page_num;
    if ($page_num == 1){
      $pages->prev = $page_num;
    } else {
      $pages->prev = $page_num - 1;
    }
    if ($page_num == $last_page) {
      $pages->next = $last_page;
    } else {
      $pages->next = $page_num + 1;
    }
    $bprev = ceil(($page_num-$show_pages)/$show_pages) * $show_pages;
    if( $bprev < 1 ){
      $pages->bprev = 1;
    }else{
      $pages->bprev = $bprev;
    }
    $bnext = ceil($page_num/$show_pages) * $show_pages + 1;
    if( $bnext > $last_page ){
      $pages->bnext = $last_page;
    }else{
      $pages->bnext = $bnext;
    }
    $pages->last = $last_page;
    $pages->info = "Page ({$page_num} of {$last_page})";
    $pages->pages = $this->get_pages($page_num, $last_page, $pages->next, $show_pages);
    return $pages;
  }
  public function get_pages($page_num, $last_page, $next, $show_pages) {
    $arr = array();

    $start = floor(($page_num-1) / $show_pages)*$show_pages;//주석대신 추가부분
    $end = $start + $show_pages; //주석대신 추가부분

    if ($start < 0) {
      $start = 0; $end = $show_pages;
    }
    for ($i = $start; $i < $end; $i++) {
      if ($i == $last_page){
        break;
      }
      array_push($arr, $i + 1);
    }
    return $arr;
  }
}