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

class Cronjobs extends MY_Controller {

	function __construct(){
		parent::__construct();	
			
	}


	
	public function index($param=null)
	{
			
	}
	
	public function fivemin(){
		
		//file_put_contents(FCPATH."cronlog.txt", '5min-'.date('Y-m-d H:i:s'), FILE_APPEND);
	}
	
	public function tenmin(){
		//file_put_contents(FCPATH."cronlog.txt", '10min-'.date('Y-m-d H:i:s'), FILE_APPEND);
	}
	
	public function fifteenmin(){
		$this->sendwahtsappmessage();
		//file_put_contents(FCPATH."cronlog.txt", '10min-'.date('Y-m-d H:i:s'), FILE_APPEND);
	}
	
	public function twentymin(){
		//file_put_contents(FCPATH."cronlog.txt", '10min-'.date('Y-m-d H:i:s'), FILE_APPEND);
	}
	
	public function halfhr(){
		file_put_contents(FCPATH."cronlog.txt", '1/2hr', FILE_APPEND);
	}
	
	public function onehr(){
		file_put_contents(FCPATH."cronlog.txt", '1hr', FILE_APPEND);
	}
	
	
	public function sendwahtsappmessage(){
		$this->load->model('Cronjob_model');
		$messages = $this->Cronjob_model->getmessages();
		//print_r($messages);
		foreach($messages as $msg){
			if($msg->merchantId != '18')
			{
				
				if($msg->operator == 'Infobip')
				{
					$this->sendinfobipmsg($msg);
					//$this->sendrmlmsg($msg);
				}
				elseif($msg->operator == 'RMS')
				{
					$this->sendrmlmsg($msg);
				}
				elseif($msg->operator == 'Airtel')
				{
					$this->sendairtelmsg($msg);
				}
			}
		}
	}
	
	public function sendinfobipmsg($msg){
		//	echo "Infobip";
		//	echo "<br/>";
		//	echo "<br/>";
			
			
			if($msg->btn_1_url != null && $msg->btn_1_url == "{{var}}"){
				$msg->btn1url = 'redirect.html?url='.$msg->btn1url;
			}
			elseif($msg->btn_1_url != null && $msg->btn_1_url != "{{var}}"){
				$msg->btn1url = $msg->btn1url;
			}
			
			if($msg->btn_2_url != null && $msg->btn_2_url == "{{var}}"){
				$msg->btn2url = 'redirect.html?url='.$msg->btn2url;
			}
			elseif($msg->btn_2_url != null && $msg->btn_2_url != "{{var}}"){
				$msg->btn2url = $msg->btn2url;
			}
			//print_r($msg);
			//echo "<br/>";
			//echo "<br/>";
			
		$keys = $this->Cronjob_model->getkey($msg->merchantId);
		
			if($msg->header_type == 'VIDEO'){
				videoheader($msg->name,'+91'.$msg->contact,unserialize($msg->parameters),$msg->media,$msg->header_type,$msg->btn1url,$msg->btn2url,$keys[0]->headerkeyINFOBIP,$keys[0]->scenariokeyINFOBIP);
			}else{
				imageheader($msg->name,'+91'.$msg->contact,unserialize($msg->parameters),$msg->media,$msg->header_type,$msg->btn1url,$msg->btn2url,$keys[0]->headerkeyINFOBIP,$keys[0]->scenariokeyINFOBIP);
			}
	}
	
	public function sendrmlmsg($msg=null){
			//echo "RMS";
			//print_r($msg);
			
			if($msg->btn_1_url != null && $msg->btn_1_url == "{{var}}"){
				$msg->btn1url = 'redirect.html?url='.$msg->btn1url;
			}
			elseif($msg->btn_1_url != null && $msg->btn_1_url != "{{var}}"){
				$msg->btn1url = $msg->btn1url;
			}
			
			if($msg->btn_2_url != null && $msg->btn_2_url == "{{var}}"){
				$msg->btn2url = 'redirect.html?url='.$msg->btn2url;
			}
			elseif($msg->btn_2_url != null && $msg->btn_2_url != "{{var}}"){
				$msg->btn2url = $msg->btn2url;
			}
			
			$keys = $this->Cronjob_model->getkey($msg->merchantId);
			//print_r($keys);
			rml_imageheader($msg->name,'+91'.$msg->contact,unserialize($msg->parameters),$msg->media,$msg->header_type,$msg->btn1url,$msg->btn2url,$keys[0]->headerkeyRML);
			
			//rml_imageheader('seasonalgreetting_1','+917838222290','test');
			
	}
	//To create a function for sendairtelmsg
	public function sendairtelmsg($msg = null, $header_type = null)
	{
		// Adjust the button URLs
		
		$notifyURL = ''; 
		$method = ''; 
		$headers = []; 
	
		// Call the appropriate function based on the message type
		if ($header_type == "no_header") {
			$keys = $this->Cronjob_model->getkey($msg->merchantId);
			airtel_noheader($msg->templateId, $msg->to, $msg->from, $msg->headerVars, $msg->variables, $msg->payload, $keys[0]->headerkeyAirtel);
		} elseif ($header_type == "IMAGE") {
			$keys = $this->Cronjob_model->getkey($msg->merchantId);
			airtel_imageheader($msg->templateId, $msg->to, $msg->from, $msg->headerVars, $msg->variables, $msg->payload, $msg->mediaType, $msg->mediaId, $keys[0]->headerkeyAirtel);
		} elseif ($header_type == "callback") {
			$keys = $this->Cronjob_model->getkey($msg->merchantId);
			airtel_callback($msg->templateId, $msg->to, $msg->from, $msg->headerVars, $msg->variables, $msg->payload, $notifyURL, $method, $headers, $keys[0]->headerkeyAirtel);
		} elseif ($header_type == "VIDEO") {
			$keys = $this->Cronjob_model->getkey($msg->merchantId);
			airtel_imageheader($msg->templateId, $msg->to, $msg->from, $msg->headerVars, $msg->variables, $msg->payload, $msg->mediaType, $msg->mediaId, $keys[0]->headerkeyAirtel);
		} elseif ($header_type == "DOCUMENT") {
			$keys = $this->Cronjob_model->getkey($msg->merchantId);
			airtel_imageheader($msg->templateId, $msg->to, $msg->from, $msg->headerVars, $msg->variables, $msg->payload, $msg->mediaType, $msg->mediaId, $keys[0]->headerkeyAirtel);
		}
	}

	//Till here
}
