<?php
	require_once( $GLOBALS["site_root"] . "/includes/tasks/GeneralTask.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/Members.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/Globals.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/Status.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/TemplateMail.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/Constants.inc.php" );

	class StatusSendTask extends GeneralTask {
		protected $mm;
		protected $membersm;
		protected $um;

		public function __construct( $task_id , $task_processor ) {
			parent::__construct( $task_id , $task_processor );
			$this->mm = new MemberModel();
			$this->membersm = new MembersModel();
			$this->um = new UserModel();

			$this->sm = new StatusModel();
		}
		
		protected function processMember( $member_id ) {
			$member = $this->mm->getMemberFromMemberId( $member_id );
			if (isset( $member["status_senttstamp"] )) return;

			$user = $this->um->getUserFromUserId( $member["user_id"] );

			$mail_template_id = $this->sm->getStatusTemplateMailIdFromStatus( $member["current_status"] );
			$tmm = new TemplateMailModel( $mail_template_id );
			$tmm->setHtml( true );
			$tmm->setPlain( true );

			$cm = new ConstantsModel( $tmm );
			$cm->addSystemParametersToTemplate();
			$cm->addMemberConstantsToTemplate( $member["member_id"] , "member" , $member );
			$cm->addUserConstantsToTemplate( $member["user_id"] , "user" , $user );

			switch ($member["current_status"]) {
			case STATUS_HAS_TO_PAY_4W:
				$tmm->setParam( "expiration_in_weeks" , 4 );
				break;
			case STATUS_HAS_TO_PAY_3W:
				$tmm->setParam( "expiration_in_weeks" , 3 );
				break;
			case STATUS_HAS_TO_PAY_2W:
				$tmm->setParam( "expiration_in_weeks" , 2 );
				break;
			case STATUS_HAS_TO_PAY_1W:
				$tmm->setParam( "expiration_in_weeks" , 1 );
				break;
			case STATUS_EXPIRED_1W:
			case STATUS_NEW_USER_EXPIRED_1W:
				$tmm->setParam( "expired_weeks" , 1 );
				break;
			case STATUS_EXPIRED_2W:
			case STATUS_NEW_USER_EXPIRED_2W:
				$tmm->setParam( "expired_weeks" , 2 );
				break;
			case STATUS_EXPIRED_3W:
			case STATUS_NEW_USER_EXPIRED_3W:
				$tmm->setParam( "expired_weeks" , 3 );
				break;
			case STATUS_EXPIRED_4W:
			case STATUS_NEW_USER_EXPIRED_4W:
				$tmm->setParam( "expired_weeks" , 4 );
				break;
			case STATUS_EXPIRED_5W:
			case STATUS_NEW_USER_EXPIRED_5W:
				$tmm->setParam( "expired_weeks" , 5 );
				break;
			case STATUS_EXPIRED_6W:
			case STATUS_NEW_USER_EXPIRED_6W:
				$tmm->setParam( "expired_weeks" , 6 );
				break;
			}
			if ($mail_template_id)
				$tmm->sendMail( $user["email"] );

			$this->mm->deactivateMemberStatusEmail( $member_id );
			$this->task_processor->log(
				sprintf( "(#%d/%s) => %s status notification sent",
					$member["member_id"],
					$member["member_username"],
					$member["current_status"] )
			);
		}
		
		protected function processStatusSendTask() {
			if ($this->task_processor->timeLeft()<3) return false;
			$ts = $this->tm->getTaskState();

			if (!isset( $ts["number_of_members"] )) {
				$ts["number_of_members"]=count( $ts["member_ids"] );
				$ts["offset"]=0;
			}

			while ($ts["offset"]<$ts["number_of_members"]) {
				if ($this->task_processor->timeleft()<3) return false;
				$member_id = $ts["member_ids"][$ts["offset"]];
				$this->processMember( $member_id );
				$ts["offset"]++;
				$this->tm->setTaskState( $ts );
				$this->tm->updateTask();
				usleep( 1000000 );
			}
			return true;
		}

		public function processTask() {
			$success = $this->processStatusSendTask();
			if ($success)
				$this->tm->closeTask();
			else
				$this->tm->releaseTask();
			return;
		}

	}

?>
