<?php namespace App\Controllers;

class Download extends FrontController{	
	public function index($download_category_id){
		$data['meta_data']['title'] = "Download";
		$data['meta_data']['description'] = "Download technical catalogs and brochures";
		$data['meta_data']['keywords'] = "Download, technical catalogs,brochures";
		$data['cat_list']= $this->get_download_category_list();
		// $data['download_list'] = $this->get_download_list($download_category_id);

		echo $this->view_inner_template('download_category',$data);
	}
	public function category($cat_url){
		$data['language_code'] = $this->get_requested_lang_code();
		$data['cat'] = $this->get_cat_detail($cat_url);
		$data['meta_data']['title'] = $data['cat']['meta_title'];
		$data['meta_data']['description'] = $data['cat']['meta_description'];
		$data['meta_data']['keywords'] = $data['cat']['meta_keywords'];
		
		$data['download_list']= $this->get_download_list($data['cat']['download_category_id']);
		echo $this->view_inner_template('download',$data);
	}
	private function get_cat_detail($url){
		$builder = $this->cmObj->get_table_builder('download_category AS c');
		$query = $builder->join('download_category_ln AS cl',"c.download_category_id=cl.download_category_id AND cl.language_code='en'",'inner')
			->select('c.download_category_id,cl.*')
			->where('c.download_category_url',$url)
			->where('c.status','Active')
			->where('c.is_deleted','No')
			->orderBy('cl.download_category_id','ASC')
			->get();
		$rec = $query->getRowArray();
		return $rec;
	}
	private function get_category_list(){
		$builder = $this->cmObj->get_table_builder('download_category AS mt');
		$builder->join('download_category_ln AS ln',"mt.download_category_id=ln.download_category_id AND ln.language_code='en'","inner");
		$query = $builder->select('mt.download_category_id,mt.download_category_url,ln.title')
		       ->orderBy('mt.download_category_id','ASC')
			->where('mt.status','Active')
			->where('mt.is_deleted','No')
			->get();
		return $query->getResultArray();
	}
	
	private function get_download_list($download_category_id){
		$builder = $this->cmObj->get_table_builder('download AS d');
		$query = $builder->join('download_ln AS ln',"d.download_id=ln.download_id AND ln.language_code='en'",'left')
			->select('d.download_id,ln.title, 
			d.document_file,
			d.upload_type')
			->where('d.download_category_id',$download_category_id)
			->where('d.status','Active')
			->where('d.is_deleted','No')
			->get();
		$rec = $query->getResultArray();
		return $rec;
	}

	private function get_download_category_list(){
		$builder = $this->cmObj->get_table_builder('download_category AS d');
		$query = $builder->join('download_category_ln AS ln',"d.download_category_id=ln.download_category_id AND ln.language_code='en'",'left')
			->select('d.download_category_id,ln.title')
			->where('d.status','Active')
			->where('d.is_deleted','No')
			->get();
		$rec = $query->getResultArray();
		return $rec;
	}
	
}

