diff --git a/CRM/TwingleCampaign/Utils/APIWrapper.php b/CRM/TwingleCampaign/Utils/APIWrapper.php new file mode 100644 index 0000000..3700811 --- /dev/null +++ b/CRM/TwingleCampaign/Utils/APIWrapper.php @@ -0,0 +1,90 @@ +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); + } + +} \ No newline at end of file diff --git a/twinglecampaign.php b/twinglecampaign.php index 59d461b..2ceb017 100644 --- a/twinglecampaign.php +++ b/twinglecampaign.php @@ -5,6 +5,25 @@ use CRM_TwingleCampaign_ExtensionUtil as E; 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(). @@ -27,13 +46,13 @@ function twinglecampaign_civicrm_postSave_civicrm_campaign($dao) { if (CRM_Core_Transaction::isActive()) { CRM_Core_Transaction::addCallback( CRM_Core_Transaction::PHASE_POST_COMMIT, - 'twinglecampaign_postSave_callback', + 'twinglecampaign_postSave_campaign_callback', [$dao->id, $dao->campaign_type_id] ); } // If the transaction is already finished, call the function directly 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 */ -function twinglecampaign_postSave_callback ( +function twinglecampaign_postSave_campaign_callback ( int $campaign_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(). *