<?php
namespace App\Controllers;

class News extends FrontController
{ 
	public function index(){
		$data['language_code'] = $this->get_requested_lang_code();
		$data['meta_data'] = $this->get_meta_data($data['language_code']);
		$data['news_list'] = $this->get_news_list();
		echo $this->view_inner_template('news',$data);
	}
	
	private function get_meta_data($language_code){
		$data['en']['title'] = "News";
		$data['en']['description'] = "News";
		$data['en']['keywords'] = "News";
		
		if(isset($data[$language_code])){
			return $data[$language_code];
		}else{
			return $data['en'];
		}
	}
	
	public function detail($url){
		$data['language_code'] = $this->get_requested_lang_code();
		$data['rec'] = $this->get_detail($url,$data['language_code']);
		$data['meta_data']['title'] = $data['rec']['meta_title'];
		$data['meta_data']['description'] = $data['rec']['meta_description'];
		$data['meta_data']['keywords'] = $data['rec']['meta_keywords'];
		
		echo $this->view_inner_template('news_details',$data);
		
	}
	
	private function get_detail($url){
		$builder = $this->cmObj->get_table_builder('news AS mt');
		$builder->join('news_ln AS ln',"mt.news_id=ln.news_id AND ln.language_code='".$this->project->default_lang_code."'","inner");
		$query  = $builder->select('mt.news_id,ln.title,mt.photo_image,
			mt.last_updated_date,mt.news_url,mt.publisher_name,mt.publish_date,ln.*')
				->where('mt.is_deleted','No')
				->where('mt.status','Active')
				->where('mt.news_url',$url)
				->get();
		return $query->getRowArray();
	}

	private function get_news_list(){
		$builder = $this->cmObj->get_table_builder('news AS mt');
		$builder->join('news_ln AS ln',"mt.news_id=ln.news_id AND ln.language_code='".$this->project->default_lang_code."'","inner");
		$query  = $builder->select('mt.news_id,ln.title,mt.photo_image,
			mt.last_updated_date,mt.news_url,mt.publisher_name,mt.publish_date,ln.description')
				->where('mt.is_deleted','No')
				->where('mt.status','Active')
				->where('mt.start_date_time<=',date('Y-m-d H:i:s'))
				->where('mt.end_date_time>=',date('Y-m-d H:i:s'))
				->get();
		return $query->getResultArray();
	}
}
