<?php namespace App\Controllers\Admin;

class Download_category extends AdminController
{
	private $mObj;
	private $write_path; 
	
	public function __construct(){		
        $this->mObj = model('App\Models\Admin\DownloadCategoryModel');
		$this->menu_id = 28;
		$this->write_path = ROOTPATH . 'public/uploads/';
		
    }
	
	public function index(){
		$this->allowed('is_list');
		$data['footer_includes'] = ['download_category_list_js'];		
		echo $this->view_inner_template('download_category_list',$data);
	}
	
	public function ajax_list(){
		$this->allowed('is_list');
		$result = $this->mObj->get_datatable();
		$output = array(
			"meta" => $result['meta'],
            "data" => $result['data']
        );
        
        // Output to JSON format
        echo json_encode($output);
	}
	 
	public function add(){
		$this->allowed('is_add');
		$data['form_heading'] = "Add Download Category";
		$data['action'] = "add";
		$data['language_list'] = $this->cmObj->get_language_list();
		
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules($data['language_list']);
			if($this->validate($rules)) { 
				$data_in = $this->get_column_values();
				$id = $this->mObj->insert($data_in);
				$this->save_language_data($data['language_list'],$id);
				$this->notify_success('Record Added Successfully');
				return redirect()->to(get_admin_path('download_category'));
			}
		}
		$data['footer_includes'] = ['download_category_add_edit_js'];	
		echo $this->view_inner_template('download_category_add_edit',$data);
	}
	
	public function edit($download_category_id){
		$this->allowed('is_edit');
		$data['form_heading'] = "Edit Download Category";
		$data['action'] = "edit";
		$data['language_list'] = $this->cmObj->get_language_list();
		
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules($data['language_list'],$download_category_id);
			if ($this->validate($rules)) { 
				$data_in = $this->get_column_values();
				$this->mObj->update($data_in,$download_category_id);
				$this->save_language_data($data['language_list'],$download_category_id);
				$this->remove_prev_file($data_in);
				$this->notify_success('Record Updated Successfully');
				return redirect()->to(get_admin_path('download_category'));
			}
		}else{
			$data['update_data'] = $this->mObj->get($data['language_list'],$download_category_id);
		}
		$data['footer_includes'] = ['download_category_add_edit_js'];	
		echo $this->view_inner_template('download_category_add_edit',$data);
	}
	
	private function remove_prev_file($data_in){
		if(isset($data_in['photo_image']) && 
			$data_in['photo_image']!="" && 
			$this->request->getPost('uploaded_photo_image')!="" &&
			$this->request->getPost('uploaded_photo_image')!="" &&
			$data_in['photo_image']!=$this->request->getPost('uploaded_photo_image') && 
			file_exists($this->write_path.$this->request->getPost('uploaded_photo_image'))){
			unlink($this->write_path.$this->request->getPost('uploaded_photo_image'));
		}
	}
	
	public function delete($download_category_id){
		$this->allowed('is_edit');
		$this->mObj->delete($download_category_id);
		$this->notify_success('Record Deleted Successfully');
		return redirect()->to(get_admin_path('download_category'));
	}
	
	private function get_column_values(){
		$data['download_category_url'] = $this->request->getPost('download_category_url');
		$data['status'] = $this->request->getPost('status');
		$this->save_uploaded_file($data);
		return $data;
	}
	// .' '.$this->request->getPost('publish_time')));
	private function get_validation_rules($language_list,$record_id=""){
		if($language_list){
			foreach($language_list as $language){
				$rules['title_'.$language['code']] = ['label'=>'Title', 'rules' => 'trim|required'];
				$rules['meta_title_'.$language['code']] = ['label'=>'Meta Title', 'rules' => 'trim|required'];
				$rules['meta_keywords_'.$language['code']] = ['label'=>'Meta Keywords', 'rules' => 'trim|required'];
				$rules['meta_description_'.$language['code']] = ['label'=>'Meta Description', 'rules' => 'trim|required'];
				//$rules['short_description_'.$language['code']] = ['label'=>'Short Description', 'rules' => 'trim|required'];
				//$rules['description_'.$language['code']] = ['label'=>'Description', 'rules' => 'trim|required'];
			}
		}
		$rules['download_category_url'] = ['label'=>'News URL', 'rules' => 'trim|required|pr_callback['.$this->mObj->is_unique_download_category_url($this->request->getPost('download_category_url'),$record_id).']'];
		$rules['status'] = ['label'=>'Status', 'rules' => 'trim|required'];

		return $rules;
	}
	
	public function is_valid_datetime_range($start_dt,$end_dt){
		if(is_valid_datetime_range($start_dt,$end_dt)){
			$response = "TRUE|";			
		}else{
			$response = "FALSE|The End Date Time must be greater than Start Date Time";
		}
		return $response;
	}
	
	private function save_uploaded_file(&$data){
		$file = $this->request->getFile('photo_image');
		if($file->getName()){
			$file->move(ROOTPATH . 'public/uploads/',$file->getRandomName());
			$data['photo_image'] = $file->getName();
		}
	}
	
	public function save_language_data($language_list,$id=""){
		if($language_list){			
			foreach($language_list as $language){
				$data = [];
				$data['title'] = $this->request->getPost('title_'.$language['code']);
				$data['meta_title'] = $this->request->getPost('meta_title_'.$language['code']);
				$data['meta_keywords'] = $this->request->getPost('meta_keywords_'.$language['code']);
				$data['meta_description'] = $this->request->getPost('meta_description_'.$language['code']);
				//$data['short_description'] = $this->request->getPost('short_description_'.$language['code']);
				//$data['description'] = $this->request->getPost('description_'.$language['code']);
				$data['language_code'] = $language['code'];
				if($this->request->getPost('download_category_ln_id_'.$language['code'])){
					$this->mObj->update_language_data($data,$this->request->getPost('download_category_ln_id_'.$language['code']));
				}else{
					$data['download_category_id'] = $id;
					$this->mObj->insert_language_data($data);
				}
			}
		}
	}
}