refactor to have createupdate and delete methods

master
Marek Isalski 8 years ago
parent 42d242b56b
commit 4d7ea19c7f

@ -46,7 +46,9 @@ function fulcrm_webhook_get_pk_for_entity_type( $entity_type, $entity_id, $fulcr
return $result->fetchField(); return $result->fetchField();
} }
function fulcrm_webhook_find_entity( $data ) { function fulcrm_webhook_createupdate_entity( $data ) {
$return = array( 'data' => $data );
if ( array_key_exists( 'url', $data ) ) { if ( array_key_exists( 'url', $data ) ) {
$url = $data[ 'url' ]; $url = $data[ 'url' ];
$fulcrm_type = fulcrm_apiv2_url_to_type( $url ); $fulcrm_type = fulcrm_apiv2_url_to_type( $url );
@ -59,14 +61,70 @@ function fulcrm_webhook_find_entity( $data ) {
$result = $query->execute(); $result = $query->execute();
foreach ( $result as $row ) { foreach ( $result as $row ) {
return entity_load( $row->entity_type, array( $row->entity_id ) ); $return = array( 'data' => $data, 'entity' => entity_load( $row->entity_type, array( $row->entity_id ) ) );
foreach ( module_implements( 'update_entity_for_fulcrm_' . $fulcrm_type ) as $module ) {
$function = $module . '_' . 'update_entity_for_fulcrm_' . $fulcrm_type;
if ( function_exists( $function ) ) {
$function( $return );
}
}
return $return;
} }
module_invoke_all( 'fulcrm_find_entity', $fulcrm_type, $fulcrm_pk ); foreach ( module_implements( 'create_entity_for_fulcrm_' . $fulcrm_type ) as $module ) {
$function = $module . '_' . 'create_entity_for_fulcrm_' . $fulcrm_type;
if ( function_exists( $function ) ) {
$function( $return );
if ( array_key_exists( 'entity', $return ) ) {
db_insert( 'fulcrm_webhook_entity_mapping' )->fields( array( 'fulcrm_type' => $fulcrm_type,
'fulcrm_pk' => $fulcrm_pk,
'entity_type' => $return[ 'entity_type' ],
'bundle' => array_key_exists( 'bundle', $return ) ? $return[ 'bundle' ] : $return[ 'entity_type' ],
'entity_id' => $return[ 'entity_id' ],
) )->execute();
return $return;
}
}
}
} }
return $return;
} }
function fulcrm_webhook_fulcrm_find_entity( $fulcrm_type, $fulcrm_pk ) { function fulcrm_webhook_delete_entity( $data ) {
$return = array( 'data' => $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' ] = entity_load( $row->entity_type, array( $row->entity_id ) );
foreach ( module_implements( 'delete_entity_for_fulcrm_' . $fulcrm_type ) as $module ) {
$function = $module . '_' . 'delete_entity_for_fulcrm_' . $fulcrm_type;
if ( function_exists( $function ) ) {
$function( $return );
}
}
}
db_delete( 'fulcrm_webhook_entity_mapping', 'fwem' )
->condition( 'fulcrm_type', $fulcrm_type )
->condition( 'fulcrm_pk', $fulcrm_pk )
->execute();
}
return $return;
} }
function fulcrm_webhook_webhook( $uuid ) { function fulcrm_webhook_webhook( $uuid ) {
@ -77,7 +135,7 @@ function fulcrm_webhook_webhook( $uuid ) {
$method = $_SERVER[ 'REQUEST_METHOD' ]; $method = $_SERVER[ 'REQUEST_METHOD' ];
if ( array_key_exists( 'Content-Type', $headers ) && ( $headers[ 'Content-Type' ] === 'application/json' ) ) { if ( array_key_exists( 'Content-Type', $headers ) && ( $headers[ 'Content-Type' ] === 'application/json' ) ) {
$payload = json_decode( file_get_contents( "php://input" ) ); $payload = json_decode( file_get_contents( "php://input" ), $assoc = TRUE );
$payload_error = json_last_error(); $payload_error = json_last_error();
$payload_error_msg = json_last_error_msg(); $payload_error_msg = json_last_error_msg();
} }
@ -87,32 +145,39 @@ function fulcrm_webhook_webhook( $uuid ) {
if ( fulcrm_apiv2_prevent_loop( $headers ) ) { if ( fulcrm_apiv2_prevent_loop( $headers ) ) {
$op = null; $op = null;
switch ( $method ) { if ( $method === 'GET' ) {
case 'GET':
drupal_json_output( array( "status" => "error", "info" => "GET requests do nothing; webhooks must POST, PATCH, PUT, or DELETE" ) ); drupal_json_output( array( "status" => "error", "info" => "GET requests do nothing; webhooks must POST, PATCH, PUT, or DELETE" ) );
break; return;
}
if ( $payload === null ) {
drupal_json_output( array( 'status' => 'error', 'info' => $payload_error_msg ) );
return;
}
$return = null;
switch ( $method ) {
case 'DELETE': case 'DELETE':
$op = 'DELETE'; $return = fulcrm_webhook_delete_entity( $payload );
break; break;
case 'POST': case 'POST':
$op = 'CREATE';
break;
case 'PATCH': case 'PATCH':
case 'PUT': case 'PUT':
$op = 'UPDATE'; $return = fulcrm_webhook_createupdate_entity( $payload );
break; break;
default: default:
drupal_json_output( array( 'status' => 'error', 'info' => 'unsupported method' ) ); drupal_json_output( array( 'status' => 'error', 'info' => 'unsupported method' ) );
break; break;
} }
if ( $op ) { if ( is_array( $return ) ) {
if ( $payload === null ) { if ( array_key_exists( 'response', $return ) )
drupal_json_output( array( 'status' => 'error', 'info' => $payload_error_msg ) ); drupal_json_output( array( 'status' => 'ok', 'response' => $return[ 'response' ] ) );
} else { else
// actually do something :) drupal_json_output( array( 'status' => 'ok' ) );
drupal_json_output( array( 'status' => 'ok', 'headers' => $headers, 'method' => $method ) ); } else {
} drupal_json_output( array( 'status' => 'error', 'info' => 'no return from internal methods' ) );
} }
} else { } else {
drupal_json_output( array( 'status' => 'ok', 'info' => 'loop prevention' ) ); drupal_json_output( array( 'status' => 'ok', 'info' => 'loop prevention' ) );

@ -22,6 +22,18 @@ function fulcrm_webhook_menu() {
return $items; return $items;
} }
function fulcrm_webhook_user_fulcrm_find_entity( $fulcrm_type, $fulcrm_pk ) { function fulcrm_webhook_create_entity_for_fulcrm_person( &$vars ) {
// $vars[ 'data' ];
// $vars[ 'entity' ] = XXX;
// $vars[ 'entity_type' ] = 'user';
// $vars[ 'entity_id' ] = XXX;
}
function fulcrm_webhook_update_entity_for_fulcrm_person( &$vars ) {
// $vars[ 'data' ];
// $vars[ 'entity' ];
}
function fulcrm_webhook_delete_entity_for_fulcrm_person( &$vars ) {
// $vars[ 'entity' ];
} }

Loading…
Cancel
Save