<?php

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

require_once XOOPS_ROOT_PATH . "/class/XCube_ActionForm.class.php";

class User_AvatarSelectForm extends XCube_ActionForm
{
	var $mSelectedId = null;
	
	function getTokenName()
	{
		return "module.user.AvatarSelectForm.TOKEN";
	}

	function prepare()
	{
		$this->mFormProperties['uid'] =& new XCube_IntProperty('uid');
		$this->mFormProperties['avatar_id'] =& new XCube_IntArrayProperty('avatar_id');
	}
	
	function validateAvatar_id()
	{
		$ids = $this->get('avatar_id');
		
		if (count($ids) != 1) {
			$this->addErrorMessage(_MD_USER_ERROR_AVATAR_SELECT);
		}
		
		foreach ($ids as $avatar_id => $dmy_value) {
			$this->mSelectedId = $avatar_id;
		}
		
		//
		// Check whether specified avatar_id exists. 
		//
		$handler =& xoops_getmodulehandler('avatar');
		$obj =& $handler->get($this->mSelectedId);
		
		if (!is_object($obj)) {
			$this->addErrorMessage(_MD_USER_ERROR_AVATAR_SELECT);
		}
	}
	
	function load(&$obj)
	{
		$this->set('uid', $obj->get('uid'));
	}
	
	function update(&$obj)
	{
		$handler =& xoops_getmodulehandler('avatar');
		$avatar =& $handler->get($this->mSelectedId);
		
		$obj->set('user_avatar', $avatar->get('avatar_file'));
	}
}

?>
