<?php
require_once("../classes/main_classes.php");

//OBJECTS
$retrieveObj=new RETRIEVE();
$insertObj=new INSERT();
$sendemailObj=new SEND_EMAIL();
$rowcountObj=new ROWCOUNT();
$updateObj=new UPDATE();
$root_dir_getObj=new ROOTDIRGET();
$root_dir=$root_dir_getObj->root_dir_getter();
$data_tableObj=new DATA_TABLE_RETRIEVE();

$mailer_r_path="../";


if(isset($_POST['search_blog'])){
		$search_value=$_POST['search_value'];
		$searchData="";

		if(!empty($_POST['search_value'])){
			$tableVariable="blog JOIN blog_categories ON blog_categories.b_category_id=blog.b_category_id";
			$searchData=$data_tableObj->data_table_display($tableVariable,"b_title LIKE '%$search_value%' or b_body LIKE '%$search_value%' or b_category_name LIKE '%$search_value%'","data");
			if($searchData!=1020){
				echo "
					<table class='table borderless table-striped'>
				";
				$elementId="";
				foreach ($searchData as $key) {
					$elementId=$elementId."I";
				?>
					<tr id="<?php echo $elementId;?>">
		              <td>
		                <div class="col-md-12 col-sm-12 col-xs-12">
		                  <div class="single-blog">
		                    <div class="single-blog-img">
		                      <a href="blog-details.html">
		                          <img src="img/blogs/<?php echo $key->b_image ?>" alt="">
		                        </a>
		                    </div>
		                    <div class="blog-meta">
		                      <span class="comments-type">
		                          <i class="fa fa-comment-o"></i>
		                          <a href="#"><?php echo $rowcountObj->rowcounter("blog_comments","*","b_id",$key->b_id); ?> comment/s</a>
		                        </span>
		                      <span class="date-type">
		                          <i class="fa fa-calendar"></i>
		                          <?php
		                            echo date("M j, Y", strtotime($key->b_date_posted)) ;
		                            echo " / ";
		                            echo date("H:i A", strtotime($key->b_date_posted)) ;
		                          ?>
		                        </span>
		                    </div>
		                    <div class="blog-text">
		                      <h4>
		                          <a href="#"><?php echo $key->b_title ?></a>
		                        </h4>
		                      <div>
		                        <?php echo $key->b_body ?>
		                      </div>
		                    </div>
		                    <span>
		                        <a href="#!" onclick="blog_details_view(<?php echo $key->b_id; ?>)" class="ready-btn">Read more</a>
		                      </span>
		                  </div>
		                </div>

		              </td>
		            </tr>
		        <?php
				}
				echo "</table>";

			}else{
				echo "<p class='alert alert-warning text-center'>No results found</p>";
			}
		}
	}

