<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "action") 
    $action = $paramValue;  
  else if ($paramName == "rowid") 
    $cp_id = $paramValue;  
  else if ($paramName == "selid") 
    $pointtype_id = $paramValue;  
  else if ($paramName == "currid") 
    $cu_id = $paramValue;  
  else if ($paramName == "text") 
    $pos = $paramValue;  
}

$dal = new DAL();

if ($action == "delete") {
  $sql = "DELETE FROM points WHERE id = " .
         "(SELECT points_id FROM culturalunitpoints WHERE id = $cp_id) ";
  $result = $dal->query($sql);

  $sql = "DELETE FROM culturalunitpoints WHERE id = $cp_id ";
  $result = $dal->query($sql);
}  

else if ($cp_id == 0 ) {

  $po_id = hexdec(uniqid());
         
  $sql = "INSERT INTO points (id, pos) " .
         "VALUES ($po_id, '$pos') ";
  $po_id = $dal->query($sql);

  $cp_id = hexdec(uniqid());

  $sql = "INSERT INTO culturalunitpoints (id, culturalunit_id, points_id, pointtype_id) " .
         "VALUES ($cp_id, $cu_id, $po_id, $pointtype_id) ";
  $cp_id = $dal->query($sql);
  
  }
else {
  $sql = "UPDATE points SET pos='$pos' WHERE id = " .
         "(SELECT points_id FROM culturalunitpoints WHERE id = $cp_id) ";
  $result = $dal->query($sql);

  $sql = "UPDATE culturalunitpoints SET pointtype_id=$pointtype_id " .
         "WHERE id = $cp_id ";
  $result = $dal->query($sql);
}
    
echo json_encode($cp_id);

?>

