<?php
if(!defined('BASEPATH')) exit('Hacking Attempt : Will Kill You Do Not Worry..!!');

class Templates extends MY_Controller
{

	var $model ='Templates_model';
	
	public function __construct(){
		parent::__construct();
		if($this->session->userdata('isLogin') == FALSE ){ redirect(base_url('admin/login/login_form'));}	
		$this->load->model('admin/'.$this->model); 
		
		$config['upload_path']          = 'uploads/templatedata/';
		$config['allowed_types']        = 'gif|jpg|png|jpeg|doc|pdf|mp4';
		$config['max_size'] 			= 12000;
        $config['max_width'] 			= 2000;
        $config['max_height'] 			= 2000;
		$this->upload->initialize($config);
		
		$this->session->unset_userdata('form_error');
		$this->session->unset_userdata('file_error');
	}
	
	
public function index($param=null){
			$data['pagetitle']='Templates';
			
			if(isset($_POST['status'])){$data['status'] = $_POST['status'];}else{$data['status']='-1';}
			if($param == 'active'){	$data['status'] =1;}
			if($param == 'pending'){	$data['status'] =0;}

			
			$data['response'] = $this->Templates_model->get_templates($data['status']);
			$data['updatelink']=base_url('admin/templates/templatespublish');
			$data['deletelink']=base_url('admin/templates/templatesdelete');
			$this->adminview('templates/templates',$data);
	}
	
