<?php

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

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

$dal = new DAL();
$sql = "SELECT OC.id, CONCAT (CO.surname, ': ', OZ.name) AS text " .
       "FROM organizationcontact OC, organization OZ, contact CO " .
       "WHERE (OZ.name LIKE '%$pattern%' OR CO.surname LIKE '%$pattern%') " .
       "AND OC.contact_id = CO.id AND OC.organization_id = OZ.id " .
       "AND OC.id NOT IN (SELECT OCNO.id " .
       "FROM organizationcontact OCNO, projectorganizationcontact POC " .
       "WHERE POC.project_id = $pr_id " .
       "AND OCNO.id = POC.organizationcontact_id) " ;
$result = $dal->query($sql);

echo json_encode ($result);


