<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "scopeid") 
    $pwo_id = $paramValue;  
  else if ($paramName == "currid") 
    $cu_id = $paramValue;  
  else if ($paramName == "id") 
    $su_id = $paramValue;  
  else if ($paramName == "ptp_name") 
    $ptp_name = $paramValue;  
  else if ($paramName == "ptp_location") 
    $ptp_location = $paramValue;  
}

$dal = new DAL();

if ($su_id == 0) {

  $su_id = hexdec(uniqid());

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

  $os_id = hexdec(uniqid());

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

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

?>

