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

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

require_once dirname(__FILE__) . "/ImageListAction.class.php";
require_once dirname(dirname(__FILE__)) . "/forms/ImageFilterForm.class.php";

class Myuploader_ImageBoardAction extends Myuploader_ImageListAction
{
    var $mTarget = null;

    function prepare() {
        $root = Xcube_Root::getSingleton();
        $root->mController->setDialogMode(true);
    }

    function getDefaultView()
    {
        $this->mFilter =& $this->_getFilterForm();
        $this->mFilter->fetch();

        $handler =& $this->_getHandler();
        $criteria =& $this->mFilter->getCriteria();

        $this->mTarget = xoops_getrequest('target');

        $catid = xoops_getrequest('category_id');
        $allCategories =& $this->mModuleGlobal->getAllCategories();
        $subscribers = $this->mModuleGlobal->getSubscribers();
        if(!empty($catid)) {
            if(!in_array($catid, $this->mModuleGlobal->getViewable())) {
                return MYUPLOADER_FRAME_VIEW_ERROR;
            }
            $this->mCategory =& $allCategories[$catid];
        }

        $this->mObjects =& $handler->getObjects($criteria);
        foreach(array_keys($this->mObjects) as $id) {
            $obj =& $this->mObjects[$id];
            $obj->mCatName = $allCategories[$obj->get('category_id')]->getShow('cat_name');
            $obj->mUploadable = in_array($obj->get('category_id'), $this->mModuleGlobal->getUploadable());
            $obj->mSubscriber =& $subscribers[$obj->get('image_uid')];
        }

        return MYUPLOADER_FRAME_VIEW_INDEX;
    }


    /**
     * @protected
     */
    function _getBaseUrl()
    {
        return './index.php?op=ImageBoard';
    }

    /**
     * @public
     */

    function executeViewIndex(&$render)
    {
        $render->setTemplateName($this->mAsset->mDirname."_main_image_board.html");
        #cubson::lazy_load_array('image', $this->mObjects);
        $render->setAttribute('objects', $this->mObjects);
        $render->setAttribute('pageNavi', $this->mFilter->mNavi);
        $render->setAttribute('textFilter', $this->mRoot->mTextFilter);

        $category_handler =& $this->mAsset->getObject('handler', 'category');
        $render->setAttribute('categories', $category_handler->getChildTreeArray(0, 'view'));
        $render->setAttribute('hasUploadPerm', is_object($this->mCategory) && in_array($this->mCategory->get('category_id'), $this->mModuleGlobal->getUploadable()));
        $render->setAttribute('category_id', xoops_getrequest('category_id'));
        $render->setAttribute('category', $this->mCategory);
        $render->setAttribute('target', $this->mTarget);
        $render->setAttribute('moduleConfig', $this->mRoot->mContext->mModuleConfig);
        $render->setAttribute('moduleUrl', sprintf('%s/%s', XOOPS_MODULE_URL, $this->mAsset->mDirname));
        $render->setAttribute('uploadDir', str_replace(XOOPS_ROOT_PATH, XOOPS_URL, $this->mModuleGlobal->mTargetDir));
        $render->setAttribute('siteUploadDir', substr($this->mModule->getModuleConfig('upload_dir'), 1));
        $render->setAttribute('tag', ($this->mModule->getModuleConfig('figure_tag'))? 'fig' : 'img');

    }

}

?>