Created
July 16, 2020 05:21
-
-
Save agileware-fj/d109c1f1aeb26f8a0a2337cd47a2439f to your computer and use it in GitHub Desktop.
Working symfony dispatcher for civicrm api wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements an API Wrapper to signal the membership creation preHook that | |
* we're currently inside of a payment transaction. | |
*/ | |
class CRM_MembershipApproval_APIWrapper { | |
private static $in_approval = []; | |
/** | |
* Callback to wrap completetransaction API calls. | |
*/ | |
public static function PREPARE ($event) { | |
$request = $event->getApiRequestSig(); //getApiRequestSig() returns '<api version>.<entity>.<action>', all lower case | |
switch($request) { | |
// Wrap completetransaction in the v3 API. | |
// Doesn't exist yet in the v4 API. | |
case '3.contribution.completetransaction': | |
$event->wrapApi(['CRM_MembershipApproval_APIWrapper', 'completeTransaction']); | |
break; | |
default: | |
break; | |
} | |
} | |
/** | |
* Accessor function to check for approval status. | |
*/ | |
public static function isInApproval($id) { | |
return (isset(static::$in_approval[$id]) && static::$in_approval[$id]); | |
} | |
/** | |
* API wrapper for completetransaction. | |
*/ | |
public static function completeTransaction($apiRequest, $callsame) { | |
$approval_status = CRM_Core_ManagedEntities::singleton()->get('au.com.agileware.membershipapproval', 'membership-status:awaiting-approval'); | |
// Filter line items for this contributions for memberships. | |
// Actual request parameters are passed via the 'params' value of the $apiRequest array | |
$items = civicrm_api3('LineItem', 'get', [ 'contribution_id' => $apiRequest['params']['id'] ])['values']; | |
foreach ($items as &$item) { | |
if(($item['entity_table'] == 'civicrm_membership')) { | |
// Found a membership, record whether it's in_approval for the pre hook | |
$status = civicrm_api3('Membership', 'getvalue', [ 'id' => $item['entity_id'], 'return' => 'status_id' ]); | |
static::$in_approval[$item['entity_id']] = ($status == $approval_status['id']); | |
} | |
} | |
// Continue through to the actual API call. | |
$callsame($apiRequest); | |
foreach ($items as &$item) { | |
if(($item['entity_table'] == 'civicrm_membership') && static::isInApproval($item['entity_id'])) { | |
unset(static::$in_approval[$item['entity_id']]); | |
} | |
} | |
} | |
/** | |
* Stub for a createMembership API wrapper. | |
*/ | |
public static function createMembership($apiRequest, $callsame) { | |
if(!self::isInApproval($apiRequest['params']['id'])) { | |
$callsame($apiRequest); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Implements hook_civicrm_config(). | |
* Most of the magic is in the CRM_MembershipApproval_APIWrapper class | |
*/ | |
function membershipapproval_civicrm_config(&$config){ | |
// Set up our extension for autoloading | |
$extRoot = dirname(__FILE__) . DIRECTORY_SEPARATOR; | |
$include_path = $extRoot . PATH_SEPARATOR . get_include_path( ); | |
set_include_path($include_path); | |
// Bind our wrapper for API Events | |
Civi::dispatcher()->addListener(\Civi\API\Events::PREPARE, ['CRM_MembershipApproval_APIWrapper', 'PREPARE'], -100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment