<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "selid") 
    $pr_id = $paramValue;  
  else if ($paramName == "inpids") 
    $inpids = $paramValue;  
  else if ($paramName == "addids") 
    $addids = $paramValue;  
}

$oc_id = explode (",", $inpids);
$addid = explode (",", $addids);

$dal = new DAL();

for ($i=0; $i<count($oc_id); $i++) {

  if ($addid[$i] == -1) {

    $sql = "DELETE FROM projectorganizationcontact " .
           "WHERE project_id = $pr_id AND organizationcontact_id = $oc_id[$i] ";
    $result = $dal->query($sql);
  }
    
  else {

    $sql = "SELECT id FROM projectorganizationcontact " .
           "WHERE project_id = $pr_id AND organizationcontact_id = $oc_id[$i] ";
    $result = $dal->query($sql);

    if (count ($result) > 0) {

      $sql = "UPDATE projectorganizationcontact " .
             "SET contactype_id=$addid[$i] " .
             "WHERE project_id = $pr_id AND " .
             "organizationcontact_id = $oc_id[$i] ";
      $result = $dal->query($sql);
    }
    else {
      $poc_id = hexdec(uniqid());

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

}
    
echo json_encode($pr_id);

?>

