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

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

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

if(isset($_POST['action']) and $_POST['action']=='Edit'){
	include $_SERVER['DOCUMENT_ROOT'].
	'/includes/db.inc.php';
	try{
		$sql='select id,poemtext,senderid from poem where id=:id';
		$s=$pdo->prepare($sql);
		$s->bindValue(':id',$_POST['id']);
		$s->execute();
	}
	catch(PDOException $e){
		$error='Error fetching poem details.';
		include 'error.html.php';
		exit();
	}
	$row=$s->fetch();
	
	$pageTitle='ویرایش شعر';
	$action='editform';
	$text=$row['poemtext'];
	$senderid=$row['senderid'];
	$id=$row['id'];
	$button='ویرایش شعر';
	
	//build the list of senders 
	try{
		$result=$pdo->query('select id,name from sender');
	}
	catch(PDOException $e){
		$error='Error fetching list of senders.';
		include 'error.html.php';
		exit();
	}
	
	foreach($result as $row){
		$senders[]=array(
		'id'=>$row['id'],
		'name'=>$row['name']);
	}
	
	//Get list of categories containing this poem
	try{
		$sql='select categoryid from poemcategory where poemid=:id';
		$s=$pdo->prepare($sql);
		$s->bindValue(':id',$id);
		$s->execute();
	}
	catch(PDOException $e){
		$error='Error fetching list of selected categories.';
		include 'error.html.php';
		exit();
	}
	
	foreach($s as $row){
		$selectedCategories[]=$row['categoryid'];
	}
	
	//build the list of all categories
	try{
		$result=$pdo->query('select id,name from category');
	}
	catch(PDOException $e){
		$error='Error fetching list of all categories.';
		include 'error.html.php';
		exit();
	}
	$count=$s->rowCount();
	if($count){
		foreach($result as $row){
			$categories[]=array(
			'id'=>$row['id'],
			'name'=>$row['name'],
			'selected'=>in_array($row['id'],$selectedCategories));
		}
	}//if($count)
	else{
		foreach($result as $row){
			$categories[]=array(
			'id'=>$row['id'],
			'name'=>$row['name'],
			'selected'=>FALSE);
		}
		}//else 
		include 'form.html.php';
		exit();
}

if(isset($_GET['addform'])){
	include $_SERVER['DOCUMENT_ROOT'].
	'/includes/db.inc.php';
	if($_POST['sender']==''){
		$error='You must choose a sender for this poem. please try again.';
		include 'error.html.php';
		exit();
	}
	try{
		$sql='insert into poem set 
		poemtext=:poemtext,
		poemdate=CURDATE(),
		senderid=:senderid';
		$s=$pdo->prepare($sql);
		$s->bindValue(':poemtext',$_POST['text']);
		$s->bindValue(':senderid',$_POST['sender']);
		$s->execute();
	}
	catch(PDOException $e){
		$error='Error adding submitted poem.';
		include 'error.html.php';
		exit();
	}
	$poemid=$pdo->lastInsertId();
	
	if(isset($_POST['categories'])){
		try{
			$sql='insert into poemcategory set 
			poemid=:poemid,
			categoryid=:categoryid';
			$s=$pdo->prepare($sql);
			foreach($_POST['categories'] as $categoryid){
				$s->bindValue(':poemid',$poemid);
				$s->bindValue(':categoryid',$categoryid);
				$s->execute();
			}
		}//try
		catch(PDOException $e){
			$error='Error inserting poem into selected categories.';
			include 'error.html.php';
			exit();
		}
	}//if(isset($categories))
		header('location:.');
		exit();	
}

if(isset($_GET['editform'])){
	include $_SERVER['DOCUMENT_ROOT'].
	'/includes/db.inc.php';
	if($_POST['sender']==''){
		$error='You must choose a sender. please try again.';
		include 'error.html.php';
		exit();
	}
	
	try{
		$sql='update poem set 
		poemtext=:poemtext,
		senderid=:senderid 
		where id=:id';
		$s=$pdo->prepare($sql);
		$s->bindValue(':id',$_POST['id']);
		$s->bindValue(':poemtext',$_POST['text']);
		$s->bindValue(':senderid',$_POST['sender']);
		$s->execute();		
	}
	catch(PDOException $e){
		$error='Error updating submitted poem.';
		include 'error.html.php';
		exit();
	}
	
	try{
		$sql='delete from poemcategory where poemid=:id';
		$s=$pdo->prepare($sql);
		$s->bindValue(':id',$_POST['id']);
		$s->execute();
	}
	catch(PDOException $e){
		$error='Error removing old poem category entries.';
		include 'error.html.php';
		exit();
	}
	if(isset($_POST['categories'])){
		try{
			$sql='insert into poemcategory set 
			poemid=:poemid,
			categoryid=:categoryid';
			$s=$pdo->prepare($sql);
		foreach($_POST['categories'] as $categoryid){
			$s->bindValue(':poemid',$_POST['id']);
			$s->bindValue(':categoryid',$categoryid);
			$s->execute();
		}//foreach		
		}//try
		catch(PDOException $e){
			$error='Error inserting poem into selected categories.';
			include 'error.html.php';
			exit();
		}//catch 	
	}//if(isset($_POST['categories'])) 
		header('location:.');
		exit();	
}


if(isset($_POST['action']) and $_POST['action']=='Delete'){
	include $_SERVER['DOCUMENT_ROOT'].'/includes/db.inc.php';
	//delete category assignments for this poem
	try{
		$sql='delete from poemcategory where poemid=:id';
		$s=$pdo->prepare($sql);
		$s->bindValue(':id',$_POST['id']);
		$s->execute();
	}
	catch(PDOException $e){
		$error='Error removing poem from categories.';
		include 'error.html.php';
		exit();
	}
	
	//delete the poem 
	try{
		$sql='delete from poem where id=:id';
		$s=$pdo->prepare($sql);
		$s->bindValue(':id',$_POST['id']);
		$s->execute();
	}
	catch(PDOException $e){
		$error='Error deleting poem.';
		include 'error.html.php';
		exit();
	}
	header('location:.');
	exit();
}

if(isset($_GET['action']) and 
$_GET['action']=='search'){
include $_SERVER['DOCUMENT_ROOT'].'/includes/db.inc.php';
$select='select id,poemtext';
$from=' from poem';
$where=' where true';

$placeholders=array();
if($_GET['sender']!='')	{
	$where.=" and senderid=:senderid";
	$placeholders[':senderid']=$_GET['sender'];
}
if($_GET['category']!=''){
	$from.=' inner join poemcategory on id=poemid';
	$where.=" and categoryid=:categoryid";
	$placeholders[':categoryid']=$_GET['category'];
}
if($_GET['text']!=''){
	$where.=" and poemtext like :poemtext";
	$placeholders[':poemtext']='%'.$_GET['text'].'%';
}
try{
	$sql=$select.$from.$where;
	$s=$pdo->prepare($sql);
	$s->execute($placeholders);
}
catch(PDOException $e){
	$error='Error fetching poems.';
	include 'error.html.php';
	exit();
}

foreach($s as $row){
	$poems[]=array(
	'id'=>$row['id'],
	'text'=>$row['poemtext']);
}
include 'poems.html.php';
exit();
}



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

//fetch categories
try{
	$result=$pdo->query('select id,name from category');
}
catch(PDOException $e){
	$error='Error fetching categories from database.';
	include 'error.html.php';
	exit();
}
foreach($result as $row){
	$categories[]=array(
	'id'=>$row['id'],
	'name'=>$row['name']);
}

include 'searchform.html.php';



