Pageable Varien Data Collection

The customer Pageable Varien Data Collection

Posted on March 21, 2016 in Magento, PHP

In Magento, the Catalog and other Models extends from Mage_Core_Model_Mysql4_Collection_Abstract will automatically has pageable feature. But, for the normal Varien Data Collection, there is no such pageable function available.

Following code is the working Pageable Varien Data collection:


class Pageable_Varien_Data_Collection extends Varien_Data_Collection
{
    /**
     * Load data
     *
     * @param   bool $printQuery
     * @param   bool $logQuery
     *
     * @return  Pageable_Varien_Data_Collection
     */
    public function load($printQuery = false, $logQuery = false)
    {
        if ($this->isLoaded()) {
            return $this;
        }
        $this->_renderLimit();
        $this->_setIsLoaded();
        return $this;
    }

    /**
     * @return  Pageable_Varien_Data_Collection
     */
    protected function _renderLimit()
    {
        if ($this->_pageSize) {
            $currentPage = $this->getCurPage();
            $pageSize = $this->_pageSize;
            $firstItem = (($currentPage - 1) * $pageSize + 1);
            $lastItem = $firstItem + $pageSize -1;
            $iterator = 1;
            foreach ($this->getItems() as $key => $item) {
                $pos = $iterator;
                $iterator++;
                if ($pos >= $firstItem && $pos <= $lastItem) {
                    continue;
                }
                $this->removeItemByKey($key);
            }
        }
        return $this;
    }

    /**
     * Retrieve collection all items count
     *
     * @return int
     */
    public function getSize()
    {
        if (is_null($this->_totalRecords)) {
            $this->_totalRecords = count($this->getItems());
        }
        return intval($this->_totalRecords);
    }

    /**
     * Retrieve collection items
     *
     * @return array
     */
    public function getItems()
    {
        return $this->_items;
    }
}


Following code is the example to use this Pageable Varien Data Collection:


class Gwiusa_Factory_Block_Factory extends Mage_Core_Block_Template {

    //get Subcategories of factory
    public function getCategories() {
        //Instantiate the Pageable_Varien_Data_Collection Object
        $result = new Pageable_Varien_Data_Collection();  

        $parentCategoryId = 90;
        $parent = Mage::getModel('catalog/category')->load($parentCategoryId);
        $subcats = $parent->getChildren();

        foreach(explode(',',$subcats) as $id) {
            $category = Mage::getModel('catalog/category')->load($id);

            if($category->getIsActive()) {
                $result->addItem($category);
            }
        }
        return $result;
    }

    public function getItemSize() {
        $parentCategoryId = 90;
        $parent = Mage::getModel('catalog/category')->load($parentCategoryId);
        $subcats = $parent->getChildren();

        //Mage::log('subcats:'. $subcats);

        return count(explode(',', $subcats));
    }

    public function __construct() {
        parent::__construct();
        $collection = $this->getCategories();
        $this->setCollection($collection);
    }

    public function _prepareLayout() {
        parent::_prepareLayout();
        $this->getLayout()->getBlock('head')->setTitle(Mage::helper('factory')->__('Factory direct'));
        $pager = $this->getLayout()->createBlock('page/html_pager', 'factory.pager');
        $pager->setAvailableLimit(array(9=>9,15=>15,'all'=>'all'));

        $pager->setCollection($this->getCollection());
        //$pager->setAvailableLimit(array(4=>5,9=>10,19=>20,'all'=>'all'));
        $this->setChild('pager', $pager);
        $this->getCollection()->load();

        return $this;
    }

    public function getPagerHtml() {
        return $this->getChildHtml('pager');
    }
}


I hope this new class will help you in the future.


comments powered by Disqus