<?php
require("codebase/connector/scheduler_connector.php");
require("codebase/connector/db_mysqli.php");
 

 if(isset($_GET['table'])){
  $table = $_GET['table'];
  date_default_timezone_set("America/Costa_Rica");
  $res = db ();
  if (!$res){
      echo "NO ESTOY CONECTADO"; 
    }

  $scheduler = new SchedulerConnector($res,"MySQLi");
  $scheduler->enable_log("logDataBaseScheduler.log",true);
  $scheduler->event->attach("beforeUpdate","myUpdate");
  $scheduler->event->attach("beforeDelete","myDelete");
  $scheduler->render_table($table,"id","start_date,end_date,text,calendar,personnel,location,customer,vehicle,color,eventComm,serieComm");
  }else{
  echo "Ocurrio un problema con la tabla temporal en la base de datos."; 
  die();
  }
  
  function doOnDBError($action, $exception){
    $action->set_response_xml("<errorinfo>".((String)$exception)."</errorinfo>");
  };
 
  function myUpdate($action){
    $user = $_GET['user'];
    LogMaster::log($user);
    $table = $_GET['eventbl'];
    
    $sql = "UPDATE ".$table." SET
          start_date='".$action->get_value('start_date')."' ,
          end_date='".$action->get_value('end_date')."' ,
          text='".$action->get_value('text')."' ,
          calendar='".$action->get_value('calendar')."' ,
          personnel='".$action->get_value('personnel')."' ,
          location='".$action->get_value('location')."' ,
          customer='".$action->get_value('customer')."' ,
          vehicle='".$action->get_value('vehicle')."' ,
          color='".$action->get_value('color')."' ,
          eventComm='".$action->get_value('eventComm')."' ,
          serieComm='".$action->get_value('serieComm')."' 
                WHERE id='".$action->get_id()."';";
    
    
    $sql = "UPDATE rmscheduler_orkincr_old.".$table." SET
          start_date='".$action->get_value('start_date')."' ,
          end_date='".$action->get_value('end_date')."' ,
          text='".$action->get_value('text')."' ,
          calendar='".$action->get_value('calendarNew')."' ,
          personnel='".$action->get_value('personnelNew')."' ,
          location='".$action->get_value('location')."' ,
          customer='".$action->get_value('customer')."' ,
          vehicle='".$action->get_value('vehicleNew')."' ,
          eventComm='".$action->get_value('eventCommNew')."' ,
          serieComm='".$action->get_value('serieCommNew')."' 
                WHERE id='".$action->get_id()."'";
      


     $oldValues = " Old Values : start_date='".$action->get_value('start_date_old')."' ,
          end_date='".$action->get_value('end_date_old')."' ,
          text='".$action->get_value('text_old')."' ,
          calendar='".$action->get_value('calendar')."' ,
          personnel='".$action->get_value('personnel')."' ,
          location='".$action->get_value('location')."' ,
          customer='".$action->get_value('customer')."' ,
          vehicle='".$action->get_value('vehicle')."' ,
          eventComm='".$action->get_value('eventComm')."' ,
          serieComm='".$action->get_value('serieComm');

     LogMaster::log($oldValues);            


     $res = db ();
     $result = $res->query($sql);


     if($res->affected_rows > 0){

       $serie = "";

       $sql1 = "Select seriesId from rmscheduler_orkincr_old.eventsPermanent where id='".$action->get_id()." limit 1';";
       $res1 = db ();
       $result1 = $res1->query($sql1);
        while($row = $result1->fetch_assoc()){
          $serie = $row['seriesId'];
        }

       $sql2 = "Update rmscheduler_orkincr_old.eventsPermanent set serieComm='".$action->get_value('serieComm')."' 
       where seriesId = ".$serie.";";
       $res2 = db ();
       $result2 = $res2->query($sql2);

      LogMaster::log($sql);
      LogMaster::log($sql1);
      LogMaster::log($sql2);
      LogMaster::log("Success!");
      $action->success();
     }else{
        LogMaster::log($sql);
        if($res->error == ""){
          LogMaster::log("No hubo actualizacion.");
          $action->set_status("samevalues");
        }else{
          
          LogMaster::log($res->error);
          $action->error();
        }
     }
 };
 
 function myDelete($action){
    $user = $_GET['user'];
    LogMaster::log($user);
    $table = $_GET['eventbl'];
    $sql = "DELETE FROM ".$table." WHERE id='".$action->get_id()."'";
    $res = db ();
    $result = $res->query($sql);
    if($res->affected_rows > 0){
      LogMaster::log($sql);
      LogMaster::log("Success!");
      $action->success();
     }else{
        LogMaster::log($sql);
        if($res->error == ""){
          LogMaster::log("No se encontro el evento a borrar.");
          $action->set_status("notfound");
        }else{
          LogMaster::log($res->error);
          $action->error();
        }
     }
 };
 
 function db () {
    static $conn;
    if ($conn===NULL){ 
        $conn = new mysqli('localhost','orkindb','rmschpsw1','rmscheduler_orkincr_old'); 
        $conn->set_charset("utf8");
    }
    return $conn;
}
 
  

?>
