implement TwingleCampaign.sync
This commit is contained in:
parent
c48928052a
commit
027b7a40cd
2 changed files with 176 additions and 0 deletions
123
api/v3/TwingleCampaign/Sync.php
Normal file
123
api/v3/TwingleCampaign/Sync.php
Normal file
|
@ -0,0 +1,123 @@
|
|||
<?php
|
||||
|
||||
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||
use CRM_TwingleCampaign_BAO_TwingleCampaign as TwingleCampaign;
|
||||
|
||||
/**
|
||||
* TwingleCampaign.Sync API specification (optional)
|
||||
* This is used for documentation and validation.
|
||||
*
|
||||
* @param array $spec description of fields supported by this API call
|
||||
*
|
||||
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
|
||||
*/
|
||||
function _civicrm_api3_twingle_campaign_Sync_spec(array &$spec) {
|
||||
$spec['id'] = [
|
||||
'name' => 'id',
|
||||
'title' => E::ts('Twingle Campaign ID'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('The Twingle Campaign ID'),
|
||||
];
|
||||
$spec['project_id'] = [
|
||||
'name' => 'project_id',
|
||||
'title' => E::ts('Parent Twingle Project ID'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('Twingle ID of the parent TwingleProject'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* # TwingleCampaign.Sync API
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return array
|
||||
* API result descriptor
|
||||
*
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
* @see civicrm_api3_create_success
|
||||
*/
|
||||
function civicrm_api3_twingle_campaign_Sync(array $params): array {
|
||||
|
||||
// filter parameters
|
||||
$allowed_params = [];
|
||||
_civicrm_api3_twingle_campaign_Sync_spec($allowed_params);
|
||||
$params = array_intersect_key($params, $allowed_params);
|
||||
|
||||
// Get TwingleCampaigns
|
||||
$campaigns = civicrm_api3('TwingleCampaign', 'get', $params);
|
||||
|
||||
$returnValues = [];
|
||||
$errors_occurred = 0;
|
||||
|
||||
if ($campaigns['is_error'] == 0 && $campaigns['count'] > 0) {
|
||||
|
||||
// Instantiate and re-create TwingleCampaigns
|
||||
foreach ($campaigns['values'] as $campaign) {
|
||||
try {
|
||||
$campaign = new TwingleCampaign($campaign, $campaign['id']);
|
||||
} catch (CiviCRM_API3_Exception $e) {
|
||||
$errors_occurred++;
|
||||
$status = [
|
||||
"id" => $campaign['id'],
|
||||
"name" => $campaign['name'],
|
||||
"parent_project_id" => $campaign['parent_project_id'],
|
||||
"status" => 'TwingleCampaign could not get instantiated',
|
||||
];
|
||||
$returnValues[$campaign->getId()] = $status;
|
||||
Civi::log()->error(
|
||||
E::LONG_NAME .
|
||||
' could not instantiate TwingleCampaign campaign',
|
||||
$status
|
||||
);
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
$campaign->create(TRUE);
|
||||
$returnValues[$campaign->getId()] =
|
||||
$campaign->getResponse('TwingleCampaign updated');
|
||||
} catch (CiviCRM_API3_Exception $e) {
|
||||
$errors_occurred++;
|
||||
$returnValues[$campaign->getId()] =
|
||||
$campaign->getResponse(
|
||||
'TwingleCampaign update failed: ' .
|
||||
$e->getMessage()
|
||||
);
|
||||
Civi::log()->error(
|
||||
E::LONG_NAME .
|
||||
' could not update TwingleCampaign campaign: ' .
|
||||
$e->getMessage(),
|
||||
$campaign->getResponse()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
return civicrm_api3_create_error(
|
||||
'Could not get TwingleCampaigns: ' .
|
||||
$campaigns['error_message'],
|
||||
$params
|
||||
);
|
||||
}
|
||||
|
||||
// Return results
|
||||
if ($errors_occurred > 0) {
|
||||
$errorMessage = ($errors_occurred > 1)
|
||||
? "$errors_occurred synchronisation processes resulted with an error"
|
||||
: "1 synchronisation process resulted with an error";
|
||||
return civicrm_api3_create_error(
|
||||
$errorMessage,
|
||||
$returnValues
|
||||
);
|
||||
}
|
||||
else {
|
||||
return civicrm_api3_create_success(
|
||||
$returnValues,
|
||||
$params,
|
||||
'TwingleCampaign',
|
||||
'Sync'
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue