<?php
/**
 * @version $Id: ImageViewAction.class.php 535 2008-09-20 04:41:20Z hodaka $
 * @package myuploader
 */

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

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

class Myuploader_ImageViewAction extends Myuploader_AbstractViewAction
{
    var $mNext = null;
    var $mPrev = null;

	/**
	 * @public
	 */
	function _getId()
	{
		return xoops_getrequest('image_id');
	}

	/**
	 * @public
	 */
	function &_getHandler()
	{
		$handler =& $this->mAsset->getObject('handler', 'image');
		return $handler;
	}

    function hasPermission()
    {
        if(!(in_array($this->mObject->get('category_id'), $this->mModuleGlobal->getViewable()))) {
            return false;
        }

        return true;
    }

    function getDefaultView() {
        if ($this->mObject == null) {
            return MYUPLOADER_FRAME_VIEW_ERROR;
        }

        //
        // prev/next
        //
        $catid = xoops_getrequest('category_id');
        $uid = xoops_getrequest('uid');
        $criteria = new criteriaCompo();
        if($catid != null) {
        	$criteria->add(new criteria('category_id', intval($catid)));
        }
        if($uid != null) {
            $criteria->add(new criteria('image_uid', intval($uid)));
        }

        // next
        $ncriteria = new criteriaCompo(new criteria('image_created', $this->mObject->get('image_created'), '>'));
        if($criteria->hasChildElements())
            $ncriteria->add($criteria);
        $ncriteria->setSort('image_created');
        $ncriteria->setOrder('ASC');
        $ncriteria->setLimit(1);
        $objs =& $this->mObjectHandler->getObjects($ncriteria);
        if(count($objs)) {
        	$this->mNext = $objs[0];
        }

        // prev
        $pcriteria = new criteriaCompo(new criteria('image_created', $this->mObject->get('image_created'), '<'));
        if($criteria->hasChildElements())
            $pcriteria->add($criteria);
        $pcriteria->setSort('image_created');
        $pcriteria->setOrder('DESC');
        $pcriteria->setLimit(1);
        $objs =& $this->mObjectHandler->getObjects($pcriteria);
        if(count($objs)) {
            $this->mPrev = $objs[0];
        }


        return MYUPLOADER_FRAME_VIEW_SUCCESS;
    }

	/**
	 * @public
	 */
	function executeViewSuccess(&$render)
	{
		$render->setTemplateName($this->mAsset->mDirname . "_main_image_view.html");
		#cubson::lazy_load('image', $this->mObject);

        $this->mObjectHandler->loadGlobalInfo($this->mObject);
        $render->setAttribute('object', $this->mObject);
        $render->setAttribute('textFilter', $this->mRoot->mTextFilter);

        $render->setAttribute('next', $this->mNext);
        $render->setAttribute('prev', $this->mPrev);
        $render->setAttribute('category_id', xoops_getrequest('category_id'));
        $render->setAttribute('uid', xoops_getrequest('uid'));
        $render->setAttribute('uploadDir', str_replace(XOOPS_ROOT_PATH, XOOPS_URL, $this->mModuleGlobal->mTargetDir));

	}

	/**
	 * @public
	 */
	function executeViewError(&$render)
	{
		$this->mRoot->mController->executeRedirect("./index.php?op=ImageList", 1, _MD_MYUPLOADER_ERROR_CONTENT_IS_NOT_FOUND);
	}
}

?>