implement APIWrapper for TwingleDonation.submit
This commit is contained in:
parent
73b92d0c0b
commit
54fe303465
2 changed files with 112 additions and 13 deletions
90
CRM/TwingleCampaign/Utils/APIWrapper.php
Normal file
90
CRM/TwingleCampaign/Utils/APIWrapper.php
Normal file
|
@ -0,0 +1,90 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* # APIWrapper class for TwingleDonation.submit
|
||||||
|
* This wrapper maps an incoming contribution to a campaign created by the
|
||||||
|
* de.forumzfd.twinglecampaign extension.
|
||||||
|
*/
|
||||||
|
class CRM_TwingleCampaign_Utils_APIWrapper {
|
||||||
|
|
||||||
|
private static $campaign_id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ## Callback method for event listener
|
||||||
|
* @param $event
|
||||||
|
*/
|
||||||
|
public static function PREPARE($event) {
|
||||||
|
$request = $event->getApiRequestSig();
|
||||||
|
if ($request == '3.twingledonation.submit') {
|
||||||
|
$event->wrapAPI(['CRM_TwingleCampaign_Utils_APIWrapper', 'mapDonation']);
|
||||||
|
//TODO: Exception handling and logging
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ## Map donation to Campaign
|
||||||
|
* This functions tries to map an incoming Twingle donation to a campaign
|
||||||
|
* by referring to its project, event or campaign id. The identified campaign
|
||||||
|
* id will be written into the **campaign_id** field of the request, so that
|
||||||
|
* the de.systopia.twingle extension can include the campaign into the
|
||||||
|
* contribution which it will create.
|
||||||
|
*
|
||||||
|
* @throws CiviCRM_API3_Exception
|
||||||
|
*/
|
||||||
|
public function mapDonation($apiRequest, $callsame) {
|
||||||
|
|
||||||
|
if (array_key_exists(
|
||||||
|
'campaign_id',
|
||||||
|
$apiRequest['params'])
|
||||||
|
) {
|
||||||
|
if (is_numeric($apiRequest['params']['campaign_id'])) {
|
||||||
|
$targetCampaign = $apiRequest['params']['campaign_id'];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
try {
|
||||||
|
$targetCampaign = civicrm_api3(
|
||||||
|
'TwingleCampaign',
|
||||||
|
'getsingle',
|
||||||
|
['cid' => $apiRequest['params']['campaign_id']]
|
||||||
|
);
|
||||||
|
} catch (CiviCRM_API3_Exception $e) {
|
||||||
|
unset($apiRequest['params']['campaign_id']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
elseif (array_key_exists(
|
||||||
|
'event',
|
||||||
|
$apiRequest['params']['custom_fields'])
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
$targetCampaign = civicrm_api3(
|
||||||
|
'TwingleEvent',
|
||||||
|
'getsingle',
|
||||||
|
['event_id' => $apiRequest['params']['custom_fields']['event']]
|
||||||
|
);
|
||||||
|
} catch (CiviCRM_API3_Exception $e) {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
try {
|
||||||
|
$targetCampaign = civicrm_api3(
|
||||||
|
'TwingleProject',
|
||||||
|
'getsingle',
|
||||||
|
['identifier' => $apiRequest['params']['project_id']]
|
||||||
|
);
|
||||||
|
} catch (CiviCRM_API3_Exception $e) {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Soft credits
|
||||||
|
|
||||||
|
if (isset($targetCampaign)) {
|
||||||
|
$apiRequest['params']['campaign_id'] = $targetCampaign['values']['id'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $callsame($apiRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -5,6 +5,25 @@ use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||||
|
|
||||||
require_once 'twinglecampaign.civix.php';
|
require_once 'twinglecampaign.civix.php';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements hook_civicrm_config().
|
||||||
|
*
|
||||||
|
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/
|
||||||
|
* @param $config
|
||||||
|
*/
|
||||||
|
function twinglecampaign_civicrm_config(&$config) {
|
||||||
|
_twinglecampaign_civix_civicrm_config($config);
|
||||||
|
|
||||||
|
// This dispatcher adds an event listener to TwingleDonation.submit
|
||||||
|
// (de.systopia.twingle) and calls an API-Wrapper which maps incoming Twingle
|
||||||
|
// donations to TwingleCampaigns.
|
||||||
|
Civi::dispatcher()->addListener(
|
||||||
|
'civi.api.prepare',
|
||||||
|
['CRM_TwingleCampaign_Utils_APIWrapper', 'PREPARE'],
|
||||||
|
-100
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements hook_civicrm_postSave_Campaign().
|
* Implements hook_civicrm_postSave_Campaign().
|
||||||
|
@ -27,13 +46,13 @@ function twinglecampaign_civicrm_postSave_civicrm_campaign($dao) {
|
||||||
if (CRM_Core_Transaction::isActive()) {
|
if (CRM_Core_Transaction::isActive()) {
|
||||||
CRM_Core_Transaction::addCallback(
|
CRM_Core_Transaction::addCallback(
|
||||||
CRM_Core_Transaction::PHASE_POST_COMMIT,
|
CRM_Core_Transaction::PHASE_POST_COMMIT,
|
||||||
'twinglecampaign_postSave_callback',
|
'twinglecampaign_postSave_campaign_callback',
|
||||||
[$dao->id, $dao->campaign_type_id]
|
[$dao->id, $dao->campaign_type_id]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// If the transaction is already finished, call the function directly
|
// If the transaction is already finished, call the function directly
|
||||||
else {
|
else {
|
||||||
twinglecampaign_postSave_callback($dao->id, $dao->campaign_type_id);
|
twinglecampaign_postSave_campaign_callback($dao->id, $dao->campaign_type_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -51,7 +70,7 @@ function twinglecampaign_civicrm_postSave_civicrm_campaign($dao) {
|
||||||
*
|
*
|
||||||
* @throws \CiviCRM_API3_Exception
|
* @throws \CiviCRM_API3_Exception
|
||||||
*/
|
*/
|
||||||
function twinglecampaign_postSave_callback (
|
function twinglecampaign_postSave_campaign_callback (
|
||||||
int $campaign_id,
|
int $campaign_id,
|
||||||
int $campaign_type_id
|
int $campaign_type_id
|
||||||
) {
|
) {
|
||||||
|
@ -189,16 +208,6 @@ function twinglecampaign_postSave_callback (
|
||||||
// }
|
// }
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Implements hook_civicrm_config().
|
|
||||||
*
|
|
||||||
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/
|
|
||||||
*/
|
|
||||||
function twinglecampaign_civicrm_config(&$config) {
|
|
||||||
_twinglecampaign_civix_civicrm_config($config);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements hook_civicrm_xmlMenu().
|
* Implements hook_civicrm_xmlMenu().
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue