<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "selectid") 
    $su_id = $paramValue;  
  else if ($paramName == "ids") 
    $ids = $paramValue;  
  else if ($paramName == "selvals") 
    $selects = $paramValue;  
  else if ($paramName == "inpvals") 
    $inputs = $paramValue;  
}

$bo_id =  explode (",", $ids);
$cardid = explode (",", $selects);
$descr =  explode (",", $inputs);

$dal = new DAL();

for ($i=0; $i<count($bo_id); $i++) {
    
  if ($bo_id[$i] == 0) {

    $bo_id = hexdec(uniqid());

    $sql = "INSERT INTO bound (id, cardinal_id, descr, part_id) " .
           "VALUES ($bo_id, '$cardid[$i]', '$descr[$i]', " .
           "(SELECT id FROM part WHERE spatialunit_id = $su_id)) ";
    $result = $dal->query($sql);
  }

  else if ($cardid[$i] == -1) {
    $sql = "DELETE FROM bound WHERE id = $bo_id[$i] ";
    $result = $dal->query($sql);
  }
  
  else {
    $sql = "UPDATE bound SET cardinal_id=$cardid[$i], descr='$descr[$i]' " .
           "WHERE id = $bo_id[$i] ";
    $result = $dal->query($sql);
  }    
}
    
echo json_encode($su_id);

?>

