<?php
/**
 * @version $Id: ImageFilterForm.class.php 680 2012-08-22 03:12:37Z hodaka $
 * @package myuploader
 */

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

require_once dirname(dirname(__FILE__)) . "/class/AbstractFilterForm.class.php";

define('MYUPLOADER_IMAGE_SORT_KEY_IMAGE_ID', 1);
define('MYUPLOADER_IMAGE_SORT_KEY_IMAGE_TITLE', 2);
define('MYUPLOADER_IMAGE_SORT_KEY_CATEGORY_ID', 3);
define('MYUPLOADER_IMAGE_SORT_KEY_IMAGE_DESCRIPTION', 4);
define('MYUPLOADER_IMAGE_SORT_KEY_IMAGE_FILENAME', 5);
define('MYUPLOADER_IMAGE_SORT_KEY_IMAGE_BODYNAME', 6);
define('MYUPLOADER_IMAGE_SORT_KEY_IMAGE_EXTENSION', 7);
define('MYUPLOADER_IMAGE_SORT_KEY_IMAGE_MIMETYPE', 8);
define('MYUPLOADER_IMAGE_SORT_KEY_IMAGE_WIDTH', 9);
define('MYUPLOADER_IMAGE_SORT_KEY_IMAGE_HEIGHT', 10);
define('MYUPLOADER_IMAGE_SORT_KEY_IMAGE_THUMBWIDTH', 11);
define('MYUPLOADER_IMAGE_SORT_KEY_IMAGE_THUMBHEIGHT', 12);
define('MYUPLOADER_IMAGE_SORT_KEY_IMAGE_ACTIVE', 13);
define('MYUPLOADER_IMAGE_SORT_KEY_IMAGE_WEIGHT', 14);
define('MYUPLOADER_IMAGE_SORT_KEY_IMAGE_UID', 15);
define('MYUPLOADER_IMAGE_SORT_KEY_IMAGE_CREATED', 16);
define('MYUPLOADER_IMAGE_SORT_KEY_IMAGE_MODIFIED', 17);
define('MYUPLOADER_IMAGE_SORT_KEY_DEFAULT', MYUPLOADER_IMAGE_SORT_KEY_IMAGE_WEIGHT);

class Myuploader_ImageFilterForm extends Myuploader_AbstractFilterForm
{
    var $mSortKeys = array(
        MYUPLOADER_IMAGE_SORT_KEY_IMAGE_ID => 'image_id',
        MYUPLOADER_IMAGE_SORT_KEY_IMAGE_TITLE => 'image_title',
        MYUPLOADER_IMAGE_SORT_KEY_CATEGORY_ID => 'category_id',
        MYUPLOADER_IMAGE_SORT_KEY_IMAGE_DESCRIPTION => 'image_description',
        MYUPLOADER_IMAGE_SORT_KEY_IMAGE_FILENAME => 'image_filename',
        MYUPLOADER_IMAGE_SORT_KEY_IMAGE_BODYNAME => 'image_bodyname',
        MYUPLOADER_IMAGE_SORT_KEY_IMAGE_EXTENSION => 'image_extension',
        MYUPLOADER_IMAGE_SORT_KEY_IMAGE_MIMETYPE => 'image_mimetype',
        MYUPLOADER_IMAGE_SORT_KEY_IMAGE_WIDTH => 'image_width',
        MYUPLOADER_IMAGE_SORT_KEY_IMAGE_HEIGHT => 'image_height',
        MYUPLOADER_IMAGE_SORT_KEY_IMAGE_THUMBWIDTH => 'image_thumbwidth',
        MYUPLOADER_IMAGE_SORT_KEY_IMAGE_THUMBHEIGHT => 'image_thumbheight',
        MYUPLOADER_IMAGE_SORT_KEY_IMAGE_ACTIVE => 'image_active',
        MYUPLOADER_IMAGE_SORT_KEY_IMAGE_WEIGHT => 'image_weight',
        MYUPLOADER_IMAGE_SORT_KEY_IMAGE_UID => 'image_uid',
        MYUPLOADER_IMAGE_SORT_KEY_IMAGE_CREATED => 'image_created',
        MYUPLOADER_IMAGE_SORT_KEY_IMAGE_MODIFIED => 'image_modified'
    );

    /**
     * @public
     */
    function getDefaultSortKey()
    {
        return MYUPLOADER_IMAGE_SORT_KEY_DEFAULT;
    }

    /**
     * @public
     */
    function fetch()
    {
        parent::fetch();

        $root = XCube_Root::getSingleton();

        // set per page
        $this->mNavi->setPerpage($root->mContext->mModule->getModuleConfig('num_per_page'));

        if (($value = $root->mContext->mRequest->getRequest('category_id')) !== null) {
            $catid = intval($value);
            $this->mNavi->addExtra('category_id', $catid);

            $category_handler =& $this->mModuleGlobal->mAssetManager->getObject('handler', 'category');
            $children = $category_handler->getChildIds($catid);
            $children[] = $catid;

            //$this->_mCriteria->add(new criteria('category_id', '('.implode(',', $children).')', 'IN'));
            $cCriteria = new criteriaCompo();
            foreach($children as $cid) {
                $cCriteria->add(new criteria('category_id', $cid), 'OR');
            }
            $this->_mCriteria->add($cCriteria);
        } elseif(count($this->mModuleGlobal->getViewable())>0) {
            //$this->_mCriteria->add(new criteria('category_id', '('.implode(',', $this->mModuleGlobal->getViewable()).')', 'IN'));
            $cCriteria = new criteriaCompo();
            foreach($this->mModuleGlobal->getViewable() as $cid) {
                $cCriteria->add(new criteria('category_id', $cid), 'OR');
            }
            $this->_mCriteria->add($cCriteria);
        }

        if (($value = $root->mContext->mRequest->getRequest('uid')) !== null) {
            $this->mNavi->addExtra('uid', $value);
            $this->_mCriteria->add(new Criteria('image_uid', $value));
        }

        // for use of image board
        if (($value = $root->mContext->mRequest->getRequest('target')) !== null) {
            $this->mNavi->addExtra('target', $value);
        }

        //$this->_mCriteria->addSort($this->getSort(), $this->getOrder());
        if (abs($this->mSort) != MYUPLOADER_IMAGE_SORT_KEY_IMAGE_WEIGHT) {
            $this->_mCriteria->addSort($this->mSortKeys[MYUPLOADER_IMAGE_SORT_KEY_IMAGE_WEIGHT], 'ASC');
        }
        if (abs($this->mSort) != MYUPLOADER_IMAGE_SORT_KEY_IMAGE_CREATED) {
            $this->_mCriteria->addSort($this->mSortKeys[MYUPLOADER_IMAGE_SORT_KEY_IMAGE_CREATED], 'DESC');
        }
    }
}

?>