    <?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "id") 
    $su_id = $paramValue;  
  else if ($paramName == "culturalunit_fk") 
    $cu_id = $paramValue;  
  else if ($paramName == "pwoperation_fk") 
    $pwo_id = $paramValue;  
  else if ($paramName == "id") 
    $su_id = $paramValue;  
  else if ($paramName == "name") 
    $name = $paramValue;  
  else if ($paramName == "mapareaid") 
    $mapareaid = $paramValue;  
}

$dal = new DAL();

if ($su_id == 0) {
    
  $su_id = hexdec(uniqid());

  $sql = "INSERT INTO spatialunit " .
         "(id, name, spatialunittype_id, mapareaid) " .
         "VALUES ($su_id, '$name', 5, '$mapareaid')";
  $result = $dal->query($sql);

  $pos_id = hexdec(uniqid());

  $sql = "INSERT INTO projectworkdayoperationspatialunit " .
         "(id, spatialunit_id, projectworkdayoperation_id) " .
         "VALUES ($pos_id, $su_id, $pwo_id) ";
  $result = $dal->query($sql);

  $cs_id = hexdec(uniqid());

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

else {
  $sql = "UPDATE spatialunit ". 
         "SET name='$name', mapareaid='$mapareaid' " .
         "WHERE id = $su_id ";
  $result = $dal->query($sql);
}
    
echo json_encode($su_id);

?>

