<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "selectid") 
    $pwo_id = $paramValue;  
  else if ($paramName == "fkeyid") 
    $pw_id = $paramValue;  
  else if ($paramName == "string") 
    $name = $paramValue;  
  else if ($paramName == "action") 
    $action = $paramValue;  
      
}

$dal = new DAL();

if ($action == "New") {

  $op_id = hexdec(uniqid());

  $sql = "INSERT INTO operation " .
         "(id, name) " .
         "VALUES ($op_id, '$name') ";
  $result = $dal->query($sql);

  $pwo_id = hexdec(uniqid());

  $sql = "INSERT INTO projectworkdayoperation " .
         "(id, projectworkday_id, operation_id) " .
         "VALUES ($pwo_id, $pw_id, $op_id) ";
  $result = $dal->query($sql);

}

else if ($action == "Change") {
  $sql = "UPDATE operation SET name='$name' WHERE id = " .
         "(SELECT operation_id FROM projectworkdayoperation " .
         "WHERE id = $pwo_id) ";
  $result = $dal->query($sql);
}

else if ($action == "Select") {

  $pwo_id = hexdec(uniqid());

  $sql = "INSERT INTO projectworkdayoperation " .
         "(id, projectworkday_id, operation_id) " .
         "VALUES ($pwo_id, $pw_id, $op_id) ";
  $result = $dal->query($sql);
}

  

echo json_encode($pwo_id);

?>

