<?php 
require_once $_SERVER['DOCUMENT_ROOT'].
'/includes/access.inc.php';
if(!userIsLoggedIn()){
	include '../login.html.php';
	exit();
}

if(!userHasRole('Account Administrator')){
	$error='Only Account Administrators may access this page.';
	include '../accessdenied.html.php';
	exit();
}

include $_SERVER['DOCUMENT_ROOT'].'/includes/db.inc.php';
try{
	$result=$pdo->query('select id, name from sender');
}
catch(PDOException $e){
	$error='Error fetching senders from the database.';
	include 'error.html.php';
	exit();
}

while($row=$result->fetch()){
	$senders[]=array(
	'id'=>$row['id'],
	'name'=>$row['name']);
}
include 'senders.html.php';

if(isset($_POST['action']) and $_POST['action']=='Delete'){
	include $_SERVER['DOCUMENT_ROOT'].'/includes/db.inc.php';
	
	//Delete role assignments for this sender
	try{
		$sql='delete from senderrole where 
		senderid=:id';
		$s=$pdo->prepare($sql);
		$s->bindValue(':id',$_POST['id']);
		$s->execute();
	}//try 
	catch(PDOException $e){
		$error='Error removing sender from roles.';
		include 'error.html.php';
		exit();
	}//catch
	
	//get poems belonging to sender
	try{
		$sql="select id from poem where senderid=:id";
		$s=$pdo->prepare($sql);
		$s->bindValue(':id',$_POST['id']);
		$s->execute();		
	}
	catch(PDOException $e){
		$error='Error getting list of poems to delete.';
		include 'error.html.php';
		exit();
	}
	$result=$s->fetchAll();
	//Delete poem category entries
	try{
		$sql='delete from poemcategory where poemid=:id';
		$s=$pdo->prepare($sql);
		//for each poem 
		foreach($result as $row){
			$poemid=$row['id'];
			$s->bindValue(':id',$poemid);
			$s->execute();
		}//foreach
	}//try
	catch(PDOException $e){
		$error='Error deleting category entries for poem.';
		include 'error.html.php';
		exit();
	}//catch
	//Delete poems belonging to sender 
	try{
		$sql="delete from poem where senderid=:id";
		$s=$pdo->prepare($sql);
		$s->bindValue(':id',$_POST['id']);
		$s->execute();
	}//try
	catch(PDOException $e){
		$error='Error deleting poems for sender.';
		include 'error.html.php';
		exit();
	}//catch 
	//Delete the sender 
	try{
		$sql="delete from sender where id=:id";
		$s=$pdo->prepare($sql);
		$s->bindValue(':id',$_POST['id']);
		$s->execute();
	}
	catch(PDOException $e){
		$error='Error deleting sender.';
		include 'error.html.php';
		exit();
	}//catch
	header('location:.');
	exit();
}//if isset action and action=delete

if(isset($_GET['add'])){
	include $_SERVER['DOCUMENT_ROOT'].
	'/includes/db.inc.php';
	$pageTitle='ارسال کننده جدید';
	$action='addform';
	$name='';
	$email='';
	$id='';
	$button='اضافه کردن ارسال کننده';
	
	//Build the list of roles
	try{
		$result=$pdo->query('select id,description from role');
	}catch(PDOException $e){
		$error='Error fetching list of roles.';
		include 'error.html.php';
		exit();
	}
	
	foreach($result as $row){
		$roles[]=array(
		'id'=>$row['id'],
		'description'=>$row['description'],
		'selected'=>FALSE);
	}
	
	include 'form.html.php';
	exit();
}

if(isset($_GET['addform'])){
	include $_SERVER['DOCUMENT_ROOT'].'/includes/db.inc.php';
	try{
		$sql="insert into sender set 
		name=:name,
		email=:email";
		$s=$pdo->prepare($sql);
		$s->bindValue(':name',$_POST['name']);
		$s->bindValue(':email',$_POST['email']);
		$s->execute();
	}
	catch(PDOException $e){
		$error='Error adding submitted sender.';
		include 'error.html.php';
		exit();
	}
	
	$senderid=$pdo->lastInsertId();
	if($_POST['password']!=''){
		$password=md5($_POST['password'].'poems');
		try{
			$sql='update sender set 
			password=:password
			where id=:id';
			$s=$pdo->prepare($sql);
			$s->bindValue(':password',$password);
			$s->bindValue(':id',$senderid);
			$s->execute();
		}catch(PDOException $e){
			$error='Error setting sender password.';
			include 'error.html.php';
			exit();
		}//catch
	}//if password is set
	
	if(isset($_POST['roles'])){
		foreach($_POST['roles'] as $role){
			try{
				$sql='insert into senderrole set 
				senderid=:senderid,
				roleid=:roleid';
				$s=$pdo->prepare($sql);
				$s->bindValue(':senderid',$senderid);
				$s->bindValue(':roleid',$role);
				$s->execute();
			}//try
			catch(PDOException $e){
				$error='Error assigning selected role to sender.';
				include 'error.html.php';
				exit();
			}//catch
		}//foreach roles
	}//if roles is set
	
	header('location:.');
	exit();
}//addform

