<?php namespace App\Models\Admin;

use App\Models\Project\DatatableModel;

class PhotoAlbumModel extends AdminModel{
	private $table; 
	
    public function __construct(){
		parent::__construct();
		$this->table = "photo_album";
    }

	public function get_datatable(){
		$dtObj = new DatatableModel($this->db);
		$dtObj->set_table($this->table." AS pa");
		// Set Datatable columns 
		$dtObj->set_column('pa.photo_album_id', 'desc|sort|search', 'RecordID');
		$dtObj->set_column('pa.photo_album_url', '');
		$dtObj->set_column('pa.status', 'sort|search');
		$dtObj->set_column('pal.title', 'sort|search');
		$builder = $dtObj->get_builder();
		$builder->join($this->table."_ln AS pal","pa.photo_album_id=pal.photo_album_id AND pal.language_code='".$this->project->default_lang_code."'",'left');
		$builder->where('pa.is_deleted','No');
		return $dtObj->get_data($builder);
	} 
	
	public function get($language_list,$photo_album_id){
		$builder = $this->db->table($this->table." AS mt");
		$columns = ["mt.*"];
		$ln_columns = ['photo_album_ln_id',
			'title',
			'short_description',
			'description',
			'meta_title',
			'meta_keywords',
			'meta_description',
			'language_code'];
		if($language_list){
			foreach($language_list as $language){
				foreach($ln_columns as $col_name){
					array_push($columns,$language['code'].".".$col_name." AS ".$col_name."_".$language['code']);
				}
				$builder->join($this->table."_ln AS ".$language['code'],"mt.photo_album_id=".$language['code'].".photo_album_id AND ".$language['code'].".language_code='".$language['code']."'",'left');
			}
		}
		$builder->select(implode(", ",$columns));
		$builder->where('mt.photo_album_id',$photo_album_id);
		$query = $builder->get();
		//echo $builder->getCompiledSelect();
		$rec = $query->getRowArray();
		if($rec){
			$rec['start_date'] = date('d-m-Y',strtotime($rec['start_date_time']));
			$rec['publish_date'] = date('d-m-Y',strtotime($rec['publish_date']));
			$rec['publish_time'] = date('h:i A',strtotime($rec['publish_time']));
			$rec['end_date'] = date('d-m-Y',strtotime($rec['end_date_time']));
			$rec['start_time'] = date('h:i A',strtotime($rec['start_date_time']));
			$rec['end_time'] = date('h:i A',strtotime($rec['end_date_time']));
		}
		return $rec;
	}
	
	public function insert($data){
		$this->add_insert_log($data);
		$builder = $this->db->table($this->table);
		$builder->insert($data);
		return $this->db->insertID();
	}
	
	public function update($data, $photo_album_id){
		$this->add_update_log($data);
		$builder = $this->db->table($this->table);
		$builder->where('photo_album_id',$photo_album_id);
		$builder->update($data);
	}
	
	public function delete($photo_album_id){
		$data['is_deleted'] = "Yes";
		$this->add_update_log($data);
		$builder = $this->db->table($this->table);
		$builder->where('photo_album_id',$photo_album_id)
				->update($data);
	}
	
	public function is_unique_photo_album_url($url,$photo_album_id){
		$builder = $this->db->table($this->table);
		$builder->where('is_deleted','No')
			->where('photo_album_url',$url);
		if($photo_album_id!=""){
			$builder->where('photo_album_id!=',$photo_album_id);
		}
		$query = $builder->get();
		if($query->getRowArray()){
			$response = "FALSE|The photo_album url already exist!";
		}else{
			$response = "TRUE|";
		}
		return $response;
	}
	
	public function update_language_data($data,$photo_album_ln_id){
		$builder = $this->db->table($this->table.'_ln');
		$builder->where('photo_album_ln_id',$photo_album_ln_id)
		->update($data);
	}
	
	public function insert_language_data($data){
		$builder = $this->db->table($this->table.'_ln');
		$builder->insert($data);
	}
}