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

/*
 * Orkin.php
 *
 * Created on March 30, 2017, 10:46 PM
 *
*/

/**
 <P>This class is used to receive request by Orkin

 @author Giovanni G. Mendez M.
 @see RMScheduler
 */

class Orkin extends CI_Controller {

	function __construct() {
        @session_start();
        parent::__construct();
       // $this->load->library('security');
        $this->ci = & get_instance();
        $this->ci->load->database();
        $this->allow_get_array = TRUE;
        $this->ci->load->model('Model_orkin');
        $this->ci->load->model('Model_log');

    }

	public function index()
	{

		$data['content_variable'] = "";
		$this->load->view('response/response', $data);
	}
        

     public function test(){
        $query_result =  $this->Model_orkin->test();
        //$this->output->enable_profiler(TRUE);
        $data['content_variable'] = "";

        
        if (true){

      //$data['content_variable'] .= $param;

            

            if($query_result == null){
                $data['content_variable'] .= "null";
                $this->load->view('response/response', $data);
                return;
               
            }

            $result_string = "";

            $result_string.='<?xml version="1.0" encoding="UTF-8”?>';
            $result_string.='<riweb>';
            $result_string.='<type>result</type>';

            $result_string.='<fields>';

            $sepa0 = "";
            foreach ($query_result->list_fields() as $field)
            {
                     $result_string.= $sepa0 . $field;
                     $sepa0 = "#nv";
            }

            $result_string.='</fields>';

             $result_string.='<data>';
             $count1 = $query_result->num_rows();
             $i = 1;
            foreach ($query_result->result_array() as $row) {
                 $arraytemp = array();

                $count2 = count($query_result->list_fields());
                $i2 = 1;
                foreach ($query_result->list_fields() as $field)
                {
                    $result_string.= $row[$field];
                    
                    if($i2 < $count2){
                        $result_string.= "#nv";
                    }

                    $i2++;

                }

                if($i < $count1){
                   $result_string.= "#nl";
                }
                
                $i++;

            }
            $result_string.='</data>';

            $result_string.='</riweb>';

            $data['content_variable'] .= $result_string;

            
            $this->load->view('response/response', $data);

        }else{

              if(strtoupper($contains_select) == "CALL"){
                $this->Model_orkin->execute_query('use rmscheduler_orkincr_old'); 
                $this->load->view('response/response', $data);
              }else{
                  $query_result =  $this->Model_orkin->execute_query($param);
                  $data['content_variable'].= $this->void_xml();
                  $this->load->view('response/response', $data);
              }
        }
        //echo implode(",", $query_result->result_array());
    }
    
    
   public function test2(){
   	   $resu = "test..";
       echo $resu;
   }

  public function test3(){
	   $resu = "test..";
       echo $resu;
 }
    

    


    public function echo_query(){
        $data['content_variable'] = "";
        $param = $this->uid = $this->input->get('query', TRUE);
        $data['content_variable'] .= $param;
        $param = str_replace("!!!!", "|", $param);
        $data['content_variable'] .= '<br/>';
        $data['content_variable'] .= $param;
        $this->load->view('response/response', $data);

    }


    public function rm_execute_base64_AWS($typeQuery = null){

        $data['content_variable'] = "";
        $param;
        
        $param = $this->uid = $this->input->post("query", TRUE);//
        
        $param = base64_decode($param);

        $contains_select =  substr($param, 0, 6);
        
        if(strtoupper($contains_select) == "SELECT"){

      //$data['content_variable'] .= $param;

            $query_result =  $this->Model_orkin->execute_query_AWS($param);

            if($query_result == null){
                $data['content_variable'] .= "null";
                $this->load->view('response/response', $data);
                return;
               
            }

            $result_string = "";

            $result_string.='<?xml version="1.0" encoding="UTF-8”?>';
            $result_string.='<riweb>';
            $result_string.='<type>result</type>';

            $result_string.='<fields>';

            $sepa0 = "";
            foreach ($query_result->list_fields() as $field)
            {
                     $result_string.= $sepa0 . $field;
                     $sepa0 = "#nv";
            }

            $result_string.='</fields>';

             $result_string.='<data>';
             $count1 = $query_result->num_rows();
             $i = 1;
            foreach ($query_result->result_array() as $row) {
                 $arraytemp = array();

                $count2 = count($query_result->list_fields());
                $i2 = 1;
                foreach ($query_result->list_fields() as $field)
                {
                    $result_string.= $row[$field];
                    
                    if($i2 < $count2){
                        $result_string.= "#nv";
                    }

                    $i2++;

                }

                if($i < $count1){
                   $result_string.= "#nl";
                }
                
                $i++;

            }
            $result_string.='</data>';

            $result_string.='</riweb>';

            $data['content_variable'] .= $result_string;

            
            $this->load->view('response/response', $data);

        }else{
              $query_result =  $this->Model_orkin->execute_query_AWS($param);
              $data['content_variable'].= $this->void_xml();
              $this->load->view('response/response', $data);
        }

      

    }

