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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "sequenceid")
    $pa_id = $paramValue;
}

$dal = new DAL();

$sql = "SELECT CU.name_seq " .
       "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.spatialunittype_id = 1 AND SS.operationspatialunit_id  = OS.id " .
       "AND CU.id = SS.culturalunit_id " .
       "ORDER BY name_seq DESC LIMIT 1 " ;
$result = $dal->query($sql);

if (null == $result) {
  $next_seq = 1;
}  
else {
  $next_seq = $result->name_seq + 1;
}

$sql = "SELECT PR.site_name " .
       "FROM project PR, parcel PA " .
       "WHERE PA.id = $pa_id AND PR.id = PA.project_id LIMIT 1 " ;
$next_site_name = $dal->query($sql)->site_name . " " . $next_seq;

echo json_encode($next_site_name);

?>


