<?php
	require_once( $GLOBALS["site_root"]."/includes/models/TemplateMail.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/User.inc.php" );

	class StatusModel {
		protected $tmm = null;

		public function __construct() {
			$this->tmm = new TemplateMailModel();
		}

		public function getStatusTemplateMailIdFromStatus( $original_name , $fallback = true ) {
			// megnezzuk, hogy jo e ugy ahogy van
			$mail_template_id = $this->tmm->getTemplateIdFromName( "sys_status_" . $original_name );
			if ($mail_template_id) return $mail_template_id;	// ezek szerint van ilyen
			if (!$fallback) return false;

			// nincs meg, fallbackelni akarunk
			switch ($original_name) {
			case STATUS_NEW_USER_DELETED:
				return $this->getStatusTemplateMailIdFromStatus( "deleted" , false );
			case STATUS_HAS_TO_PAY_4W:
			case STATUS_HAS_TO_PAY_3W:
			case STATUS_HAS_TO_PAY_2W:
			case STATUS_HAS_TO_PAY_1W:
				return $this->getStatusTemplateMailIdFromStatus( "has_to_pay_N" , false );
			case STATUS_EXPIRED_1W:
			case STATUS_EXPIRED_2W:
			case STATUS_EXPIRED_3W:
			case STATUS_EXPIRED_4W:
			case STATUS_EXPIRED_5W:
			case STATUS_EXPIRED_6W:
				return $this->getStatusTemplateMailIdFromStatus( "expired_N" , false );
			case STATUS_NEW_USER_EXPIRED_1W:
			case STATUS_NEW_USER_EXPIRED_2W:
			case STATUS_NEW_USER_EXPIRED_3W:
			case STATUS_NEW_USER_EXPIRED_4W:
			case STATUS_NEW_USER_EXPIRED_5W:
			case STATUS_NEW_USER_EXPIRED_6W:
				return $this->getStatusTemplateMailIdFromStatus( "new_user_expired_N" , false );
			case STATUS_ACTIVE:
				return $this->getStatusTemplateMailIdFromStatus( "active_user" , false );
			default:
				return false;
			}
			return false;
		}

		protected function getStatuses() {
			$statuses = array(
				STATUS_NEW_USER => array( "name" => STATUS_NEW_USER),
				STATUS_ACTIVE => array( "name" => STATUS_ACTIVE),
				STATUS_HAS_TO_PAY_4W => array( "name" => STATUS_HAS_TO_PAY_4W),
				STATUS_HAS_TO_PAY_3W => array( "name" => STATUS_HAS_TO_PAY_3W),
				STATUS_HAS_TO_PAY_2W => array( "name" => STATUS_HAS_TO_PAY_2W),
				STATUS_HAS_TO_PAY_1W => array( "name" => STATUS_HAS_TO_PAY_1W),
				STATUS_HAS_TO_PAY => array( "name" => STATUS_HAS_TO_PAY),
				STATUS_NEW_USER_HAS_TO_PAY => array( "name" => STATUS_NEW_USER_HAS_TO_PAY),
				STATUS_EXPIRED_1W => array( "name" => STATUS_EXPIRED_1W),
				STATUS_NEW_USER_EXPIRED_1W => array( "name" => STATUS_NEW_USER_EXPIRED_1W),
				STATUS_EXPIRED_2W => array( "name" => STATUS_EXPIRED_2W),
				STATUS_NEW_USER_EXPIRED_2W => array( "name" => STATUS_NEW_USER_EXPIRED_2W),
				STATUS_EXPIRED_3W => array( "name" => STATUS_EXPIRED_3W),
				STATUS_NEW_USER_EXPIRED_3W => array( "name" => STATUS_NEW_USER_EXPIRED_3W),
				STATUS_EXPIRED_4W => array( "name" => STATUS_EXPIRED_4W),
				STATUS_NEW_USER_EXPIRED_4W => array( "name" => STATUS_NEW_USER_EXPIRED_4W),
				STATUS_EXPIRED_5W => array( "name" => STATUS_EXPIRED_5W),
				STATUS_NEW_USER_EXPIRED_5W => array( "name" => STATUS_NEW_USER_EXPIRED_5W),
				STATUS_EXPIRED_6W => array( "name" => STATUS_EXPIRED_6W),
				STATUS_NEW_USER_EXPIRED_6W => array( "name" => STATUS_NEW_USER_EXPIRED_6W),
				STATUS_EXPIRED_TOTAL => array( "name" => STATUS_EXPIRED_TOTAL),
				STATUS_NEW_USER_EXPIRED_TOTAL => array( "name" => STATUS_NEW_USER_EXPIRED_TOTAL),
				STATUS_DELETED => array( "name" => STATUS_DELETED),
				STATUS_NEW_USER_DELETED => array( "name" => STATUS_NEW_USER_DELETED),
				STATUS_ARCHIVE => array( "name" => STATUS_ARCHIVE)
			);
			$retval = array();
			foreach ($statuses as $status) {
				$status["title"]=__("status_".$status["name"],"attr");
				$status["email_template_name"]="sys_status_".$status["name"];
				$retval[]=$status;
			}
			return $retval;
		}

		public function getStatusFromName( $name ) {
			$statuses = $this->getStatuses();
			if (!isset( $statuses[$name] )) return false;
			return $statuses[$name];
		}

		public function getStatusOptions( $choose = true ) {
			$retval = array();
			if ($choose) $retval[""]=__("Choose","attr");
			$statuses = $this->getStatuses();
			foreach ($statuses as $status) $retval[$status["name"]]=$status["title"];
			return $retval;
		}

		public function getNewMemberStatus( $member ) {
			$original_expires = $member["expires_tstamp"];

			// toroltek nem mocorognak sehova
			switch ($member["current_status"]) {
			case STATUS_NEW_USER_DELETED:
			case STATUS_DELETED:
				return $member["current_status"];
			}

			// ha nem visszaigazolt, akkor mindenkeppen uj jelentkezo
			$um = new UserModel();
			$user = $um->getUserFromUserId( $member["user_id"] );
			if ($user["is_acknowledged"]=="f") return STATUS_NEW_USER;

			// egyebkent vegigmassza a letrat
			if (!isset( $member["expires_tstamp"] )) 
				$member["expires_tstamp"]=$user["reg_tstamp"];	// fo3_date( "Y-m-d H:i:s" , fo3_time()-3600 );
			$expires_tstamp = substr( $member["expires_tstamp"] , 0 , 19 );
			$expires_date = substr( $member["expires_tstamp"] , 0 , 10 );

			$last_midnight = fo3_nextday( 0 );
			$right_now = fo3_time();	// fo3_nextday( 86400 );
			$next_midnight = fo3_nextday( 2*86400 );

			$oneday_in_secs = 24*3600;

			$time_has_to_pay_4w = fo3_date( "Y-m-d H:i:s" , $right_now + 28*$oneday_in_secs );
			$time_has_to_pay_3w = fo3_date( "Y-m-d H:i:s" , $right_now + 21*$oneday_in_secs );
			$time_has_to_pay_2w = fo3_date( "Y-m-d H:i:s" , $right_now + 14*$oneday_in_secs );
			$time_has_to_pay_1w = fo3_date( "Y-m-d H:i:s" , $right_now + 7*$oneday_in_secs );
			$time_has_to_pay = fo3_date( "Y-m-d H:i:s" , $right_now );
			$time_expired_1w = fo3_date( "Y-m-d H:i:s" , $right_now - 7*$oneday_in_secs );
			$time_expired_2w = fo3_date( "Y-m-d H:i:s" , $right_now - 14*$oneday_in_secs );
			$time_expired_3w = fo3_date( "Y-m-d H:i:s" , $right_now - 21*$oneday_in_secs );
			$time_expired_4w = fo3_date( "Y-m-d H:i:s" , $right_now - 28*$oneday_in_secs );
			$time_expired_5w = fo3_date( "Y-m-d H:i:s" , $right_now - 35*$oneday_in_secs );
			$time_expired_6w = fo3_date( "Y-m-d H:i:s" , $right_now - 42*$oneday_in_secs );
			$time_expired_7w = fo3_date( "Y-m-d H:i:s" , $right_now - 49*$oneday_in_secs );


			$newstatus = "";

			if ($expires_date>$time_has_to_pay_4w) {		// ha tobb, mint 4 hete van lejaratig, akkor aktiv
				$newstatus = STATUS_ACTIVE;
			} else if ($expires_date>$time_has_to_pay_3w) {	// ha tobb, mint 3 hete van lejaratig, akkor 4 het mulva esedekes
				$newstatus = STATUS_HAS_TO_PAY_4W;
			} else if ($expires_date>$time_has_to_pay_2w) {	// ha tobb, mint 2 hete van lejaratig, akkor 3 het mulva esedekes
				$newstatus = STATUS_HAS_TO_PAY_3W;
			} else if ($expires_date>$time_has_to_pay_1w) {	// ha tobb, mint 1 hete van lejaratig, akkor 2 het mulva esedekes
				$newstatus = STATUS_HAS_TO_PAY_2W;
			} else if ($expires_date>$time_has_to_pay) {	// ha lejarati idon tul van, akkor 1 het mulva esedekes
				$newstatus = STATUS_HAS_TO_PAY_1W;
			} else if ($expires_date>$time_expired_1w) {	// ha meg nem jart le 1 hete, akkor fizetes esedekes
				$newstatus = STATUS_HAS_TO_PAY;
			} else if ($expires_date>$time_expired_2w) {	// ha meg nem jart le 2 hete, akkor 1 hete jart le
				$newstatus = STATUS_EXPIRED_1W;
			} else if ($expires_date>$time_expired_3w) {	// ha meg nem jart le 3 hete, akkor 2 hete jart le
				$newstatus = STATUS_EXPIRED_2W;
			} else if ($expires_date>$time_expired_4w) {	// ha meg nem jart le 4 hete, akkor 3 hete jart le
				$newstatus = STATUS_EXPIRED_3W;
			} else if ($expires_date>$time_expired_5w) {	// ha meg nem jart le 5 hete, akkor 4 hete jart le
				$newstatus = STATUS_EXPIRED_4W;
			} else if ($expires_date>$time_expired_6w) {	// ha meg nem jart le 6 hete, akkor 5 hete jart le
				$newstatus = STATUS_EXPIRED_5W;
			} else if ($expires_date>$time_expired_7w) {	// ha meg nem jart le 7 hete, akkor 6 hete jart le
				$newstatus = STATUS_EXPIRED_6W;
			} else {
				$newstatus = STATUS_EXPIRED_TOTAL;			// ha tobb, mint 6 hete jart le, akkor vegleg lejart
			}
			// if (!isset( $member["paid_tstamp"]) && strpos($newstatus,"expired")===0) {
			// ez itt mi akart lenni ez az expired?
			if (!isset( $member["paid_tstamp"] )) {
				$newstatus=sprintf( "new_user_%s" , $newstatus );
			}
			return $newstatus;
		}


	}

?>
