<?php

function send_project_task_reminder($variables=null) {
	/*
		$variables['date']
		$variables['return_example']
	*/
	include_once("includes/projects/projects_functions.php");
	global $system_user;

	$this_var =	array('ongoing' => true, 'depth' => 2);
	$ongoing_project = get_projects($this_var);
	
	$return_example_array = array();
	if(!isset($variables['return_example'])) {
		$variables['return_example'] = false;
	}
	
	if(isset($variables['date'])) {
		$today = $variables['date'];
	} else {
		$today = date('Y-m-d', time());
	}
	$today_ts = strtotime($today);
	$two_day_notice = date('Y-m-d', strtotime($today.' +2 day'));

	$project_status_types = project_task_status_types();
	$finished_status = 2;
	foreach($project_status_types as $this_status_id => $this_status_data) {
		if($this_status_data['name_id'] == 'finished') {
			$finished_status = $this_status_id;
		}
	}
	
	foreach($ongoing_project as $this_project_id => $this_project_data) {
		if(!is_array($this_project_data)) {
			continue;
		} 
		
		foreach($this_project_data['tasks'] as $this_project_task_id => $this_project_task_data) {
			if($this_project_task_data['status'] >= $finished_status) {
				// error_log('Task id '.$this_project_task_id.' status is finished');
				continue;
			} elseif($this_project_task_data['reminder_active'] < 1) {
				// error_log('Task id '.$this_project_task_id.' not reminder active');
				continue;
			}

			$subject = '';
			$email_body = '';
			// print $this_project_task_id.' => '.json_encode($this_project_task_data).'<br>';
			if(strtotime($this_project_task_data['end_date']) < $today_ts) {
				// Send mail about missed deadline
				$subject = 'HFKF-project reminder. '.'Uppgiftens deadline passerad ('.$this_project_task_data['end_date'].')';
				
				$email_body .= "<div style='width:100%;color:red;'>Din uppgift (".$this_project_task_id.") på fyrk-projekt är över deadline datumet ".$this_project_task_data['end_date']."</div>";
				$email_body .= "<div style='display:block;width:100%;color:red;'>Om du är klar med uppgiften, logga in på <a href='https://project.hfkf.fi'>fyrk-projekt sidan</a> och ändra uppgiftens status till avslutad/klar</div>";

				// error_log('Task id '.$this_project_task_id.' missed deadline');

			} elseif($two_day_notice == $this_project_task_data['end_date']) {
				// Send a two day notice of upcomming deadline
				$subject = 'HFKF-project reminder. '.'Uppgift deadline på kommande ('.$this_project_task_data['end_date'].')';
				
				// Html body
				$email_body .= "<div style='display:block;width:100%;padding 4 0 4 0;font-weight:bold;font-size:100%;text-align:center;'>Din uppgift (".$this_project_task_data['name'].") på fyrk-projekt har deadline inom två dagar (".$this_project_task_data['end_date'].")</div>";
				
				// error_log('Task id '.$this_project_task_id.' 2 days ahead notice');
			} else {
				// error_log('Task id '.$this_project_task_id.' skip as '.$this_project_task_data['end_date'].' is not for reminding');
				continue;
			}
			
			// Get user data
			if(!$user_data = $system_user->get_user_data($this_project_task_data['owner'])) {
				// Could not get user data
				continue;
			}
			
			if((isset($_SESSION['id']) ? $_SESSION['id'] : 0) == 1) {
				$email_body .= json_encode($this_project_task_data);
			}
			
			$email_to = $user_data['login_name'];
			// $email_to = 'dcco@hotmail.com';
			
			// Plain text
			$email_body = '<h1>Project uppgift påminnelse</h1>'."\r\n".$email_body;

			$email_body .= "<div style='display:block;width:95%;margin:15px auto;padding 4 2 4 0;font-size:100%;text-align:center;border:solid 1px;'>".$this_project_task_data['description']."</div>";
			
			$this_var = array('to' => $email_to, 'subject' => $subject, 'message' => $email_body, 'html' => true);
			
			if($variables['return_example']) {
				$this_var['task_data'] = $this_project_task_data;
				$return_example_array[] = $this_var;
			} elseif(send_mail($this_var)) {
				// Log sent mail
				$log_project_reminder_ts = array('task_id' => $this_project_task_data['task_id'], 'allow_changed_by' => false, 'reminder_sent' => date('Y-m-d H:i:s', time()));
				if(!save_project_task($log_project_reminder_ts)) {
					print 'Failed to log successfull reminder!';
				}
			}
		}
	}
	
	if($variables['return_example']) {
		return $return_example_array;
	}
	
	return true;
	
	$this_var = array('to' => 'dcco@hotmail.com', 'subject' => 'HFKF-project reminder test', 'message' => $email_body, 'html' => true);
	return send_mail($this_var);
}



?>