<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Contact_us extends CI_Controller {
     
	public function __construct()
	{
	    parent::__construct();
	  
	} 
	
	public function contact(){
		
	    $this->form_validation->set_rules('fname','First Name', 'required');	
		$this->form_validation->set_rules('lname','Last Name', 'required');
		$this->form_validation->set_rules('con_email','Email', 'required');
		$this->form_validation->set_rules('con_subject','Subject', 'required');
		$this->form_validation->set_rules('con_comment','Comment', 'required');
		if ($this->form_validation->run() == FALSE)
		{
			echo 0; 
			
		}else{
			$data = array(  
			'contact_fname' =>ucwords($this->input->post('fname')),
			'contact_lname' =>ucwords($this->input->post('lname')),
			'contact_email' =>$this->input->post('con_email'), 
			'contact_subject' =>$this->input->post('con_subject'),
			'contact_comment' => $this->input->post('con_comment'),
			);
			$this->Data_access_model->insert_data('hs_contact',$data);
			if($this->db->affected_rows() >0){
				echo 1;
			}
		}
	}
	
	public function user_comment(){
		
	    $this->form_validation->set_rules('comm_name','Name', 'required');	
		$this->form_validation->set_rules('comm_email','Email', 'required');
		$this->form_validation->set_rules('comm_content','Comment', 'required');
		if ($this->form_validation->run() == FALSE)
		{
			echo 0; 
			
		}else{
			$data = array(  
			'comment_name' =>ucwords($this->input->post('comm_name')),
			'comment_email' =>$this->input->post('comm_email'),
			'comment_content' =>$this->input->post('comm_content'),
			'comment_date'  =>date('d M Y h:i:s'),
			'comment_blog_id' =>$this->input->post('commentblog_id'),
			);
			$this->Data_access_model->insert_data('hs_comment',$data);
			if($this->db->affected_rows() >0){
				echo 1;
			}
		}
	}
	
	public function user_rating(){
		$this->form_validation->set_rules('rating_stars','Rating', 'required');
		$this->form_validation->set_rules('comment','Comment', 'required');
		if ($this->form_validation->run() == FALSE)
		{
			echo 0; 
			
		}else{
			$ratingstar = $this->input->post('rating_stars');
			$update_data = array(  
			'rating_star' =>$this->input->post('rating_stars'),
			'rating_review' =>$this->input->post('comment'),
			);
								
			$insert_data = array(  
			'rating_uid' =>$this->input->post('rating_userid'),
			'rating_prod_id' =>$this->input->post('rating_prodid'),
			'rating_star' =>$this->input->post('rating_stars'),
			'rating_review' =>$this->input->post('comment'),
			'rating_date'  =>date('d M Y h:i:s'),
			);			
			
			$check_olduser_rating = $this->Data_access_model->select_data('rating_uid,rating_prod_id','hs_rating',array('rating_uid' =>$this->input->post('rating_userid'),'rating_prod_id' =>$this->input->post('rating_prodid')));   
			if($check_olduser_rating->num_rows() > 0){
				
				$this->Data_access_model->update_data('hs_rating',array('rating_uid' =>$this->input->post('rating_userid'),'rating_prod_id' =>$this->input->post('rating_prodid')),$update_data);
				echo 'Thanks! You have rated this '.$ratingstar.' stars';
			}else{
					$this->Data_access_model->insert_data('hs_rating',$insert_data);
					if($this->db->affected_rows() >0){
						echo 'Thanks! You have rated this '.$ratingstar.' stars';
					}
			 }
		}
	}
}

 