	public function templatespublish($para=null){
			$tempdata=array();
			
			$formerror = false;
			
			if(isset($_POST['submit'])){
				  
				  	$rules = array(
									array(
											'field' => 'name',
											'label' => 'Template Name',
											'rules' => 'required'
									)
							);

				$this->form_validation->set_rules($rules);
				if ($this->form_validation->run() == FALSE) { 
					$formerror = 1; 
					$this->session->set_flashdata('form_error', '<div class="alert alert-danger text-center"> '.validation_errors().'</div>');
				}else{$formerror = 0; }
				
				if($_POST['type'] != 'TEXT'){
					if($_POST['headerfileold'] != $_FILES['headerfile']['name'] && $_FILES['headerfile']['name'] !=''){
						if (!$this->upload->do_upload('headerfile')) {    
							$this->session->set_flashdata('file_error', '<div class="alert alert-danger text-center"> '.$this->upload->display_errors().'</div>');
							$formerror = 1; 
						} 				
						else {  
							$imgdata = $this->upload->data('file_name');  $formerror = 0; 
							$tempdata['templates']['header_value'] = $imgdata;	
						}
					}
				}else{
					$tempdata['templates']['header_value'] = $_POST['headertext'];
				}
					if($_POST['id'] ==''){
					if(checkunique('templateId',array('templatename'=>$_POST['name'],'operator'=>$_POST['operator']),'templates')){
						$this->session->set_flashdata('image_error', '<div class="alert alert-danger text-center"> Template with Same Name and Operator Already Exist...</div>');
							$formerror = true; 
					}
				}
				
			   if(!$formerror){		
				 
				  $id = $_POST['id'];
				  $tempdata['templates']['templatename'] = $_POST['name'];
				  $tempdata['templates']['operator'] = $_POST['operator'];
				  $tempdata['templates']['category'] = $_POST['category'];
				  $tempdata['templates']['name'] = $_POST['name'];
				  $tempdata['templates']['language'] = $_POST['language'];
				  $tempdata['templates']['header_type'] = $_POST['type'];
				  $tempdata['templates']['body'] = $_POST['body'];
				  $tempdata['templates']['footer'] = $_POST['footertext'];
				  $tempdata['templates']['btn_1_text'] = $_POST['btn1text'];
				  $tempdata['templates']['btn_1_url'] = $_POST['btn1url'];
				  $tempdata['templates']['btn_2_text'] = $_POST['btn2text'];
				  $tempdata['templates']['btn_2_url'] = $_POST['btn2url'];
				  
				  if($id==''){$tempdata['templates']['submited_on'] = date('Y-m-d H:i:s');}
				  $tempdata['templates']['edited_on'] = date('Y-m-d H:i:s');
								  
				  if($_POST['status']){ $tempdata['templates']['activated_on'] = date('Y-m-d H:i:s');}
				
				  $tempdata['templates']['status'] = $_POST['status'];				  
				  $tempdata['templates']['parameters'] = substr_count($tempdata['templates']['body'], "{{var}}");
				  $tempdata['templates_merchant'] = $_POST['merchants'];
				  
				 
				  $id = $this->Templates_model->set_templates($tempdata,$id);
					
					$this->session->set_flashdata('page_message', '<div class="alert alert-success text-center"> Templated Updated Successfully...</div>');
					redirect(base_url('admin/templates'));
			   }
			   else{
				  $tempdata['templates']['templatename'] = $_POST['name'];
				  $tempdata['templates']['operator'] = $_POST['operator'];
				  $tempdata['templates']['category'] = $_POST['category'];
				  $tempdata['templates']['name'] = $_POST['name'];
				  $tempdata['templates']['language'] = $_POST['language'];
				  $tempdata['templates']['header_type'] = $_POST['type'];
				  $tempdata['templates']['header_value'] = $_POST['headertext'];
				  $tempdata['templates']['body'] = $_POST['body'];
				  $tempdata['templates']['footer'] = $_POST['footertext'];
				  $tempdata['templates']['btn_1_text'] = $_POST['btn1text'];
				  $tempdata['templates']['btn_1_url'] = $_POST['btn1url'];
				  $tempdata['templates']['btn_2_text'] = $_POST['btn2text'];
				  $tempdata['templates']['btn_2_url'] = $_POST['btn2url'];
				  $tempdata['templates']['status'] = $_POST['status'];
				}
			}
			
			if($para >0){
					
					$response = $this->Templates_model->get_single_templates($para);
					$data['id']=$response[0]->templateId;
					$data['templates']['templatename'] = $response[0]->templatename;
				  	$data['templates']['operator'] = $response[0]->operator;
				  	$data['templates']['category'] = $response[0]->category;
				  	$data['templates']['name'] = $response[0]->name;
				  	$data['templates']['language'] = $response[0]->language;
				  	$data['templates']['header_type'] = $response[0]->header_type;
					$data['templates']['header_value'] = $response[0]->header_value;
				  	$data['templates']['body'] = $response[0]->body;
				  	$data['templates']['footer'] = $response[0]->footer;
				  	$data['templates']['btn_1_text'] = $response[0]->btn_1_text;
				  	$data['templates']['btn_1_url'] = $response[0]->btn_1_url;
				  	$data['templates']['btn_2_text'] = $response[0]->btn_2_text;
				  	$data['templates']['btn_2_url'] = $response[0]->btn_2_url;
					$data['templates']['status'] = $response[0]->status;
					$data['templates_merchant'] = $this->Templates_model->get_single_templates_merchants($para);
			}
			
			$data['pagetitle']='Update Templates';
			$data['merchantoptions'] = getdata('merchantId,storename','merchant',array('status='=>1));
			$data['languageoptions'] = getdata('*','language');
			$data['typeoptions'] = array('TEXT','MEDIA','VIDEO','DOCUMENT');
			$data['operatorsoptions'] = getdata('*','operators',array('status'=>1));
			$data['categoryoptions'] = getdata('*','template_types');
				
			$this->adminview('templates/templatespublish',$data);
	}
	
