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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "scopeid")
    $pwo_id = $paramValue;
  else if ($paramName == "baseid")
    $pa_id = $paramValue;
  else if ($paramName == "currid")
    $os_id = $paramValue;
}

$dal = new DAL();

$sql = "SELECT * FROM (SELECT CU.id, CU.name AS text, SS.operationspatialunit_id, " .
       "CASE WHEN OS.id = $os_id THEN 'disabled' ELSE '' END AS attribute " .
       "FROM projectworkday PW, projectworkdayoperation PWO, operationspatialunit OS, " .
       "spatialunit SU, sitespatialunit SS, culturalunit CU " .
       "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.spatialunttype_id = 1 AND SS.operationspatialunit_id  = OS.id " .
       "AND CU.id = SS.culturalunit_id " .
       "ORDER BY text, attribute DESC) " .
       "AS TT GROUP BY text " ;
$result = $dal->query($sql);

echo json_encode($result);

?>

