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


	class MiniPosterReminderTask extends GeneralTask {

		public function __construct( $task_id , $task_processor ) {
			parent::__construct( $task_id , $task_processor );
		}

		protected function processJobPosterNotificationMail( $job_id ) {
			$query = sprintf( "select count(*) as num from jobs where job_id=%d and not is_revoked and not is_closed and poster_acknowledged and admin_acknowledged" , $job_id );
			$jobs = FNTH_db_get_field( $query , "num" );
			if (!$jobs) return;	// nem kell, mert vagy visszavont vagy lezart, vagy nem jovahagyott, vagy nem admin jovahagyott
			
			$query = sprintf( "select count(*) as num from job_quotes where job_id=%d" , $job_id );
			$numofquotes = FNTH_db_get_field( $query , "num" );
			$query = sprintf( "select count(*) as num from job_quotes where job_id=%d and accepted_at is null" , $job_id );
			$numofunacceptedquotes = FNTH_db_get_field( $query , "num" );
			if (!$numofunacceptedquotes) return;
			// there are unaccepted quotes
			require_once( $GLOBALS["site_root"]."/includes/models/TemplateMail.inc.php" );
			require_once( $GLOBALS["site_root"]."/includes/models/Constants.inc.php" );
			require_once( $GLOBALS["site_root"]."/includes/models/Job.inc.php" );
			require_once( $GLOBALS["site_root"]."/includes/models/User.inc.php" );
			$tm = new TemplateMailModel( "sys_job_poster_reminder" );

			$jm = new JobModel();
			$um = new UserModel();
			$job = $jm->getJobFromId( $job_id , true );
			$user = $um->getUserFromUserId( $job["poster_user_id"] );
			
			$cm = new ConstantsModel( $tm );
			$cm->addJobConstantsToTemplate( $job_id , "job" , $job );
			$cm->addUserConstantsToTemplate( $job["poster_user_id"] , "user" , $user );

			$tm->sendMail( $user["email"] );
		}

		protected function processJobPosterNotifications1st() {
			$query = sprintf( "select job_id from jobs where not job_posts_notified_1st and job_quote_deadline<now()" );
			$job_ids = FNTH_db_get_column( $query , "job_id" );
			if (empty( $job_ids )) return;
			foreach ($job_ids as $job_id) {
				$this->processJobPosterNotificationMail( $job_id );
				$query = sprintf( "update jobs set job_posts_notified_1st=true where job_id=%d" , $job_id );
				FNTH_db_free_result( FNTH_db_query( $query ) );
			}
		}
		
		protected function processJobPosterNotifications2nd() {
			$query = sprintf( "select job_id from jobs where not job_posts_notified_2nd and job_quote_deadline<now() - interval '3 days'" );
			$job_ids = FNTH_db_get_column( $query , "job_id" );
			if (empty( $job_ids )) return;
			foreach ($job_ids as $job_id) {
				$this->processJobPosterNotificationMail( $job_id );
				$query = sprintf( "update jobs set job_posts_notified_2nd=true where job_id=%d" , $job_id );
				FNTH_db_free_result( FNTH_db_query( $query ) );
			}
		}

		public function processTask() {
			if ($this->task_processor->timeLeft()<3) return false;	// lejar az ido
			$this->processJobPosterNotifications1st();
			$this->processJobPosterNotifications2nd();
		}
	}

?>
