<?php
define('FYRK_BASIC', 10);
define('FYRK_MANAGER', 50);
define('FYRK_ADMIN', 100);

$fyrk_projects = new fyrk_projects($_SESSION['id']);

class fyrk_projects {
	public $my_permission = 0;
	
	public function __construct ($_user_id) {
		$this->my_permission = $this->get_user_permission($_user_id);
	}
	
	private function get_user_permission($user_id) {
		$query = 'SELECT * FROM user_permission WHERE function="fyrk_project" AND id='.$user_id;
		
		if(!$result = run_query($query)) {
			return false;
		}
		
		$row = $result->fetch_assoc();
		
		return (isset($row['permission_lvl']) ? $row['permission_lvl'] : 0);
	}
	
}

function get_projects($variables=null) {
	/*
		$variables['id']
		$variables['ongoing'] = true / false
		$variables['depth'] = 1/2
	*/
	include(LANGUAGEFILE);
	$returndata = array();
	
	if(!isset($variables['depth'])) {
		$variables['depth'] = 1;
	}
	
	$query = 'SELECT * FROM fyrk_projects';
	$query_where = '';
	
	if(isset($variables['id'])) {
		if($query_where) {
			$query_where .= ' AND ';
		}
		$query_where .= 'id='.$variables['id'];
	}
	
	if(isset($variables['ongoing'])) {
		if($query_where) {
			$query_where .= ' AND ';
		}
		$query_where .= 'date>="'.date("Y-m-d", time()).'"';
	}
	
	if($query_where) {
		$query .= ' WHERE '.$query_where;
	}
	
	if(!$result = run_query($query)) {
		$GLOBALS['errors'][] = __FUNCTION__.' could not get project data '.$query;
		return false;
	}
	
	while($row = $result->fetch_assoc()) {
		if($variables['depth'] > 1) {
			$row['tasks'] = array();
			$this_var = array('project_id' => $row['id'], 'ongoing' => $variables['ongoing']);
			$this_var['depth'] = $variables['depth']-1;
			
			if($project_tasks = get_project_tasks($this_var)) {
				$row['tasks'] = $project_tasks;
			}
		}
		
		$returndata[$row['id']] = $row;
	}
	
	return $returndata;
}

function get_project_users() {
	
	$query = 'SELECT t1.id, t1.user_name, t2.permission_lvl FROM accounts t1, user_permission t2 WHERE t2.function="fyrk_project" AND t2.permission_lvl>='.FYRK_BASIC.' AND t1.id=t2.id ORDER BY user_name ASC';
	
	if(!$result = run_query($query)) {
		$GLOBALS['errors'][] = __FUNCTION__.' could not retrieve project users';
		return false;
	}
	
	$returndata = array();
	
	while($row = $result->fetch_assoc()) {
		$returndata[$row['id']] = $row;
	}
	
	return $returndata;
}

function check_project_save() {
	global $sop;
	
	if(!isset($_POST['submit'])) {
		return 0;
	}
	
	$variables = get_passed_variables('post');
	$variables['date'] = $sop->date_to_db($variables['date']);
	
	$query = 'INSERT INTO fyrk_projects (id, date, name, owner) VALUES ('.$variables['id'].', "'.$variables['date'].'", "'.$variables['name'].'", '.$variables['owner'].') ON DUPLICATE KEY UPDATE date=VALUES(date), name=VALUES(name), owner=VALUES(owner)';
	
	if(!$result = run_query($query)) {
		$GLOBALS['errors'][] = __FUNCTION__.' Could not save project';
		return false;
	}
	
	if($variables['id']) {
		return $variables['id'];
	} else {
		return  get_insert_id();
	}
	
}

function project_task_status_types() {
	include(LANGUAGEFILE);
	
	$returndata = array();
	
	$returndata[0] = array('name_id' => 'not_started', 'name' => $l_not_started, 'color' => 'white');
	$returndata[1] = array('name_id' => 'started', 'name' => $l_started, 'color' => 'yellow');
	$returndata[2] = array('name_id' => 'finished', 'name' => $l_finished, 'color' => 'green');
	
	return $returndata;
}

function get_project_tasks($variables) {
	/*
		$variables['task_id']
		$variables['project_id']
		$variables['owner']
		$variables['status']
		$variables['free_query']
		
		$variables['order_by']
		
	*/
	global $sop;
	
	$query = 'SELECT * FROM fyrk_project_tasks';
	$query_where = '';
	
	if(isset($variables['task_id'])) {
		if($query_where) {
			$query_where .= ' AND ';
		}
		$query_where .= 'task_id='.$variables['task_id'];
	}
	
	if(isset($variables['project_id'])) {
		if($query_where) {
			$query_where .= ' AND ';
		}
		$query_where .= 'project_id='.$variables['project_id'];
	}
	
	if(isset($variables['owner'])) {
		if($query_where) {
			$query_where .= ' AND ';
		}
		$query_where .= 'owner='.$variables['owner'];
	}
	
	if(isset($variables['free_query'])) {
		if($query_where) {
			$query_where .= ' AND ';
		}
		$query_where .= $variables['free_query'];
	}
	
	
	if($query_where) {
		$query .= ' WHERE '.$query_where;
	}
	
	if(isset($variables['order_by'])) {
		$query .= ' ORDER BY '.$variables['order_by'];
	}
	
	
	if(!$result = run_query($query)) {
		$GLOBALS['errors'][] = __FUNCTION__.' could not get project task data '.$query;
		return false;
	}
	
	$returndata = [];
	while($row = $result->fetch_assoc()) {
		$row['description'] = $sop->dbHtml_to_print($row['description']); // Decode html stored in database
		$returndata[$row['task_id']] = $row;
	}
	
	return $returndata;	
}

