<?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'); 
                $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'); 
                $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_alter($param);
                $data['content_variable'].= $this->void_xml($query_result);
                $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'); 
                $this->load->view('response/response', $data);
              }else{
        	      $query_result =  $this->Model_orkin->execute_query_alter($param);
	              $data['content_variable'].= $this->void_xml($query_result);
                echo $param."<br>";
                var_dump($query_result);
                echo "<br>".$this->void_xml($query_result)."<br>";
    	          $this->load->view('response/response', $data);
              }
        }

    }


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

        $xml_result.='<?xml version="1.0" encoding="UTF-8”?>';
        $xml_result.='<riweb>';
        $xml_result.='<type>result</type>';
        if(is_null($id)){
            $xml_result.='<fields>';
            $xml_result.='</fields>';
            $xml_result.='<data>';
            $xml_result.='</data>';
        }else{
          $xml_result.='<fields>id</fields>';
          $xml_result.='<data>'.$id.'</data>';
        }

        $xml_result.='</riweb>';

        return $xml_result;

    }


         /**
     ** Funcion que se encargar de ingresar un reporte en el sistema, en su lugar correspondiente.
     **/
    public function getData_b14_2(){
            $query = $this->input->post('query');

            $query = str_replace("%26","&",$query);
            $result = $this->Model_orkin->execute_query_b14_2($query);
            if(!is_null($result)){
                if($result){
                    $sendResult = array();
                    if(strpos(strtolower($query), 'select') !== false){
                        $sendResult = $result->result();
                    }
                    $data = array(
                        'data' => $sendResult,
                        'status' => 1
                    ); 
                }else{
                    $data = array(
                        'data' => array(),
                        'status' => 1
                    ); 
                }
            }else{
                $data = array(
                    'status' => 0
                ); 
            }
            echo json_encode($data, JSON_PRETTY_PRINT);
    }

    /**
     ** Funcion que se encargar de ingresar un reporte en el sistema, en su lugar correspondiente.
     **/
    public function getData(){

            
            $query = $this->input->post('query');
            $query = str_replace("#sc#","-",$query);
            //$query = str_replace("-","%2D",$query);
            $query = str_replace("#","%23",$query);
            //var_dump($query);
            $result = $this->Model_orkin->execute_query_2($query);
            if(!is_null($result)){
                if($result){
                    $sendResult = array();
                    if(strpos(strtolower($query), 'select') !== false){
                        $sendResult = $result->result();
                    }
                    $data = array(
                        'data' => $sendResult,
                        'status' => 1
                    ); 
                }else{
                    $data = array(
                        'data' => array(),
                        'status' => 1
                    ); 
                }
            }else{
                $data = array(
                    'status' => 0
                ); 
            }
            echo json_encode($data, JSON_PRETTY_PRINT);
    }



}

?>
