<?php
namespace App\Controllers;

class Home extends FrontController
{ 
	public function index(){
		$data['language_code'] = $this->get_requested_lang_code();
		$data['meta_data'] = $this->get_meta_data($data['language_code']);
		$data['banner_list'] = $this->get_banner_list();
		$data['prod_cat_list'] = $this->get_prod_cat_list($data['language_code']);
		if($data['prod_cat_list']){
			foreach($data['prod_cat_list'] as $i => $prod_cat){
				$data['prod_cat_list'][$i]['prod_list'] = $this->get_cat_prod_list($prod_cat['product_category_id'],$data['language_code']);
			}
		}		
		//$data['current_news_list'] = $this->get_current_news_list();
		//$data['news_category_list'] = $this->get_news_category_list();
		$data['testimonial_list']= $this->get_testimonial_list();
		//$data['client_list']= $this->get_client_list();
		//echo '<pre>';
		//print_r($data['prod_cat_list']);
		$data['controller_name'] = 'home';
		echo $this->view_inner_template('home',$data);
		//$this->session->set('show_enq_modal',false);
	}
	
	private function get_meta_data($language_code){
		$data['en']['title'] = "Dhumal Industries: Leading Poultry Equipment Manufacturer";
		$data['en']['description'] = "Dhumal Indstries Top-quality Poultry Equipments Manufacturer.We offer a wide range of innovative and efficient poultry farm equipments to help maximize your farm's productivity.";
		$data['en']['keywords'] = " Poultry Equipment, Poultry Equipments, Poultry Equipment Manufacturer, Poultry Farm Equipment, Poultry Farm Equipment, Poultry Simplified,dhumal industries products, Poultry Equipment Supplier, Poultry Cage Supplier, Layer Cage Manufacturer, Breeder Cage Manufacturer, Breeder Cage Supplier, Parent Cage Manufacturer, Battery Cage Manufacturer, Battery Cage Supplier, Poultry Ventilation System, Poultry Environment Control System, Poultry Cooling Fan Pad, Gas Brooder Supplier, Poultry Heater, Nipple Drinking System, Pan Feeding System, Poultry Fogger System, Poultry Foggers, Poultry Controller Systems, Poultry Nest, Poultry Equipment For , Pakistan, Srilanka, Bangladesh, Nepal, Africa, Europe, USA";
		
		if(isset($data[$language_code])){
			return $data[$language_code];
		}else{
			return $data['en'];
		}
	}
	
	private function get_banner_list(){
		$builder = $this->cmObj->get_table_builder('banner');
		$query = $builder->select('banner_id,title,image,video_url,banner_type,
			link,open_in_new_tab')
			->where('status','Active')
			->where('is_deleted','No')
			->orderBy('sort_order','ASC')
			->get();
		return $query->getResultArray();
	}
	
	private function get_prod_cat_list($language_code){
		$builder = $this->cmObj->get_table_builder('product_category AS mt');
		$builder->join('product_category_ln AS ln',"mt.product_category_id=ln.product_category_id AND ln.language_code='".$language_code."'","inner");
		$query  = $builder->select('mt.product_category_id,ln.title,mt.photo_file,mt.product_category_url,ln.meta_description')
				->where('mt.is_deleted','No')
				->where('mt.status','Active')
				->where('mt.show_on_home_page','Yes')
				->where('mt.photo_file!=','')
				->orderBy('ln.title','ASC')
				->get();
		
		return $query->getResultArray();
	}
	
	private function get_cat_prod_list($prod_cat_id,$language_code){
		$builder = $this->cmObj->get_table_builder('products AS mt');
		$builder->join('products_ln AS ln',"mt.product_id=ln.product_id AND ln.language_code='".$language_code."'","inner");
		$query  = $builder->select('mt.product_id,ln.title,mt.product_icon_img_1,mt.product_image_1,mt.product_url')
				->where('mt.is_deleted','No')
				->where('mt.status','Active')
				->where('mt.product_icon_img_1!=','')
				->where('mt.product_category_id',$prod_cat_id)
				->orderBy('ln.title','ASC')
				->get();
		
		return $query->getResultArray();
	}

	private function get_current_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.publish_time,mt.news_url')
			// ->where('mt.breaking_news','Yes')
			->where('status','Active')
			->where('is_deleted','No')
			->get();
		return $query->getResultArray();
	}

	private function get_news_category_list(){
		$builder = $this->cmObj->get_table_builder('news_category AS mt');
		$builder->join('news_category_ln AS ln',"mt.news_category_id=ln.news_category_id AND ln.language_code='".$this->project->default_lang_code."'","inner");
		$query = $builder->select('mt.news_category_id,ln.title')
			->where('status','Active')
			->where('is_deleted','No')
			->get();
		return $query->getResultArray();
	}
	
	private function get_testimonial_list(){		
		$builder = $this->cmObj->get_table_builder('testimonial AS mt');
		$builder->join('testimonial_ln AS ln',"mt.testimonial_id=ln.testimonial_id AND ln.language_code='".$this->project->default_lang_code."'","inner");
		$query = $builder->select('mt.testimonial_id,ln.name,ln.city,ln.description,mt.photo_image')
			->where('mt.status','Active')
			->where('mt.is_deleted','No')
			->get();
		return $query->getResultArray();
	}

	private function get_product_images_list(){
		$builder = $this->cmObj->get_table_builder('products AS p');
		$builder->join('products_ln AS pl',"p.product_id=pl.product_id AND pl.language_code='".$this->project->default_lang_code."'","inner");
		$query = $builder->select(' p.product_id,p.product_url,pl.title,p.product_image_1,p.product_image_2,p.product_image_3,p.product_image_4,p.product_icon_img_1,p.product_icon_img_2,p.product_icon_img_3,p.product_icon_img_4')
			// ->where('mt.breaking_news','Yes')
			->where('status','Active')
			->where('is_deleted','No')
			->get();
		return $query->getResultArray();
	}	
	
	private function get_client_list(){
		$builder = $this->cmObj->get_table_builder('client');
		$builder->select('client_id,
				title,
				logo,
				city')
			->where('status','Active')
			->where('is_deleted','No')
			->where('logo!=','')
			->orderBy('title','ASC');
		$query = $builder->get();
		return $query->getResultArray();
	}
}
