<?php
class eventclass_api_biometrico  extends TableEventsBase {
	
	function init() {
		$this->events = array(
			'selectList' => true,
			'insert' => true 
		);
		$this->fieldValues = array(
			'filterLimit' => array(
				 
			),
			'mapIcon' => array(
				 
			),
			'viewCustom' => array(
				 
			),
			'lookupWhere' => array(
				 
			),
			'viewFileText' => array(
				 
			),
			'defaultValue' => array(
				 
			),
			'autoUpdateValue' => array(
				 
			),
			'uploadFolder' => array(
				 
			),
			'viewPluginInit' => array(
				 
			),
			'editPluginInit' => array(
				 
			) 
		);
			}
		function selectList( $dataSource, $command ) {
			$method = "GET";
	$url = "http://localhost/biometrico/api/v1.php?table=socio&action=list&records=1";
	//	replace variables in the URL
	$url = RunnerContext::PrepareRest( $url );
	
	//	prepare HTTP request
	$request = new HttpRequest( $url, $method );
	
	//	run request with authorization data
	$response = $dataSource->getConnection()->requestWithAuth( $request );
	if( $response["error"] ) {
		$dataSource->setError( $response["errorMessage"] );
		return false;
	}
	
	// parse response
	$responseHeaders = HttpRequest::parseHeaders( $response );
	$responseData = HttpRequest::parseResponseArray( $response );
	if( $responseData === null ) {
		$dataSource->setError( "Unable to parse JSON result" . $response["content"] );
		return false;
	}
	
	//	convert API response data into recordset
	$rs = $dataSource->resultFromJson( $responseData, true );
	if( !$rs ) {
		return false;
	}
	
	// apply search and filter parameters. Comment out this line if filtering is done by REST API provider
	$rs = $dataSource->filterResult( $rs, $command->filter );
	
	// apply order parameters
	$rs = $dataSource->reorderResult( $command, $rs );
	
	//	apply pagination
	$rs->seekRecord( $command->startRecord );
	
	return $rs;
	
			;
			
		}
	
		function insert( $dataSource, $command ) {
			$method = "POST";
	$url = "http://localhost/biometrico/api/v1.php?table=socio&action=list&records=1";
	//	replace variables in the URL
	$url = RunnerContext::PrepareRest( $url );
	
	//	prepare HTTP request
	$request = new HttpRequest( $url, $method );
	
	//	run request with authorization data
	$response = $dataSource->getConnection()->requestWithAuth( $request );
	if( $response["error"] ) {
		$dataSource->setError( $response["errorMessage"] );
		return false;
	}
	
	// parse response
	$responseHeaders = HttpRequest::parseHeaders( $response );
	$responseBody = $response["content"];
	
	return $command->values;
	
			;
			
		}
		

}


?>