<?php
	require_once( $GLOBALS["site_root"]."/includes/pages/BackendPopup.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminTextbox.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminSelect.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminNumeric.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminButton.inc.php" );

	require_once( $GLOBALS["site_root"]."/includes/models/Mailtemplates.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/TemplateMail.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Constants.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Member.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/User.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Job.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/JobQuote.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/CreditCard.inc.php" );
		

	class AdminMailtemplateTestPage extends BackendPage {

		public function __construct( $id = "AdminMailtemplateTestPage" , $parent = null ) {
			parent::__construct( $id , $parent );
			if (!AdminUserModel::isAdminLoggedIn()) return;

		}


		public function modifyGetPost( &$g , &$p ) {
			parent::modifyGetPost( $g , $p );
			$w = 80;

			$mt = new MailtemplatesModel();
			$this->mail_template_id = new AdminSelect( "mail_template_id" , $this );
			$this->mail_template_id->setTitle( "mail_template" );
			$this->mail_template_id->setShowTitle( true );
			$this->mail_template_id->setOptions( $mt->getMailtemplateOptions( false , true ) );
			if (isset( $this->aG["uid"] )) $this->mail_template_id->setValue( $this->aG["uid"] );

			$this->member_id = new AdminNumeric( "member_id" , $this );
			$this->member_id->setTitle( "member_id" );
			$this->member_id->setShowTitle( true );
			$this->member_id->setValue( 259 );
			$this->member_id->setWidth( $w );
			
			$this->poster_member_id = new AdminNumeric( "poster_member_id" , $this );
			$this->poster_member_id->setTitle( "poster_member_id" );
			$this->poster_member_id->setShowTitle( true );
			$this->poster_member_id->setValue( 259 );
			$this->poster_member_id->setWidth( $w );
			
			$this->receiver_member_id = new AdminNumeric( "receiver_member_id" , $this );
			$this->receiver_member_id->setTitle( "receiver_member_id" );
			$this->receiver_member_id->setShowTitle( true );
			$this->receiver_member_id->setValue( 395 );
			$this->receiver_member_id->setWidth( $w );

			$this->job_id = new AdminNumeric( "job_id" , $this );
			$this->job_id->setTitle( "job_id" );
			$this->job_id->setShowTitle( true );
			$this->job_id->setValue( 259 );
			$this->job_id->setWidth( $w );
			
			$this->job_quote_id = new AdminNumeric( "job_quote_id" , $this );
			$this->job_quote_id->setTitle( "job_quote_id" );
			$this->job_quote_id->setShowTitle( true );
			$this->job_quote_id->setValue( 259 );
			$this->job_quote_id->setWidth( $w );
			
			$this->cc_tran_id = new AdminNumeric( "cc_tran_id" , $this );
			$this->cc_tran_id->setTitle( "cc_tran_id" );
			$this->cc_tran_id->setShowTitle( true );
			$this->cc_tran_id->setValue( 259 );
			$this->cc_tran_id->setWidth( $w );

			$this->smb_render = new AdminButton( "smb_render" , $this );
			$this->smb_render->setValue( "Render!" );
			$this->smb_render->setAttribute( "onclick" , "MailtemplateRender();" );
			$this->smb_render->setAttribute( "style" , sprintf( "width: %dpx;" , $w ) );
		}

		protected function initializeWidgets() {
			$mt = new MailtemplatesModel();
		}

		protected function renderMailTemplate( ) {
			$tm = new TemplateMailModel();
			$tm->setTemplateFromId( $this->aP["mail_template_id"] );

			$cm = new ConstantsModel( $tm );
			$mm = new MemberModel();
			$um = new UserModel();
			$jm = new JobModel();
			$jq = new JobQuoteModel();
			$ccm = new CreditCardModel();

			$cm->addSystemParametersToTemplate();

			$member = $mm->getMemberFromMemberId( $this->aP["member_id"] );
			$user = $um->getUserFromUserId( $member["user_id"] );
			$poster_member = $mm->getMemberFromMemberId( $this->aP["poster_member_id"] );
			$poster_user = $um->getUserFromUserId( $poster_member["user_id"] );
			$receiver_member = $mm->getMemberFromMemberId( $this->aP["receiver_member_id"] );
			$receiver_user = $um->getuserFromUserId( $receiver_member["user_id"] );
			$cc_tran = $ccm->getTransactionFromTransactionId( $this->aP["cc_tran_id"] );

			$job = $jm->getJobFromId( $this->aP["job_id"] , true );
			
			$cm->addUserConstantsToTemplate( $user["user_id"] , "user" , $user );
			$cm->addUserConstantsToTemplate( $poster_user["user_id"] , "poster_user" , $poster_user );
			$cm->addUserConstantsToTemplate( $receiver_user["user_id"] , "receiver_user" , $receiver_user );

			$cm->addMemberConstantsToTemplate( $member["member_id"] , "member" , $member );
			$cm->addMemberConstantsToTemplate( $poster_member["member_id"] , "poster_member" , $poster_member );
			$cm->addMemberConstantsToTemplate( $receiver_member["member_id"] , "receiver_member" , $receiver_member );

			$cm->addJobConstantsToTemplate( $job["job_id"] , "job" , $job );

			$cm->addCreditcardConstantsToTemplate( $cc_tran["cc_transaction_id"] , "cc_tran" , $cc_tran );
			

			return $tm->fetchBody();
		}

		public function handleEvents() {
			if (!$this->isLoggedIn()) return parent::handleEvents();
			if (!isset( $this->aP["task"] )) return parent::handleEvents();
			switch ($this->aP["task"]) {
			case "render_mail_template":
				$str = $this->renderMailTemplate();
				FNTH_direct_echo( $str );
				fo3_exit();
			}
			return parent::handleEvents();
		}

		public function render() {
			if (!$this->isLoggedIn()) return parent::render();
			$this->initializeWidgets();
			return parent::render();
		}
	}
?>
