<?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 'missionarymoments':
			work_missionarymoments();
			break;
		case 'emailupdates':
			work_emailupdates();
			break;
		case 'prayerletters':
			work_prayerletters();
			break;
		default:
			$page->content('admin/prayerrequests.php');
	}
}

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

$page->process();

############################################# FUNCTION: work_missionarymoments #
function work_missionarymoments()
{
	global $DOCROOT, $page;
	$file_path = $DOCROOT.'/site/data/missionarymoments.xml';
	$moments = 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 moment data
		if($do != 'add')
		{
			$c = 0;
			while(($moments->moment[$c]['id'] != $_GET['which'])
							&& isset($moments->moment[$c]))
				$c++;
			$page->data('moment',$moments->moment[$c]);
		}
		$page->content('admin/missionarymoments/'.$do.'.php');
	}
	# add the moment and return to the list
	else if($do == 'doadd')
	{
		# make sure the file gets uploaded and moved correctly
		if($new_link = file_process_upload('file',$DOCROOT.'/site/data/missionarymoments'))
		{
			# get the last id from the file
			$last_id = $moments->moment[count($moments->moment) - 1]['id'];

			$new_title = isset($_POST['title']) ? $_POST['title'] : $_FILES['file']['name'];
			$new_moment = $moments->addChild('moment');
			$new_moment->addChild('title',$new_title);
			$new_moment->addChild('date',$_POST['date']);
			$new_moment->addChild('file',$new_link);
			$new_moment->addChild('summary',$_POST['summary']);
			$new_moment->addAttribute('id',$last_id + 1);
			
			file_put_contents($file_path,$moments->asXML());
			
			$page->addMessage('Missionary moment successfully added.');
			$page->data('moments',$moments);
			$page->content('admin/missionarymoments/list.php');
		}
		else
		{
			$page->addError('An error occurred while uploading the file.  Please try again.');
			$page->content('admin/missionarymoments/add.php');
		}
	}	
	# edit the moment and return to the list
	else if($do == 'doedit')
	{
		# find the moment with the right id
		$c = 0;
		while(isset($moments->moment[$c])
						&& ($moments->moment[$c]['id'] != $_POST['which']))
			$c++;
			
		# if a new file is specified then delete the old one and replace
		# the new one
		if($_FILES['file']['name'] !== '')
		{
			# delete old file
			unlink($DOCROOT.'/site/data/missionarymoments/'.$moments->moment[$c]->file);
			# try to upload the new file
			if($new_link = file_process_upload('file',$DOCROOT.'/site/data/missionarymoments'))
			{
				$moments->moment[$c]->title = isset($_POST['title']) ? $_POST['title'] : $_FILES['file']['name'];
				$moments->moment[$c]->file = $new_link;
				$moments->moment[$c]->date = $_POST['date'];
				$moments->moment[$c]->summary = $_POST['summary'];
				
				file_put_contents($file_path,$moments->asXML());
				$page->addMessage('Missionary moment successfully edited.');
				$page->data('moments',$moments);
				$page->content('admin/missionarymoments/list.php');
			}
			else
			{
				$page->addError('An error occurred while uploading the file.  Please try again.');
				$page->content('admin/missionarymoments/edit.php');
			}			
		}
		# if no new file is uploaded
		else
		{
			if($_POST['title'] !== '')
				$moments->moment[$c]->title = $_POST['title'];
				
			$moments->moment[$c]->date = $_POST['date'];
			$moments->moment[$c]->summary = $_POST['summary'];
				
			file_put_contents($file_path,$moments->asXML());
			$page->addMessage('Missionary moment successfully edited.');
			$page->data('moments',$moments);
			$page->content('admin/missionarymoments/list.php');
		}
	}
	# perform the deletion of the specified moment
	else if($do == 'dodelete')
	{
		# find the moment with the right id
		$c = 0;
		while(($moments->moment[$c]['id'] != $_POST['which'])
						&& isset($moments->moment[$c]))
			$c++;
			
		$file_name = $DOCROOT.'/site/data/missionarymoments/'.$moments->moment[$c]->file;
		if(unlink($file_name))
		{
			unset($moments->moment[$c]);
			file_put_contents($file_path,$moments->asXML());
			$page->addMessage('Missionary moment successfully deleted.');
		}
		else
		{
			$page->addError('Could not delete missionary moment.');
		}
		$page->data('moments',$moments);	
		$page->content('admin/missionarymoments/list.php');
	}
	# display the list of moments
	else
	{
		$page->data('moments',$moments);
		$page->content('admin/missionarymoments/list.php');
	}
}
################################################## FUNCTION: work_emailupdates #
function work_emailupdates()
{
	global $DOCROOT, $page;
	$file_path = $DOCROOT.'/site/data/prayerrequests/emailupdates.xml';
	$updates = 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 update data
		if($do != 'add')
		{
			$c = 0;
			while(($updates->emailupdate[$c]['id'] != $_GET['which'])
							&& isset($updates->emailupdate[$c]))
				$c++;
			$page->data('update',$updates->emailupdate[$c]);
		}
		$page->content('admin/prayerrequests/emailupdates/'.$do.'.php');
	}
	# add the update and return to the list
	else if($do == 'doadd')
	{
		# make sure the file gets uploaded and moved correctly
		if($new_link = file_process_upload('file',$DOCROOT.'/site/data/prayerrequests/emailupdates'))
		{
			# get the last id from the file
			$last_id = $updates->emailupdate[count($updates->emailupdate) - 1]['id'];

			$new_title = isset($_POST['title']) ? $_POST['title'] : $_FILES['file']['name'];
			$new_update = $updates->addChild('emailupdate');
			$new_update->addChild('title',$new_title);
			$new_update->addChild('date',$_POST['date']);
			$new_update->addChild('file',$new_link);
			$new_update->addAttribute('id',$last_id + 1);
			
			file_put_contents($file_path,$updates->asXML());
			
			$page->addMessage('E-mail update successfully added.');
			$page->data('updates',$updates);
			$page->content('admin/prayerrequests/emailupdates/list.php');
		}
		else
		{
			$page->addError('An error occurred while uploading the file.  Please try again.');
			$page->content('admin/prayerrequests/emailupdates/add.php');
		}
	}	
	# edit the update and return to the list
	else if($do == 'doedit')
	{
		# find the update with the right id
		$c = 0;
		while(isset($updates->emailupdate[$c])
						&& ($updates->emailupdate[$c]['id'] != $_POST['which']))
			$c++;
			
		# if a new file is specified then delete the old one and replace
		# the new one
		if($_FILES['file']['name'] !== '')
		{
			# delete old file
			unlink($DOCROOT.'/site/data/prayerrequests/emailupdates/'.$updates->emailupdate[$c]->file);
			# try to upload the new file
			if($new_link = file_process_upload('file',$DOCROOT.'/site/data/prayerrequests/emailupdates'))
			{
				$updates->emailupdate[$c]->title = isset($_POST['title']) ? $_POST['title'] : $_FILES['file']['name'];
				$updates->emailupdate[$c]->file = $new_link;
				$updates->emailupdate[$c]->date = $_POST['date'];
				
				file_put_contents($file_path,$updates->asXML());
				$page->addMessage('Update successfully edited.');
				$page->data('updates',$updates);
				$page->content('admin/prayerrequests/emailupdates/list.php');
			}
			else
			{
				$page->addError('An error occurred while uploading the file.  Please try again.');
				$page->content('admin/prayerrequests/emailupdates/edit.php');
			}			
		}
		# if no new file is uploaded
		else
		{
			if($_POST['title'] !== '')
				$updates->emailupdate[$c]->title = $_POST['title'];
				
			$updates->emailupdate[$c]->date = $_POST['date'];
				
			file_put_contents($file_path,$updates->asXML());
			$page->addMessage('Update successfully edited.');
			$page->data('updates',$updates);
			$page->content('admin/prayerrequests/emailupdates/list.php');
		}
	}
	# perform the deletion of the specified update
	else if($do == 'dodelete')
	{
		# find the update with the right id
		$c = 0;
		while(($updates->emailupdate[$c]['id'] != $_POST['which'])
						&& isset($updates->emailupdate[$c]))
			$c++;
			
		$file_name = $DOCROOT.'/site/data/prayerrequests/emailupdates/'.$updates->emailupdate[$c]->file;
		if(unlink($file_name))
		{
			unset($updates->emailupdate[$c]);
			file_put_contents($file_path,$updates->asXML());
			$page->addMessage('Update successfully deleted.');
		}
		else
		{
			$page->addError('Could not delete update.');
		}
		$page->data('updates',$updates);	
		$page->content('admin/prayerrequests/emailupdates/list.php');
	}
	# display the list of updates
	else
	{
		$page->data('updates',$updates);
		$page->content('admin/prayerrequests/emailupdates/list.php');
	}
}
################################################# FUNCTION: work_prayerletters #
function work_prayerletters()
{
	global $DOCROOT, $page;
	$file_path = $DOCROOT.'/site/data/prayerrequests/prayerletters.xml';
	$letters = 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 letter data
		if($do != 'add')
		{
			$c = 0;
			while(($letters->prayerletter[$c]['id'] != $_GET['which'])
							&& isset($letters->prayerletter[$c]))
				$c++;
			$page->data('letter',$letters->prayerletter[$c]);
		}
		$page->content('admin/prayerrequests/prayerletters/'.$do.'.php');
	}
	# add the letter and return to the list
	else if($do == 'doadd')
	{
		# make sure the file gets uploaded and moved correctly
		if($new_link = file_process_upload('file',$DOCROOT.'/site/data/prayerrequests/prayerletters'))
		{
			# get the last id from the file
			$last_id = $letters->prayerletter[count($letters->prayerletter) - 1]['id'];

			$new_title = isset($_POST['title']) ? $_POST['title'] : $_FILES['file']['name'];
			$new_letter = $letters->addChild('prayerletter');
			$new_letter->addChild('title',$new_title);
			$new_letter->addChild('date',$_POST['date']);
			$new_letter->addChild('file',$new_link);
			$new_letter->addAttribute('id',$last_id + 1);
			
			file_put_contents($file_path,$letters->asXML());
			
			$page->addMessage('Prayer letter successfully added.');
			$page->data('letters',$letters);
			$page->content('admin/prayerrequests/prayerletters/list.php');
		}
		else
		{
			$page->addError('An error occurred while uploading the file.  Please try again.');
			$page->content('admin/prayerrequests/prayerletters/add.php');
		}
	}	
	# edit the letter and return to the list
	else if($do == 'doedit')
	{
		# find the letter with the right id
		$c = 0;
		while(isset($letters->prayerletter[$c])
						&& ($letters->prayerletter[$c]['id'] != $_POST['which']))
			$c++;
			
		# if a new file is specified then delete the old one and replace
		# the new one
		if($_FILES['file']['name'] !== '')
		{
			# delete old file
			unlink($DOCROOT.'/site/data/prayerrequests/prayerletters/'.$letters->prayerletter[$c]->file);
			# try to upload the new file
			if($new_link = file_process_upload('file',$DOCROOT.'/site/data/prayerrequests/prayerletters'))
			{
				$letters->prayerletter[$c]->title = isset($_POST['title']) ? $_POST['title'] : $_FILES['file']['name'];
				$letters->prayerletter[$c]->file = $new_link;
				$letters->prayerletter[$c]->date = $_POST['date'];
				
				file_put_contents($file_path,$letters->asXML());
				$page->addMessage('Prayer letter successfully edited.');
				$page->data('letters',$letters);
				$page->content('admin/prayerrequests/prayerletters/list.php');
			}
			else
			{
				$page->addError('An error occurred while uploading the file.  Please try again.');
				$page->content('admin/prayerrequests/prayerletters/edit.php');
			}			
		}
		# if no new file is uploaded
		else
		{
			if($_POST['title'] !== '')
				$letters->prayerletter[$c]->title = $_POST['title'];
				
			$letters->prayerletter[$c]->date = $_POST['date'];
				
			file_put_contents($file_path,$letters->asXML());
			$page->addMessage('Prayer letter successfully edited.');
			$page->data('letters',$letters);
			$page->content('admin/prayerrequests/prayerletters/list.php');
		}
	}
	# perform the deletion of the specified letter
	else if($do == 'dodelete')
	{
		# find the letter with the right id
		$c = 0;
		while(($letters->prayerletter[$c]['id'] != $_POST['which'])
						&& isset($letters->prayerletter[$c]))
			$c++;
			
		$file_name = $DOCROOT.'/site/data/prayerrequests/prayerletters/'.$letters->prayerletter[$c]->file;
		if(unlink($file_name))
		{
			unset($letters->prayerletter[$c]);
			file_put_contents($file_path,$letters->asXML());
			$page->addMessage('Prayer letter successfully deleted.');
		}
		else
		{
			$page->addError('Could not delete letter.');
		}
		$page->data('letters',$letters);	
		$page->content('admin/prayerrequests/prayerletters/list.php');
	}
	# display the list of letters
	else
	{
		$page->data('letters',$letters);
		$page->content('admin/prayerrequests/prayerletters/list.php');
	}
}
?>