<?php

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

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

$dal = new DAL();

$sql = "SELECT PR.id, PR.refno AS RefNo, PR.name AS 'Project Name', " .
       "PR.pif AS PIF, ST.descr AS Stage, CO.surname AS Contact, " .
       "OZ.name AS Organization, DE.descr AS Department " .
       "FROM (organizationcontact OC, contact CO, organization OZ, ".
       "projectorganizationcontact POC, project PR) " .
       "LEFT JOIN (stage ST, department DE) " .
       "ON ST.id = PR.stage_id AND DE.id = PR.department_id " ;

if ($source == 'sosClientContact')
  $sql .= "WHERE OC.contact_id = $key_id " .
          "AND CO.id = OC.contact_id AND OZ.id = OC.organization_id " .
          "AND POC.organizationcontact_id = OC.id AND PR.id = POC.project_id " ; 

else if ($source == 'livClientProject')
  $sql .= "WHERE PR.id = $key_id AND POC.project_id = PR.id " .
          "AND OC.id = POC.organizationcontact_id " .
          "AND OZ.id = OC.organization_id AND CO.id = OC.contact_id " ;

else if ($source == 'sosClientOrganization')
  $sql .= "WHERE OZ.id = $key_id " .
          "AND OC.organization_id = OZ.id AND CO.id = OC.contact_id " .
          "AND POC.organizationcontact_id = OC.id AND PR.id = POC.project_id " ;
else 
  $sql .= "WHERE 0 " ;

$result = $dal->query($sql);

echo json_encode($result);

?>
