<?php namespace App\Controllers\Admin;

class Photo_gallery extends AdminController
{
	private $mObj;
	
	public function __construct(){		
        $this->mObj = model('App\Models\Admin\Photo_galleryModel');
		$this->menu_id =12;
    }
	
	public function index(){
		$this->allowed('is_list');
		$data['footer_includes'] = ['photo_gallery_list_js'];		
		echo $this->view_inner_template('photo_gallery_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 Photo Gallery";
		$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('photo_gallery'));
			}
		}
		$data['footer_includes'] = ['photo_gallery_add_edit_js'];	
		echo $this->view_inner_template('photo_gallery_add_edit',$data);
	}
	
	public function edit($photo_gallery_id){
		$this->allowed('is_edit');
		$data['form_heading'] = "Edit Photo Gallery";
		$data['language_list'] = $this->cmObj->get_language_list();
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules($data['language_list'],$photo_gallery_id);
			if ($this->validate($rules)) { 
				$data_in = $this->get_column_values();
				$this->mObj->update($data_in,$photo_gallery_id);
				$this->save_language_data($data['language_list'],$photo_gallery_id);
				$this->notify_success('Record Updated Successfully');
				return redirect()->to(get_admin_path('photo_gallery'));
			}
		}else{
			$data['update_data'] = $this->mObj->get($data['language_list'],$photo_gallery_id);
		}
		$data['footer_includes'] = ['photo_gallery_add_edit_js'];	
		echo $this->view_inner_template('photo_gallery_add_edit',$data);
	}
	
	public function delete($photo_gallery_id){
		$this->allowed('is_edit');
		$this->mObj->delete($photo_gallery_id);
		$this->notify_success('Record Deleted Successfully');
		return redirect()->to(get_admin_path('photo_gallery'));
	}
	
	private function get_column_values(){
		$data['photo_album_id'] = $this->request->getPost('photo_album_id');				
		$data['status'] = $this->request->getPost('status');
		$data['sort_order'] = $this->request->getPost('sort_order');
		$this->save_uploaded_file($data);
		return $data;
	}
	
	private function get_validation_rules($language_list,$photo_gallery_id=""){
		if($language_list){
			foreach($language_list as $language){
				$rules['title_'.$language['code']] = ['label'=>'Title', 'rules' => 'trim|required'];
			}
		}
		$rules['photo_album_id'] = ['label'=>'Photo Album ID', 'rules' => 'trim|required'];
		$rules['status'] = ['label'=>'Status', 'rules' => 'trim|required'];
		return $rules;
	}
	
	private function save_uploaded_file(&$data){
		$file = $this->request->getFile('image');
		if($file->getName()){
			$file->move(ROOTPATH . 'public/uploads/',$file->getRandomName());
			$data['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['language_code'] = $language['code'];
				if($this->request->getPost('photo_gallery_ln_id_'.$language['code'])){
					$this->mObj->update_language_data($data,$this->request->getPost('photo_gallery_ln_id_'.$language['code']));
				}else{
					$data['photo_gallery_id'] = $id;
					$this->mObj->insert_language_data($data);
				}
			}
		}
	}
}