function save_project_task($variables) {
	/*
		$variables['force_insert'] = true / false (default)
		
		$variables['task_id']
		$variables['project_id']
		$variables['name']
		$variables['owner']
		$variables['end_date']
		$variables['status']
		$variables['description']
		$variables['reminder_active']
		$variables['reminder_sent']
		$variables['allow_changed_by'] = true (default) / false
	*/
	global $sop;
	
	$query_update = '';
	$query_columns = '';
	$query_values = '';
	
	if(isset($variables['project_id'])) {
		if($query_update) $query_update .= ', ';
		$query_update .= 'project_id="'.$variables['project_id'].'"';
	}
	
	if(isset($variables['name'])) {
		if($query_update) $query_update .= ', ';
		$query_update .= 'name="'.$variables['name'].'"';
	}
	
	if(isset($variables['owner'])) {
		if($query_update) $query_update .= ', ';
		
		$query_update .= 'owner='.$variables['owner'];
		$query_columns .= ', owner';
		$query_values .= ', '.$variables['owner'];
	}
	
	if(isset($variables['end_date'])) {
		if($query_update) $query_update .= ', ';
		
		$query_update .= 'end_date="'.$variables['end_date'].'"';
		$query_columns .= ', end_date';
		$query_values .= ', "'.$variables['end_date'].'"';
	}
	
	if(isset($variables['status'])) {
		if($query_update) $query_update .= ', ';
		
		$query_update .= '`status`='.$variables['status'];
		$query_columns .= ', `status`';
		$query_values .= ', '.$variables['status'];
	}
	
	if(isset($variables['description'])) {
		if($query_update) $query_update .= ', ';
		
		$variables['description'] = $sop->html_to_db($variables['description']);
		
		$query_update .= 'description="'.$variables['description'].'"';
		$query_columns .= ', description';
		$query_values .= ', "'.$variables['description'].'"';
	}
		
	if(isset($variables['reminder_active'])) {
		if($query_update) $query_update .= ', ';
		
		$query_update .= 'reminder_active='.$variables['reminder_active'];
		$query_columns .= ', reminder_active';
		$query_values .= ', "'.$variables['reminder_active'].'"';
	}
		
	if(isset($variables['reminder_sent'])) {
		if($query_update) $query_update .= ', ';
		
		$query_update .= 'reminder_sent="'.$variables['reminder_sent'].'"';
		$query_columns .= ', reminder_sent';
		$query_values .= ', "'.$variables['reminder_sent'].'"';
	}
	
	if(isset($variables['allow_changed_by']) ? $variables['allow_changed_by'] : true) {
		if($query_update) $query_update .= ', ';
		$query_update .= 'changed_by="'.(isset($_SESSION['id']) ? $_SESSION['id'] : 0).'"';
		$query_columns .= ', changed_by';
		$query_values .= ', '.(isset($_SESSION['id']) ? $_SESSION['id'] : 0);
	}
	

	if($variables['task_id'] > 0 AND !(isset($variables['force_insert']) ? $variables['force_insert'] : false)) {
		// Update
		$query = 'UPDATE fyrk_project_tasks SET '.$query_update.' WHERE task_id='.$variables['task_id'];
	} else {
		// Insert
		$query = 'INSERT INTO fyrk_project_tasks (task_id, project_id, name'.$query_columns.') VALUES ('.$variables['task_id'].', '.$variables['project_id'].', "'.$variables['name'].'"'.$query_values.')';
	}

	if($result = run_query($query)) {
		return true;
	} else {
		$GLOBALS['errors'][] = __FUNCTION__.' failed to save project task with query '.$query;
		return false;
	}
}

function delete_project_task($variables) {
	/*
		$variables['task_id']
	*/
	
	$task_data = get_project_tasks($variables);
	
	// Store for undo
	$_SESSION['undo']['project_task_delete'] = $task_data[$variables['task_id']];
	
	$query = 'DELETE FROM fyrk_project_tasks WHERE task_id='.$variables['task_id'];
	
	if(!run_query($query)) {
		$GLOBALS['errors'][] = __FUNCTION__.' could not delete project task '.$query;
		return false;
	}
	
	$_SESSION['undo']['project_task_delete']['delet_ts'] = date('Y-m-d H:i:s', time());
	
	return $task_data[$variables['task_id']]['project_id'];
}

?>