<?php
session_start();

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "summaryid")
    $summaryId = $paramValue;
  if ($paramName == "selectid")
    $selectId = $paramValue;
  else if ($paramName == "field")
    $field = $paramValue;
}


if ($field == "fkyProjectParcel") {

  if ($summaryId == 0) {
    $_SESSION['projectparcel'] = $selectId;
    $_SESSION['sitedefinition'] = 0;
  }
  
  else {
    $dal = new DAL();
    $sql = "SELECT PP.id, CONCAT (PR.name, ': ',  PA.name) AS text " .
           "FROM projectparcel PP, parcel PA, project PR " .
           "WHERE PP.id = $summaryId AND PA.id = PP.parcel_id " .
           "AND PR.id = PP.project_id ORDER BY text ";
    $result = $dal->query($sql);
  }    
}         

else if ($field == "fkyPropertyPart") {

  if ($summaryId == 0) {
   $_SESSION['propertypart'] = $selectId;
   $_SESSION['sitedefinition'] = 0;
   
  }

  else {
    $dal = new DAL();
    $sql = "SELECT SU.id, SU.name AS text " .
           "FROM projectparcelspatialunit PS, spatialunit SU " .
           "WHERE PS.projectparcel_id = $summaryId AND SU.id = PS.spatialunit_id ";
    $result = $dal->query($sql);
  }
}         

else if ($field == "fkyDailyOperation") {

  if ($summaryId == 0) {
    $_SESSION['dailyoperation'] = $selectId;
   $_SESSION['sitedefinition'] = 0;
  }

  else {
    $dal = new DAL();
    $sql = "SELECT PWO.id, OP.name AS text " .
           "FROM projectworkevent PW, projectworkdayoperation PWO, operation OP " .
           "WHERE PW.projectparcel_id = $summaryId AND PWO.projectworkevent_id = PW.id " .
           "AND OP.id = PWO.operation_id "; 
    $result = $dal->query($sql);
  }    
}         

echo json_encode ($result);

?>

