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

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

//require_once XOOPS_TRUST_PATH."/modules/$mytrustdirname/class/AbstractEditAction.class.php";
require_once dirname(dirname(dirname(__FILE__)))."/class/AbstractEditAction.class.php";

class Myuploader_Admin_CategoryEditAction extends Myuploader_AbstractEditAction
{
    /**
     * @protected
     */
    function _getId()
    {
        return xoops_getrequest('category_id');
    }

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

    function _setupObject() {
        parent::_setupObject();

        // group permission
        if(!$this->mObject->isNew()) {
            $this->mObject->mCatViewGroups = $this->mModuleGlobal->groupsByCat('cat_view', $this->mObject->get('category_id'));
            $this->mObject->mCatUploadGroups = $this->mModuleGlobal->groupsByCat('cat_upload', $this->mObject->get('category_id'));
            $this->mObject->mCatHtmlAllowedGroups = $this->mModuleGlobal->groupsByCat('html_allowed', $this->mObject->get('category_id'));;
            $this->mObject->mParentIds = $this->mObjectHandler->getAllParentIds($this->mObject->get('cat_pid'));
        }
    }

    /**
     * @protected
     */
    function _setupActionForm()
    {
        // $this->mActionForm = new Myuploader_Admin_CategoryEditForm();
        $this->mActionForm =& $this->mAsset->getObject('form', 'category', true, 'edit');
        $this->mActionForm->prepare();
    }

    function _doExecute() {
        $result = parent::_doExecute();
        if($result == MYUPLOADER_FRAME_VIEW_ERROR) {
            return $result;
        }

        if(!$this->_gpermRefresh()) {
            return MYUPLOADER_FRAME_VIEW_ERROR;
        }

        return MYUPLOADER_FRAME_VIEW_SUCCESS;
    }

    function _gpermRefresh() {
        $gperm_handler =& xoops_gethandler('groupperm');

        //
        // group permission
        //
        // delete once
        if(!$this->mObject->isNew()) {
            $criteria = new criteriaCompo(new criteria('gperm_modid', $this->mModule->mXoopsModule->get('mid')));
            $criteria->add(new criteria('gperm_itemid', $this->mObject->get('category_id')));
            $criteria->add(new criteria('gperm_name', '("cat_view", "cat_upload", "html_allowed")', 'IN'));
            if(!$gperm_handler->deleteAll($criteria)) {
                return false;
            }
            unset($criteria);
        }

        // insert
        if(isset($this->mObject->mCatViewGroups) && count($this->mObject->mCatViewGroups)) {
            foreach($this->mObject->mCatViewGroups as $groupid) {
                $gperm =& $gperm_handler->create();
                $gperm->set('gperm_name', 'cat_view');
                $gperm->set('gperm_modid', $this->mModule->mXoopsModule->get('mid'));
                $gperm->set('gperm_itemid', $this->mObject->get('category_id'));
                $gperm->set('gperm_groupid', $groupid);
                if(!$gperm_handler->insert($gperm)) {
                    return false;
                }
                unset($gperm);
            }
        }
        if(isset($this->mObject->mCatUploadGroups) && count($this->mObject->mCatUploadGroups)) {
            foreach($this->mObject->mCatUploadGroups as $groupid) {
                $gperm =& $gperm_handler->create();
                $gperm->set('gperm_name', 'cat_upload');
                $gperm->set('gperm_modid', $this->mModule->mXoopsModule->get('mid'));
                $gperm->set('gperm_itemid', $this->mObject->get('category_id'));
                $gperm->set('gperm_groupid', $groupid);
                if(!$gperm_handler->insert($gperm)) {
                   return false;
                }
                unset($gperm);
            }
        }
        if(isset($this->mObject->mCatHtmlAllowedGroups) && count($this->mObject->mCatHtmlAllowedGroups)) {
            foreach($this->mObject->mCatHtmlAllowedGroups as $groupid) {
                if(isset($this->mObject->mCatUploadGroups) && in_array($groupid, $this->mObject->mCatUploadGroups)) {
                    $gperm =& $gperm_handler->create();
                    $gperm->set('gperm_name', 'html_allowed');
                    $gperm->set('gperm_modid', $this->mModule->mXoopsModule->get('mid'));
                    $gperm->set('gperm_itemid', $this->mObject->get('category_id'));
                    $gperm->set('gperm_groupid', $groupid);
                    if(!$gperm_handler->insert($gperm)) {
                    return false;
                    }
                    unset($gperm);
                }
            }
        }

        // check children
        $children = $this->mObjectHandler->getChildIds($this->mObject->get('category_id'), true, false);
        if(count($children)) {
            $criteria = new criteriaCompo(new criteria('gperm_modid', $this->mModule->mXoopsModule->get('mid')));

            // delete children's gperm with no view perm
            $vCriteria = new criteriaCompo(new criteria('gperm_name', 'cat_view'));
            $vCriteria->add(new criteria('gperm_itemid', '('.implode(',', $children).')', 'IN'));
            if(count($this->mObject->mCatViewGroups)) {
                $vCriteria->add(new criteria('gperm_groupid', '('.implode(',', $this->mObject->mCatViewGroups).')', ' NOT IN'));
            }
            $criteria->add($vCriteria, 'OR');

            // delete children's gperm with no upload perm
            $uCriteria = new criteriaCompo(new criteria('gperm_name', 'cat_upload'));
            $uCriteria->add(new criteria('gperm_itemid', '('.implode(',', $children).')', 'IN'));
            if(count($this->mObject->mCatUploadGroups)) {
                $uCriteria->add(new criteria('gperm_groupid', '('.implode(',', $this->mObject->mCatUploadGroups).')', ' NOT IN'));
            }
            $criteria->add($vCriteria, 'OR');

            // delete children's gperm with no html perm
            $uCriteria = new criteriaCompo(new criteria('gperm_name', 'html_allowed'));
            $uCriteria->add(new criteria('gperm_itemid', '('.implode(',', $children).')', 'IN'));
            if(count($this->mObject->mCatHtmlAllowedGroups)) {
                $uCriteria->add(new criteria('gperm_groupid', '('.implode(',', $this->mObject->mCatHtmlAllowedGroups).')', ' NOT IN'));
            }
            $criteria->add($vCriteria, 'OR');

            @$gperm_handler->deleteAll($criteria);
        }

        return true;
    }

    /**
     * @public
     */
    function executeViewInput(&$render)
    {
        $render->setTemplateName("category_edit.html");
        $render->setAttribute('actionForm', $this->mActionForm);
        #cubson::lazy_load('category', $this->mObject);
        $render->setAttribute('object', $this->mObject);

        $render->setAttribute('categoryTree', $this->mObjectHandler->getChildTreeArray());
        $member_handler =& xoops_gethandler('member');
    $render->setAttribute('groupList', $member_handler->getGroupList());
        $render->setAttribute('groupObjs', $member_handler->getGroups(null, true));
        $render->setAttribute('module', $this->mModuleGlobal->mXoopsModule);
    }

    /**
     * @public
     */
    function executeViewSuccess(&$render)
    {
        $this->mRoot->mController->executeForward("./index.php?op=CategoryList");
    }

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

    /**
     * @public
     */
    function executeViewCancel(&$render)
    {
        $this->mRoot->mController->executeForward("./index.php?op=CategoryList");
    }
}

?>