✨️ implement validation of TwingleProject settings
This commit is contained in:
parent
060f7ec2dc
commit
34e2e77d83
7 changed files with 652 additions and 183 deletions
|
@ -1,6 +1,8 @@
|
|||
<?php
|
||||
|
||||
use CRM_TwingleCampaign_Utils_ExtensionCache as ExtensionCache;
|
||||
use CRM_TwingleCampaign_BAO_TwingleProject as TwingleProject;
|
||||
use CRM_TwingleCampaign_BAO_TwingleApiCall as TwingleApiCall;
|
||||
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||
|
||||
require_once 'twinglecampaign.civix.php';
|
||||
|
@ -9,6 +11,7 @@ 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) {
|
||||
|
@ -63,15 +66,20 @@ function twinglecampaign_civicrm_postSave_civicrm_campaign($dao) {
|
|||
// 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_campaign_callback',
|
||||
[$dao->id, $dao->campaign_type_id]
|
||||
);
|
||||
|
||||
if (_validateAndSendInput($dao->id, $dao->campaign_type_id)) {
|
||||
|
||||
CRM_Core_Transaction::addCallback(
|
||||
CRM_Core_Transaction::PHASE_POST_COMMIT,
|
||||
'twinglecampaign_postSave_campaign_update_callback',
|
||||
[$dao->id, $dao->campaign_type_id]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// If the transaction is already finished, call the function directly
|
||||
else {
|
||||
twinglecampaign_postSave_campaign_callback($dao->id, $dao->campaign_type_id);
|
||||
twinglecampaign_postSave_campaign_update_callback($dao->id, $dao->campaign_type_id);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -89,21 +97,13 @@ function twinglecampaign_civicrm_postSave_civicrm_campaign($dao) {
|
|||
*
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
function twinglecampaign_postSave_campaign_callback (
|
||||
function twinglecampaign_postSave_campaign_update_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'];
|
||||
|
||||
$twingle_project_campaign_type_id = _get_campaign_type_id_twingle_project();
|
||||
$twingle_campaign_campaign_type_id = _get_campaign_type_id_twingle_campaign();
|
||||
|
||||
// If $campaign_type_id is a TwingleProject or TwingleCampaign campaign,
|
||||
// synchronize it
|
||||
|
@ -121,15 +121,14 @@ function twinglecampaign_postSave_campaign_callback (
|
|||
}
|
||||
|
||||
if (isset($_POST['action'])) {
|
||||
if ($_POST['action'] == 'clone') {
|
||||
if ($_POST['action'] == 'clone' && $entity == 'TwingleProject') {
|
||||
unset($_POST['action']);
|
||||
$result = civicrm_api3($entity, 'getsingle',
|
||||
$result = civicrm_api3('TwingleProject', 'getsingle',
|
||||
['id' => $campaign_id]
|
||||
)['values'][$campaign_id];
|
||||
$className = 'CRM_TwingleCampaign_BAO_' . $entity;
|
||||
);
|
||||
$id = $result['id'];
|
||||
unset($result['id']);
|
||||
$project = new $className($result, $id);
|
||||
$project = new TwingleProject($result, $id);
|
||||
try {
|
||||
$project->clone();
|
||||
} catch (Exception $e) {
|
||||
|
@ -137,58 +136,232 @@ function twinglecampaign_postSave_campaign_callback (
|
|||
E::LONG_NAME .
|
||||
' could not clone ' . $entity . ': ' . $e->getMessage()
|
||||
);
|
||||
CRM_Utils_System::setUFMessage($entity . ' could not get cloned.');
|
||||
}
|
||||
}
|
||||
elseif ($entity == 'TwingleProject') {
|
||||
try {
|
||||
civicrm_api3('TwingleProject', 'sync', ['id' => $campaign_id]);
|
||||
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()
|
||||
CRM_Core_Session::setStatus(
|
||||
$e->getMessage(),
|
||||
E::ts("Campaign cloning failed"),
|
||||
error,
|
||||
[unique => TRUE]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If a TwingleProject is getting saved
|
||||
elseif ($entity == 'TwingleProject') {
|
||||
// Also synchronize all child TwingleCampaign campaigns
|
||||
|
||||
// Synchronize all child TwingleCampaign campaigns
|
||||
try {
|
||||
civicrm_api3('TwingleCampaign', 'sync', ['project_id' => $campaign_id]);
|
||||
civicrm_api3(
|
||||
'TwingleCampaign',
|
||||
'sync',
|
||||
['parent_id' => $campaign_id]);
|
||||
} catch (CiviCRM_API3_Exception $e) {
|
||||
CRM_Core_Session::setStatus(
|
||||
$e->getMessage(),
|
||||
E::ts("TwingleCampaign update failed"),
|
||||
error, [unique => TRUE]
|
||||
);
|
||||
Civi::log()->error(
|
||||
E::SHORT_NAME .
|
||||
' Update of TwingleCampaigns failed: ' . $e->getMessage()
|
||||
);
|
||||
}
|
||||
}
|
||||
else {
|
||||
try {
|
||||
civicrm_api3('TwingleCampaign', 'create',
|
||||
['id' => $campaign_id, 'parent_id' => $_POST['parent_id']]);
|
||||
CRM_Utils_System::setUFMessage(E::ts('TwingleCampaign was saved.'));
|
||||
} catch (CiviCRM_API3_Exception $e) {
|
||||
Civi::log()->error(
|
||||
'twinglecampaign_postSave_callback ' . $e->getMessage()
|
||||
);
|
||||
}
|
||||
try {
|
||||
civicrm_api3('TwingleProject', 'sync', ['id' => $campaign_id]);
|
||||
CRM_Utils_System::setUFMessage('TwingleProject was saved.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function _get_campaign_type_id_twingle_project() {
|
||||
return ExtensionCache::getInstance()
|
||||
->getCampaignIds()['campaign_types']['twingle_project']['id'];
|
||||
->getCampaignIds()['campaign_types']['twingle_project']['id'];
|
||||
}
|
||||
|
||||
function _get_campaign_type_id_twingle_campaign() {
|
||||
return ExtensionCache::getInstance()
|
||||
->getCampaignIds()['campaign_types']['twingle_campaign']['id'];
|
||||
->getCampaignIds()['campaign_types']['twingle_campaign']['id'];
|
||||
}
|
||||
|
||||
'twinglecampaign_postSave_callback ' . $e->getMessage()
|
||||
);
|
||||
/**
|
||||
* Callback to sync a project after its creation.
|
||||
* @param int $campaign_id
|
||||
*/
|
||||
function twinglecampaign_postSave_project_create_callback(
|
||||
int $campaign_id
|
||||
) {
|
||||
try {
|
||||
civicrm_api3(
|
||||
'TwingleProject',
|
||||
'sync',
|
||||
['id' => $campaign_id]);
|
||||
} catch (Exception $e) {
|
||||
CRM_Core_Session::setStatus(
|
||||
$e->getMessage(),
|
||||
E::ts("TwingleProject creation failed"),
|
||||
error, [unique => TRUE]
|
||||
);
|
||||
Civi::log()->error(
|
||||
E::SHORT_NAME .
|
||||
' Update of TwingleProject creation failed: ' . $e->getMessage()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* First validate and then sends the input of this transaction to Twinge.
|
||||
* If the call to the Twingle API succeeded, this function returns TRUE;
|
||||
*
|
||||
* @param $id
|
||||
* @param $campaign_type_id
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
function _validateAndSendInput($id, $campaign_type_id): bool {
|
||||
|
||||
// Set callback for cloning
|
||||
if (isset($_POST['action'])) {
|
||||
CRM_Core_Transaction::addCallback(
|
||||
CRM_Core_Transaction::PHASE_POST_COMMIT,
|
||||
'twinglecampaign_postSave_campaign_update_callback',
|
||||
[$id, $campaign_type_id]
|
||||
);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if ($campaign_type_id == _get_campaign_type_id_twingle_project()) {
|
||||
|
||||
// Instantiate project
|
||||
$project = new TwingleProject();
|
||||
|
||||
// Translate custom fields from $_POST
|
||||
$customFields = [];
|
||||
$customFieldsKeys = preg_grep('/^custom_/', array_keys($_POST));
|
||||
foreach ($customFieldsKeys as $key) {
|
||||
$customFields[preg_replace('/_-?\d*$/', '', $key)] =
|
||||
$_POST[$key];
|
||||
}
|
||||
$project->translateCustomFields(
|
||||
$customFields,
|
||||
TwingleProject::OUT
|
||||
);
|
||||
TwingleProject::formatValues($customFields, TwingleProject::OUT);
|
||||
|
||||
// Update project
|
||||
$project->update($customFields);
|
||||
|
||||
// Validate project values
|
||||
$validation = $project->validate();
|
||||
|
||||
// If the input is valid, send it to Twingle
|
||||
if ($validation['valid']) {
|
||||
|
||||
// Try to retrieve twingleApi from cache or create a new
|
||||
$twingleApi = Civi::cache()->get('twinglecampaign_twingle_api');
|
||||
if (NULL === $twingleApi) {
|
||||
try {
|
||||
$twingleApi =
|
||||
new TwingleApiCall(Civi::settings()->get('twingle_api_key'));
|
||||
} catch (Exception $e) {
|
||||
|
||||
// Roll back transaction if input validation failed
|
||||
CRM_Core_Transaction::rollbackIfFalse(FALSE);
|
||||
|
||||
CRM_Core_Session::setStatus(
|
||||
$e->getMessage(),
|
||||
E::ts("Could not retrieve Twingle API key"),
|
||||
error,
|
||||
[unique => TRUE]
|
||||
);
|
||||
Civi::log()->error(
|
||||
E::SHORT_NAME .
|
||||
' Could not retrieve Twingle API key: ' . $e->getMessage()
|
||||
);
|
||||
}
|
||||
Civi::cache('long')->set('twinglecampaign_twingle_api', $twingleApi);
|
||||
}
|
||||
|
||||
try {
|
||||
// Complement project values with values from Twingle if it has a
|
||||
// project_id
|
||||
if ($project->getProjectId()) {
|
||||
$project_from_twingle = $twingleApi->getProject($project->getProjectId());
|
||||
$project->complement($project_from_twingle);
|
||||
}
|
||||
// If this campaign is just about to become created, add a callback to
|
||||
// sync it after the transaction has finished
|
||||
else {
|
||||
CRM_Core_Transaction::addCallback(
|
||||
CRM_Core_Transaction::PHASE_POST_COMMIT,
|
||||
'twinglecampaign_postSave_project_create_callback', [$id]
|
||||
);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Push project
|
||||
require E::path() . '/api/v3/TwingleProject/Sync.php';
|
||||
$result = _pushProjectToTwingle($project, $twingleApi, [], FALSE);
|
||||
if ($result['is_error'] != 0) {
|
||||
throw new \CiviCRM_API3_Exception($result['error_message']);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
|
||||
// Roll back transaction if input validation failed
|
||||
CRM_Core_Transaction::rollbackIfFalse(FALSE);
|
||||
|
||||
// Display and log error message
|
||||
CRM_Core_Session::setStatus(
|
||||
$e->getMessage(),
|
||||
E::ts("TwingleProject synchronization failed: %1",
|
||||
[1 => $e->getMessage()]),
|
||||
error,
|
||||
[unique => TRUE]
|
||||
);
|
||||
Civi::log()->error(
|
||||
E::SHORT_NAME .
|
||||
' TwingleProject synchronization failed: ' . $e->getMessage()
|
||||
);
|
||||
// Push failed
|
||||
return FALSE;
|
||||
}
|
||||
// Push succeeded
|
||||
return TRUE;
|
||||
}
|
||||
// Display error message if validation failed
|
||||
else {
|
||||
|
||||
// Roll back transaction if input validation failed
|
||||
CRM_Core_Transaction::rollbackIfFalse(FALSE);
|
||||
|
||||
// Build error message
|
||||
$errorMessage = '<ul>';
|
||||
foreach ($validation['messages'] as $message) {
|
||||
$errorMessage = $errorMessage . '<li>' . $message . '</li>';
|
||||
}
|
||||
$errorMessage = $errorMessage . '</ul>';
|
||||
|
||||
CRM_Core_Session::setStatus(
|
||||
$errorMessage,
|
||||
E::ts("Input validation failed"),
|
||||
error,
|
||||
[unique => TRUE]
|
||||
);
|
||||
// Validation failed
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
// TwingleCampaigns always return TRUE;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue