<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');
 
foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "id") 
    $oc_id = $paramValue; 
  else if ($paramName == "contact_fk") 
    $co_id = $paramValue; 
  else if ($paramName == "organization_fk") 
    $oz_id = $paramValue; 
  else if ($paramName == "email") 
    $email = $paramValue; 
  else if ($paramName == "note") 
    $note = $paramValue; 
  else if ($paramName == "login") 
    $login = $paramValue; 
  else if ($paramName == "password") 
    $password = $paramValue; 
}

$today = date("Y-m-d");
$dal = new DAL();

if ($oc_id == 0) {
    
  $ad_id = hexdec(uniqid());

  $oc_id = hexdec(uniqid());

  $sql = "INSERT INTO organizationcontact " .
         "(id, contact_id, organization_id, note, email, login, password) " .
         "VALUES " .
         "($oc_id, $co_id, $oz_id, '$note', '$email', '$login', '$password') ";
  $result = $dal->query($sql);

  $ocj_id = hexdec(uniqid());
  
  $sql = "INSERT INTO organizationcontactjobtitle " .
         "(id, organizationcontact_id, title, status_id, date) " .
         "VALUES ($ocj_id, $oc_id, 'New', 1, '$today') ";
  $result = $dal->query($sql);

}

else {

  $sql = "UPDATE organizationcontact SET " .
         "email='$email', note='$note', login='$login', password='$password' " .
         "WHERE id = $oc_id ";
  $result = $dal->query($sql);

}  

echo json_encode($oc_id);

?>

