diff --git a/CRM/TwingleCampaign/BAO/TwingleApiCall.php b/CRM/TwingleCampaign/BAO/TwingleApiCall.php index 010863d..b9f3c2f 100644 --- a/CRM/TwingleCampaign/BAO/TwingleApiCall.php +++ b/CRM/TwingleCampaign/BAO/TwingleApiCall.php @@ -25,7 +25,7 @@ class CRM_TwingleCampaign_BAO_TwingleApiCall { * * @throws API_Exception */ - public function __construct($apiKey, int $limit = 20) { + public function __construct($apiKey, int $limit) { $this->apiKey = $apiKey; $this->limit = $limit; @@ -116,7 +116,7 @@ class CRM_TwingleCampaign_BAO_TwingleApiCall { * * @return array */ - public function getEvent(int $projectId, $eventId = NULL) { + public function getEvent($projectId, $eventId = NULL) { $result = []; $url = empty($eventId) @@ -140,9 +140,12 @@ class CRM_TwingleCampaign_BAO_TwingleApiCall { $response = $this->curlGet($url, $params); $finished = ($eventId) || count($response['data']) < $this->limit; $offset = $offset + $this->limit; - if ($response['data']) { + if ($response['data'] && !$eventId) { $result = array_merge($result, $response['data']); } + else { + $result = $response; + } } return $result; } diff --git a/api/v3/TwingleSync/Singlesync.php b/api/v3/TwingleSync/Singlesync.php index 80fb41d..7483366 100644 --- a/api/v3/TwingleSync/Singlesync.php +++ b/api/v3/TwingleSync/Singlesync.php @@ -1,4 +1,10 @@ 'campaign_id', + 'title' => E::ts('Campaign ID'), + 'type' => CRM_Utils_Type::T_INT, + 'api.required' => 1, + 'description' => E::ts('ID of the campaign that should get synced'), + ]; + $spec['twingle_api_key'] = [ + 'name' => 'twingle_api_key', + 'title' => E::ts('Twingle API key'), + 'type' => CRM_Utils_Type::T_STRING, + 'api.required' => 0, + 'description' => E::ts('The key to access the Twingle API'), + ]; + $spec['is_test'] = [ + 'name' => 'is_test', + 'title' => E::ts('Test'), + 'type' => CRM_Utils_Type::T_BOOLEAN, + 'api.required' => 0, + 'description' => E::ts('If this is set true, no database change will be made'), + ]; } /** @@ -21,25 +47,80 @@ function _civicrm_api3_twingle_sync_Singlesync_spec(&$spec) { * @return array * API result descriptor * + * @throws API_Exception * @see civicrm_api3_create_success * - * @throws API_Exception */ function civicrm_api3_twingle_sync_Singlesync($params) { - if (array_key_exists('magicword', $params) && $params['magicword'] == 'sesame') { - $returnValues = array( - // OK, return several data rows - 12 => ['id' => 12, 'name' => 'Twelve'], - 34 => ['id' => 34, 'name' => 'Thirty four'], - 56 => ['id' => 56, 'name' => 'Fifty six'], - ); - // ALTERNATIVE: $returnValues = []; // OK, success - // ALTERNATIVE: $returnValues = ["Some value"]; // OK, return a single value - // Spec: civicrm_api3_create_success($values = 1, $params = [], $entity = NULL, $action = NULL) - return civicrm_api3_create_success($returnValues, $params, 'TwingleSync', 'Singlesync'); + $result_values = []; + + // Is this call a test? + $is_test = (boolean) $params['is_test']; + + // If function call provides an API key, use it instead of the API key set + // on the extension settings page + $apiKey = empty($params['twingle_api_key']) + ? trim(Civi::settings()->get('twingle_api_key')) + : trim($params['twingle_api_key']); + + $twingleApi = new TwingleApiCall($apiKey, 1); + + $campaign_type_id = 0; + $project_campaign_type_id = + CampaignType::fetch('twingle_project')->getValue(); + $event_campaign_type_id = + CampaignType::fetch('twingle_event')->getValue(); + + $custom_field_mapping = Cache::getInstance()->getCustomFieldMapping(); + + $result = civicrm_api3('Campaign', 'get', [ + 'sequential' => 1, + 'id' => $params['campaign_id'], + ]); + + if ($result['is_error'] == 0) { + if ($result['count'] == 1) { + $campaign_type_id = $result['values'][0]['campaign_type_id']; + } + else { + $count = $result['count']; + throw new API_Exception( + "Expected one Campaign but found $count", + ); + } } else { - throw new API_Exception(/*error_message*/ 'Everyone knows that the magicword is "sesame"', /*error_code*/ 'magicword_incorrect'); + throw new API_Exception($result['error_message']); } + + switch ($campaign_type_id) { + + case $project_campaign_type_id: + // Get a single project from the Twingle API + // TODO: Es darf nur ein Projekt sein! $result['values'][0][$custom_field_mapping['twingle_project_id']] pruefen! Sonst project pushen + $project = $twingleApi->getProject( + $result['values'][0][$custom_field_mapping['twingle_project_id']] + ); + // Create project as campaign and store results in $result_values + $result_values['project'] = + TwingleProject::sync($project, $twingleApi, $is_test); + break; + + case $event_campaign_type_id: + $event = $twingleApi->getEvent( + $result['values'][0][$custom_field_mapping['twingle_event_project_id']], + $result['values'][0][$custom_field_mapping['twingle_event_id']] + ); + $result_values['event'] = + TwingleEvent::sync($event, $twingleApi, $is_test); + break; + + default: + throw new API_Exception( + "Campaign does not belong to TwingleCampaign Extension" + ); + } + return civicrm_api3_create_success($result_values); } + diff --git a/tests/phpunit/api/v3/TwingleSync/SyncTest.php b/tests/phpunit/api/v3/TwingleSync/SyncTest.php new file mode 100644 index 0000000..3966106 --- /dev/null +++ b/tests/phpunit/api/v3/TwingleSync/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('TwingleSync', 'Sync', array('magicword' => 'sesame')); + $this->assertEquals('Twelve', $result['values'][12]['name']); + } + +}