<?php
$message = (isset($_SESSION['message'])) ? $_SESSION['message'] :"";
$action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : "insert";

$where 	= " WHERE 1=1 AND note_type = 0 ";


$query_string = "";

$button_value 	= "Add";
$table_name		= "celebrant_phone_conversation";

$next_page_condition 	= "";

if($_REQUEST['hide_top']){
	$next_page_condition.="&hide_top=1";
}
if($_REQUEST['hide_left']){
	$next_page_condition.="&hide_left=1";
}
if($_REQUEST['job_id']){
	$job_id = $_REQUEST['job_id'];
	$where 	.= " AND job_id = $job_id ";
	$next_page_condition.="&job_id=".$job_id;
}

if(isset($_REQUEST['source'])){
	$source = $_REQUEST['source'];
	$next_page_condition.="&source=$source";
}


$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
	//$next_page_condition.="&start=$start";
}
$query_string =  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
################################################## START EDIT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){
	$query_string =  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
	
	$action 	= "update";
	$button_value 	= "Update";
	$page_name		= "Edit Phone Conversation";	
		
    $id = $_REQUEST['id'];
    $sql= "SELECT * FROM $table_name WHERE id = $id";
    $result_edit = mysqli_query($conn,$sql);
    $row_edit = mysqli_fetch_array($result_edit);

    $id= $row_edit['id'];
    $job_id= $row_edit['job_id'];
    //$admin_id= explode(",",$row_edit['admin_id']);
    $admin_id= $row_edit['admin_id'];
	$aware_of_new_structure_or_role	= $row_edit['aware_of_new_structure_or_role'];
	$phone_conversation_date		= $row_edit['phone_conversation_date'];
	$happy_with_celebrant_group		= $row_edit['happy_with_celebrant_group'];
	$any_ideas_regarding_CPD		= $row_edit['any_ideas_regarding_CPD'];
	$happy_with_mentoring_work		= $row_edit['happy_with_mentoring_work'];
	$interested_in_attending_conference		= $row_edit['interested_in_attending_conference'];
	$local_media_work				= $row_edit['local_media_work'];
	$concerns_with_celebrant_work	= $row_edit['concerns_with_celebrant_work'];
	$ideas_regarding_relationship_with_humanists	= $row_edit['ideas_regarding_relationship_with_humanists'];
	
}
################################################# END EDIT ################################################# 


