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

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

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

class Myuploader_ImageListAction extends Myuploader_AbstractListAction
{
    var $mCategory = null;

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

    /**
     * @protected
     */
    function &_getFilterForm()
    {
        // $filter = new Myuploader_ImageFilterForm();
        $filter =& $this->mAsset->getObject('filter', 'image');
        $filter->prepare($this->_getPageNavi(), $this->_getHandler());
        return $filter;
    }

    function getDefaultView()
    {
        $this->mFilter =& $this->_getFilterForm();
        $this->mFilter->fetch();
        $criteria =& $this->mFilter->getCriteria();
        $handler =& $this->_getHandler();

        $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, true);
        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=ImageList";
    }

    /**
     * @public
     */
    function executeViewIndex(&$render)
    {
        $render->setTemplateName($this->mAsset->mDirname."_main_image_list.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);

        $render->setAttribute('category_id', is_object($this->mCategory)? $this->mCategory->getShow('category_id') : 0);
        $render->setAttribute('category', $this->mCategory);
        $category_handler =& $this->mAsset->getObject('handler', 'category');
        $render->setAttribute('categories', $category_handler->getChildTreeArray(0, 'view'));
        $render->setAttribute('moduleConfig', $this->mRoot->mContext->mModuleConfig);
        $cycle = array();
        for($i=0; $i<$this->mRoot->mContext->mModule->getModuleConfig('num_per_line')-1; $i++) {
            $cycle[] = '</li>';
        }
        $cycle[] = '</li></ul></div></div><div class="albumOuter"><div class="albumInner"><ul class="album">';
        $render->setAttribute('cycle', $cycle);
        $render->setAttribute('uploadable', is_object($this->mCategory) && in_array($this->mCategory->get('category_id'), $this->mModuleGlobal->getUploadable()));
        $render->setAttribute('moduleUrl', sprintf('%s/modules/%s', XOOPS_URL, $this->mAsset->mDirname));
        $render->setAttribute('uploadDir', str_replace(XOOPS_ROOT_PATH, XOOPS_URL, $this->mModuleGlobal->mTargetDir));
        $render->setAttribute('multiSubscribers', (count($this->mModuleGlobal->getSubscribers()) > 1));

    }
}

?>