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

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

require_once XOOPS_ROOT_PATH . '/core/XCube_ActionForm.class.php';
//require_once XOOPS_MODULE_PATH . '/legacy/class/Legacy_Validator.class.php';

class Myuploader_Admin_CategoryEditForm extends XCube_ActionForm
{
    var $mModuleGlobal = null;
    var $mParentIds = array();
    var $mIsCatUploadOK = null;
    
    /**
	 * @public
	 */
	function getTokenName()
	{
		return "module.myuploader.Admin_CategoryEditForm.TOKEN";
	}

	/**
	 * @public
	 */
	function prepare()
	{
        XCube_DelegateUtils::call('Module.myuploader.Event.GetModuleGlobal', new XCube_Ref($this->mModuleGlobal), $this->mContext->mXoopsModule->get('dirname'));

		//
		// Set form properties
		//
		$this->mFormProperties['category_id'] = new XCube_IntProperty('category_id');
		$this->mFormProperties['cat_name'] = new XCube_StringProperty('cat_name');
		$this->mFormProperties['cat_description'] = new XCube_TextProperty('cat_description');
        $this->mFormProperties['cat_maxsize'] = new XCube_IntProperty('cat_maxsize');
		$this->mFormProperties['cat_maxlength'] = new XCube_IntProperty('cat_maxlength');
		$this->mFormProperties['cat_resizelength'] = new XCube_IntProperty('cat_resizelength');
		$this->mFormProperties['cat_thumblength'] = new XCube_IntProperty('cat_thumblength');
        $this->mFormProperties['cat_active'] = new XCube_BoolProperty('cat_active');
		$this->mFormProperties['cat_weight'] = new XCube_IntProperty('cat_weight');
		$this->mFormProperties['cat_pid'] = new XCube_IntProperty('cat_pid');
        // group permission
        $this->mFormProperties['cat_viewgroups'] = new XCube_IntArrayProperty('cat_viewgroups');
        $this->mFormProperties['cat_uploadgroups'] = new XCube_IntArrayProperty('cat_uploadgroups');
        $this->mFormProperties['cat_htmlallowedgroups'] = new XCube_IntArrayProperty('cat_htmlallowedgroups');

		//
		// Set field properties
		//
		$this->mFieldProperties['category_id'] = new XCube_FieldProperty($this);
		$this->mFieldProperties['category_id']->setDependsByArray(array('required'));
		$this->mFieldProperties['category_id']->addMessage('required', _MD_MYUPLOADER_ERROR_REQUIRED, _MD_MYUPLOADER_LANG_CATEGORY_ID);
	
		$this->mFieldProperties['cat_name'] = new XCube_FieldProperty($this);
		$this->mFieldProperties['cat_name']->setDependsByArray(array('required','maxlength'));
		$this->mFieldProperties['cat_name']->addMessage('required', _MD_MYUPLOADER_ERROR_REQUIRED, _MD_MYUPLOADER_LANG_CAT_NAME, '100');
		$this->mFieldProperties['cat_name']->addMessage('maxlength', _MD_MYUPLOADER_ERROR_MAXLENGTH, _MD_MYUPLOADER_LANG_CAT_NAME, '100');
		$this->mFieldProperties['cat_name']->addVar('maxlength', '100');
    
        $this->mFieldProperties['cat_description'] = new XCube_FieldProperty($this);
//        $this->mFieldProperties['cat_description']->setDependsByArray(array(''));
//        $this->mFieldProperties['cat_description']->addMessage('required', _MD_MYUPLOADER_ERROR_REQUIRED, _MD_MYUPLOADER_LANG_CAT_DESCRIPTION);
	
		$this->mFieldProperties['cat_maxsize'] = new XCube_FieldProperty($this);
		$this->mFieldProperties['cat_maxsize']->setDependsByArray(array('required'));
		$this->mFieldProperties['cat_maxsize']->addMessage('required', _MD_MYUPLOADER_ERROR_REQUIRED, _MD_MYUPLOADER_LANG_CAT_MAXSIZE);
	
		$this->mFieldProperties['cat_maxlength'] = new XCube_FieldProperty($this);
		$this->mFieldProperties['cat_maxlength']->setDependsByArray(array('required'));
		$this->mFieldProperties['cat_maxlength']->addMessage('required', _MD_MYUPLOADER_ERROR_REQUIRED, _MD_MYUPLOADER_LANG_CAT_MAXLENGTH);
	
		$this->mFieldProperties['cat_resizelength'] = new XCube_FieldProperty($this);
		$this->mFieldProperties['cat_resizelength']->setDependsByArray(array('required'));
		$this->mFieldProperties['cat_resizelength']->addMessage('required', _MD_MYUPLOADER_ERROR_REQUIRED, _MD_MYUPLOADER_LANG_CAT_RESIZELENGTH);
    
        $this->mFieldProperties['cat_thumblength'] = new XCube_FieldProperty($this);
        $this->mFieldProperties['cat_thumblength']->setDependsByArray(array('required'));
        $this->mFieldProperties['cat_thumblength']->addMessage('required', _MD_MYUPLOADER_ERROR_REQUIRED, _MD_MYUPLOADER_LANG_CAT_THUMBLENGTH);
	
		$this->mFieldProperties['cat_weight'] = new XCube_FieldProperty($this);
		$this->mFieldProperties['cat_weight']->setDependsByArray(array('required'));
		$this->mFieldProperties['cat_weight']->addMessage('required', _MD_MYUPLOADER_ERROR_REQUIRED, _MD_MYUPLOADER_LANG_CAT_WEIGHT);
	
		$this->mFieldProperties['cat_pid'] = new XCube_FieldProperty($this);
		$this->mFieldProperties['cat_pid']->setDependsByArray(array('required'));
		$this->mFieldProperties['cat_pid']->addMessage('required', _MD_MYUPLOADER_ERROR_REQUIRED, _MD_MYUPLOADER_LANG_CAT_PID);
/*    
        $this->mFieldProperties['cat_viewgroups'] = new XCube_FieldProperty($this);
        $this->mFieldProperties['cat_viewgroups']->setDependsByArray(array('objectExist'));
        $this->mFieldProperties['cat_viewgroups']->addMessage('objectExist', _MD_MYUPLOADER_ERROR_CAT_VIEWGROUPS, _MD_MYUPLOADER_LANG_CAT_VIEW_GROUPS);
        $this->mFieldProperties['cat_viewgroups']->addVar('handler', 'group');
    
        $this->mFieldProperties['cat_uploadgroups'] = new XCube_FieldProperty($this);
        $this->mFieldProperties['cat_uploadgroups']->setDependsByArray(array('objectExist'));
        $this->mFieldProperties['cat_uploadgroups']->addMessage('objectExist', _MD_MYUPLOADER_ERROR_CAT_UPLOADGROUPS, _MD_MYUPLOADER_LANG_CAT_UPLOAD_GROUPS);
        $this->mFieldProperties['cat_uploadgroups']->addVar('handler', 'group');*/
	}

