<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Ts_functions {
    public function __construct() {
        $this->CI = get_instance();
    }
    /****
    getsettings : Function responsible to fetch the basic settings of the application,
    First Param : Pagename eg., login, register...
    Second Param : Type eg., title , metatags...
    ****/
    public function getsettings($pagename = '', $type = '') {
        $whrParam = $pagename . '_' . $type;
        $whrArray = array(
            'key_text' => $whrParam
        );
        $resArray = $this->CI->my_model->select_data('value_text', 'rt_settings', $whrArray, 1);
        return (!empty($resArray) ? $resArray[0]['value_text'] : 'NF');
    }
    /****
    updatesettings : Function responsible to update the settings of the application,
    First Param : key ...
    Second Param : value ...
    ****/
    public function updatesettings($key = '', $value = '') {
        $whrArray    = array(
            'key_text' => $key
        );
        $updateArray = array(
            'value_text' => $value
        );
        $this->CI->DatabaseModel->access_database('rt_settings', 'update', $updateArray, $whrArray);
    }
    public function sendnotificationemails($type, $to, $subject, $username, $linkhref = '', $password = '', $website_link = '',$loginword='') {
        
        
        $this->CI->load->library('parser');
        $link       = "<a href='" . $linkhref . "'>" . $linkhref . "</a>";
       
        $emContent  = $this->getsettings($type, 'text');
        $emContent  = str_replace("[linktext]", $link, $emContent);
        $emContent  = str_replace("[username]", $username, $emContent);
        $emContent  = str_replace("[useremail]", $to, $emContent);
        $emContent  = str_replace("[password]", $password, $emContent);
        $emContent  = str_replace("[loginword]", $loginword, $emContent);
        $emContent  = str_replace("[loginlink]", $website_link, $emContent);
        $emContent  = str_replace("[break]", "<br/>", $emContent);  
		$emContent  = str_replace("[space]", "&nbsp;", $emContent);
		$emContent  = str_replace("[s1]", $linkhref, $emContent);
		$emContent  = str_replace("[s2]", "<a href='".base_url('front/product_rating/'.$linkhref.'/2/'.$password)."'><img src='".base_url('assets/images/Star1.png')."'></a>", $emContent);
		$emContent  = str_replace("[s3]", "<a href='".base_url('front/product_rating/'.$linkhref.'/3/'.$password)."'><img src='".base_url('assets/images/Star1.png')."'></a>", $emContent);
		$emContent  = str_replace("[s4]", "<a href='".base_url('front/product_rating/'.$linkhref.'/4/'.$password)."'><img src='".base_url('assets/images/Star1.png')."'></a>", $emContent);
		$emContent  = str_replace("[s5]", "<a href='".base_url('front/product_rating/'.$linkhref.'/5/'.$password)."'><img src='".base_url('assets/images/Star1.png')."'></a>", $emContent);
		$emContent  = str_replace("[s6]", "<a href='".base_url('front/product_rating/'.$linkhref.'/6/'.$password)."'><img src='".base_url('assets/images/Star1.png')."'></a>", $emContent);
		
		$filename = base_url('assets/images/maillogo.svg');
		$img = "<img src='.$filename.'>";
        $email_body = '<table><tr><td>' . $emContent . '</td></tr></table>';
        $email_data = array(
            'email_body' => $email_body,
            'email_logo'=> $img
        );
        $body       = $this->CI->parser->parse('email_template', $email_data, true);
        $subject    = $this->getsettings($type, 'subject');
        $this->sendUserEmailCI($to, '', '', $subject, $body);
    }
	function email2($type,$to,$emContent)
	{
		 $this->CI->load->library('parser');
		 $email_body = '<table><tr><td><p>' . $emContent . '</p></td></tr></table>';
		  $subject    = $this->getsettings($type, 'subject');
        $email_data = array(
            'email_body' => $email_body
        );
        $body       = $this->CI->parser->parse('email_template', $email_data, true);
        $subject    = $this->getsettings($type, 'subject');
        $this->sendUserEmailCI($to, '', '', $subject, $body);
	}
    public function sendbookingemails($payment_id = '', $type = '') {
        $email_bodyUser = $email_bodyDoctor = '';
        $this->CI->load->library('parser');
        $paymentDetails = $this->CI->DatabaseModel->select_data('*', 'rt_paymentdetails', array(
            'payment_id' => $payment_id
        ), 1);
        if (!empty($paymentDetails)) {
            $order_status = $paymentDetails[0]['payment_order_status'];
            $user_id      = $paymentDetails[0]['payment_uid'];
            $userDetails  = $this->CI->DatabaseModel->select_data('*', 'rt_users', array(
                'user_id' => $user_id
            ), 1);
            $username     = $userDetails[0]['user_name'];
            $user_mobile  = $userDetails[0]['user_mobile'];
            $subject      = $this->getsettings('bookingemail', 'subject');
			
            // Booking start
            if ($order_status == 0) {
                $emContent       = $this->getsettings('bookorder', 'text');
                $email_bodyAdmin = $this->parseBotemp($paymentDetails, $userDetails, $emContent);
            }
            // Booking End
			
            // Intransist start
            if ($order_status == 1) {
                $emContentUser    = $this->getsettings('intransits', 'text');
                $email_bodyUser   = $this->parseBotemp($paymentDetails, $userDetails, $emContentUser);
                
            }
			
            // Intransist End
			
            // cancel order by admin start
            if ($order_status == 2) {
                $emContentUser  = $this->getsettings('delivered', 'text');
                $email_bodyUser = $this->parseBotemp($paymentDetails, $userDetails,  $emContentUser);
            }
			if ($order_status == 3) {
                $emContentUser  = $this->getsettings('canceled', 'text');
                $email_bodyUser = $this->parseBotemp($paymentDetails, $userDetails,  $emContentUser);
            }
			
            if ($email_bodyUser != '') {
                $bodyUser = $this->CI->parser->parse('email_template', array(
                    'email_body' => $email_bodyUser
                ), true);
                $this->sendUserEmailCI($userDetails[0]['user_email'], '', '', $subject, $bodyUser);
            }
            if ($email_bodyAdmin != '') {
				
                $bodyAdmin = $this->CI->parser->parse('email_template', array(
                    'email_body' => $email_bodyAdmin
                ), true);
				
                $this->sendUserEmailCI($this->getsettings('email', 'contactemail'), '', '', $subject, $bodyAdmin);
            }
        }
    }
    function parseBotemp($paymentDetails, $userDetails, $emContent) {
        $order_date   = date("d-M-Y", strtotime($paymentDetails[0]['payment_date']));
        $order_number = $paymentDetails[0]['payment_uniqid'];
        $username     = ucfirst($userDetails[0]['user_name']);
        $emContent    = str_replace("[username]", $username, $emContent);
        $emContent    = str_replace("[order_date]", $order_date, $emContent);
        $emContent    = str_replace("[order_number]", $order_number, $emContent);
        $emContent    = str_replace("[break]", "<br/>", $emContent);
        $email_body   = '<table><tr><td class="main_reason">' . $emContent . '</td></tr></table>';
        return $email_body;
    }
    public function sendUserEmailCI($to, $fromname, $fromemail, $subject = '', $body = '', $attachments = array(), $filePath = '', $emailtype = '') {
        $fromname           = ($fromname ? $fromname : $this->getsettings('email', 'fromname'));
        $fromemail          = ($fromemail ? $fromemail : $this->getsettings('email', 'fromemail'));
      
        $config['mailtype'] = "html";
        if ($this->getsettings('email', 'lanformat') == '1') {
            $config['charset'] = "utf-8";
        } else {
            $config['charset'] = "iso-8859-1";
        }
       
        $this->CI->load->library('email');
        $this->CI->email->initialize($config);
        $this->CI->email->from($fromemail, $fromname);
        $this->CI->email->to($to);
        $this->CI->email->subject($subject);
        $this->CI->email->message($body);
        if($this->CI->email->send()){
			return true;
		}
    }
    function send_msg($mob_no, $MESSAGE) {
        $msg91_status = $this->getsettings('msg91', 'status');
        $AUTHKEY      = $this->getsettings('msg91', 'key');
        
        if ($msg91_status != 0 && $AUTHKEY != '') {
            $SENDER = $this->getsettings('msg91', 'sender');
            $ROUTE  = "4";
            $url    = 'https://control.msg91.com/api/sendhttp.php?authkey=' . $AUTHKEY . '&mobiles=' . $mob_no . '&message=' . urlencode($MESSAGE) . '&sender=' . $SENDER . '&response=json&route=' . $ROUTE;
            $ch     = curl_init($url);
            curl_setopt($ch, CURLOPT_HEADER, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $resp = curl_exec($ch);
            $json = json_decode($resp);
            curl_close($ch);
            //print_r($resp);
			 
        }
		 
    }
    function upload_image($upPath, $name, $postFix = NULL) {
        $this->CI                	= get_instance();
		
		$basePath 					= explode('application',dirname(__FILE__))[0];
        $uploadPath              	= $basePath . $upPath;
        $config['upload_path']   	= $uploadPath;
        $config['allowed_types'] 	= 'gif|jpg|png';
        $this->CI->load->library('upload', $config);
		$this->CI->upload->initialize($config);
        if ($this->CI->upload->do_upload($name)) {
            $uploaddata    = $this->CI->upload->data();
            $imgName       = $uploaddata['raw_name'];
            $imgExt        = $uploaddata['file_ext'];
            $randomstr     = substr(md5(microtime()), 0, 10);
            $uploadedImage = $randomstr . $postFix . $imgExt;
            rename($uploadPath . $imgName . $imgExt, $uploadPath . $uploadedImage);
            return $uploadedImage;
        } else {
            return ;
        }
    }
	
	public function resizeImage($width,$height,$source_path,$target_path)
	{
			$config_manip = array(
				'image_library' => 'gd2',
				'source_image' => $source_path,
				'new_image' => $target_path,
				'maintain_ratio' => FALSE,
				'create_thumb' => TRUE,
				'thumb_marker' => '',
				'width' => $width,
				'height' => $height
				);


			$this->CI->load->library('image_lib', $config_manip);
			
			$this->CI->image_lib->initialize($config_manip);
			if (!$this->CI->image_lib->resize()) {
				return $this->CI->image_lib->display_errors();
			}else{
			return true;
			}

			$this->CI->image_lib->clear();
	}
	
	/****
        getlanguage : Function responsible to fetch the language depending on either key or type,
        First Param : key
        Second Param : type
    ****/

    public function getlanguage($key='',$type='',$return='')
    {
        if( $key == 'all' ) {
            $whrArray = array('language_type'=>$type);
        }
        else {
            $whrArray = array('language_key'=>$key,'language_type'=>$type);
        }

        $resArray = $this->CI->my_model->select_data('*' , 'language' , $whrArray);

        if( $return == 'solo' ) {
            $resArr = $this->CI->my_model->select_data('value_text' , 'rt_settings' , array('key_text'=>'weblanguage_text') , 1);
            $selectedLanguage =  $resArr[0]['value_text'];
            if( isset($_COOKIE['language']) ) {
            	$k = 'language_'.$_COOKIE['language'] ;
            }
            else {
            	$k = 'language_'.$selectedLanguage ;
            }
            
            return (!empty($resArray) ? $resArray[0][$k] : 'NF' );
        }
        else {
            return (!empty($resArray) ? $resArray : 'NF' );
        }
    }


    public function getlanguage_msg($msgType)
    {
        
        if($msgType) {
    	   $langType =$this->getsettings('weblanguage','text');
           $msgkey ='language_'.$langType;
           $langData = $this->CI->my_model->select_data($msgkey , 'language' , array('language_key' => $msgType),'1');
            
            return (!empty($langData[0][$msgkey]) ? $langData[0][$msgkey]: 'NF' );
        }else {
            return (!empty($langData[0][$msgkey]) ? $langData[0][$msgkey]: 'NF' );
        }
    }

	
}
/* End of file Ts_functions.php */
?>
