<?php

include '../php/classes/page.php';
include 'functions/func_file.php';
include 'functions/func_xml.php';
$page = new Page();
$page->init_user();

# make sure the user is logged in before he has access to the
# admin section
if(!$page->user()->check_role('admin'))
{
	$page->addMessage('You must login to use the administrator pages.');
	$page->display('menu',false);
	$page->content('admin/login.php');
}
else
{
	# get the page element parameter that we want to edit
	# if it doesn't exist, we just display the list of options
	# below
	$what_to_edit = isset($_REQUEST['what']) ? $_REQUEST['what'] : null;
	
	# gather the appropriate data to send to the template
	# and show the right template based on what page data we're editing
	switch($what_to_edit)
	{
		case 'shorttermopportunities':
			work_shorttermopportunities();
			break;
		case 'travelinfo':
			work_travelinfo();
			break;
		default:
			$page->content('admin/missionandtravel.php');
	}
}

$page->attr('title','Discipling the Nations: Administrator Area: Mission and Travel');
$page->menu('admin/menu.php');
$page->addCSSFile('admin.css');


$page->process();

######################################## FUNCTION: work_shorttermopportunities #
function work_shorttermopportunities()
{
	global $DOCROOT, $page;
	$file_path = $DOCROOT.'/site/data/missionandtravel/shorttermopportunities.xml';
	$opps = xml_get_from_file($file_path);
	
	# get commands from get, if there is nothing there, get
	# them from the post
	$do = isset($_REQUEST['do']) ? $_REQUEST['do'] : null;
	$which = isset($_REQUEST['which']) ? $_REQUEST['which'] : null;
	
	# add, edit and delete just display a form or other template page
	# so we can just lump them all together here
	if($do == 'add' || $do == 'edit' || $do == 'delete')
	{
		# if we are editing or deleting, the template needs the opportunity data
		if($do != 'add')
		{
			$c = 0;
			while(($opps->opportunity[$c]['id'] != $_GET['which'])
							&& isset($opps->opportunity[$c]))
				$c++;
			$page->data('opportunity',$opps->opportunity[$c]);
		}
		$page->content('admin/missionandtravel/shorttermopportunities/'.$do.'.php');
	}
	# add the opportunity and return to the list
	else if($do == 'doadd')
	{
		# make sure the file gets uploaded and moved correctly
		if(isset($_POST['title']))
		{
			# get the last id from the file
			$last_id = $opps->opportunity[count($opps->opportunity) - 1]['id'];
			if(is_null($last_id))
				$last_id = 0;

			$new_opportunity = $opps->addChild('opportunity');
			$new_opportunity->addChild('title',$_POST['title']);
			$new_opportunity->addChild('description',$_POST['description']);
			$new_opportunity->addAttribute('id',$last_id + 1);
			
			file_put_contents($file_path,$opps->asXML());
			
			$page->addMessage('Short term opportunity successfully added.');
			$page->data('opps',$opps);
			$page->content('admin/missionandtravel/shorttermopportunities/list.php');
		}
		else
		{
			$page->addError('You cannot leave the title blank.');
			$page->content('admin/missionandtravel/shorttermopportunities/add.php');
		}
	}	
	# edit the opportunity and return to the list
	else if($do == 'doedit')
	{
		# find the opportunity with the right id
		$opp_found = null;
		$c = 0;
		while(is_null($opp_found) && isset($opps->opportunity[$c]))
		{
			if((int)$opps->opportunity[$c]['id'] == $_POST['which'])
			{
				$opp_found = true;
				break;
			}
			$c++;
		}
			
		# make sure we found the one and didn't just stop at the last one!
		if(is_null($opp_found))
		{
			$page->addError('Short term opportunity was not found.  No deletion has occurred!');
			$page->data('opps',$opps);
			$page->content('admin/missionandtravel/shorttermopportunities/list.php');
			return;
		}
		elseif(!empty($_POST['title']))
		{
			$opps->opportunity[$c]->title = $_POST['title'];
			$opps->opportunity[$c]->description = $_POST['description'];
			
			file_put_contents($file_path,$opps->asXML());
			$page->addMessage('Short term opportunity successfully edited.');
			$page->data('opps',$opps);
			$page->content('admin/missionandtravel/shorttermopportunities/list.php');
		}
		else
		{
			$page->addError('You cannot leave the title blank.');
			$page->content('admin/missionandtravel/shorttermopportunities/edit.php');
		}
	}
	# perform the deletion of the specified opportunity
	else if($do == 'dodelete')
	{
		# find the opportunity with the right id
		$opp_found = null;
		$c = 0;
		while(is_null($opp_found) && isset($opps->opportunity[$c]))
		{
			if((int)$opps->opportunity[$c]['id'] == $_POST['which'])
			{
				$opp_found = true;
				break;
			}
			$c++;
		}
			
		# make sure we found the one and didn't just stop at the last one!
		if(is_null($opp_found))
		{
			$page->addError('Short term opportunity was not found.  No deletion has occurred!');
			$page->data('opps',$opps);
			$page->content('admin/missionandtravel/shorttermopportunities/list.php');
			return;
		}
		
		unset($opps->opportunity[$c]);
		file_put_contents($file_path,$opps->asXML());
		$page->addMessage('Short term opportunity successfully deleted.');
		$page->data('opps',$opps);	
		$page->content('admin/missionandtravel/shorttermopportunities/list.php');
	}
	# display the list of opps
	else
	{
		$page->data('opps',$opps);
		$page->content('admin/missionandtravel/shorttermopportunities/list.php');
	}
}