    public function rm_execute_base64_b14_2($typeQuery = null){

        $this->output->enable_profiler(TRUE);

        $data['content_variable'] = "";
        $param;
        
        $param = $this->uid = $this->input->post("query", TRUE);//
        
        $param = base64_decode($param);

        $contains_select =  substr($param, 0, 6);
        
        if (strtoupper($contains_select) == "SELECT") {

      //$data['content_variable'] .= $param;
      


            $query_result =  $this->Model_orkin->execute_query_b14_2($param);

            if($query_result == null){
                $data['content_variable'] .= "null";
                $this->load->view('response/response', $data);
                return;
               
            }

            $result_string = "";

            $result_string.='<?xml version="1.0" encoding="UTF-8”?>';
            $result_string.='<riweb>';
            $result_string.='<type>result</type>';

            $result_string.='<fields>';

            $sepa0 = "";
            foreach ($query_result->list_fields() as $field)
            {
                     $result_string.= $sepa0 . $field;
                     $sepa0 = "#nv";
            }

            $result_string.='</fields>';

             $result_string.='<data>';
             $count1 = $query_result->num_rows();
             $i = 1;
            foreach ($query_result->result_array() as $row) {
                 $arraytemp = array();

                $count2 = count($query_result->list_fields());
                $i2 = 1;
                foreach ($query_result->list_fields() as $field)
                {
                    $result_string.= $row[$field];
                    
                    if($i2 < $count2){
                        $result_string.= "#nv";
                    }

                    $i2++;

                }

                if($i < $count1){
                   $result_string.= "#nl";
                }
                
                $i++;

            }
            $result_string.='</data>';

            $result_string.='</riweb>';

            $data['content_variable'] .= $result_string;

            
            $this->load->view('response/response', $data);

        }else{
              $query_result =  $this->Model_orkin->execute_query_b14_2($param);
              $data['content_variable'].= $this->void_xml();
              $this->load->view('response/response', $data);
        }

      

    }



    public function rm_execute_base64($typeQuery = null){
        $this->output->enable_profiler(TRUE);
        $data['content_variable'] = "";
        $param;
        
        $param = $this->uid = $this->input->post("query");//
        $param =  str_replace("%26","&",$param);
        
        //$param = base64_decode($param);

        $contains_select =  substr($param, 0, 6);
        
        if (strtoupper($contains_select) == "SELECT"){

      //$data['content_variable'] .= $param;

            $query_result =  $this->Model_orkin->execute_query($param);

            if($query_result == null){
                $data['content_variable'] .= "null";
                $this->load->view('response/response', $data);
                return;
               
            }

            $result_string = "";

            $result_string.='<?xml version="1.0" encoding="UTF-8”?>';
            $result_string.='<riweb>';
            $result_string.='<type>result</type>';

            $result_string.='<fields>';

            $sepa0 = "";
            foreach ($query_result->list_fields() as $field)
            {
                     $result_string.= $sepa0 . $field;
                     $sepa0 = "#nv";
            }

            $result_string.='</fields>';

             $result_string.='<data>';
             $count1 = $query_result->num_rows();
             $i = 1;
            foreach ($query_result->result_array() as $row) {
                 $arraytemp = array();

                $count2 = count($query_result->list_fields());
                $i2 = 1;
                foreach ($query_result->list_fields() as $field)
                {
                    $result_string.= $row[$field];
                    
                    if($i2 < $count2){
                        $result_string.= "#nv";
                    }

                    $i2++;

                }

                if($i < $count1){
                   $result_string.= "#nl";
                }
                
                $i++;

            }
            $result_string.='</data>';

            $result_string.='</riweb>';

            $data['content_variable'] .= $result_string;

            
            $this->load->view('response/response', $data);

        }else{

              if(strtoupper($contains_select) == "CALL"){
                $this->Model_orkin->execute_query('use rmscheduler_orkincr_old'); 
                $this->load->view('response/response', $data);
              }else{
        	      $user = "";
                if($this->input->post("user") !== null ){
                  $user = $this->input->post("user");
                }
                $query_result =  $this->Model_orkin->execute_query($param);
                $data['content_variable'].= $this->void_xml();
                $log =  $this->Model_log->insert_log($param,$user);
    	          $this->load->view('response/response', $data);
              }
        }

    }


