<?php

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

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

if ($index == '001') 
  $pocCondition = "POC.id = $summary_id ";
if ($index == '010')
  $pocCondition = "POC.organizationcontact_id = $summary_id ";
if ($index == '100')
  $pocCondition = "POC.project_id = $summary_id ";

$dal = new DAL();

$sql = "SELECT POC.id, PR.refno AS RefNo, PR.name AS 'Project Name', " .
       "(CASE WHEN SG.id IS NULL THEN 'Not Set' ELSE SG.descr END) AS Stage, " . 
       "OZ.name, CT.descr, " .
       "(CASE WHEN " .
         "(LENGTH(CO.surname) < 10 && LENGTH(CO.firstname) < 10) " . 
           "THEN CONCAT(CO.surname,', ',CO.firstname) " . 
           "ELSE CONCAT(CO.surname,', ',SUBSTRING(CO.firstname,1,1)) " .
       "END) AS 'name' " . 
       "FROM (projectorganizationcontact POC, project PR, " .
       "organizationcontact OC, organization OZ, contact CO, contactype CT) " .
       "LEFT JOIN stage SG ON SG.id = PR.stage_id " .
       "WHERE $pocCondition " .
       "AND PR.id = POC.project_id " .
       "AND OC.id = POC.organizationcontact_id AND CT.id = POC.contactype_id " .
       "AND OZ.id = OC.organization_id AND CO.id = OC.contact_id " .
       "ORDER BY PR.name ";
$result = $dal->query($sql);

echo json_encode($result);

?>