################################################## START update ################################################## 
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){    
	utility::addslashesOnAllInput(array("query_string"));
	$id= $_REQUEST['id'];
    $job_id= $_REQUEST['job_id'];
	$admin_id= $_REQUEST['admin_id'];
	$phone_conversation_date	 = utility::setFormatedDate($_REQUEST['phone_conversation_date']);
	$aware_of_new_structure_or_role		= $_REQUEST['aware_of_new_structure_or_role'];
	$happy_with_celebrant_group			= $_REQUEST['happy_with_celebrant_group'];
	$any_ideas_regarding_CPD			= $_REQUEST['any_ideas_regarding_CPD'];
	$happy_with_mentoring_work			= $_REQUEST['happy_with_mentoring_work'];
	$interested_in_attending_conference	= $_REQUEST['interested_in_attending_conference'];
	$local_media_work					= $_REQUEST['local_media_work'];
	$concerns_with_celebrant_work		= $_REQUEST['concerns_with_celebrant_work'];
	$ideas_regarding_relationship_with_humanists	= $_REQUEST['ideas_regarding_relationship_with_humanists'];
	
	
    $sql= "UPDATE $table_name SET job_id = '$job_id',job_user_id = '$job_user_id',admin_id = '$admin_id',aware_of_new_structure_or_role = '$aware_of_new_structure_or_role',phone_conversation_date='$phone_conversation_date',happy_with_celebrant_group='$happy_with_celebrant_group',any_ideas_regarding_CPD='$any_ideas_regarding_CPD',happy_with_mentoring_work='$happy_with_mentoring_work',interested_in_attending_conference='$interested_in_attending_conference',local_media_work='$local_media_work',concerns_with_celebrant_work='$concerns_with_celebrant_work',ideas_regarding_relationship_with_humanists='$ideas_regarding_relationship_with_humanists' WHERE id = $id";
    mysqli_query($conn,$sql);
	
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
  	$query_string = str_ireplace(array("page=add_celebrant_phone_conversation","action=edit"), array("page=list",""), $query_string);
	$query_string = "&".$query_string;
	$tmp = ucfirst(str_replace("_"," ", $table_name));
	
	$_SESSION['message']	= ucfirst("Phone conversation updated sucessfully");
	$redirect = "index?page=view_celebrant_phone_conversation$query_string";
	header("Location: $redirect");
	exit();
	
}
################################################# END update ################################################# 
//$custome_limit = 2;
###########################################  PAGING WITH PER PAGE  #####################################
$file_name		= "index"; // this is file name which is used during paging  , included at the bottom of the page
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "Phone Conversation Management ";
include("paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################

	
    
################################################## START INSERT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	    
	utility::addslashesOnAllInput(array("query_string"));
    $job_id = $_REQUEST['job_id'];	
	$con_id = utility::getTableFieldById("job_admin_mapping","admin_id","job_id",$job_id);
	$admin_id= $_REQUEST['admin_id'];
    //$admin_id = $_SESSION['user_id'];
	$phone_conversation_date	 = utility::setFormatedDate($_REQUEST['phone_conversation_date']);
	$aware_of_new_structure_or_role		= $_REQUEST['aware_of_new_structure_or_role'];
	$happy_with_celebrant_group			= $_REQUEST['happy_with_celebrant_group'];
	$any_ideas_regarding_CPD			= $_REQUEST['any_ideas_regarding_CPD'];
	$happy_with_mentoring_work			= $_REQUEST['happy_with_mentoring_work'];
	$interested_in_attending_conference	= $_REQUEST['interested_in_attending_conference'];
	$local_media_work					= $_REQUEST['local_media_work'];
	$concerns_with_celebrant_work		= $_REQUEST['concerns_with_celebrant_work'];
	$ideas_regarding_relationship_with_humanists	= $_REQUEST['ideas_regarding_relationship_with_humanists'];
	
	$source	 = $_REQUEST['source'];
	$created_on = date("Y-m-d");
		
    $sql= "INSERT INTO $table_name (job_id,admin_id,job_user_id,aware_of_new_structure_or_role,happy_with_celebrant_group,any_ideas_regarding_CPD,happy_with_mentoring_work,interested_in_attending_conference,local_media_work,concerns_with_celebrant_work,ideas_regarding_relationship_with_humanists,phone_conversation_date,source,updated_by,created_on) VALUES('$job_id','$admin_id','$job_user_id','$aware_of_new_structure_or_role','$happy_with_celebrant_group','$any_ideas_regarding_CPD','$happy_with_mentoring_work','$interested_in_attending_conference','$local_media_work','$concerns_with_celebrant_work','$ideas_regarding_relationship_with_humanists','$phone_conversation_date','$source','".$admin_id."','$created_on')";
	
    mysqli_query($conn,$sql);
	
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
  	$query_string = str_ireplace("page=add_celebrant_phone_conversation", "page=view_celebrant_phone_conversation", $query_string);
	$query_string = "&".$query_string;
	$tmp = ucfirst(str_replace("_"," ", $table_name));
	
	$_SESSION['message']	= ucfirst("Phone conversation added sucessfully");
	$redirect = "index?page=view_celebrant_phone_conversation&name=$name$query_string";
	header("Location: $redirect");
	exit();
}
################################################# END INSERT #################################################
if(isset($_SESSION['job_user_id']) && $_SESSION['job_user_id'] !=""){
	$where 	.= " AND updated_by = ".$_SESSION['job_user_id']." ";
}

if($_REQUEST['job_id_search']){
	$job_id_search = $_REQUEST['job_id_search'];
	$where 	.= " AND job_id ='$job_id_search' ";
	$next_page_condition.="&job_id_search=".$_REQUEST['job_id_search'];
}

if($_REQUEST['admin_id_search']){
	$admin_id_search = $_REQUEST['admin_id_search'];
	$where 	.= " AND admin_id ='$admin_id_search' ";
	$next_page_condition.="&admin_id_search=".$_REQUEST['admin_id_search'];
}
$from_note_date = isset($_REQUEST['from_note_date']) ? utility::setFormatedDate($_REQUEST['from_note_date']) : "";
$to_note_date = isset($_REQUEST['to_note_date']) ? utility::setFormatedDate($_REQUEST['to_note_date']) : "";
if($from_note_date && $to_note_date){
	$where .= " AND phone_conversation_date BETWEEN '$from_note_date 00:00:00' and '$to_note_date 23:59:59' ";
    $next_page_condition.="&from_note_date=".$_REQUEST['from_note_date']."&to_note_date=".$_REQUEST['to_note_date']." ";
}else if($from_note_date){
	$where.= " AND phone_conversation_date >= '$from_note_date' ";
	$next_page_condition.="&from_note_date=".$_REQUEST['from_note_date'];
}else if($to_note_date){
	$where.= " AND phone_conversation_date <= '$to_note_date' ";
	$next_page_condition.="&to_note_date=".$_REQUEST['to_note_date'];
}