	public function rm_execute($typeQuery = null){

        $data['content_variable'] = "";
        $param;
        
        if($typeQuery != null){
            $param = $this->uid = $this->input->post("query", TRUE);//
        }else{
            $param = $this->uid = $this->input->get("query", TRUE);//
        }
        
        $param = str_replace("!!!!", "|", $param);
        $param = str_replace("*!!*", "#", $param);
        $param = str_replace("&lt;", "<", $param);
        $param = str_replace("-!*p!-", "+", $param);
        $contains_select =  substr($param, 0, 6);
        
        if(strtoupper($contains_select) == "SELECT"){

      //$data['content_variable'] .= $param;

            $query_result =  $this->Model_orkin->execute_query($param);

            if($query_result == null){
                $data['content_variable'] .= "null";
                $this->load->view('response/response', $data);
                return;
               
            }

            $result_string = "";

            $result_string.='<?xml version="1.0" encoding="UTF-8”?>';
            $result_string.='<riweb>';
            $result_string.='<type>result</type>';

            $result_string.='<fields>';

            $sepa0 = "";
            foreach ($query_result->list_fields() as $field)
            {
                     $result_string.= $sepa0 . $field;
                     $sepa0 = "#nv";
            }

            $result_string.='</fields>';

             $result_string.='<data>';
             $count1 = $query_result->num_rows();
             $i = 1;
            foreach ($query_result->result_array() as $row) {
                 $arraytemp = array();

                $count2 = count($query_result->list_fields());
                $i2 = 1;
                foreach ($query_result->list_fields() as $field)
                {
                    $result_string.= $row[$field];
                    
                    if($i2 < $count2){
                        $result_string.= "#nv";
                    }

                    $i2++;

                }

                if($i < $count1){
                   $result_string.= "#nl";
                }
                
                $i++;

            }
            $result_string.='</data>';

            $result_string.='</riweb>';

            $data['content_variable'] .= $result_string;

            
            $this->load->view('response/response', $data);

        }else{
              $query_result =  $this->Model_orkin->execute_query($param);
              $data['content_variable'].= $this->void_xml();
              $this->load->view('response/response', $data);
        }

      

	}
	
	public function rm_execute_define($typeQuery = null){

        $data['content_variable'] = "";
        $param;
        
        $param = $this->uid = $this->input->post("query", TRUE);//
        
        $param = base64_decode($param);

        $contains_select =  substr($param, 0, 6);
        
        if (strtoupper($contains_select) == "SELECT"){

      //$data['content_variable'] .= $param;

            $query_result =  $this->Model_orkin->execute_query($param);

            if($query_result == null){
                $data['content_variable'] .= "null";
                $this->load->view('response/response', $data);
                return;
               
            }

            $result_string = "";

            $result_string.='<?xml version="1.0" encoding="UTF-8”?>';
            $result_string.='<riweb>';
            $result_string.='<type>result</type>';

            $result_string.='<fields>';

            $sepa0 = "";
            foreach ($query_result->list_fields() as $field)
            {
                     $result_string.= $sepa0 . $field;
                     $sepa0 = "#nv";
            }

            $result_string.='</fields>';

             $result_string.='<data>';
             $count1 = $query_result->num_rows();
             $i = 1;
            foreach ($query_result->result_array() as $row) {
                 $arraytemp = array();

                $count2 = count($query_result->list_fields());
                $i2 = 1;
                foreach ($query_result->list_fields() as $field)
                {
                    $result_string.= $row[$field];
                    
                    if($i2 < $count2){
                        $result_string.= "#nv";
                    }

                    $i2++;

                }

                if($i < $count1){
                   $result_string.= "#nl";
                }
                
                $i++;

            }
            $result_string.='</data>';

            $result_string.='</riweb>';

            $data['content_variable'] .= $result_string;

            
            $this->load->view('response/response', $data);

        }else{

              if(strtoupper($contains_select) == "CALL"){
                $this->Model_orkin->execute_query('use rmscheduler_orkincr_old'); 
                $this->load->view('response/response', $data);
              }else{
        	      $query_result =  $this->Model_orkin->execute_query($param);
	              $data['content_variable'].= $this->void_xml();
    	          $this->load->view('response/response', $data);
              }
        }

    }


    public function void_xml(){
        $xml_result = "";

        $xml_result.='<?xml version="1.0" encoding="UTF-8”?>';
        $xml_result.='<riweb>';
        $xml_result.='<type>result</type>';
        $xml_result.='<fields>';
        $xml_result.='<data>';
        $xml_result.='</data>';
        $xml_result.='</fields>';
        $xml_result.='</riweb>';

        return $xml_result;

    }


}

?>
