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

	class NewsletterModel {

		public function getNewsletterGroupOptions( $choose = true ) {
			$retval = array();
			if ($choose) $retval[""]=__("Choose");
			$ds = FNTH_db_get_associative( "select newsletter_group_id,ng_title from newsletter_groups" , "newsletter_group_id" , "ng_title" );
			foreach ($ds as $k=>$v) $retval[$k]=$v;
			return $retval;
		}

		public function addNewsletterGroup( $p = array() ) {
			$ins = array();
			$ins["ng_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["newsletter_group_name"] ) );
			$ins["ng_title"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["newsletter_group_title"] ) );
			$ins["ng_from_email"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["newsletter_group_from_email"] ) );
			$q = FNTH_db_generate_insert_query( "newsletter_groups" , $ins );
			FNTH_db_free_result( FNTH_db_query( $q ) );
			$query = sprintf( "select currval('newsletter_groups_newsletter_group_id_seq') as num" );
			return FNTH_db_get_field( $query , "num" );
		}

		public function setNewsletterUsersGroups( $nu_id , $ng_ids ) {
			$query = sprintf( "delete from newsletter_group_users where newsletter_user_id=%d" , $nu_id );
			FNTH_db_free_result( FNTH_db_query( $query ) );
			$ins = array();
			$ins["newsletter_user_id"]=sprintf( "%d" , $nu_id );
			foreach ($ng_ids as $ng_id) {
				$ins["newsletter_group_id"]=sprintf( "%d" , $ng_id );
				FNTH_db_free_result( FNTH_db_query(
					FNTH_db_generate_insert_query( "newsletter_group_users" , $ins ) ) );
			}
		}

		public function getNewsletterUsersGroups( $nu_id ) {
			return FNTH_db_get_column(
				sprintf( "select newsletter_group_id from newsletter_group_users where newsletter_user_id=%d order by newsletter_group_id" , $nu_id ) , "newsletter_group_id" );
		}

		public function getNewsletterGroupFromId( $ng_id ) {
			$query = sprintf( "select
				newsletter_group_id,
				ng_name as newsletter_group_name,
				ng_title as newsletter_group_title,
				ng_from_email as newsletter_group_from_email from
				newsletter_groups where newsletter_group_id=%d" , $ng_id );
			return FNTH_db_get_row( $query );
		}
		
		public function getNewsletterGroupFromName( $ng_name ) {
			$query = sprintf( "select
				newsletter_group_id,
				ng_name as newsletter_group_name,
				ng_title as newsletter_group_title,
				ng_from_email as newsletter_group_from_email from
				newsletter_groups where ng_name='%s'" , FNTH_db_addslashes( $ng_name ) );
			return FNTH_db_get_row( $query );
		}


		public function updateNewsletterGroup( $ng_id , $p = array() ) {
			$upd = array();
			if (isset( $p["newsletter_group_name"] ))
				$upd["ng_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["newsletter_group_name"] ) );
			if (isset( $p["newsletter_group_title"] ))
				$upd["ng_title"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["newsletter_group_title"] ) );
			if (isset( $p["newsletter_group_from_email"] ))
				$upd["ng_from_email"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["newsletter_group_from_email"] ) );
			$q = FNTH_db_generate_update_query( "newsletter_groups" , $upd , sprintf( "newsletter_group_id=%d" , $ng_id ) );
			FNTH_db_free_result( FNTH_db_query( $q ) );
			return $ng_id;
		}

		public function deleteNewsletterGroup( $ng_id ) {
			// group userek
			FNTH_db_free_result( FNTH_db_query(
				sprintf( "delete from newsletter_group_users where newsletter_group_id=%d" , $ng_id ) ) );

			// mar hasznalt newsletter userek (kikuldott hirlevelek)
			FNTH_db_free_result( FNTH_db_query(
				sprintf( "delete from newsletter_newsletter_users where newsletter_newsletter_id in (select newsletter_newsletter_id from newsletter_newsletters where newsletter_group_id=%d)" , $ng_id ) ) );

			// csoportban levo hirlevelek
			FNTH_db_free_result( FNTH_db_query(
				sprintf( "delete from newsletter_newsletters where newsletter_group_id=%d" , $ng_id ) ) );

			// maga a hirlevel csoport
			FNTH_db_free_result( FNTH_db_query(
				sprintf( "delete from newsletter_groups where newsletter_group_id=%d" , $ng_id ) ) );
		}

		public function addNewsletterUser( $p = array() ) {
			$query = sprintf( "select newsletter_user_id from newsletter_users where nu_email='%s'" , FNTH_db_addslashes( $p["newsletter_user_email"] ));
			$user_id = FNTH_db_get_field( $query , "newsletter_user_id" );
			if ($user_id) return $user_id;

			if (!isset( $p["newsletter_hash"] )) $p["newsletter_hash"]=md5( uniqid("newsletter") );
			// really add
			$ins = array();
			if (isset( $p["newsletter_user_name"] )) $ins["nu_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["newsletter_user_name"] ) );
			if (isset( $p["newsletter_user_email"] )) $ins["nu_email"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["newsletter_user_email"] ) );
			if (isset( $p["newsletter_hash"] )) $ins["nu_hash"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["newsletter_hash"] ) );
			if (isset( $p["newsletter_user_parameters"] ) && is_array( $p["newsletter_user_parameters"] )) {
				$ins["nu_parameters"]=sprintf( "'%s'" , FNTH_db_addslashes( serialize( $p["newsletter_user_parameters"] ) ) );
			}
			$q = FNTH_db_generate_insert_query( "newsletter_users" , $ins );
			FNTH_db_free_result( FNTH_db_query( $q ) );
			$query = sprintf( "select currval('newsletter_users_newsletter_user_id_seq') as num" );
			return FNTH_db_get_field( $query , "num" );
		}

		public function getNewsletterUserIdFromEmail( $email ) {
			$query = sprintf( "select newsletter_user_id from newsletter_users where nu_email='%s'" , FNTH_db_addslashes( $email ) );
			return FNTH_db_get_field( $query , "newsletter_user_id" );
		}

		public function updateNewsletterUser( $nu_id , $p = array() ) {
			// really add
			$upd = array();
			if (isset( $p["newsletter_user_name"] )) $upd["nu_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["newsletter_user_name"] ) );
			if (isset( $p["newsletter_user_email"] )) $upd["nu_email"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["newsletter_user_email"] ) );
			if (isset( $p["newsletter_user_parameters"] ) && is_array( $p["newsletter_user_parameters"] )) {
				$upd["nu_parameters"]=sprintf( "'%s'" , FNTH_db_addslashes( serialize( $p["newsletter_user_parameters"] ) ) );
			}
			$q = FNTH_db_generate_update_query( "newsletter_users" , $upd , sprintf( "newsletter_user_id=%d" , $nu_id ) );
			FNTH_db_free_result( FNTH_db_query( $q ) );
			return $nu_id;
		}	

		public function getNewsletterUserFromId( $nu_id ) {
			$query = sprintf( "select
				nu_name as newsletter_user_name,
				nu_email as newsletter_user_email,
				nu_hash as newsletter_user_hash,
				nu_parameters as newsletter_user_parameters from
				newsletter_users where newsletter_user_id=%d" , $nu_id );
			$row = FNTH_db_get_row( $query );
			if (!isset( $row["newsletter_user_parameters"] )) return $row;
			if (!strlen( $row["newsletter_user_parameters"] )) return $row;

			$params = unserialize( $row["newsletter_user_parameters"] );
			$row["newsletter_user_parameters"]=$params;
			return $row;
		}

		public function getNewsletterUserIdFromHash( $hash ) {
			return FNTH_db_get_field(
				sprintf( "select newsletter_user_id from newsletter_users where nu_hash='%s'" , FNTH_db_addslashes( $hash ) ),
				"newsletter_user_id" );
		}

		public function deleteNewsletterUser( $nu_id ) {
			// group userek
			FNTH_db_free_result( FNTH_db_query(
				sprintf( "delete from newsletter_group_users where newsletter_user_id=%d" , $nu_id ) ) );

			// mar hasznalt newsletter userek (kikuldott hirlevelek)
			FNTH_db_free_result( FNTH_db_query(
				sprintf( "delete from newsletter_newsletter_users where newsletter_user_id=%d" , $nu_id ) ) );

			// maga a user
			FNTH_db_free_result( FNTH_db_query(
				sprintf( "delete from newsletter_users where newsletter_user_id=%d" , $nu_id ) ) );
		}

		public function addNewsletterUserToGroup( $nu_id , $ng_id ) {
			$query = sprintf( "select count(*) as num from newsletter_group_users where newsletter_user_id=%d and newsletter_group_id=%d",$nu_id,$ng_id );
			$vane = FNTH_db_get_field( $query , "num" );
			if ($vane) return;
			$ins = array();
			$ins["newsletter_user_id"]=sprintf( "%d" , $nu_id );
			$ins["newsletter_group_id"]=sprintf( "%d" , $ng_id );
			$q = FNTH_db_generate_insert_query( "newsletter_group_users" , $ins );
			FNTH_db_free_result( FNTH_db_query( $q ) );
		}

		public function deleteNewsletterUserFromGroup( $nu_id , $ng_id ) {
			FNTH_db_free_result( FNTH_db_query(
				sprintf( "delete from newsletter_group_users where newsletter_user_id=%d and newsletter_group_id=%d",$nu_id,$ng_id ) ) );
		}

		public function addAllNewsletterGroupUsersToNewsletter( $nl_id ) {
			// remove all users
			$query = sprintf( "delete from newsletter_newsletter_users where newsletter_newsletter_id=%d" , $nl_id );
			FNTH_db_free_result( FNTH_db_query( $query ) );

			$newsletter  = $this->getNewsletterFromId( $nl_id );
			// add all users
			$query = sprintf( "insert into newsletter_newsletter_users( newsletter_newsletter_id , newsletter_user_id )
				select %d,newsletter_user_id from newsletter_group_users where newsletter_group_id=%d",
				$newsletter["newsletter_group_id"] );
			FNTH_db_free_result( FNTH_db_query( $query ) );
		}


		public function addNewsletter( $p = array() ) {
			$ins = array();
			$ins["newsletter_group_id"] = sprintf( "%d" , $p["newsletter_group_id"] );
			$ins["nl_title"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["newsletter_title"] ) );
			$ins["nl_subject"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["newsletter_subject"] ) );
			$ins["nl_head"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["newsletter_head"] ) );
			$ins["nl_body"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["newsletter_body"] ) );
			$ins["nl_is_html"]=($p["newsletter_is_html"]) ? "true" : "false";

			$q = FNTH_db_generate_insert_query( "newsletter_newsletters" , $ins );
			FNTH_db_free_result( FNTH_db_query( $q ) );
		}

		public function updateNewsletter( $nl_id , $p = array() ) {
			$upd = array();
			if (isset( $p["newsletter_group_id"] )) $upd["newsletter_group_id"] = sprintf( "%d" , $p["newsletter_group_id"] );
			if (isset( $p["newsletter_title"] )) $upd["nl_title"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["newsletter_title"] ) );
			if (isset( $p["newsletter_subject"] )) $upd["nl_subject"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["newsletter_subject"] ) );
			if (isset( $p["newsletter_head"] )) $upd["nl_head"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["newsletter_head"] ) );
			if (isset( $p["newsletter_body"] )) $upd["nl_body"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["newsletter_body"] ) );
			if (isset( $p["newsletter_is_html"] )) $upd["nl_is_html"]=($p["newsletter_is_html"]) ? "true" : "false";

			$q = FNTH_db_generate_update_query( "newsletter_newsletters" , $upd , sprintf( "newsletter_newsletter_id=%d" , $nl_id ) );
			FNTH_db_free_result( FNTH_db_query( $q ) );
		}

		public function deleteNewsletter( $nl_id ) {
			$query = sprintf( "delete from newsletter_newsletter_users where newsletter_newsletter_id=%d" , $nl_id );
			FNTH_db_free_result( FNTH_db_query( $query ) );

			$query = sprintf( "delete from newsletter_newsletters where newsletter_newsletter_id=%d",$nl_id );
			FNTH_db_free_result( FNTH_db_query( $query ) );
		}

		public function getNewsletterFromId( $nl_id ) {
			$query = sprintf( "select
				newsletter_group_id,
				nl_title as newsletter_title,
				nl_subject as newsletter_subject,
				nl_head as newsletter_head,
				nl_body as newsletter_body,
				nl_is_html as newsletter_is_html,
				nl_is_activated as newsletter_is_activated,
				nl_is_sent as newsletter_is_sent,
				nl_senttstamp as newsletter_sent_tstamp
				from newsletter_newsletters where newsletter_newsletter_id=%d" , $nl_id );
			$row = FNTH_db_get_row( $query );
			if (!$row) return $row;
			$row["newsletter_is_html"]=($row["newsletter_is_html"]=="t");
			$row["newsletter_is_activated"]=($row["newsletter_is_activated"]=="t");
			return $row;
		}

		public function activateNewsletter( $nl_id ) {
			$tm = new TaskManagerModel();
			$tm->newTask();
			$tm->setTaskName( sprintf( "activating newsletter #%d" , $nl_id ) );
	
			$newsletter = $this->getNewsletterFromId( $nl_id );
			$tm->setTaskDescription( $newsletter["newsletter_subject"] );
			$tm->setTaskType( "newsletter_activation" );
			$tm->setTaskUID( $nl_id );
			$task_id = $tm->saveNewTask();
			FNTH_db_free_result(
				FNTH_db_query(
					sprintf( "update newsletter_newsletters set activate_task_id=%d
						where newsletter_newsletter_id=%d" , $task_id , $nl_id ) ) );
		}
		
		public function sendNewsletter( $nl_id ) {
			$tm = new TaskManagerModel();
			$tm->newTask();
			$tm->setTaskName( sprintf( "send newsletter #%d" , $nl_id ) );
	
			$newsletter = $this->getNewsletterFromId( $nl_id );
			$tm->setTaskDescription( $newsletter["newsletter_subject"] );
			$tm->setTaskType( "newsletter_send" );
			$tm->setTaskUID( $nl_id );
			$task_id = $tm->saveNewTask();
			FNTH_db_free_result(
				FNTH_db_query(
					sprintf( "update newsletter_newsletters set send_task_id=%d
						where newsletter_newsletter_id=%d" , $task_id , $nl_id ) ) );
		}
		
		public function deactivateNewsletter( $nl_id ) {
			$upd = array();
			$upd["nl_is_activated"]="false";
			$q = FNTH_db_generate_update_query( "newsletter_newsletters" , $upd ,
				sprintf( "newsletter_newsletter_id=%d",  $nl_id ) );
			FNTH_db_free_result( FNTH_db_query( $q ) );
		}

		public function sendNewsletterToUser( $nl_id , $nu_id ) {
			$newsletter = $this->getNewsletterFromId( $nl_id );
			$newsletter_group = $this->getNewsletterGroupFromId( $newsletter["newsletter_group_id"] );
			$newsletter_user = $this->getNewsletterUserFromId( $nu_id );

			$mm = new MailModel();

			$mm->setHtml( $newsletter["newsletter_is_html"] );
			$mm->setSubject( $newsletter["newsletter_subject"] );
			$mm->setBody( $newsletter["newsletter_body"] );

			// bontsuk szet
			$email = $newsletter_group["newsletter_group_from_email"];
			if (strpos( $email , "<" )!==false) {
				$pos = strpos( $email , "<" );
				$mm->sFrom = trim( substr( $email , 0 , $pos ) );
				$email = substr( $email , $pos + 1 );
				$pos = strpos( $email , ">" );
				if ($pos!==false) $email = substr( $email , 0 , $pos );
				$mm->sFromEmail = $email;
			} else {
				$mm->sFrom = $newsletter_group["newsletter_group_from_email"];
				$mm->sFromEmail = $newsletter_group["newsletter_group_from_email"];
			}

			$email = $newsletter_user["newsletter_user_email"];

			$page = URL_UNSUBSCRIBE;
			$u = array();
			$u["hash"]=$newsletter_user["newsletter_user_hash"];
			$u["ngid"]=$newsletter["newsletter_group_id"];
			$url_unsubscribe = FNTH_render_url( $page , $u );
			$mm->setParam( "url_unsubscribe" , $url_unsubscribe );
			$mm->setParam( "server_name" , $_SERVER["SERVER_NAME"] );

			if (isset( $newsletter_user["newsletter_user_parameters"] )) {
				foreach ($newsletter_user["newsletter_user_parameters"] as $k => $v) $mm->setParam( $k , $v );
			}
			$mm->sendMail( $email );
		}
	}
?>
