<?php namespace App\Controllers;
  
class Download extends FrontController
{			
	public function index(){
		$data['language_code'] = $this->get_requested_lang_code();
		$data['meta_data']['title'] = "Download";
		$data['meta_data']['description'] = "Download";
		$data['meta_data']['keywords'] = "Download";
		
		$data['cat_list']= $this->get_category_list($data['language_code']);
		echo $this->view_inner_template('download',$data);
	}
	
	public function category($cat_url){
		$data['language_code'] = $this->get_requested_lang_code();
		$data['cat'] = $this->get_cat_detail($cat_url,$data['language_code']);
		$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['language_code'],$data['cat']['download_category_id']);
		echo $this->view_inner_template('download_category',$data);
	}
	
	public function get_download_list($language_code,$download_category_id){
		$builder = $this->cmObj->get_table_builder('download AS mt');
		$builder->join('download_ln AS ln',"mt.download_id=ln.download_id AND ln.language_code='".$language_code."'","inner");
		$builder->join('icon AS ic',"mt.icon_id=ic.icon_id AND ln.language_code='".$language_code."'","left");
		$query = $builder->select('mt.*,ln.title,ic.icon_file AS default_icon_file')
			->where('mt.status','Active')
			->where('mt.is_deleted','No')
			->where('mt.download_category_id',$download_category_id)
			->orderBy('ln.title','ASC')
			->get();
		return $query->getResultArray();
	}
	
	private function get_category_list($language_code){
		$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='".$language_code."'","inner");
		$query = $builder->select('mt.download_category_id,mt.download_category_url,mt.photo_image,ln.title')
			->where('mt.status','Active')
			->where('mt.is_deleted','No')
			->orderBy('ln.title','ASC')
			->get();
		return $query->getResultArray();
	}
	
	private function get_cat_detail($url,$lang_code){
		$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='".$lang_code."'",'left')
			->select('c.download_category_id,cl.*')
			->where('c.download_category_url',$url)
			->where('c.status','Active')
			->where('c.is_deleted','No')
			->get();
		$rec = $query->getRowArray();
		return $rec;
	}
}