<?php

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

class UserRegistMailDirector
{
	var $mBuilder;
	var $mUser;
	var $mXoopsConfig;
	var $mUserConfig;

	function UserRegistMailDirector(&$user,$xoopsConfig,$userConfig)
	{
		$this->mUser=&$user;
		$this->mXoopsConfig=$xoopsConfig;
		$this->mUserConfig=$userConfig;
	}
	
	function contruct(&$builder)
	{
		$builder->setTemplate();
		$builder->setToUsers($this->mUser,$this->mUserConfig);
		$builder->setFromEmail($this->mXoopsConfig);
		$builder->setSubject($this->mUser,$this->mXoopsConfig);
		$builder->setBody($this->mUser,$this->mXoopsConfig);
	}
}

class RegistUserActivateMailBuilder
{
	var $mMailer;
	
	function RegistUserActivateMailBuilder()
	{
		$this->mMailer=&getMailer();
		$this->mMailer->useMail();
	}

	function setTemplate()
	{
		$root=&XCube_Root::getSingleton();
		$language = $root->mController->getConfig('language');
		$this->mMailer->setTemplateDir(XOOPS_ROOT_PATH.'/modules/user/language/'.$language.'/mail_template/');
		$this->mMailer->setTemplate('register.tpl');
	}

	function setToUsers($user,$userConfig)
	{
		$this->mMailer->setToUsers($user);
	}
	
	function setFromEmail($xoopsConfig)
	{
		$this->mMailer->setFromEmail($xoopsConfig['adminmail']);
		$this->mMailer->setFromName($xoopsConfig['sitename']);
	}
	
	function setSubject($user,$xoopsConfig)
	{
		$this->mMailer->setSubject(@sprintf(_US_USERKEYFOR,$user->getVar('uname')));
	}

	function setBody($user,$xoopsConfig)
	{
		$this->mMailer->assign('SITENAME',$xoopsConfig['sitename']);
		$this->mMailer->assign('ADMINMAIL',$xoopsConfig['adminmail']);
		$this->mMailer->assign('SITEURL',XOOPS_URL.'/');
		$this->mMailer->assign('USERACTLINK', XOOPS_URL.'/modules/user/index.php?action=UserActivate&uid='.$user->getVar('uid').'&actkey='.$user->getVar('actkey'));
	}
	
	function &getResult()
	{
		return $this->mMailer;
	}
}

class RegistUserAdminActivateMailBuilder extends RegistUserActivateMailBuilder
{
	function setTemplate()
	{
		$root=&XCube_Root::getSingleton();
		$language = $root->mController->getConfig('language');
		$this->mMailer->setTemplateDir(XOOPS_ROOT_PATH.'/modules/user/language/'.$language.'/mail_template/');
		$this->mMailer->setTemplate('adminactivate.tpl');
	}

	function setToUsers($user,$userConfig)
	{
		$memberHandler=&xoops_gethandler('member');
		$this->mMailer->setToGroups($memberHandler->getGroup($userConfig['activation_group']));
		$this->mMailer->setToUsers($user);
	}
	
	function setFromUser($xoopsConfig)
	{
		$this->mMailer->setFromEmail($xoopsConfig['adminmail']);
		$this->mMailer->setFromName($xoopsConfig['sitename']);
	}
	
	function setSubject($user,$xoopsConfig)
	{
		$this->mMailer->setSubject(@sprintf(_US_USERKEYFOR,$user->getVar('uname')));
	}

	function setBody($user,$xoopsConfig)
	{
		parent::setBody($user,$xoopsConfig);
		$this->mMailer->assign('USERNAME', $user->getVar('uname'));
		$this->mMailer->assign('USEREMAIL', $user->getVar('email'));
		$this->mMailer->assign('USERACTLINK', XOOPS_URL.'/modules/user/index.php?action=UserActivate&uid='.$user->getVar('uid').'&actkey='.$user->getVar('actkey'));
	}
}

class RegistUserNotifyMailBuilder extends RegistUserActivateMailBuilder
{
	function setTemplate()
	{
	}

	function setToUsers($user,$userConfig)
	{
		$memberHandler=&xoops_gethandler('member');
		$this->mMailer->setToGroups($memberHandler->getGroup($userConfig['new_user_notify_group']));
	}
	
	function setSubject($user,$xoopsConfig)
	{
		$this->mMailer->setSubject(@sprintf(_US_NEWUSERREGAT,$xoopsConfig['sitename']));
	}

	function setBody($user,$xoopsConfig)
	{
		$this->mMailer->setBody(@sprintf(_US_HASJUSTREG, $user->getVar('uname')));
	}
}

class RegistAdminCommitMailBuilder extends RegistUserActivateMailBuilder
{
	function setTemplate()
	{
		$root=&XCube_Root::getSingleton();
		$language = $root->mController->getConfig('language');
		$this->mMailer->setTemplateDir(XOOPS_ROOT_PATH.'/modules/user/language/'.$language.'/mail_template/');
		$this->mMailer->setTemplate('activated.tpl');
	}
	
	function setSubject($user,$xoopsConfig)
	{
		$this->mMailer->setSubject(@sprintf(_US_YOURACCOUNT,$xoopsConfig['sitename']));
	}
}

?>