<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "fkeyid")
    $pw_id = $paramValue;
  else if ($paramName == "searchid")
    $pa_id = $paramValue;
  else if ($paramName == "pattern")
    $pattern = $paramValue;
}

$pattern = trim($pattern);

$dal = new DAL();
$sql = "SELECT OP.id, OP.name AS hint, 'Yes' AS disabled  " .
       "FROM projectworkdayoperation PWO, operation OP " .
       "WHERE PWO.projectworkday_id = $pw_id AND OP.id = PWO.operation_id " .
       "AND OP.name LIKE '%$pattern%' " .
       "UNION ALL " .
       "SELECT OP.id, OP.name AS hint, 'No' AS disabled " .
       "FROM operation OP, projectworkday PW, projectworkdayoperation PWO " .
       "WHERE PW.parcel_id = $pa_id AND PWO.projectworkday_id != $pw_id " .
       "AND PWO.projectworkday_id = PW.id AND OP.id = PWO.operation_id " .
       "AND OP.name LIKE '%$pattern%' AND OP.id NOT IN " .
       "(SELECT OP1.id FROM projectworkdayoperation PWO1, operation OP1 " .
       "WHERE PWO1.projectworkday_id = $pw_id AND OP1.id = PWO1.operation_id " .
       "AND OP1.name LIKE '%$pattern%') " ;
$result = $dal->query($sql);

echo json_encode ($result);

?>