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

	class JobQuoteModel {

		public function addJobQuote( $p ) {
			$ins = array();
			$ins["job_id"]=sprintf( "%d" , $p["job_id"] );
			$ins["member_id"]=sprintf( "%d" , $p["member_id"] );
			if (isset( $p["ctstamp"] )) $ins["ctstamp"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["ctstamp"] ) );
			$ins["quote_pricing"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["quote_pricing"] ) );
			$ins["quote"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["quote"] ) );
			FNTH_db_free_result(
				FNTH_db_query(
					FNTH_db_generate_insert_query( "job_quotes", $ins ) ) );
			$job_quote_id = FNTH_db_get_field( "select currval( 'job_quotes_job_quote_id_seq') as num" , "num" );
			return $job_quote_id;
		}
		
		protected function getAttachmentPath( $job_quote_id ) {
			return sprintf( "/media/quote-attachments/%d/%03d/%03d",
				(int)($job_quote_id/1000000),
				((int)($job_quote_id/1000))%1000,
				$job_quote_id%1000 );
		}

		public function addJobQuoteAttachment( $job_quote_id , $attachment = array() ) {
			if (!isset( $attachment["real_path"] )) return false;
			if (!file_exists( $attachment["real_path"] )) return false;
			$fsize = filesize( $attachment["real_path"] );
			if (!$fsize) return false;

			$ins = array();
			$ins["job_quote_id"]=$job_quote_id;
			$ins["attachment_title"] = sprintf( "'%s'" , FNTH_db_addslashes( $attachment["comment"] ) );
			$ins["attachment_type"]="'general'";
			$ins["attachment_mimetype"]=sprintf( "'%s'" , FNTH_db_addslashes( $attachment["mime_type"] ) );
			$ins["attachment_original_filename"]=sprintf( "'%s'", FNTH_db_addslashes( $attachment["filename"] ) );
			$ins["attachment_path"]=sprintf( "'%s'" , FNTH_db_addslashes( $this->getAttachmentPath( $job_quote_id ) ) );
			$filename = uniqid("");
			$ins["attachment_filename"]=sprintf( "'%s'" , FNTH_db_addslashes( $filename ) );
			$ins["attachment_filesize"]=sprintf( "%d" , $fsize );
			$q = FNTH_db_generate_insert_query( "job_quote_attachments" , $ins );
			FNTH_db_free_result( FNTH_db_query( $q ) );

			$fullpath = $GLOBALS["site_root"] . $this->getAttachmentPath( $job_quote_id );
			if (!is_dir( $fullpath )) mkdir( $fullpath , 0777 , true );
			copy( $attachment["real_path"] , $fullpath . "/" . $filename );
			return true;
		}

		public function prepareAttachment( $ja ) {
			$ja["icon"]=fo3_get_mimetype_icon_path( $ja["attachment_mimetype"] );
			$ja["attachment_url"]=JobURL::renderJobQuoteAttachmentURL( $ja["job_quote_attachment_id"] );
			return $ja;
		}
		
		public function getJobQuoteAttachment( $job_quote_attachment_id ) {
			$query = sprintf( "select * from job_quote_attachments where job_quote_attachment_id=%d" , $job_quote_attachment_id );
			$retval = FNTH_db_get_row( $query );
			if (!$retval) return $retval;
			return $this->prepareAttachment( $retval );
		}
		
		public function getJobQuoteAttachments( $job_quote_id ) {
			$query = sprintf( "select * from job_quote_attachments where job_quote_id=%d" , $job_quote_id );
			$ds = FNTH_db_get_resultset( $query );
			if (empty( $ds )) return $ds;
			$retval = array();
			foreach ($ds as $row) $retval[]=$this->prepareAttachment( $row );
			return $retval;
		}

		protected function prepareJobQuote( $row ) {
			$row["job_quote_attachments"] = $this->getJobQuoteAttachments( $row["job_quote_id"] );
			$row["job_quote_url"]=JobURL::renderQuoteOfJobURL( $row["job_quote_id"] );
			return $row;
		}

		public function getJobQuoteFromJobQuoteId( $job_quote_id , $full = false ) {
			$query = sprintf( "select * from job_quotes where job_quote_id=%d" , $job_quote_id );
			$row = FNTH_db_get_row( $query );
			if (!$row) return $row;
			$row = $this->prepareJobQuote( $row );
			return $row;
		}

		public function isJobQuoteAvailableForUser( $job_quote_id , $user_id , $job_quote = false ) {
			if (!$job_quote_id) return false;
			if (!$user_id) return false;
			if (!$job_quote) $job_quote = $this->getJobQuoteFromJobQuoteId( $job_quote_id );
			$job_id = $job_quote["job_id"];
			$jm = new JobModel();

			if ($jm->isJobOwnedByUser( $job_id , $user_id )) return true;	// megnezheti, mert az ove

			$mm = new MemberModel();
			$member = $mm->getMemberFromUserId( $user_id );
			if (!$member) return false;
			$member_id = $member["member_id"];
			return ($job_quote["member_id"]==$member_id);		// megnezheti, mert o adta fel
		}

		public function acceptJobQuote( $job_quote_id , $accept = true , $comment = "" , $sendmails = true ) {
			// update the database
			$upd = array();
			$upd["is_accepted"]=($accept) ? "true" : "false";
			$upd["quote_accept_comment"]=sprintf( "'%s'" , FNTH_db_addslashes( $comment ) );
			$upd["accepted_at"]="now()";
			$q = FNTH_db_generate_update_query( "job_quotes" , $upd , sprintf( "job_quote_id=%d" , $job_quote_id ) );
			FNTH_db_free_result( FNTH_db_query( $q ) );
			if ($sendmails) {
				require_once( $GLOBALS["site_root"]."/includes/models/User.inc.php" );
				require_once( $GLOBALS["site_root"]."/includes/models/Member.inc.php" );
				require_once( $GLOBALS["site_root"]."/includes/models/Job.inc.php" );
				require_once( $GLOBALS["site_root"]."/includes/models/TemplateMail.inc.php" );

				$this->jm = new JobModel();
				$this->mm = new MemberModel();
				$this->um = new UserModel();

				$job_quote = $this->getJobQuoteFromJobQuoteId( $job_quote_id , true );
				$job = $this->jm->getJobFromId( $job_quote["job_id"] , true );
				$poster_user = $this->um->getUserFromUserId( $job["poster_user_id"] );

				$receiver_member = $this->mm->getMemberFromMemberId( $job_quote["member_id"] , true );
				$receiver_user = $this->um->getUserFromUserId( $receiver_member["user_id"] );

				if ($accept) {
					// accept mails
					// 1. mail to translator
					$tmm = new TemplateMailModel( "sys_job_quote_accepted_to_translator" );
					$tmm->setHtml( true );
					$tmm->setPlain( true );
					$cm = new ConstantsModel( $tmm );
					$cm->addJobConstantsToTemplate( $job["job_id"] , "job" , $job );
					$cm->addMemberConstantsToTemplate( $receiver_member["member_id"] , "receiver_member" , $receiver_member );
					$cm->addUserConstantsToTemplate( $poster_user["user_id"] , "poster_user" , $poster_user );
					$cm->addUserConstantsToTemplate( $receiver_user["user_id"] , "receiver_user" , $receiver_user );
					$cm->addJobQuoteConstantsToTemplate( $job_quote["job_quote_id"] , "job_quote" , $job_quote );
					$tmm->sendMail( $receiver_user["email"] );

					// 2. mail to poster
					$lang = $GLOBALS["backlang"][$job["site_language_id"]];
					$tmm = new TemplateMailModel( "sys_job_quote_accepted_to_poster" , $lang );
					$tmm->setHtml( true );
					$tmm->setPlain( true );
					$cm = new ConstantsModel( $tmm , $lang );
					$cm->addJobConstantsToTemplate( $job["job_id"] , "job" );
					$cm->addMemberConstantsToTemplate( $receiver_member["member_id"] , "receiver_member" , $receiver_member );
					$cm->addUserConstantsToTemplate( $poster_user["user_id"] , "poster_user" , $poster_user );
					$cm->addUserConstantsToTemplate( $receiver_user["user_id"] , "receiver_user" , $receiver_user );
					$cm->addJobQuoteConstantsToTemplate( $job_quote["job_quote_id"] , "job_quote" , $job_quote );
					$tmm->sendMail( $poster_user["email"] );

				} else {
					// decline email
					// 1. mail to translator
					$tmm = new TemplateMailModel( "sys_job_quote_declined_to_translator" );
					$tmm->setHtml( true );
					$tmm->setPlain( true );
					$cm = new ConstantsModel( $tmm );
					$cm->addJobConstantsToTemplate( $job["job_id"] , "job" , $job );
					$cm->addMemberConstantsToTemplate( $receiver_member["member_id"] , "receiver_member" , $receiver_member );
					$cm->addUserConstantsToTemplate( $poster_user["user_id"] , "poster_user" , $poster_user );
					$cm->addUserConstantsToTemplate( $receiver_user["user_id"] , "receiver_user" , $receiver_user );
					$cm->addJobQuoteConstantsToTemplate( $job_quote["job_quote_id"] , "job_quote" , $job_quote );
					$tmm->sendMail( $receiver_user["email"] );
				}
			}
		}
	}

?>
