    <?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "id")
    $sl_id = $paramValue;
  elseif ($paramName == "lot_id") 
    $lo_id = $paramValue;  
  elseif ($paramName == "projectparcel_fk") 
    $pp_id = $paramValue;  
  elseif ($paramName == "sitedefinition_fk") 
    $cu_id = $paramValue;  
  elseif ($paramName == "dailyoperation_fk") 
    $pwo_id = $paramValue;  
  elseif ($paramName == "interval_id") 
    $interval_id = $paramValue;  
  elseif ($paramName == "expansion_id") 
    $expansion_id = $paramValue;  
  elseif ($paramName == "observation_id") 
    $observation_id = $paramValue;  
  elseif ($paramName == "collection_id") 
    $collection_id = $paramValue;  
  elseif ($paramName == "station_id") 
    $station_id = $paramValue;  
  elseif ($paramName == "note") 
    $note = $paramValue;  
}

$dal = new DAL();

if ($sl_id == 0) {

  $su_id = hexdec(uniqid());
  $sql = "INSERT INTO spatialunit " .
         "(id, spatialunitype_id) VALUES ($su_id, 8) " ;
  $result = $dal->query($sql);
  
  $ps_id = hexdec(uniqid());
  $sql = "INSERT INTO projectparcelspatialunit " .
         "(id, spatialunit_id, projectparcel_id) " .
         "VALUES ($ps_id, $su_id, $pp_id) " ;
  $result = $dal->query($sql);


  $cs_id = hexdec(uniqid());
  $sql = "INSERT INTO culturalunitspatialunit " .
         "(id, culturalunit_id, spatialunit_id, projectworkdayoperation_id) " .
         "VALUES ($cs_id, $cu_id, $su_id, $pwo_id) ";
  $result = $dal->query($sql);

  $sl_id = hexdec(uniqid());
  $sql = "INSERT INTO spatialunitlot " .
         "(id, spatialunit_id, lot_id, projectworkdayoperation_id) " .
         "VALUES ($sl_id, $su_id, $lo_id, $pwo_id) ";
  $result = $dal->query($sql);

  $lc_id = hexdec(uniqid());
  $sql = "INSERT INTO lotculturalunit " .
         "(id, spatialunitlot_id, culturalunit_id) " .
         "VALUES ($lc_id, $sl_id, $cu_id) ";
  $result = $dal->query($sql);

  $pi_id = hexdec(uniqid());
  $sql = "INSERT INTO pedestrianintensification " .
         "(id, culturalunit_id) VALUES ($pi_id, $cu_id)";
  $result = $dal->query($sql);
  
//  $po_id = hexdec(uniqid());
//  $sql = "INSERT INTO points " .
//         "(id) VALUES ($po_id)";
//  $result = $dal->query($sql);

//  $sp_id = hexdec(uniqid());
//  $sql = "INSERT INTO spatialunitpoints " .
//         "(id, spatialunit_id, points_id) VALUES ($sp_id, $su_id, $po_id)";
//  $result = $dal->query($sql);
 
}

else {        

  $sql = "UPDATE pedestrianintensification SET " .
         "interval_id=$interval_id, expansion_id=$expansion_id, ".
         "observation_id=$observation_id, collection_id=$collection_id, " .
         "station_id=$station_id, note=$note " .  
         "WHERE id = $cu_id " ;
  $result = $dal->query($sql);
  
}

echo json_encode($sl);

?>