    function validate()
    {
        parent::validate();
        $this->_validateCat_pid();
        $this->_validateCat_viewgroups();
        $this->_validateCat_uploadgroups();
        $this->_validateCat_htmlallowedgroups();
    }

    function _validateCat_pid() {
    	$catPid = $this->get('cat_pid');
        if(empty($catPid))
            $catPid = 0;

        if($catPid > 0 && !in_array($catPid, array_keys($this->mModuleGlobal->getAllCategories()))) {
        	$this->addErrorMessage(_MD_MYUPLOADER_ERROR_PARENT_ID_NOEXIST);
            return false;
        }
        if($catPid > 0 && $catPid == $this->get('category_id')) {
            $this->addErrorMessage(_MD_MYUPLOADER_ERROR_WRONG_PARENT);
            return false;
        }

//        $category_handler =& xoops_getmodulehandler('category', 'myuploader');
        $this->mParentIds =& $this->_getParents($catPid);
        return true;
    }

    function _validateCat_viewgroups()
    {
        $catViewGroups = $this->get('cat_viewgroups');
        if(!count($catViewGroups)) {
            $this->addErrorMessage(_MD_MYUPLOADER_ERROR_VIEWGROUPS);
            return false;
        }

        // check if groups exist
        if(!$this->_checkGroups($catViewGroups)) {
            return false;
        }

        // check parent's permission
        return $this->_checkParents('cat_view', $catViewGroups);
    }
    
    function _validateCat_uploadgroups()
    {
        $catUploadGroups = $this->get('cat_uploadgroups');
        if(!count($catUploadGroups)) {
            $this->addErrorMessage(_MD_MYUPLOADER_ERROR_UPLOADGROUPS);
            return false;
        }

        if(!$this->_checkGroups($catUploadGroups)) {
        	return false;
        }

        $this->mIsCatUploadOK = $this->_checkParents('cat_upload', $catUploadGroups);
        return $this->mIsCatUploadOK;
    }
    
