implement TwingleCampaign url creation
This commit is contained in:
parent
693bc25985
commit
baa23f8639
1 changed files with 124 additions and 54 deletions
|
@ -1,86 +1,118 @@
|
|||
<?php
|
||||
|
||||
use CRM_TwingleCampaign_BAO_CampaignType as CampaignType;
|
||||
use CRM_TwingleCampaign_Utils_ExtensionCache as ExtensionCache;
|
||||
use CRM_TwingleCampaign_BAO_TwingleCampaign as TwingleCampaign;
|
||||
use CRM_TwingleCampaign_BAO_TwingleProject as TwingleProject ;
|
||||
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||
|
||||
require_once 'twinglecampaign.civix.php';
|
||||
|
||||
|
||||
/**
|
||||
* Implements hook_civicrm_postSave_Campaigns().
|
||||
* Implements hook_civicrm_postSave_Campaign().
|
||||
* This function synchronizes TwingleProject campaigns between CiviCRM and
|
||||
* Twingle when they get created, edited or cloned. To prevent recursion a no
|
||||
* hook flag is getting used.
|
||||
*
|
||||
* @param $dao
|
||||
*
|
||||
* @throws CiviCRM_API3_Exception
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
function twinglecampaign_civicrm_postSave_civicrm_campaign($dao) {
|
||||
|
||||
if (empty($_SESSION['CiviCRM']['de.forumzfd.twinglecampaign']['no_hook']) ||
|
||||
$_SESSION['CiviCRM']['de.forumzfd.twinglecampaign']['no_hook'] != TRUE) {
|
||||
|
||||
// extract variables from $dao object
|
||||
$hook_campaign_type_id = $dao->campaign_type_id;
|
||||
$hook_campaign_id = $dao->id;
|
||||
|
||||
// Get campaign type id for TwingleProject
|
||||
$twingle_project_campaign_type_id =
|
||||
ExtensionCache::getInstance()
|
||||
->getCampaigns()['campaign_types']['twingle_project']['id'];
|
||||
|
||||
// If $dao is a TwingleProject campaign, synchronize it
|
||||
if ($hook_campaign_type_id == $twingle_project_campaign_type_id) {
|
||||
// If the db transaction is still running, add a function to it that will
|
||||
// be called afterwards
|
||||
if (CRM_Core_Transaction::isActive()) {
|
||||
CRM_Core_Transaction::addCallback(
|
||||
CRM_Core_Transaction::PHASE_POST_COMMIT,
|
||||
'twinglecampaign_postSave_callback',
|
||||
[$hook_campaign_id]
|
||||
[$dao->id, $dao->campaign_type_id]
|
||||
);
|
||||
}
|
||||
// If the transaction is already finished, call the function directly
|
||||
else {
|
||||
twinglecampaign_postSave_callback($hook_campaign_id);
|
||||
}
|
||||
twinglecampaign_postSave_callback($dao->id, $dao->campaign_type_id);
|
||||
}
|
||||
|
||||
}
|
||||
// Remove no hook flag
|
||||
unset($_SESSION['CiviCRM']['de.forumzfd.twinglecampaign']['no_hook']);
|
||||
}
|
||||
|
||||
/**
|
||||
* This callback function synchronizes a recently updated TwingleProject
|
||||
* campaign
|
||||
* ## postSave callback
|
||||
* This callback function synchronizes a recently updated TwingleProject or
|
||||
* creates a TwingleCampaign
|
||||
*
|
||||
* @param $campaign_id
|
||||
* @param int $campaign_id
|
||||
* @param int $campaign_type_id
|
||||
*
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
function twinglecampaign_postSave_callback($campaign_id) {
|
||||
function twinglecampaign_postSave_callback (
|
||||
int $campaign_id,
|
||||
int $campaign_type_id
|
||||
) {
|
||||
|
||||
// Get campaign type id for TwingleProject
|
||||
$twingle_project_campaign_type_id =
|
||||
ExtensionCache::getInstance()
|
||||
->getCampaigns()['campaign_types']['twingle_project']['id'];
|
||||
|
||||
// Get campaign type id for TwingleCampaign
|
||||
$twingle_campaign_campaign_type_id =
|
||||
ExtensionCache::getInstance()
|
||||
->getCampaigns()['campaign_types']['twingle_campaign']['id'];
|
||||
|
||||
|
||||
// If $campaign_type_id is a TwingleProject or TwingleCampaign campaign,
|
||||
// synchronize it
|
||||
if (
|
||||
$campaign_type_id == $twingle_project_campaign_type_id ||
|
||||
$campaign_type_id == $twingle_campaign_campaign_type_id
|
||||
) {
|
||||
|
||||
// Set $entity for $campaign_type_id
|
||||
if ($campaign_type_id == $twingle_project_campaign_type_id) {
|
||||
$entity = 'TwingleProject';
|
||||
}
|
||||
else {
|
||||
$entity = 'TwingleCampaign';
|
||||
}
|
||||
|
||||
if ($_POST['action'] == 'clone') {
|
||||
unset($_POST['action']);
|
||||
$result = civicrm_api3('TwingleProject', 'getsingle',
|
||||
$result = civicrm_api3($entity, 'getsingle',
|
||||
['id' => $campaign_id]
|
||||
)['values'][$campaign_id];
|
||||
$project = new CRM_TwingleCampaign_BAO_TwingleProject($result);
|
||||
$project = new $entity($result);
|
||||
try {
|
||||
$project->clone();
|
||||
} catch (Exception $e) {
|
||||
Civi::log()->error(
|
||||
E::LONG_NAME .
|
||||
" could not clone TwingleProject campaign: " .
|
||||
$e->getMessage()
|
||||
' could not clone ' . $entity . ': ' . $e->getMessage()
|
||||
);
|
||||
CRM_Utils_System::setUFMessage('TwingleProject could not get cloned.');
|
||||
CRM_Utils_System::setUFMessage($entity. ' could not get cloned.');
|
||||
}
|
||||
}
|
||||
else {
|
||||
elseif ($entity == 'TwingleProject') {
|
||||
try {
|
||||
civicrm_api3('TwingleProject', 'sync', ['id' => $campaign_id]);
|
||||
CRM_Utils_System::setUFMessage('TwingleProject was cloned.');
|
||||
CRM_Utils_System::setUFMessage('TwingleProject was saved.');
|
||||
} catch (CiviCRM_API3_Exception $e) {
|
||||
Civi::log()->error(
|
||||
'twinglecampaign_postSave_callback ' . $e->getMessage()
|
||||
);
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
civicrm_api3('TwingleCampaign', 'create', ['id' => $campaign_id]);
|
||||
CRM_Utils_System::setUFMessage('TwingleCampaign was saved.');
|
||||
} catch (CiviCRM_API3_Exception $e) {
|
||||
Civi::log()->error(
|
||||
'twinglecampaign_postSave_callback ' . $e->getMessage()
|
||||
|
@ -88,6 +120,44 @@ function twinglecampaign_postSave_callback($campaign_id) {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///**
|
||||
// * ## Implements hook_civicrm_post().
|
||||
// *
|
||||
// * @throws CiviCRM_API3_Exception
|
||||
// */
|
||||
//function twinglecampaign_civicrm_post($op, $objectName, $objectId, &$objectRef) {
|
||||
// if ($op == 'delete') {
|
||||
//
|
||||
// if (CRM_Core_Transaction::isActive()) {
|
||||
// CRM_Core_Transaction::addCallback(
|
||||
// CRM_Core_Transaction::PHASE_POST_COMMIT,
|
||||
// 'twinglecampaign_post_callback',
|
||||
// [$hook_campaign_id]
|
||||
// );
|
||||
// }
|
||||
// // If the transaction is already finished, call the function directly
|
||||
// else {
|
||||
// twinglecampaign_post_callback($hook_campaign_id);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
///**
|
||||
// * ## post callback
|
||||
// * This callback function deletes a TwingleProject on Twingle's side
|
||||
// *
|
||||
// * @param $campaign_id
|
||||
// */
|
||||
//function twinglecampaign_post_callback($campaign_id) {
|
||||
// $result = civicrm_api('TwingleProject', 'delete', [$campaign_id]);
|
||||
// if ($result['is_error'] != 0) {
|
||||
// CRM_Utils_System::setUFMessage($result['error_message']);
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
/**
|
||||
* Implements hook_civicrm_config().
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue