<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');
 
foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "id") 
    $pr_id = $paramValue; 
  else if ($paramName == "contact_id") 
    $contact_id = $paramValue; 
  else if ($paramName == "organization_id") 
    $organization_id = $paramValue; 
  else if ($paramName == "name") 
    $name = $paramValue; 
  else if ($paramName == "site_name") 
    $site_name = $paramValue; 
  else if ($paramName == "development_refno") 
    $development_refno = $paramValue; 
  else if ($paramName == "department_id") 
    $department_id = $paramValue; 
  else if ($paramName == "refno") 
    $refno = $paramValue; 
  else if ($paramName == "licence_no") 
    $licence_no = $paramValue; 
  else if ($paramName == "stage_id") 
    $stage_id = $paramValue; 
}

$dal = new DAL();

if ($pr_id == 0) {

$sql = "SELECT id FROM organizationcontact " . 
       "WHERE organization_id = $organization_id " .
       "AND contact_id = $contact_id LIMIT 1 " ;
$oc_id = $dal->query($sql)->id;

$sql = "INSERT INTO project " .
       "(id, name, site_name, development_refno, department_id, refno, " .
         "licence_no, stage_id) " . 
         "VALUES ($pr_id, '$name', '$site_name', '$development_refno', " .
         "$department_id, '$refno', '$licence_no', $stage_id) ";
  $result = $dal->query($sql)->id;

  $pr_id = hexdec(uniqid());

  $poc_id = hexdec(uniqid());

  $sql = "INSERT INTO projectorganizationcontact " .
         "(id, organizationcontact_id, project_id) " . 
         "VALUES ($poc_id, $oc_id, $pr_id) ";
  $result = $dal->query($sql)->id;
}

else {
  $sql = "UPDATE project " .
         "SET site_name='$site_name', " .
         "development_refno='$development_refno', " .
         "department_id=$department_id, refno='$refno', " .
         "licence_no='$licence_no', stage_id=$stage_id " . 
         "WHERE id = $pr_id ";
  $result = $dal->query($sql)->id;
    
}

echo json_encode($result);

?>

