<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "action") 
    $action = $paramValue;  
  else if ($paramName == "rowid") 
    $bo_id = $paramValue;  
  else if ($paramName == "selid") 
    $cardinal_id = $paramValue;  
  else if ($paramName == "currid") 
    $su_id = $paramValue;  
  else if ($paramName == "text") 
    $descr = $paramValue;  
}

$dal = new DAL();

if ($action == "delete") {
  $sql = "DELETE FROM bound WHERE id = $bo_id ";
  $result = $dal->query($sql);
}  

else if ($bo_id == 0 ) {
         
  $bo_id = hexdec(uniqid());

  $sql = "INSERT INTO bound (id, cardinal_id, part_id) " .
         "VALUES ($bo_id, $cardinal_id, (SELECT P.id FROM spatialunit SU, part P " . 
         "WHERE SU.id = $su_id AND P.spatialunit_id = SU.id)) ";
  $result = $dal->query($sql);
}
else {
  $sql = "UPDATE bound SET cardinal_id=$cardinal_id, descr='$descr' " .
         "WHERE id = $bo_id ";
  $result = $dal->query($sql);
}
    
echo json_encode($bo_id);

?>

