<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/Widget.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/SystemArticle.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Constants.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/MyMember.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/TemplateMail.inc.php" );

	class MyStatusPane extends Widget {
		protected $oTodoPanel;
		
		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->oArticle = new SystemArticle( "article" , $this );
			$this->oArticle->setArticleFromName( "sys_member_status" );
		}

		protected function generateLangDefs() {
			fo3_add_js_langdef( "Request_postal_cheque" );
			fo3_add_js_langdef( "button_Request_postal_cheque" );
			fo3_add_js_langdef( "Cancel" );
		}

		public function handleEvents() {
			if (!isset( $this->aP["inajax"] )) return parent::handleEvents();
			if (!isset( $this->aP["ajaxevent"] )) return parent::handleEvents();
			switch ($this->aP["ajaxevent"]) {
			case "request_postal_cheque":
				$mmm = new MyMemberModel();
				$mm = new MemberModel();
				$um = new UserModel();

				$member_id = $mmm->getLoggedInMemberId();
				if (!$member_id) {
					FNTH_direct_echo( sprintf( "alert('%s')" , __("globalerror_User_has_to_login_to_reach_functionality","attr") ) );
					fo3_exit();
				}
				$member = $mm->getMemberFromMemberId( $member_id );
				$user = $um->getUserFromUserId( $member["user_id"] );

				// send mail to user
				$mail = new TemplateMailModel( "sys_postal_cheque_requested" );
				$cm = new ConstantsModel( $mail );
				$cm->addSystemParametersToTemplate();
				$cm->addMemberConstantsToTemplate( $member_id , "member" , $member );
				$cm->addUserConstantsToTemplate( $member["user_id"] , "user" , $user );
				$mail->sendMail( $user["email"] );

				// send mail to admin
				$mail = new TemplateMailModel( "sys_admin_postal_cheque_requested" );
				$cm = new ConstantsModel( $mail );
				$cm->addSystemParametersToTemplate();
				$cm->addMemberConstantsToTemplate( $member_id , "member" , $member );
				$cm->addUserConstantsToTemplate( $member["user_id"] , "user" , $user );
				$mail->sendMail( $GLOBALS["fo3_admin_email"] );

				FNTH_direct_echo( sprintf( "alert('%s')" , __("Postal_cheque_requested","attr") ) );
				fo3_exit();
			default:
				return parent::handleEvents();
			}
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			// generate langdefs
			$this->generateLangDefs();

			// fill out article
			$cm = new ConstantsModel( $this->oArticle );
			$mmm = new MyMemberModel();
			$cm->addUserConstantsToTemplate( $mmm->getLoggedinUserId() );
			$cm->addMemberConstantsToTemplate( $mmm->getLoggedinMemberId() );
			$cm->addTodoConstantsToTemplate( $mmm->getLoggedInUserId() );

			return parent::render();
		}

	}

?>
