diff --git a/api/v3/TwingleCampaign/Sync.php b/api/v3/TwingleCampaign/Sync.php new file mode 100644 index 0000000..4ba8332 --- /dev/null +++ b/api/v3/TwingleCampaign/Sync.php @@ -0,0 +1,123 @@ + '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' + ); + } +} \ No newline at end of file diff --git a/tests/phpunit/api/v3/TwingleCampaign/SyncTest.php b/tests/phpunit/api/v3/TwingleCampaign/SyncTest.php new file mode 100644 index 0000000..56541f1 --- /dev/null +++ b/tests/phpunit/api/v3/TwingleCampaign/SyncTest.php @@ -0,0 +1,53 @@ +installMe(__DIR__) + ->apply(); + } + + /** + * The setup() method is executed before the test is executed (optional). + */ + public function setUp() { + parent::setUp(); + } + + /** + * The tearDown() method is executed after the test was executed (optional) + * This can be used for cleanup. + */ + public function tearDown() { + parent::tearDown(); + } + + /** + * Simple example test case. + * + * Note how the function name begins with the word "test". + */ + public function testApiExample() { + $result = civicrm_api3('TwingleCampaign', 'Sync', array('magicword' => 'sesame')); + $this->assertEquals('Twelve', $result['values'][12]['name']); + } + +}