#################################################### FUNCTION: work_travelinfo #
function work_travelinfo()
{
	global $DOCROOT, $page;
	$file_path = $DOCROOT.'/site/data/missionandtravel/travelinfo.xml';
	$infos = xml_get_from_file($file_path);
	
	# get commands from get, if there is nothing there, get
	# them from the post
	$do = isset($_REQUEST['do']) ? $_REQUEST['do'] : null;
	$which = isset($_REQUEST['which']) ? $_REQUEST['which'] : null;
	
	# add, edit and delete just display a form or other template page
	# so we can just lump them all together here
	if($do == 'add' || $do == 'edit' || $do == 'delete')
	{
		# if we are editing or deleting, the template needs the information data
		if($do != 'add')
		{
			$c = 0;
			while(($infos->information[$c]['id'] != $_GET['which'])
							&& isset($infos->information[$c]))
				$c++;
			$page->data('information',$infos->information[$c]);
		}
		$page->content('admin/missionandtravel/travelinfo/'.$do.'.php');
	}
	# add the information and return to the list
	else if($do == 'doadd')
	{
		# make sure the file gets uploaded and moved correctly
		if(isset($_POST['title']))
		{
			# get the last id from the file
			$last_id = $infos->information[count($infos->information) - 1]['id'];
			if(is_null($last_id))
				$last_id = 0;

			$new_information = $infos->addChild('information');
			$new_information->addChild('title',$_POST['title']);
			$new_information->addChild('description',$_POST['description']);
			$new_information->addAttribute('id',$last_id + 1);
			
			file_put_contents($file_path,$infos->asXML());
			
			$page->addMessage('Travel info successfully added.');
			$page->data('infos',$infos);
			$page->content('admin/missionandtravel/travelinfo/list.php');
		}
		else
		{
			$page->addError('You cannot leave the title blank.');
			$page->content('admin/missionandtravel/travelinfo/add.php');
		}
	}	
	# edit the information and return to the list
	else if($do == 'doedit')
	{
		# find the information with the right id
		$info_found = null;
		$c = 0;
		while(is_null($info_found) && isset($infos->information[$c]))
		{
			if((int)$infos->information[$c]['id'] == $_POST['which'])
			{
				$info_found = true;
				break;
			}
			$c++;
		}
			
		# make sure we found the one and didn't just stop at the last one!
		if(is_null($info_found))
		{
			$page->addError('Travel info was not found.  No deletion has occurred!');
			$page->data('infos',$infos);
			$page->content('admin/missionandtravel/travelinfo/list.php');
			return;
		}
		elseif(!empty($_POST['title']))
		{
			$infos->information[$c]->title = $_POST['title'];
			$infos->information[$c]->description = $_POST['description'];
			
			file_put_contents($file_path,$infos->asXML());
			$page->addMessage('Travel info successfully edited.');
			$page->data('infos',$infos);
			$page->content('admin/missionandtravel/travelinfo/list.php');
		}
		else
		{
			$page->addError('You cannot leave the title blank.');
			$page->content('admin/missionandtravel/travelinfo/edit.php');
		}
	}
	# perform the deletion of the specified information
	else if($do == 'dodelete')
	{
		# find the information with the right id
		$info_found = null;
		$c = 0;
		while(is_null($info_found) && isset($infos->information[$c]))
		{
			if((int)$infos->information[$c]['id'] == $_POST['which'])
			{
				$info_found = true;
				break;
			}
			$c++;
		}
			
		# make sure we found the one and didn't just stop at the last one!
		if(is_null($info_found))
		{
			$page->addError('Travel info was not found.  No deletion has occurred!');
			$page->data('infos',$infos);
			$page->content('admin/missionandtravel/travelinfo/list.php');
			return;
		}
		
		unset($infos->information[$c]);
		file_put_contents($file_path,$infos->asXML());
		$page->addMessage('Travel info successfully deleted.');
		$page->data('infos',$infos);	
		$page->content('admin/missionandtravel/travelinfo/list.php');
	}
	# display the list of infos
	else
	{
		$page->data('infos',$infos);
		$page->content('admin/missionandtravel/travelinfo/list.php');
	}
}

?>