	public function templatespublishes($para=null){
		
			$formerror=0;
			
			//print_r($_POST);
			if(isset($_POST['submit'])){
				$rules = array(
									array(
											'field' => 'accountId',
											'label' => 'Merchant Account Id',
											'rules' => 'required'
									),
									array(
											'field' => 'storename',
											'label' => 'Merchant Store Name',
											'rules' => 'required'
									),
									array(
											'field' => 'name',
											'label' => 'Merchant Name',
											'rules' => 'required'
									),
									array(
											'field' => 'email',
											'label' => 'Merchant Email Id',
											'rules' => 'required'
									),
									array(
											'field' => 'contact',
											'label' => 'Merchant Contact No.',
											'rules' => 'required'
									),
									array(
											'field' => 'whatsappno',
											'label' => 'Merchant Whatsapp No.',
											'rules' => 'required'
									)
							);

				$this->form_validation->set_rules($rules);
				if ($this->form_validation->run() == FALSE) { 
					$formerror = 1; 
					$this->session->set_flashdata('form_error', '<div class="alert alert-danger text-center"> '.validation_errors().'</div>');
				}else{$formerror = 0; }
				
				if($_POST['iconold'] != $_FILES['icon']['name'] && $_FILES['icon']['name'] !=''){
					if (!$this->upload->do_upload('icon')) {    
						$this->session->set_flashdata('image_error', '<div class="alert alert-danger text-center"> '.$this->upload->display_errors().'</div>');
						$formerror = 1; 
					} 				
					else {  
						$imgdata = $this->upload->data('file_name');  $formerror = 0; 
						$data['icon']=$imgdata;		
					}
				}
				
				
				
				if($_POST['id'] ==''){
					if(checkunique('accountId',array('accountId'=>$_POST['accountId']),'merchant')){
						$this->session->set_flashdata('image_error', '<div class="alert alert-danger text-center"> Merchant Account Id Already In Database...</div>');
							$formerror = 1; 
					}
				}
				
				if(!$formerror){
					
					$id = $_POST['id'];
					$data['accountId']=$_POST['accountId'];
					$data['groupId']=$_POST['groupId'];
					$data['storename']=$_POST['storename'];
					$data['name']=$_POST['name'];
					$data['email']=$_POST['email'];
					$data['contact']=$_POST['contact'];
					$data['whatsappno']=$_POST['whatsappno'];
					$data['description']=$_POST['description'];
					
					if(isset($_POST['status'])){$data['status']=1;}else{$data['status'] = 0;}
					if(isset($_POST['calltoactionbtn'])){$data['calltoactionbtn']=1;}else{$data['calltoactionbtn'] = 0;}
					
					if($id ==''){$data['createdon']=date('Y-m-d H:i:s');}					
					$data['updatedon']=date('Y-m-d H:i:s');
					$id = $this->Templates_model->set_merchant($data,$id);
					
					$this->session->set_flashdata('page_message', '<div class="alert alert-success text-center"> Merchant Updated Successfully...</div>');
					redirect(base_url('admin/merchants/merchants'));
				}else{
					$data['accountId']=$_POST['accountId'];
					$data['groupId']=$_POST['groupId'];
					$data['storename']=$_POST['storename'];
					$data['name']=$_POST['name'];
					$data['email']=$_POST['email'];
					$data['contact']=$_POST['contact'];
					$data['whatsappno']=$_POST['whatsappno'];
					$data['description']=$_POST['description'];
					
					if(@$_POST['calltoactionbtn'] == 'on'){$data['calltoactionbtn']=1;}else{$data['calltoactionbtn'] = 0;}
					if(@$_POST['calltoactionbtn'] == 'on'){$data['calltoactionbtn']=1;}else{$data['calltoactionbtn'] = 0;}
				}
			
			}
			
			if($para >0){
					
					$response = $this->Templates_model->get_single_merchant($para);
					$data['id']=$response[0]->merchantId;
					$data['accountId']=$response[0]->accountId;
					$data['groupId']=$response[0]->groupId;
					$data['storename']=$response[0]->storename;
					$data['name']=$response[0]->name;
					$data['email']=$response[0]->email;
					$data['contact']=$response[0]->contact;
					$data['whatsappno']=$response[0]->whatsappno;
					$data['description']=$response[0]->description;
					$data['icon']=$response[0]->icon;
					$data['status']=$response[0]->status;
					$data['calltoactionbtn']=$response[0]->calltoactionbtn;
			}
			
			
			$data['pagetitle']='Update Templates';
			$data['merchantoptions'] = getdata('merchantId,storename','merchant',array('status='=>1));
			$data['languageoptions'] = getdata('*','language');
			$data['typeoptions'] = array('TEXT','MEDIA','VIDEO','DOCUMENT');
			$data['operatorsoptions'] = getdata('*','operators',array('status'=>1));
			$data['categoryoptions'] = getdata('*','template_types');
				
			$this->adminview('templates/templatespublish',$data);
	}

	public function templatesdelete($para = null){
		$this->Templates_model->delete_templates($para);
		$this->session->set_flashdata('page_message', '<div class="alert alert-danger text-center"> Selected Entry Deleted Successfully</div>');
		redirect(base_url('admin/templates'));
					
	}

}	

?>