<?php
session_start();

if (isset($_SESSION['spatialunit'])) 
  $su_id = $_SESSION['spatialunit'];
else
  $su_id = 0;

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "baseid")
    $pa_id = $paramValue;
  else if ($paramName == "selid")
    $remove_id = $paramValue;
    
}
  
$dal = new DAL();

$sql = "SELECT LO.id, LO.name as text,
        CASE WHEN ST.relationship_id = null THEN 0 ELSE ST.relationship_id END as role_id " .
       "FROM (projectworkday PW, projectworkdayoperation PWO, operationspatialunit OS, " .
       "spatialunit SU, spatialunitlot SL, lot LO) " .
       "LEFT JOIN stratigraphy ST ON  ST.spatialunitlot_id = SL.id " .
       "WHERE PW.parcel_id = $pa_id AND PWO.projectworkday_id = PW.id " .
       "AND OS.projectworkdayoperation_id = PWO.id AND SU.id = OS.spatialunit_id " .
       "AND SU.spatialunittype_id = 1 AND SL.operationspatialunit_id = OS.id " .
       "AND LO.id = SL.lot_id AND LO.id <> $remove_id " ;
$result = $dal->query($sql);

echo json_encode($result);

?>

