<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "selid") 
    $oc_id = $paramValue;  
  else if ($paramName == "rowids") 
    $rowids = $paramValue;  
  else if ($paramName == "addids") 
    $addids = $paramValue;  
  else if ($paramName == "inputs") 
    $inputs = $paramValue;  
  else if ($paramName == "dates") 
    $dates = $paramValue;  
}

$oc_id = explode (",", $rowids);
$addid = explode (",", $addids);
$input = explode (",", $inputs); 
$date  = explode (",", $dates);

$dal = new DAL();

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

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

    $sql = "DELETE FROM organizationcontactjobtitle " .
           "WHERE id = $oc_id[$i] ";
    $result = $dal->query($sql);
  }
    
  else {

    $sql = "SELECT id FROM organizationcontactjobtitle " .
           "WHERE id = $oc_id AND date = $date[i] ";
    $result = $dal->query($sql);

    if (count ($result) > 0) {
      $sql = "UPDATE organizationcontactjobtitle " .
             "SET descr=$input[$i], status_id=$addid[$i], date=$date[$i] " .
             "WHERE id = $oc_id ";
      $result = $dal->query($sql);
    }

    else {
      $ocj_id = hexdec(uniqid());

      $sql = "INSERT INTO organizationcontactjobtitle " .
             "(id, descr, status_id, date) " .
             "VALUES (id, $input[$i], $addid[$i], $date[$i]) ";
      $result = $dal->query($sql);
    }  
  }

}
    
echo json_encode($pr_id);

?>

