<?php
/**
 * Netgo_News extension
 * 
 * NOTICE OF LICENSE
 * 
 * This source file is subject to the MIT License
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/mit-license.php
 * 
 * @category       Netgo
 * @package        Netgo_News
 * @copyright      Copyright (c) 2015
 * @license        http://opensource.org/licenses/mit-license.php MIT License
 */
/**
 * News list block
 *
 * @category    Netgo
 * @package     Netgo_News
 * @author Ultimate Module Creator
 */
class Netgo_News_Block_News_List
    extends Mage_Core_Block_Template {
    /**
     * initialize
     * @access public
     * @author Ultimate Module Creator
     */
     public function __construct(){
        parent::__construct();
         $newss = Mage::getResourceModel('netgo_news/news_collection')
                         ->addStoreFilter(Mage::app()->getStore())
                         ->addFieldToFilter('status', 1);
        $newss->setOrder('title', 'asc');
        $this->setNewss($newss);
    }
    /**
     * prepare the layout
     * @access protected
     * @return Netgo_News_Block_News_List
     * @author Ultimate Module Creator
     */
    protected function _prepareLayout(){
        parent::_prepareLayout();
        $pager = $this->getLayout()->createBlock('page/html_pager', 'netgo_news.news.html.pager')
            ->setCollection($this->getNewss());
			  $pager->setAvailableLimit(array(3=>3,6=>6,9=>9,'all'=>'all'));
		$pager->setCollection($this->getNewss());
        $this->setChild('pager', $pager);
        $this->getNewss()->load();
        return $this;
    }
    /**
     * get the pager html
     * @access public
     * @return string
     * @author Ultimate Module Creator
     */
	   private $_itemPerPage = 3;
    private $_pageFrame = 3;
    private $_curPage = 1;
    public function getPagerHtml(){
        return $this->getChildHtml('pager');
    }
	
	public function getCollectionc($collection = 'null')
    {
        if($collection != 'null'){
            $page = $this->getRequest()->getParam('p');
            if($page) $this->_curPage = $page;
            
            $collection->setCurPage($this->_curPage);
            $collection->setPageSize(3);
            return $collection;
        }
    }
	 public function getPagersHtml($collection = 'null')
    {    
        $html = false;
        if($collection == 'null') return;
		
		
        if($collection->count() > $this->_itemPerPage)
        {
		
            $curPage = $this->getRequest()->getParam('p');
            $pager = (int)($collection->count() / $this->_itemPerPage);
            $count = ($collection->count() % $this->_itemPerPage == 0) ? $pager : $pager + 1 ;
            $url = $this->getPagerUrl();
            $start = 1;
            $end = $this->_pageFrame;
            $pre = $curPage - 1; 
            $html .= '<ul>';
            if(isset($curPage) && $curPage != 1){
                $start = $curPage - 1;                                        
                $end = $start + $this->_pageFrame;
            }else{
                $end = $start + $this->_pageFrame;
            }
            if($end > $count){
                $start = $count - ($this->_pageFrame-1);
            }else{
                $count = $end-1;
            }
             $html .='<li><a href="'.$url.'?p='.$pre.'">Previous</a></li>';
            for($i = $start; $i<=$count; $i++)
            {
                if($i >= 1){
				   
                    if($curPage){
                        $html .= ($curPage == $i) ? '<li class="current">'. $i .',</li>' : '<li><a href="'.$url.'?p='.$i.'">'. $i .',</a></li>';
                    }else{
                        $html .= ($i == 1) ? '<li class="current">'. $i .',</li>' : '<li><a href="'.$url.'?p='.$i.'">'. $i .' > '.',</a></li>';
                    }
                }
                
            }
            
            $html .= '</ul>';
        }
        
        return $html;
    }
    
    public function getPagerUrl()   
    {
        //$cur_url = mage::helper('core/url')->getCurrentUrl();
	   
	    $cur_url =Mage::getBaseUrl().'netgo_news/news/index';
        $new_url = preg_replace('/\&p=.*/', '', $cur_url);
        
        return $new_url;
    }
}
