<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "action") 
    $action = $paramValue;  
  else if ($paramName == "rowid") 
    $pg_id = $paramValue;  
  else if ($paramName == "selid") 
    $geomorphology_id = $paramValue;  
  else if ($paramName == "currid") 
    $su_id = $paramValue;  
  else if ($paramName == "text") 
    $name = $paramValue;  
}

$dal = new DAL();

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

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

  $sql = "INSERT INTO partgeomorpholgy (id, geomorphology_id, part_id) " .
         "VALUES ($pg_id, $geomorphology_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 partgeomorpholgy SET geomorphology_id=$geomorphology_id, name='$name' " .
         "WHERE id = $pg_id ";
  $result = $dal->query($sql);
}
    
echo json_encode($pg_id);

?>

