<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "selectid") 
    $os_id = $paramValue;  
  else if ($paramName == "ids") 
    $ids = $paramValue;  
  else if ($paramName == "selects") 
    $selects = $paramValue;  
  else if ($paramName == "inputs") 
    $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 P.id FROM operationspatialunit OS, spatialunit SU, part P " .
           "WHERE OS.id = $os_id AND SU.id = OS.spatialunit_id " .
           "AND P.spatialunit_id = SU.id)) ";
           
           "(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);

?>

