<?php namespace App\Models\Admin;

use App\Models\Project\DatatableModel;

class Download_brochureModel extends AdminModel{
	private $table;
	
    public function __construct(){
		parent::__construct();
		$this->table = "download_brochure";
    }
	
	public function get_datatable(){		
		$dtObj = new DatatableModel($this->db);
		$dtObj->set_table($this->table." AS mt");
		// Set Datatable columns 
		$dtObj->set_column('mt.download_brochure_id', 'desc|sort|search', 'RecordID');
		$dtObj->set_column('mt.name', 'sort|search');
		$dtObj->set_column('mt.email_id', 'sort|search');
		$dtObj->set_column('mt.phone_no', 'sort|search');
		$dtObj->set_column('mt.city', 'sort|search');
		$dtObj->set_column('mt.state', 'sort|search');
		$dtObj->set_column('mt.customer_profile', 'sort|search');
		$dtObj->set_column('mt.looking_for', 'sort|search');
		$dtObj->set_column('mt.status', 'sort|search');
		$builder = $dtObj->get_builder();
		$builder->where('mt.is_deleted','No');
		return $dtObj->get_data($builder);	
	}
	
	public function get($language_list,$download_id){		
		$builder = $this->db->table($this->table." AS mt");
		$columns = ["mt.*"];
		$ln_columns = ['download_ln_id',
			'title',
			'language_code'];
		if($language_list){
			foreach($language_list as $language){
				foreach($ln_columns as $col_name){
					array_push($columns,$language['code'].".".$col_name." AS ".$col_name."_".$language['code']);
				}
				$builder->join($this->table."_ln AS ".$language['code'],"mt.download_id=".$language['code'].".download_id AND ".$language['code'].".language_code='".$language['code']."'",'left');
			}
		}
		$builder->select(implode(", ",$columns));
		$builder->where('mt.download_id',$download_id);
		$query = $builder->get();
		//echo $builder->getCompiledSelect();
		return $query->getRowArray();
	}
	
	public function insert($data){		 
		$this->add_insert_log($data);
		$builder = $this->db->table($this->table);
		$builder->insert($data);
		return $this->db->insertID();
	}
	
	public function update($data, $download_id){
		$this->add_update_log($data);
		$builder = $this->db->table($this->table);
		$builder->where('download_id',$download_id);
		$builder->update($data);
	}
	
	public function delete($download_id){
		$data['is_deleted'] = "Yes";
		$this->add_update_log($data);
		$builder = $this->db->table($this->table);
		$builder->where('download_id',$download_id)
				->update($data);
	}
	
	public function is_unique_title($title,$download_id){
		$builder = $this->db->table($this->table);
		$builder->where('is_deleted','No')
				->where('title',$title);
		if($download_id){
			$builder->where('download_id!=',$download_id);
		}
		$query = $builder->get();
		if($query->getRowArray()){
			$response = "FALSE|The Name already exist!";
		}else{
			$response = "TRUE|";
		}
		return $response;
	}
	
	public function update_language_data($data,$download_ln_id){
		$builder = $this->db->table($this->table.'_ln');
		$builder->where('download_ln_id',$download_ln_id)
		->update($data);
	}
	
	public function insert_language_data($data){
		$builder = $this->db->table($this->table.'_ln');
		$builder->insert($data);
	}
}