<?php 
namespace App\Controllers\Admin;
use App\Views;
use App\Models\CommonModel;
use App\Libraries\MyEncrypt;

class Emailsettings extends BaseController {

	/**
	 * 
	 * Start 
	 * Create Date: 22 Jun, 2016
	 * This controller to manage email setting page.
	 * Created karthiga.
	 * @see http://ihyip.com/admin/emailsettings/
	 * Last Updated: 22 Jun, 2016
	 * End
	 * 
	 */

	public function __construct() {
		$this->db = db_connect();
		$myencrypt = new MyEncrypt(true);
		$this->common_model = model('CommonModel');
		$session = \Config\Services::session();
		$validation = \Config\Services::validation();
		$uri = service('uri');
		if(!$session->get('admin_login')) {
			return redirect()->to('admin/login');
		}
		// admin login only
		if($session->get('admin_login')) {

			// form validation library
			// $this->load->library('form_validation');
			$this->isSubadmin();
			

		} else {
			if(isset($_COOKIE['remember_token'])) {

				$myencrypt = new MyEncrypt;
				$token_value = $myencrypt->decode($this->input->cookie('remember_token'));
				$this->isCookie($token_value);	

			} else {
				$session->set('last_page', current_url());
				return redirect()->to('admin/login');
			}
		}
		
	}

	private function isSubadmin() {
$this->db = db_connect();
		$myencrypt = new MyEncrypt(true);
		$this->common_model = model('CommonModel');
		$session = \Config\Services::session();
		$validation = \Config\Services::validation();
		$uri = service('uri');
		if(!$session->get('admin_login')) {
			return redirect()->to('admin/login');
		}
		if($session->get('user_type') == 'subadmin') {
			$controller = $this->router->controllerName();
			$pages = json_decode($session->get('permission'));
		
			if(!in_array(ucfirst($controller), $pages)) {
				return redirect()->to('admin');
			}
		}
	}

	private function isCookie($values) {

		$value = explode('|', $values);
		$result = $this->common_model->getPassword($value[0]);
		$hashed_password = $result->password;

		if(crypt(urlencode($value[1]), $hashed_password)) {

			$array = array(
				"logged_in" => TRUE,
				"userid" => $result->id,
				"admin_login" => TRUE
			);
			$session->set($array);

		} else {
			return redirect()->to('admin/login');
		}
	}

	public function index()
	{
		$this->db = db_connect();
		$myencrypt = new MyEncrypt(true);
		$this->common_model = model('CommonModel');
		$session = \Config\Services::session();
		$validation = \Config\Services::validation();
		$uri = service('uri');
		if(!$session->get('admin_login')) {
			return redirect()->to('admin/login');
		}
        
        $this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'text' => lang('language_lang.text_home'),
			'href' => base_url().'admin'
		);
		$this->data['breadcrumbs'][] = array(
			'text' => lang('language_lang.text_settings')
			
		);
		// $this->data['breadcrumbs'][] = array(
		// 	'text' => lang('language_lang.email_text'),
		// 	'href' => base_url().'admin/emailsetting'
		// );
		$this->data['module_name'] = 'settings';
		$this->data['action'] =  'admin/emailsettings';

		$site_data = $this->common_model->GetSettingall('email');
		
		if($site_data) {
			
			foreach ($site_data as $row) {
				$this->data[$row->site_key] = $row->site_value;
			}

		} 
		// else {
		// 	$site_data_default = $this->common_model->defaultlangSettingall('email');
		// 	foreach ($site_data_default as $row) {
		// 		$this->data[$row->site_key] = $row->site_value;
		// 	}
		// }

		if($this->request->getPost()) {

			$validation->setRules([
				'admin_email' => ['label' => lang('language_lang.Admin_email_id'), 'rules' => 'trim|required'],
				'admin_email' => ['email_charset' => lang('language_lang.Email_charset'), 'rules' => 'trim|required'],
				'email_throw' => ['label' => lang('language_lang.email_throw'), 'rules' => 'trim|required']
			]);
			// check form validate
			if ($validation->withRequest($this->request)->run() == TRUE) {
				// $lang_id = ($session->get('langid')) ? $session->get('langid') : '1';

    //         	$condition = "settings_name ='email' AND language_id = '".$lang_id."'";
				// $data_avail = $this->common_model->GetSettingall_lang($condition);

		
				// if($data_avail) {

	                foreach ($site_data as $row) {

						$key_value = $row->site_key;
						
						if($this->request->getPost($key_value)){

							$sitedata = array(
								"site_value"	=>	$this->request->getPost($key_value)
							);
							$update_status = $this->db->table('site_controls')->where('settings_name','email')->where('site_key',$row->site_key)->update($sitedata);


						}
						else {
							if($key_value=='show_email'){

								$sitedata = array(
									"site_value"	=>	'0'
								);
								$update_status = $this->db->table('site_controls')->where('settings_name','email')->where('site_key',$row->site_key)->update($sitedata);
							}

						}

					}
				


                if($update_status) {
					$session->setFlashData('success_message', lang('language_lang.email_success_message'));
					return redirect()->to('admin/emailsettings');
				} else {
					$session->setFlashData('error_message', lang('language_lang.email_error_message'));
					return redirect()->to('admin/emailsettings');
				}


			}
			
		}

		echo view('admin/settings/email',$this->data);

	}
	
}

