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

class User_guide extends CI_Model{
	

	//Gather all user guide topic
	public function getTopic(){
		$query = $this->db->select("*")
						->from('ug_topic')
						->get();

		if($query->num_rows() > 0){
			return $query->result();
		}else{
			return false;
		}
	}

	//Gather Topic previous content
	public function getCont($ugID){
		$query = $this->db->select('*,ID contID')
						->from('ug_content')
						->where("ugID",$ugID)->get();
						
		if($query->num_rows() > 0){
			$data['cont'] = $query->result();
			$query = $this->db->select('*')->from('ug_video')->where('ugID',$ugID)->get();
			$data['videoLink'] = $query->result();
			$query = $this->db->select('rt.*,t.name')
							->from('ug_relatedtopic rt')
							->join('ug_topic t','t.fldid = rt.relatedUgID','left')
							->where('ugID',$ugID)->get();
			$data['relatedTopic'] = $query->result();
			return $data;
		}else{
			return false;
		}
	}

	//Check content available or not
	public function update($data){
		$query = $this->db->from('ug_content')->where("ugID",$data[0])->get();
		if($query->num_rows() > 0){
			$this->db
					->where("ugID",$data[0])
					->update('ug_content',array('title'=>$data[1],'content'=>$data[2]));

			return "Updated Successfully";
		}else{
			$this->db->insert('ug_content',array("ugID"=>$data[0],'title'=>$data[1],'content'=>$data[2]));

			return "Inserted Successfully";
		}
	}


/****************Related Topic Transactions*************************/
	//Insert Related Topic
	public function addTopic($ugID,$topicID){
		//check for video link
		if(!empty($topicID)){
			$this->db->insert('ug_relatedtopic',array("ugID"=>$ugID,'relatedUgID'=>$topicID));
			$query = $this->db->select('t.name')
							->from('ug_relatedtopic rt')
							->join('ug_topic t','t.fldid=rt.relatedUgID')
							->where('ID',$this->db->insert_id())
							->get();
			$rtInfo = $query->result();
			$pageName = $this->lang->line($rtInfo[0]->name);
			$data = array(
							"success" => "Related Topic inserted successfully",
							"id"	  => $this->db->insert_id(),
							"info"	  => $pageName
						);
			echo json_encode($data);
		}
	}

	//Delete Video link
	public function delTopic($id){
		$this->db->where('ID',$id)->delete('ug_relatedtopic');
		if($this->db->affected_rows() > 0){
			echo json_encode("Topic deleted successfully");
		}else{
			echo json_encode("false");
		}
	}


/****************Video Transactions*************************/
	//Insert video link
	public function addVideo($ugID,$videoLink){
		//check for video link
		if(!empty($videoLink)){
			$this->db->insert('ug_video',array("ugID"=>$ugID,'videoLink'=>$videoLink));
			$data = array(
							"success" => "Video link inserted successfully",
							"id"	  => $this->db->insert_id()
						);
			echo json_encode($data);
		}
	}

	//Delete Video link
	public function delVideo($id){
		$this->db->where('ID',$id)->delete('ug_video');
		if($this->db->affected_rows() > 0){
			echo json_encode("Video Link deleted successfully");
		}else{
			echo json_encode("false");
		}
	}
}