if(isset($_POST['action']) and $_POST['action']=='Edit'){
	include $_SERVER['DOCUMENT_ROOT'].'/includes/db.inc.php';
	try{
		$sql='select id,name,email from sender where id=:id';
		$s=$pdo->prepare($sql);
		$s->bindValue(':id',$_POST['id']);
		$s->execute();
	}
	catch(PDOException $e){
		$error='Error fetching sender details.';
		include 'error.html.php';
		exit();
	}
	$row=$s->fetch();
	$pageTitle='ویرایش ارسال کننده';
	$action='editform';
	$name=$row['name'];
	$email=$row['email'];
	$id=$row['id'];
	$button='ویرایش ارسال کننده';
	
	//Get list of roles assigned to this sender
	try{
		$sql='select roleid from senderrole 
		where senderid=:id';
		$s=$pdo->prepare($sql);
		$s->bindValue(':id',$id);
		$s->execute();
	}//try 
	catch(PDOException $e){
		$error='Error fetching list of assigned roles.';
		include 'error.html.php';
		exit();
	}//catch
	$selectedRoles[]=array();
	foreach($s as $row){
		$selectedRoles[]=$row['roleid'];
	}
	
	//Build the list of all roles 
	try{
		$result=$pdo->query('select id,description from role');
	}catch(PDOException $e){
		$error='Error fetching list of roles.';
		include 'error.html.php';
		exit();
	}
	
	foreach($result as $row){
		$roles[]=array(
		'id'=>$row['id'],
		'description'=>$row['description'],
		'selected'=>in_array($row['id'],$selectedRoles));
	}//foreach	
	
	include 'form.html.php';
	exit();
	}//if isset action and action=edit
	
	if(isset($_GET['editform'])){
		include $_SERVER['DOCUMENT_ROOT'].'/includes/db.inc.php';
		try{
			$sql="update sender set 
			name=:name,
			email=:email
			where id=:id";
			$s=$pdo->prepare($sql);
			$s->bindValue(':id',$_POST['id']);
			$s->bindValue(':name',$_POST['name']);
			$s->bindValue(':email',$_POST['email']);
			$s->execute();			
		}
		catch(PDOException $e){
			$error='Error updating submitted sender.';
			include 'error.html.php';
			exit();
		}
		
		if($_POST['password']!=''){
			$password=md5($_POST['password'].'poems');
			try{
				$sql='update sender set 
				password=:password
				where id=:id';
				$s=$pdo->prepare($sql);
				$s->bindValue(':password',$password);
				$s->bindValue(':id',$_POST['id']);
				$s->execute();
			}//try
			catch(PDOException $e){
				$error='Error setting sender password.';
				include 'error.html.php';
				exit();
			}//catch
		}//if password is set
		
		try{
			$sql='delete from senderrole 
			where senderid=:id';
			$s=$pdo->prepare($sql);
			$s->bindValue(':id',$_POST['id']);
			$s->execute();
		}//try 
		catch(PDOException $e){
			$error='Error removing old sender role entries.';
			include 'error.html.php';
			exit();
		}//catch
		
		if(isset($_POST['roles'])){
			foreach($_POST['roles'] as $role){
				try{
					$sql='insert into senderrole set 
					senderid=:senderid,
					roleid=:roleid';
					$s=$pdo->prepare($sql);
					$s->bindValue(':senderid',
					$_POST['id']);
					$s->bindValue(':roleid',$role);
					$s->execute();
				}//try
				catch(PDOException $e){
					$error='Error assigning selected roles to sender.';
					include 'error.html.php';
					exit();
				}//catch
			}//foreach
		}//if roles is set
		
		header('location:.');
		exit();
	}//editform