    function _validateCat_htmlallowedgroups()
    {
        $catHtmlAllowedGroups = $this->get('cat_htmlallowedgroups');
	$member_handler =& xoops_gethandler('member');
        $groupList =& $member_handler->getGroupList();
	if($this->mIsCatUploadOK && count($catHtmlAllowedGroups)) {
            foreach($catHtmlAllowedGroups as $groupid) {
            	if(!in_array(intval($groupid), $this->get('cat_uploadgroups'))) {
                    $this->addErrorMessage(XCube_Utils::formatString(_MD_MYUPLOADER_ERROR_HTMLALLOWEDGROUPS, in_array(intval($groupid), array_keys($groupList))?$groupList[intval($groupid)]:intval($groupid)));
                    return false;
                }
            }
        }
    }

    function _checkGroups($groups) {
        $member_handler =& xoops_gethandler('member');
        $allGroups =& $member_handler->getGroups(null, true);
        foreach($groups as $group) {
            if(!in_array(intval($group), array_keys($allGroups))) {
                $this->addErrorMessage(Xcube_Utils::formatString(_MD_MYUPLOADER_ERROR_GROUP_NOEXIST, intval($group)));
                return false;
            }
        }
        return true;   	
    }

    function _checkParents($name, $groups) {
	if(count($this->mParentIds)) {
            $member_handler =& xoops_gethandler('member');
            $groupList = $member_handler->getGroupList();
	    // drop priviliges if the parents has no privilige
            foreach($this->mParentIds as $parentid) {
                foreach($groups as $key=>$groupid) {
                    if(!in_array($groupid, $this->mModuleGlobal->mModuleGperm[$name][$parentid])) {
                        $this->addErrorMessage(XCube_Utils::formatString(_MD_MYUPLOADER_ERROR_NO_GROUP_IN_PARENT, $groupList[$groupid], htmlspecialchars($name, ENT_QUOTES)));
                        return false;
                    }
                }
            }
        }
        return true;
    }

    function &_getParents($pid, $array=array()) {
        $allCategories =& $this->mModuleGlobal->getAllCategories();
	if(!count($allCategories))
            return $array;

        if(empty($pid))
            return $array;

        if(in_array($pid, array_keys($allCategories))) {
            $array[] = $pid;
            $array = $this->_getParents($allCategories[$pid]->get('cat_pid'), $array);            
        }

        return $array;
    }

	/**
	 * @public
	 */
	function load(&$obj)
	{
		$this->set('category_id', $obj->get('category_id'));
		$this->set('cat_name', $obj->get('cat_name'));
        $this->set('cat_description', $obj->get('cat_description'));
		$this->set('cat_maxsize', $obj->get('cat_maxsize'));
		$this->set('cat_maxlength', $obj->get('cat_maxlength'));
		$this->set('cat_resizelength', $obj->get('cat_resizelength'));
		$this->set('cat_thumblength', $obj->get('cat_thumblength'));
		$this->set('cat_active', $obj->get('cat_active'));
		$this->set('cat_weight', $obj->get('cat_weight'));
		$this->set('cat_pid', $obj->get('cat_pid'));

        // group permission
        $this->set('cat_viewgroups', $obj->mCatViewGroups);
        $this->set('cat_uploadgroups', $obj->mCatUploadGroups);
        $this->set('cat_htmlallowedgroups', $obj->mCatHtmlAllowedGroups);
	}

	/**
	 * @public
	 */
	function update(&$obj)
	{
		$obj->set('category_id', $this->get('category_id'));
		$obj->set('cat_name', $this->get('cat_name'));
        $obj->set('cat_description', $this->get('cat_description'));
		$obj->set('cat_maxsize', $this->get('cat_maxsize'));
		$obj->set('cat_maxlength', $this->get('cat_maxlength'));
		$obj->set('cat_resizelength', $this->get('cat_resizelength'));
		$obj->set('cat_thumblength', $this->get('cat_thumblength'));
		$obj->set('cat_active', $this->get('cat_active'));
		$obj->set('cat_weight', $this->get('cat_weight'));
		$obj->set('cat_pid', $this->get('cat_pid'));

        // group permission
        unset($obj->mCatViewGroups);
        foreach($this->get('cat_viewgroups') as $group) {
        	$obj->mCatViewGroups[] = intval($group);
        }
        unset($obj->mCatUploadGroups);
        foreach($this->get('cat_uploadgroups') as $group) {
            $obj->mCatUploadGroups[] = intval($group);
        }
        unset($obj->mCatHtmlAllowedGroups);
        foreach($this->get('cat_htmlallowedgroups') as $group) {
            $obj->mCatHtmlAllowedGroups[] = intval($group);
        }
	}
}

?>