$query = "SELECT * FROM $table_name $where ORDER BY phone_conversation_date DESC LIMIT $eu, $limit ";
$sql=mysqli_query($conn,$query);
$result=mysqli_num_rows($sql);

#################################   DELETE    #########################################
// when we click on delete this area of php notes will execute
if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete'){
	$id	= $_REQUEST['id'];
	mysqli_query($conn,"DELETE FROM $table_name WHERE id = $id ");
	
	$query_string =  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
	$query_string = str_ireplace(array("action=delete"), array(""), $query_string);
	$query_string = "&".$query_string;
	
	$_SESSION['message']	= ucfirst("Phone conversation deleted sucessfully");
	$redirect = "index?page=view_celebrant_phone_conversation";
	header("Location: $redirect$query_string");
	exit();
}
#################################  END DELETE    #########################################
?>

<section class="content-header hide">
  <h1>
	<? echo str_replace(array("_"),array(" "),$page_header); ?>
  </h1>		
</section>
<!----------------------- Message ------------------------------->
	<? if(isset($_SESSION['message'])){ ?>
	<section class="content-header">
	  <div class="alert alert-info alert-dismissable" style="margin-bottom:0px;">
		<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
		<? echo $_SESSION['message'];
			unset($_SESSION['message']);
		?>
	  </div>
	</section>
	<? } ?>
<!----------------------- End Message ------------------------------->
	<section class="content">
		<div class="row">
			<div class="col-xs-12">
				<div class="box">

				<div class="box-body">
				<div class="row" style="padding:0px 10px;">
					<?php include_once('add_celebrant_phone_conversation.inc.php');?>
				</div>
				<!--<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">-->					
				<form action="?page=view_celebrant_phone_conversation&name=<?=$name?>" method="post" name="form1" id="form1">
				<? if($result>0){
				?>
					<!-- Start  records header -->
				<table id="example1" class="table table-bordered table-striped">
					<thead>
						<tr>
							<th>Client</th>
							<th>Aware of new structure/role?</th>
							<th>Consultant</th>
							<th>Date</th>
							<th>Edit</th>						
							<th>Delete</th>
						</tr>
					</thead>
					<tbody>
					<!-- End records header -->
					<?
					while($row = mysqli_fetch_array($sql)){
					?>
					<!-- Start data -->
					<tr>
						<td><a href="index?page=view_celebrant_phone_conversation_detail&from=view_celebrant_phone_conversation&id=<?=$row['id'];?>"><?php echo $gsi->getEnqQuoteJobById($row['job_id']); ?></a></td>
						<td><?php echo $row['aware_of_new_structure_or_role']; ?></td>
						<td>
						
						<?php echo Admin::getAdminById($row['admin_id']); ?>
						</td>
						<td><?php echo utility::getFormatedDateReturn($row['phone_conversation_date']); ?></td>
						<td><a href="index?page=view_celebrant_phone_conversation&action=edit&id=<?=$row['id'].$next_page_condition.$paging;?>"> <img src="../images/edit.gif" border="0" /> </a></td>
						<td><a
							href="index?page=view_celebrant_phone_conversation&action=delete&id=<?=$row['id'].$next_page_condition.$paging; ?>"
							onclick=" javascript:return confirm('Are you sure! You want to delete');"><img
							src="../images/del.gif" border="0" /> </a></td>
					</tr>
					<!-- End data -->
					<? 
					}?>
					
					<?}else{?>
					<tr>
						<td colspan="6">No celebrant phone conversation found...</td>
					</tr>
					<?php }?>
					</tbody>
					
				</table>
					<!-- Paging starts here  -->
					<? 
					$next_page_condition .="&page=view_celebrant_phone_conversation";
					include("paging/paging_row.inc.php") ?>
					<!-- Paging ends starts here  -->
						
					<!--</table>-->
					</form>
					</div>
				</div>
			</div>
		</div>
	</section>
		