<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Language extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->model('common_model');
		$this->load->model('users_model');
		if(get_cookie('admin_id'))
		{
			$this->session->set_userdata('admin_id',get_cookie('admin_id'));
		}
		if(get_cookie('admin_id'))
		{
			$this->session->set_userdata('admin_id',get_cookie('admin_id'));
		}
		if(!$this->session->userdata('admin_id'))
		{
			redirect('admin/login');
		}
	}
	public function index(){
		$content_data = array();
		$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
		$content_data['languages'] = $this->common_model->getAll('sys_languages') ;
		$data['content'] = $this->load->view('admin/language/list', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function add()
	{
		if($this->input->post())
		{
			$name=$this->utility->info_cleanQuery($this->input->post('name'));
			$archDetails_con=array(
						'name'=>$name,
						'created'=>time()
				);
			$this->common_model->insertData('sys_languages',$archDetails_con);

			$this->utility->setMsg('Language added','SUCCESS');	
			redirect(base_url().'admin/language');
		}

		$content_data = array();
		$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
		$data['content'] = $this->load->view('admin/language/add', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function visible($enc_userID='',$status='')
	{
		if($enc_userID!='')
		{
			if($status==1)
			{
				$update = 1;
			}
			else
			{
				$update = 0;
			}
			$articleId=base64_decode($enc_userID);
			$this->common_model->UpdateData('sys_languages',array('visible'=>$update),array('id'=>$articleId));
			$this->utility->setMsg('Language status changed','SUCCESS');	
			redirect(base_url().'admin/language');
		}
		else
		{
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/language');
		}
	}
	public function required(){
		$content_data = array();
		$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
		$data['content'] = $this->load->view('admin/language/required', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function translation(){
		$content_data = array();
		$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
		$content_data['translation'] = $this->common_model->getAll('language_values',array('lang_id'=>2,'page'=>0),'','','','key') ;
		$content_data['languages'] = $this->common_model->getAll('sys_languages',array('id !='=>2)) ;
		$data['content'] = $this->load->view('admin/language/translation', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function update()
	{
		$translation = $this->common_model->getAll('language_values',array('lang_id'=>2,'page !='=>1));
		$languages = $this->common_model->getAll('sys_languages',array('id !='=>2)) ;
		//print_r($this->input->post()); die;
		if(!empty($translation))
		{
			foreach ($translation as $value) {
				if(!empty($languages))
				{
					foreach ($languages as $lang) {
						$text = $this->input->post('text_'.$value['key'].'_'.$lang['id']);
						$exist = $this->common_model->getAllRow('language_values',array('key'=>$value['key'],'lang_id'=>$lang['id'])) ;
						if($text!='')
						{
							if(empty($exist))
							{
								$this->common_model->insertData('language_values',array('key'=>$value['key'],'lang_id'=>$lang['id'],'value'=>$text));
							}
							else
							{
								$this->common_model->UpdateData('language_values',array('value'=>$text),array('key'=>$value['key'],'lang_id'=>$lang['id']));
							}
						}
					}
				}
			}
		}
		redirect(base_url().'admin/language/translation');
	}
	public function translations(){
		$content_data = array();
		$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
		$content_data['translation'] = $this->common_model->getAll('language_values',array('lang_id'=>2),'','','','key') ;
		$content_data['languages'] = $this->common_model->getAll('sys_languages',array('id !='=>2)) ;
		$data['content'] = $this->load->view('admin/language/translations', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function edit($enc_userID='')
	{
		if($enc_userID!='')
		{
			$evtId=base64_decode($enc_userID);
			if($this->input->post())
			{
				$data = $this->common_model->getAllRow('language_values',array('id'=>$evtId));
				$languages = $this->common_model->getAll('sys_languages') ;
				if(!empty($languages))
				{
					foreach ($languages as $value) {
						$exist = $this->common_model->getAllRow('language_values',array('key'=>$data['key'],'lang_id'=>$value['id']));
						//print_r($this->input->post()); die;
						if(empty($exist))
						{
							$this->common_model->insertData('language_values',array('key'=>$data['key'],'lang_id'=>$value['id'],'value'=>$this->input->post('name_'.$value['id'])));
						}
						else
						{
							$this->common_model->UpdateData('language_values',array('value'=>$this->input->post('name_'.$value['id'])),array('key'=>$data['key'],'lang_id'=>$value['id']));
						}
					}
				}
				
			}

			$content_data = array();
			$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
			$content_data['translation'] = $this->common_model->getAllRow('language_values',array('id'=>$evtId)) ;
			$content_data['languages'] = $this->common_model->getAll('sys_languages') ;
			$data['content'] = $this->load->view('admin/language/edit', $content_data, TRUE);
			$this->load->view('admin/layout', $data);
		}
		else
		{
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/language/translations');
		}
	}
	public function email_translation(){
		$content_data = array();
		$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
		$content_data['translation'] = $this->common_model->getAll('language_values',array('lang_id'=>2,'page'=>1),'','','','key') ;
		$content_data['languages'] = $this->common_model->getAll('sys_languages',array('id !='=>2)) ;
		$data['content'] = $this->load->view('admin/language/email_translation', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function email_update()
	{
		$translation = $this->common_model->getAll('language_values',array('lang_id'=>2,'page'=>1));
		$languages = $this->common_model->getAll('sys_languages',array('id !='=>2)) ;
		//print_r($this->input->post()); die;
		if(!empty($translation))
		{
			foreach ($translation as $value) {
				if(!empty($languages))
				{
					foreach ($languages as $lang) {
						$text = $this->input->post('text_'.$value['key'].'_'.$lang['id']);
						$exist = $this->common_model->getAllRow('language_values',array('key'=>$value['key'],'lang_id'=>$lang['id'])) ;
						if($text!='')
						{
							if(empty($exist))
							{
								$this->common_model->insertData('language_values',array('key'=>$value['key'],'lang_id'=>$lang['id'],'value'=>$text));
							}
							else
							{
								$this->common_model->UpdateData('language_values',array('value'=>$text),array('key'=>$value['key'],'lang_id'=>$lang['id']));
							}
						}
					}
				}
			}
		}
		redirect(base_url().'admin/language/email_translation');
	}
	public function meta_translation(){
		$content_data = array();
		$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
		$content_data['translation'] = $this->common_model->getAll('language_values',array('lang_id'=>2,'page'=>2),'','','','key') ;
		$content_data['languages'] = $this->common_model->getAll('sys_languages') ;
		$data['content'] = $this->load->view('admin/language/meta_translation', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function meta_update()
	{
		$translation = $this->common_model->getAll('language_values',array('lang_id'=>2,'page'=>2));
		$languages = $this->common_model->getAll('sys_languages') ;
		//print_r($this->input->post()); die;
		if(!empty($translation))
		{
			foreach ($translation as $value) {
				if(!empty($languages))
				{
					foreach ($languages as $lang) {
						$text = $this->input->post('text_'.$value['key'].'_'.$lang['id']);
						$exist = $this->common_model->getAllRow('language_values',array('key'=>$value['key'],'lang_id'=>$lang['id'])) ;
						if($text!='')
						{
							if(empty($exist))
							{
								$this->common_model->insertData('language_values',array('key'=>$value['key'],'lang_id'=>$lang['id'],'value'=>$text));
							}
							else
							{
								$this->common_model->UpdateData('language_values',array('value'=>$text),array('key'=>$value['key'],'lang_id'=>$lang['id']));
							}
						}
					}
				}
			}
		}
		redirect(base_url().'admin/language/meta_translation');
	}


	public function full_text(){
		$content_data = array();
		$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
		$content_data['translation'] = $this->common_model->getAll('language_values',array('lang_id'=>2),'','','','key') ;
		$content_data['languages'] = $this->common_model->getAll('sys_languages',array('id !='=>2)) ;
		$data['content'] = $this->load->view('admin/language/full_text', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}

	public function email_text(){
		$content_data = array();
		$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
		$content_data['translation'] = $this->common_model->getAll('language_values',array('lang_id'=>2,'page'=>1),'','','','key') ;
		$content_data['languages'] = $this->common_model->getAll('sys_languages',array('id !='=>2)) ;
		$data['content'] = $this->load->view('admin/language/email_text', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
}