<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "id") 
    $su_id = $paramValue;  
  else if ($paramName == "projectparcel_fk") 
    $pp_id = $paramValue;  
  else if ($paramName == "name") 
    $name = $paramValue;  
  else if ($paramName == "location") 
    $location = $paramValue;  
  else if ($paramName == "longaxis") 
    $longaxis = $paramValue;  
  else if ($paramName == "shortaxis") 
    $shortaxis = $paramValue;  
  else if ($paramName == "location") 
    $location = $paramValue;  
  else if ($paramName == "longdirection_id") 
    $longdirection_id = $paramValue;  
  else if ($paramName == "mapareaid") 
    $mapareaid = $paramValue;  
  else if ($paramName == "topography_id") 
    $topography_id = $paramValue;  
  else if ($paramName == "landuse_id") 
    $landuse_id = $paramValue;  
  else if ($paramName == "geomorphology_id") 
    $geomorphology_id = $paramValue;  
  else if ($paramName == "namedfeature_id") 
    $namedfeature_id = $paramValue;  
  else if ($paramName == "descr") 
    $descr = $paramValue;  
}

$dal = new DAL();

if ($su_id == 0) {         
  $su_id = hexdec(uniqid());
  $sql = "INSERT INTO spatialunit " .
         "(id, spatialunitype_id, name) " .
         "VALUES ($su_id, 1, '$name') ";
  $result = $dal->query($sql);

  $p_id = hexdec(uniqid());
  $sql = "INSERT INTO part (id, spatialunit_id) VALUES ($p_id, $su_id) ";
  $result = $dal->query($sql);

  $ps_id = hexdec(uniqid());
  $sql = "INSERT INTO projectparcelspatialunit " .
         "(id, spatialunit_id, projectparcel_id) " .
         "VALUES ($ps_id, $su_id, $pp_id) ";
  $result = $dal->query($sql);
}

else {
  $sql = "UPDATE spatialunit ". 
         "SET longaxis='$longaxis', shortaxis='$shortaxis', " .
         "mapareaid='$mapareaid', longdirection_id='$longdirection_id' " .
         "WHERE id = $su_id " ;
  $result = $dal->query($sql);

  $sql = "UPDATE part ". 
         "SET location='$location', topography_id=$topography_id, " .
         "landuse_id=$landuse_id, geomorphology_id=$geomorphology_id, " .
         "namedfeature_id=$namedfeature_id, descr='$descr' " .
         "WHERE spatialunit_id = $su_id ";
  $result = $dal->query($sql);
}

echo json_encode($su_id);

?>

