add helper methods; get closer to making operations

master
Marek Isalski 9 years ago
parent a2ed839c01
commit fcb2285119

@ -36,6 +36,39 @@ function fulcrm_webhook_menu() {
return $items; return $items;
} }
function fulcrm_webhook_get_pk_for_entity_type( $entity_type, $entity_id, $fulcrm_type ) {
$query = db_select( 'fulcrm_webhook_entity_mapping', 'fwem' )
->fields( 'fwem', array( 'fulcrm_pk' ) )
->condition( 'entity_type', $entity_type )
->condition( 'entity_id', $entity_id )
->condition( 'fulcrm_type', $fulcrm_type );
$result = $query->execute();
return $result->fetchField();
}
function fulcrm_webhook_find_entity( $data ) {
if ( array_key_exists( 'url', $data ) ) {
$url = $data[ 'url' ];
$fulcrm_type = fulcrm_apiv2_url_to_type( $url );
$fulcrm_pk = fulcrm_apiv2_url_to_pk( $url );
$query = db_select( 'fulcrm_webhook_entity_mapping', 'fwem' )
->fields( 'fwem', array( 'entity_type', 'entity_id' ) )
->condition( 'fulcrm_type', $fulcrm_type )
->condition( 'fulcrm_pk', $fulcrm_pk );
$result = $query->execute();
foreach ( $result as $row ) {
return entity_load( $row->entity_type, array( $row->entity_id ) );
}
module_invoke_all( 'fulcrm_find_entity', $fulcrm_type, $fulcrm_pk );
}
}
function fulcrm_webhook_fulcrm_find_entity( $fulcrm_type, $fulcrm_pk ) {
}
function fulcrm_webhook_webhook( $uuid ) { function fulcrm_webhook_webhook( $uuid ) {
if ( $uuid === variable_get( 'fulcrm_webhook_url' ) ) { if ( $uuid === variable_get( 'fulcrm_webhook_url' ) ) {
$headers = getallheaders(); $headers = getallheaders();
@ -52,24 +85,34 @@ function fulcrm_webhook_webhook( $uuid ) {
$client_id = variable_get( 'fulcrm_apiv2_client_id' ); $client_id = variable_get( 'fulcrm_apiv2_client_id' );
if ( fulcrm_apiv2_prevent_loop( $headers ) ) { if ( fulcrm_apiv2_prevent_loop( $headers ) ) {
$op = null;
switch ( $method ) { switch ( $method ) {
case 'GET': case 'GET':
drupal_json_output( array( "status" => "error", "info" => "GET requests do nothing; webhooks must be POST, PATCH, PUT, or DELETE" ) ); drupal_json_output( array( "status" => "error", "info" => "GET requests do nothing; webhooks must POST, PATCH, PUT, or DELETE" ) );
break; break;
case 'DELETE': case 'DELETE':
$op = 'DELETE';
break;
case 'POST': case 'POST':
$op = 'CREATE';
break;
case 'PATCH': case 'PATCH':
case 'PUT': case 'PUT':
$op = 'UPDATE';
break;
default:
drupal_json_output( array( 'status' => 'error', 'info' => 'unsupported method' ) );
break;
}
if ( $op ) {
if ( $payload === null ) { if ( $payload === null ) {
drupal_json_output( array( 'status' => 'error', 'info' => $payload_error_msg ) ); drupal_json_output( array( 'status' => 'error', 'info' => $payload_error_msg ) );
} else { } else {
// actually do something :) // actually do something :)
drupal_json_output( array( 'status' => 'ok', 'headers' => $headers, 'method' => $method ) ); drupal_json_output( array( 'status' => 'ok', 'headers' => $headers, 'method' => $method ) );
} }
break;
default:
drupal_json_output( array( 'status' => 'error', 'info' => 'unsupported method' ) );
break;
} }
} else { } else {
drupal_json_output( array( 'status' => 'ok', 'info' => 'loop prevention' ) ); drupal_json_output( array( 'status' => 'ok', 'info' => 'loop prevention' ) );

Loading…
Cancel
Save