<?php
/**
 * @version $Id: AbstractFilterForm.class.php 679 2012-08-20 12:41:35Z hodaka $
 * @package myuploader
 */

if(!defined('XOOPS_ROOT_PATH'))
{
    exit;
}

/**
 * Myuploader_AbstractFilterForm
 */
class Myuploader_AbstractFilterForm
{
    /**
     * @var  Enum
     *
     * @public
    **/
    var $mSort = 0;

    /**
     * @var  string[]
     *
     * @public
    **/
    var $mSortKeys = array();

    /**
     * @var  XCube_PageNavigator
     *
     * @public
    **/
    var $mNavi = null;

    /**
     * @var  XoopsObjectGenericHandler
     *
     * @protected
    **/
    var $_mHandler = null;

    /**
     * @var  Criteria
     *
     * @protected
    **/
    var $_mCriteria = null;

    /**
     * @var  Myuploader_Global
     *
     * @public
     */
    var $mModuleGlobal;

    /**
     * _getId
     *
     * @param   void
     *
     * @return  int
     *
     * @protected
    **/
    function _getId()
    {
    }

    /**
     * &_getHandler
     *
     * @param   void
     *
     * @return  XoopsObjectGenericHandler
     *
     * @protected
    **/
    function &_getHandler()
    {
    }

    /**
     * Myuploader_AbstractFilterForm
     *
     * @param   void
     *
     * @return  void
     *
     * @protected
    **/
    function Myuploader_AbstractFilterForm()
    {
        $this->_mCriteria = new CriteriaCompo();
        // get module global
        $root = XCube_Root::getSingleton();
        XCube_DelegateUtils::call('Module.myuploader.Event.GetModuleGlobal', new XCube_Ref($this->mModuleGlobal), $root->mContext->mModule->mXoopsModule->get('dirname'));
    }

    /**
     * prepare
     *
     * @param   XCube_PageNavigator  &$navi
     * @param   XoopsObjectGenericHandler  &$handler
     *
     * @return  void
     *
     * @protected
    **/
    function prepare(&$navi, &$handler)
    {
        $this->mNavi =& $navi;
        $this->_mHandler =& $handler;

        $this->mNavi->mGetTotalItems->add(array(&$this, 'getTotalItems'));
    }

    /**
     * getTotalItems
     *
     * @param   int  &$total
     *
     * @return  void
     *
     * @protected
    **/
    function getTotalItems(&$total)
    {
        $total = $this->_mHandler->getCount($this->getCriteria());
    }

    /**
     * fetchSort
     *
     * @param   void
     *
     * @return  void
     *
     * @protected
    **/
    function fetchSort()
    {
        $root = XCube_Root::getSingleton();
        $this->mSort = intval($root->mContext->mRequest->getRequest($this->mNavi->mPrefix . 'sort'));

        if(!isset($this->mSortKeys[abs($this->mSort)]))
        {
            $this->mSort = $this->getDefaultSortKey();
        }

        $this->mNavi->mSort[$this->mNavi->mPrefix . 'sort'] = $this->mSort;
    }

    /**
     * fetch
     *
     * @param   void
     *
     * @return  void
     *
     * @public
    **/
    function fetch()
    {
        $this->mNavi->fetch();
        $this->fetchSort();
    }

    /**
     * getSort
     *
     * @param   void
     *
     * @return  Enum
     *
     * @public
    **/
    function getSort()
    {
        $sortkey = abs($this->mSort);
        return $this->mSortKeys[$sortkey];
    }

    /**
     * getOrder
     *
     * @param   void
     *
     * @return  Enum
     *
     * @public
    **/
    function getOrder()
    {
        return ($this->mSort < 0) ? 'desc' : 'asc';
    }

    /**
     * &getCriteria
     *
     * @param   int  $start
     * @param   int  $limit
     *
     * @return  Criteria
     *
     * @public
    **/
    function &getCriteria($start = null,$limit = null)
    {
        $t_start = ($start === null) ? $this->mNavi->getStart() : intval($start);
        $t_limit = ($limit === null) ? $this->mNavi->getPerpage() : intval($limit);

        $criteria = $this->_mCriteria;

        $criteria->setStart($t_start);
        $criteria->setLimit($t_limit);
        return $criteria;
    }
}

?>