<?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){
    $table = $_GET['table'];
    $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()."'";
     $res = db ();
   	 $result = $res->query($sql);
     if($result){
     	$action->success();
     }else{
     	LogMaster::log($sql);
     	LogMaster::log($res->error);
     	$action->invalid();
     }
 };
 
 function myDelete($action){
    $table = $_GET['table'];
    $sql = "DELETE FROM ".$table." WHERE id='".$action->get_id()."'";
    $res = db ();
   	$result = $res->query($sql);
     if($result){
     	$action->success();
     }else{
     	LogMaster::log($sql);
     	LogMaster::log($res->error);
     	$action->invalid();
     }
 };
 
 function db () {
    static $conn;
    if ($conn===NULL){ 
        $conn = new mysqli('localhost','orkindb','rmschpsw1','rmscheduler_orkincr'); 
        $conn->set_charset("utf8");
    }
    return $conn;
}
 
	

?>