<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "sumid")
    $id = $paramValue;
  else if ($paramName == "fkey")
    $keyName = $paramValue;
}

$dal = new DAL();

if ($keyName == "parcel_fk") {

  $sql = "SELECT CU.id, CU.name AS text " .
         "FROM spatialunit SU, operationspatialunit OS, " .
         "sitespatialunit SS, culturalunit CU " .
         "WHERE SU.parentunit = $id AND SU.spatialunittype_id = 1 " .
         "AND OS.spatialunit_id = SU.id AND SS.operationspatialunit_id = OS.id " .
         "AND CU.id = SS.culturalunit_id ORDER BY CU.name ";
}         

else if ($keyName == "site_fk") {

  $sql = "SELECT SU.id, SU.name AS text " .
         "FROM sitespatialunit SS, operationspatialunit OS, spatialunit SU " .
         "WHERE SS.culturalunit_id = $id " .
         "AND OS.id = SS.operationspatialunit_id AND SU.id = OS.spatialunit_id " .
         "AND SU.spatialunittype_id = 5 "; 
}         

else if ($keyName == "ptp_fk") {

$sql = "SELECT SL.id, LO.name AS text " .
       "FROM operationspatialunit OS, spatialunitlot SL, lot LO " .
       "WHERE OS.spatialunit_id = $id AND SL.operationspatialunit_id = OS.id " .
       "AND LO.id = SL.lot_id ";
}         
         
$result = $dal->query($sql);

echo json_encode ($result);

?>

