<?php 
/**
 * File			: Paging.php
 * Description  : Library untuk paging frontend website, diload pake classmap di Config/Autoload.php
 * Created By	: Hendi
 * Created Date : 4 Feb 2021
 * Last Update  : 5 Feb 2021
**/
class Paging {
    # Fungsi untuk mencek halaman dan posisi data
    public function getPosition($limit, $page)
	{
        if (empty($page))
		{
        	$position = 0;
        	$page = 1;
        }
		else
		{
        	$position = ($page - 1) * $limit;
        }
        
        return $position;
    }

    # Fungsi untuk menghitung total halagenda
    function getCountPage($count, $limit)
	{
        $count_page = ceil($count / $limit);
        return $count_page;
    }

    # Fungsi untuk link halaman 1,2,3 (untuk admin)
    function getNavPage($type = "", $key, $page_active, $count_page)
	{
        $link_page = "";

        # Link ke halaman pertama (first) dan sebelumnya (prev)
        if ($type!="") 
		{
            if ($page_active > 1) 
			{
            	$prev = $page_active-1;
            	$link_page .= "<a href='".base_url("/".$type."/".$key."/1")."'>&laquo;</a>
                                  <a href='".base_url("/".$type."/".$key."/".$prev)."'>&lsaquo;</a>";
            }
			else
			{ 
            	$link_page .= "<a href='".base_url("/".$type."/".$key."/1")."'>&laquo;</a>
            					  <a href='".base_url("/".$type."/".$key."/1")."'>&lsaquo;</a>";
            }
        }
		else
		{
            if($page_active > 1)
			{
            	$prev = $page_active-1;
            	$link_page .= "<a href='".base_url("/".$key."/1")."'>&laquo;</a>
                                  <a href='".base_url("/".$key."/".$prev)."'>&lsaquo;</a>";
            }
			else
			{ 
            	$link_page .= "<a href='".base_url("/".$key."/1")."'>&laquo;</a>
            					  <a href='".base_url("/".$key."/1")."'>&lsaquo;</a>";
            }            
        }

        # Link halaman 1,2,3, ...
        $number = ($page_active > 3 ? " ... " : " "); 
        
        for ($i=$page_active-2; $i<$page_active; $i++)
		{
            if ($type != "")
			{
                if ($i < 1)
				{
                    continue;
                    $number .= "<a href='".base_url("/".$type."/".$key."/".$i)."'>".$i."</a>";
                }
            }
			else
			{
                if ($i < 1)
				{
                    continue;
                    $number .= "<a href='".base_url("/".$key."/".$i)."'>".$i."</a>";
                }    
            }
        }
            
	    $number .= "<strong>".$page_active."</strong>";
        //$number .= "<span class=\"current\">".$page_active."</span>";
        
        if ($type != "")
		{
            for ($i=$page_active+1; $i<($page_active+3); $i++)
			{
                if ($i > $count_page)
					break;
    	        $number .= "<a href='".base_url("/".$type."/".$key."/".$i)."'>".$i."</a>";
            }
        }
		else
		{
            for ($i=$page_active+1; $i<($page_active+3); $i++)
			{
                if($i > $count_page)
					break;
    	        $number .= "<a href='".base_url("/".$key."/".$i)."'>".$i."</a>";
            }
        }
        
        if ($type!="")
		{
            $number .= ($page_active+2<$count_page ? " ... <a href='".base_url("/".$type."/".$key."/".$count_page)."'>".$count_page."</a> " : " ");
        }
		else
		{
            $number .= ($page_active+2<$count_page ? " ... <a href='".base_url("/".$key."/".$count_page)."'>".$count_page."</a> " : " ");
        }
		
        $link_page .= $number;
    
        # Link ke halaman berikutnya (Next) dan terakhir (Last) 
        if ($type!="")
		{
            if ($page_active < $count_page)
			{
            	$next = $page_active+1;
            	$link_page .= "<a href='".base_url("/".$type."/".$key."/".$next)."'>&rsaquo;</a>
                                  <a href='".base_url("/".$type."/".$key."/".$count_page)."'>&raquo;</a>";
            } 
			else
			{
            	$link_page .= "<a href='".base_url("/".$type."/".$key."/".$count_page)."'>&rsaquo;</a>
                                 <a href='".base_url("/".$type."/".$key."/".$count_page)."'>&raquo;</a>";
            }
        }
		else
		{
            if ($page_active < $count_page)
			{
            	$next = $page_active+1;
            	$link_page .= "<a href='".base_url("/".$key."/".$next)."'>&rsaquo;</a>
                                  <a href='".base_url("/".$key."/".$count_page)."'>&raquo;</a>";
            }
			else
			{
            	$link_page .= "<a href='".base_url("/".$key."/".$count_page)."'>&rsaquo;</a>
                                 <a href='".base_url("/".$key."/".$count_page)."'>&raquo;</a>";
            }
        }
        
        return $link_page;
    }
}
?>