<?php
	require_once '../../../autoload.php';

	$result = array (
			"status"	=> 'KO',
			"results"	=> -1
	);
	
	$pdo = getPDO ();
		
	if ( $pdo )
	{
		$mod_id						= -1;
		$modident_id				= -1;
		$status 					= -1;
				
		if ( isset($_POST['mod_id']) )			$mod_id 		= $_POST['mod_id'];
		if ( isset($_POST['modident_id']) )		$modident_id	= $_POST['modident_id'];
		if ( isset($_POST['status']) )			$status			= $_POST['status'];

		$sql = 'update modality_identity set mod_id = :mod_id,  status = :status1 where ((mod_id = -1 and :status2 in (0,2)) or (mod_id != -1)) and modident_id = :modident_id;';

		$s0 = $pdo->prepare ( $sql );

		$s0->bindParam ( ':mod_id', 		$mod_id 		);
		$s0->bindParam ( ':modident_id',	$modident_id 	);
		$s0->bindParam ( ':status1',		$status 		);
		$s0->bindParam ( ':status2',		$status 		);
		
		if ( $s0->execute () == true )
		{
			$result['status'] = 'OK';
			$result['results'] = $mod_id;
		}
	}
	
	header('Content-type: application/json');
	
	echo json_encode ( $result );
?>