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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "summaryid")
    $id = $paramValue;
  else if ($paramName == "index")
    $index = $paramValue;
}    

if ($index == 'fkyProjectParcel') {
  $where = "PS.projectparcel_id = $id AND SU.id = PS.spatialunit_id " .
       "AND CS.spatialunit_id = SU.id";
  $table = "projectparcelspatialunit PS, culturalunitspatialunit CS" ;
}

else if ($index == 'fkyOperation') {
  $where = "POS.projectworkdayoperation_id = $id " .
           "AND SU.id = POS.spatialunit_id" ;
  $table = "projectworkdayoperationspatialunit POS" ;
}

else if ($index == 'fkyPropertyPart') {
  $where = "CS.spatialunit_id = $id";
  $table = "culturalunitspatialunit CS" ;
}

$dal = new DAL();
      
$sql = "SELECT CU.id, CU.name AS 'Site Name', SU.name AS 'Property Part', " .
       "(CASE " .
       "  WHEN LC.id IS NOT NULL THEN 'Shovel-testing survey' " .
       "  WHEN SI.id IS NOT NULL THEN 'Pedestrian survey' " .
       "  ELSE 'Not Defined' END) AS Method, " .
       "CU.descr AS Description " .
//       "CU.descr AS Description, IP.descr AS Interpretation " .
       "FROM ($table, spatialunit SU, culturalunit CU) " .
//       "LEFT JOIN interpret IP ON IP.id = CU.interpret_id " .
       "LEFT JOIN lotculturalunit LC ON LC.culturalunit_id = CU.id " .
       "LEFT JOIN siteintensification SI ON SI.culturalunit_id = CU.id " .
       "WHERE $where AND CU.id = CS.culturalunit_id AND SU.id = CS.spatialunit_id " .
       "ORDER BY SU.name, CU.name ";
$result = $dal->query($sql);

echo json_encode($result);

?>

