<?php
	require_once( $GLOBALS["site_root"]."/includes/pages/BackendPage.inc.php" );

	require_once( $GLOBALS["site_root"] . "/includes/models/Globals.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/Newsletter.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/Member.inc.php" );

	class NewsletterUserGenerator {
		public function __construct() {
			$this->nm = new NewsletterModel();
			$this->mm = new MemberModel();
		}

		protected function getNewsletterFilters() {
			$f = array();
			$filters = array(
				"all_members",
				"active_members",
				"active_translators",
				"all_not_expired_members",
				"all_expired_members",
				"expired_at_least_3_weeks",
				"all_not_expired_translators",
				"all_expired_translators",
				"all_not_expired_agencies",
				"all_expired_agencies",
				"all_users",
				"all",
				"temp" );
			foreach ($filters as $filter) {
				$ng = $this->nm->getNewsletterGroupFromName($filter);
				if (!$ng) continue;
				$f[$filter]=$ng;
			}
			return $f;
		}

		protected function getUserGroups( $nuid , $p ) {
			$data = $p["newsletter_user_parameters"];
			$retval = array();
			foreach ($this->filters as $fname => $filter) {
				$ngid = $filter["newsletter_group_id"];
				switch ($fname) {
				case "all":
					// mindenki kivetel a toroltek es a regisztralatlanok
					if ($data["user"]["is_deleted"]=="t") break;
					if ($data["user"]["is_admin_acknowledged"]=="f") break;
					if ($data["user"]["is_registered"]=="f") break;
					$retval[]=$ngid;
					break;	
				case "all_users":
					// csak ajanlatkerok
					if ($data["user"]["is_deleted"]=="t") break;
					if ($data["user"]["is_admin_acknowledged"]=="f") break;
					if ($data["user"]["is_registered"]=="f") break;
					if (isset( $data["member"] )) break;
					$retval[]=$ngid;
					break;
				case "all_members":
					// csak tagok, kiveve toroltek
					if ($data["user"]["is_deleted"]=="t") break;
					if ($data["user"]["is_admin_acknowledged"]=="f") break;
					if ($data["user"]["is_registered"]=="f") break;
					if (!isset( $data["member"] )) break;
					if ($data["member"]["current_status"]==STATUS_DELETED ||
						$data["member"]["current_status"]==STATUS_NEW_USER_DELETED) break;
					$retval[]=$ngid;
					break;
				case "temp":
					// csak nem lejart tagok 
					if ($data["user"]["is_deleted"]=="t") break;
					if ($data["user"]["is_admin_acknowledged"]=="f") break;
					if ($data["user"]["is_registered"]=="f") break;
					if (!isset( $data["member"] )) break;
					if ($data["member"]["member_type"]=="agency") break;
					if ($data["member"]["current_status"]==STATUS_DELETED ||
						$data["member"]["current_status"]==STATUS_NEW_USER_DELETED) break;
					if ($data["member"]["current_status"]==STATUS_EXPIRED_TOTAL) break;
					if (substr( $data["member"]["expires_tstamp"] , 0 , 19 )>fo3_date( "Y-m-d H:i:s" )) break;
					$retval[]=$ngid;
					break;
				case "all_expired_members":
					// csak nem lejart tagok
					if ($data["user"]["is_deleted"]=="t") break;
					if ($data["user"]["is_admin_acknowledged"]=="f") break;
					if ($data["user"]["is_registered"]=="f") break;
					if (!isset( $data["member"] )) break;
					if ($data["member"]["current_status"]==STATUS_DELETED ||
						$data["member"]["current_status"]==STATUS_NEW_USER_DELETED) break;
					if ($data["member"]["current_status"]!=STATUS_EXPIRED_TOTAL) break;
					$retval[]=$ngid;
					break;
				case "3_weeks_expired":
					if ($data["user"]["is_deleted"]=="t") break;
					if ($data["user"]["is_admin_acknowledged"]=="f") break;
					if ($data["user"]["is_registered"]=="f") break;
					if (!isset( $data["member"] )) break;
					if ($data["member"]["current_status"]==STATUS_DELETED ||
						$data["member"]["current_status"]==STATUS_NEW_USER_DELETED) break;

					if ($data["member"]["current_status"]==STATUS_EXPIRED_TOTAL) break;
					if ($data["member"]["current_status"]!=STATUS_EXPIRED_TOTAL) break;
					
					break;
				case "all_not_expired_members":
					// csak a lejart tagok
					if ($data["user"]["is_deleted"]=="t") break;
					if ($data["user"]["is_admin_acknowledged"]=="f") break;
					if ($data["user"]["is_registered"]=="f") break;
					if (!isset( $data["member"] )) break;
					if ($data["member"]["current_status"]==STATUS_DELETED ||
						$data["member"]["current_status"]==STATUS_NEW_USER_DELETED) break;
					if ($data["member"]["current_status"]==STATUS_EXPIRED_TOTAL ||
						$data["member"]["current_status"]==STATUS_NEW_USER_EXPIRED_TOTAL) break;
					$retval[]=$ngid;
					break;
				case "all_not_expired_translators":
					// csak a nem lejart forditok
					if ($data["user"]["is_deleted"]=="t") break;
					if ($data["user"]["is_admin_acknowledged"]=="f") break;
					if ($data["user"]["is_registered"]=="f") break;
					if (!isset( $data["member"] )) break;
					if ($data["member"]["member_type"]=="agency") break;
					if ($data["member"]["current_status"]==STATUS_DELETED ||
						$data["member"]["current_status"]==STATUS_NEW_USER_DELETED) break;
					if ($data["member"]["current_status"]==STATUS_EXPIRED_TOTAL ||
						$data["member"]["current_status"]==STATUS_NEW_USER_EXPIRED_TOTAL) break;
					$retval[]=$ngid;
					break;
				case "all_translators":
					// csak tagok, kiveve toroltek
					if ($data["user"]["is_deleted"]=="t") break;
					if ($data["user"]["is_admin_acknowledged"]=="f") break;
					if ($data["user"]["is_registered"]=="f") break;
					if (!isset( $data["member"] )) break;
					if ($data["member"]["member_type"]=="agency") break;
					if ($data["member"]["current_status"]==STATUS_DELETED ||
						$data["member"]["current_status"]==STATUS_NEW_USER_DELETED) break;
					$retval[]=$ngid;
					break;
				case "all_expired_translators":
					// csak nem lejart tagok
					if ($data["user"]["is_deleted"]=="t") break;
					if ($data["user"]["is_admin_acknowledged"]=="f") break;
					if ($data["user"]["is_registered"]=="f") break;
					if (!isset( $data["member"] )) break;
					if ($data["member"]["member_type"]=="agency") break;
					if ($data["member"]["current_status"]==STATUS_DELETED ||
						$data["member"]["current_status"]==STATUS_NEW_USER_DELETED) break;
					if (substr( $data["member"]["expires_tstamp"] , 0 , 19 )>fo3_date( "Y-m-d H:i:s" )) break;
					$retval[]=$ngid;
					break;
				case "expired_at_least_3_weeks":
					// csak a lejart tagok
					if ($data["user"]["is_deleted"]=="t") break;
					if ($data["user"]["is_admin_acknowledged"]=="f") break;
					if ($data["user"]["is_registered"]=="f") break;
					if (!isset( $data["member"] )) break;
					if ($data["member"]["member_type"]=="agency") break;
					if ($data["member"]["current_status"]==STATUS_DELETED ||
						$data["member"]["current_status"]==STATUS_NEW_USER_DELETED) break;
					if (
						($data["member"]["current_status"]==STATUS_EXPIRED_3W) ||
						($data["member"]["current_status"]==STATUS_EXPIRED_4W) ||
						($data["member"]["current_status"]==STATUS_EXPIRED_5W) ||
						($data["member"]["current_status"]==STATUS_EXPIRED_6W) ||
						($data["member"]["current_status"]==STATUS_EXPIRED_TOTAL)) {
						$retval[]=$ngid;
					}
					break;
				case "all_agencies":
					// csak tagok, kiveve toroltek
					if ($data["user"]["is_deleted"]=="t") break;
					if ($data["user"]["is_admin_acknowledged"]=="f") break;
					if ($data["user"]["is_registered"]=="f") break;
					if (!isset( $data["member"] )) break;
					if ($data["member"]["member_type"]!="agency") break;
					if ($data["member"]["current_status"]==STATUS_DELETED ||
						$data["member"]["current_status"]==STATUS_NEW_USER_DELETED) break;
					$retval[]=$ngid;
					break;
				case "all_expired_agencies":
					// csak nem lejart tagok
					if ($data["user"]["is_deleted"]=="t") break;
					if ($data["user"]["is_admin_acknowledged"]=="f") break;
					if ($data["user"]["is_registered"]=="f") break;
					if (!isset( $data["member"] )) break;
					if ($data["member"]["member_type"]!="agency") break;
					if ($data["member"]["current_status"]==STATUS_DELETED ||
						$data["member"]["current_status"]==STATUS_NEW_USER_DELETED) break;
					if ($data["member"]["current_status"]!=STATUS_EXPIRED_TOTAL) break;
					$retval[]=$ngid;
					break;
				case "all_not_expired_agencies":
					// csak a lejart tagok
					if ($data["user"]["is_deleted"]=="t") break;
					if ($data["user"]["is_admin_acknowledged"]=="f") break;
					if ($data["user"]["is_registered"]=="f") break;
					if (!isset( $data["member"] )) break;
					if ($data["member"]["member_type"]!="agency") break;
					if ($data["member"]["current_status"]==STATUS_DELETED ||
						$data["member"]["current_status"]==STATUS_NEW_USER_DELETED) break;
					if ($data["member"]["current_status"]==STATUS_EXPIRED_TOTAL ||
						$data["member"]["current_status"]==STATUS_NEW_USER_EXPIRED_TOTAL) break;
					$retval[]=$ngid;
					break;
				case "active_members":
					// csak aktiv tagok, kiveve toroltek
					if ($data["user"]["is_deleted"]=="t") break;
					if ($data["user"]["is_admin_acknowledged"]=="f") break;
					if ($data["user"]["is_registered"]=="f") break;
					if (!isset( $data["member"] )) break;
					if ($data["member"]["current_status"]==STATUS_DELETED ||
						$data["member"]["current_status"]==STATUS_NEW_USER_DELETED) break;
					if (substr( $data["member"]["expires_tstamp"] , 0 , 19 )<fo3_date( "Y-m-d H:i:s" )) break;
					$retval[]=$ngid;
					break;
				case "active_translators":
					// csak aktiv tagok, kiveve toroltek
					if ($data["user"]["is_deleted"]=="t") break;
					if ($data["user"]["is_admin_acknowledged"]=="f") break;
					if ($data["user"]["is_registered"]=="f") break;
					if (!isset( $data["member"] )) break;
					if ($data["member"]["member_type"]=="agency") break;
					if ($data["member"]["current_status"]==STATUS_DELETED ||
						$data["member"]["current_status"]==STATUS_NEW_USER_DELETED) break;
					if (substr( $data["member"]["expires_tstamp"] , 0 , 19 )<fo3_date( "Y-m-d H:i:s" )) break;
					$retval[]=$ngid;
					break;

				}
			}
			return $retval;
		}

		public function calculateNewsletterUsers() {
			$retval = "";
			$this->filters = $this->getNewsletterFilters();

			FNTH_db_query( "begin transaction" );

			$query = sprintf( "select * from users order by users.email" );
			$res = FNTH_db_query( $query );

			while ($row = FNTH_db_fetch( $res )) {
				$p = array();
				$p["newsletter_user_name"]=$row["user_name"];
				$p["newsletter_hash"]=$row["hash"];
				$p["newsletter_user_email"]=$row["email"];

				$data = array( "user" => $row );
				$member = $this->mm->getMemberFromUserId( $row["user_id"] );
				if ($member) {
					$data["member"]=$member;
				}

				$p["newsletter_user_parameters"]=$data;

				$nuid = $this->nm->getNewsletterUserIdFromEmail( $row["email"] );
				if (!$nuid) {
					$nuid = $this->nm->addNewsletterUser( $p );
				} else {
					$this->nm->updateNewsletterUser( $nuid , $p );
				}

				$ngids = $this->getUserGroups( $nuid , $p );
				$retval.= FNTH_da( $ngids , false , true );
				$this->nm->setNewsletterUsersGroups( $nuid , $ngids );
			}
			FNTH_db_free_result( $row );

			FNTH_db_query( "commit transaction" );


			return $retval;
		}
	}

	class AdminNewsletterUserRegeneratorPage extends BackendPage {

		public function renderContent() {
			$nug = new NewsletterUserGenerator();
			return $nug->calculateNewsletterUsers();
		}
	}

?>
