<?php

	require_once '../../autoload.php';



  $res = array (
      "status"    => 'KO',
      "actionlinkid" => -1
  ); 
	
	$actionid       = $_POST['actionid'];
	$scope          = $_POST['scope'];
  $scopeid        = $_POST['scopeid'];
  $action         = $_POST['action'];
  $actionlinkedid = $_POST['actionlinkid'];

  $sql = '';
  switch($action)
  {
      case 'add':
        $sql = "insert into action_link(action_id,ext_id,scope) values ({$actionid},{$scopeid},'{$scope}') returning id;";
        break;
      case 'delete':
        $sql = "delete from action_link where id = {$actionlinkedid} returning id;";
        break;
  }
  $conn = get_dbconnection();
  
  if ( $sql != '' )
  {
      if ( $query = pg_query($conn,$sql) )
      {
          if ( $row = pg_fetch_row($query) )
          {
              $res ["status"]       = "OK";
              $res ["actionlinkid"] = $row [0];
          }
     }
  }

  header('Content-type: application/json');
  $result = json_encode ( $res );
  
  echo $result;
?>