//ADD CONTACT MESSAGE
if(isset($_POST["contact_us_note"])){
	//VALIDATE IF THERE IS NO EMPTY FIELD
	if(empty($_POST["full_name"])){
		die("<p class='alert alert-warning'>Enter your full name</p>");
	}else if(empty($_POST["email"])){
		die("<p class='alert alert-warning'>Enter your email</p>");
	}else if(empty($_POST["subject"])){
		die("<p class='alert alert-warning'>Enter subject</p>");
	}else if(empty($_POST["message"])){
		die("<p class='alert alert-warning'>Enter message</p>");
	}

	//GET DATA
	$full_name=addslashes($_POST["full_name"]);
	$email=addslashes($_POST["email"]);
	$subject=addslashes($_POST["subject"]);
	$message=addslashes($_POST["message"]);

	//INSERT DATA TO contact_us TABLE
	$fields=array("fullname","email","subject","message");
	$data=array($full_name,$email,$subject,$message);
	$insert=$insertObj->inserter("contact_us",$fields,$data);

	if($insert=="1"){
		//SEND EMAILS TO ADMINS ABOUT Contact message
		$get_last_contact=$retrieveObj->retriever("contact_us","*","email",$email,"c_id","DESC","fetch");

		//get all admins
		$get_admins=$retrieveObj->retriever("users 
			JOIN user_roles ON user_roles.user_id=users.user_id
			JOIN user_role ON user_roles.role_id=user_role.role_id","*","user_role.user_role='su_admin' or user_role.user_role","admin","fetchAll");
		if($get_admins!="1020"){
			foreach ($get_admins as $key => $admin_) {
				//insert to notifications
				$nfields=array("an_message","an_source","an_source_id");
				$ndata=array("Message from <strong>".$full_name."</strong>","contact_us_message",$get_last_contact->c_id);

				$ninsert=$insertObj->inserter("admin_notifications",$nfields,$ndata);

				if($ninsert=="1"){
					$get_last_admin_notification=$retrieveObj->retriever("admin_notifications ORDER BY an_id DESC","*","fetch");
					//insert to target
					
					$nfields2=array("user_id","an_id");
					$ndata2=array($admin_->user_id,$get_last_admin_notification->an_id);

					$ninsert2=$insertObj->inserter("admin_notification_targets",$nfields2,$ndata2);

				}

				$subject2_="CONTACT US MESSAGE";
				$body2_="
					<p>Dear ".$admin_->firstname." ".$admin_->lastname.",</p>
					<p>$full_name sent you a message.<br>
						<strong>Full Name </strong> : $full_name <br>
						<strong>Email : </strong> $email <br>
						<strong>Subject : </strong> $subject <br>
						<strong>Message : </strong> $message <br>
					</p>
					<p><a href='provamamw.com/admin'>Login</a> to your panel for details</p>
				
					<p>Regards,</p>
					<p>Provama System</p>
				";
				$email_to2=$admin_->email;
				$send_email=$sendemailObj->phpmailer_send($mailer_r_path,$subject2_,$body2_,$email_to2);	
			}	
		}
		echo 1;	
	}else if($insert=="entry_exists"){
		echo "exists";
	}else{
		echo 0;
	}

}

//ADD COMMENT MESSAGE
if(isset($_POST["add_comment_note"])){
	//VALIDATE IF THERE IS NO EMPTY FIELD
	if(empty($_POST["full_name"])){
		die("<p class='alert alert-warning'>Enter your full name</p>");
	}else if(empty($_POST["email"])){
		die("<p class='alert alert-warning'>Enter your email</p>");
	}else if(empty($_POST["message"])){
		die("<p class='alert alert-warning'>Enter message</p>");
	}

	//GET DATA
	$full_name=addslashes($_POST["full_name"]);
	$email=addslashes($_POST["email"]);
	$message=addslashes($_POST["message"]);
	$b_id=addslashes($_POST["add_comment_note"]);

	//INSERT DATA TO contact_us TABLE
	$fields=array("bc_fullname","bc_email","bc_text","b_id");
	$data=array($full_name,$email,$message,$b_id);
	$insert=$insertObj->inserter("blog_comments",$fields,$data);

	if($insert=="1"){
		$get_last_blog_comment=$retrieveObj->retriever("blog_comments","*","bc_email",$email,"bc_id","DESC","fetch");

		//insert to notifications
		$nfields=array("an_message","an_source","an_source_id");
		$ndata=array("<strong>".$full_name."</strong> Commented on a blog post.","blog_comment",$get_last_blog_comment->bc_id);

		$ninsert=$insertObj->inserter("admin_notifications",$nfields,$ndata);

		if($ninsert=="1"){
			$get_last_admin_notification=$retrieveObj->retriever("admin_notifications ORDER BY an_id DESC","*","fetch");

			//get all admins
			$get_admins=$retrieveObj->retriever("users 
			JOIN user_roles ON user_roles.user_id=users.user_id
			JOIN user_role ON user_roles.role_id=user_role.role_id","*","user_role.user_role='su_admin' or user_role.user_role","admin","fetchAll");
			foreach ($get_admins as $key => $admin_) {
				//insert to target
				
				$nfields2=array("user_id","an_id");
				$ndata2=array($admin_->user_id,$get_last_admin_notification->an_id);

				$ninsert2=$insertObj->inserter("admin_notification_targets",$nfields2,$ndata2);
			}

		}

		echo 1;	
	}else{
		echo 0;
	}

}

//ADD COMMENT MESSAGE
if(isset($_POST["add_comment_reply_note"])){
	//VALIDATE IF THERE IS NO EMPTY FIELD
	if(empty($_POST["full_name"])){
		die("<p class='alert alert-warning'>Enter your full name</p>");
	}else if(empty($_POST["email"])){
		die("<p class='alert alert-warning'>Enter your email</p>");
	}else if(empty($_POST["message"])){
		die("<p class='alert alert-warning'>Enter message</p>");
	}

	//GET DATA
	$full_name=addslashes($_POST["full_name"]);
	$email=addslashes($_POST["email"]);
	$message=addslashes($_POST["message"]);
	$b_id=addslashes($_POST["add_comment_reply_note"]);

	//INSERT DATA TO contact_us TABLE
	$fields=array("bcr_fullname","bcr_email","bcr_text","bc_id");
	$data=array($full_name,$email,$message,$b_id);
	$insert=$insertObj->inserter("blog_comment_replies",$fields,$data);

	if($insert=="1"){
		$get_last_blog_comment=$retrieveObj->retriever("blog_comment_replies","*","bcr_email",$email,"bcr_id","DESC","fetch");

		//insert to notifications
		$nfields=array("an_message","an_source","an_source_id");
		$ndata=array("<strong>".$full_name."</strong> Commented on a blog post.","blog_comment",$get_last_blog_comment->bcr_id);

		$ninsert=$insertObj->inserter("admin_notifications",$nfields,$ndata);

		if($ninsert=="1"){
			$get_last_admin_notification=$retrieveObj->retriever("admin_notifications ORDER BY an_id DESC","*","fetch");

			//get all admins
			$get_admins=$retrieveObj->retriever("users 
			JOIN user_roles ON user_roles.user_id=users.user_id
			JOIN user_role ON user_roles.role_id=user_role.role_id","*","user_role.user_role='su_admin' or user_role.user_role","admin","fetchAll");
			foreach ($get_admins as $key => $admin_) {
				//insert to target
				
				$nfields2=array("user_id","an_id");
				$ndata2=array($admin_->user_id,$get_last_admin_notification->an_id);

				$ninsert2=$insertObj->inserter("admin_notification_targets",$nfields2,$ndata2);
			}

		}

		echo 1;	
	}else{
		echo 0;
	}

}
?>