<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "fkeyid") 
    $op_id = $paramValue;  
  else if ($paramName == "string") 
    $name = $paramValue;  
  else if ($paramName == "selectid") 
    $su_id = $paramValue;  
}

$dal = new DAL();
         
if ($su_id == 0) {         
  $su_id = hexdec(uniqid());

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

  $p_id = hexdec(uniqid());

  $sql = "INSERT INTO part (id, spatialunit_id) VALUES ($p_id, $su_id) ";
  $result = $dal->query($sql);

  $os_id = hexdec(uniqid());

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

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

?>

