implement TwingleSync.singlesync
This commit is contained in:
parent
8ed58aa625
commit
12b941e01d
3 changed files with 155 additions and 18 deletions
|
@ -25,7 +25,7 @@ class CRM_TwingleCampaign_BAO_TwingleApiCall {
|
||||||
*
|
*
|
||||||
* @throws API_Exception
|
* @throws API_Exception
|
||||||
*/
|
*/
|
||||||
public function __construct($apiKey, int $limit = 20) {
|
public function __construct($apiKey, int $limit) {
|
||||||
$this->apiKey = $apiKey;
|
$this->apiKey = $apiKey;
|
||||||
$this->limit = $limit;
|
$this->limit = $limit;
|
||||||
|
|
||||||
|
@ -116,7 +116,7 @@ class CRM_TwingleCampaign_BAO_TwingleApiCall {
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getEvent(int $projectId, $eventId = NULL) {
|
public function getEvent($projectId, $eventId = NULL) {
|
||||||
$result = [];
|
$result = [];
|
||||||
|
|
||||||
$url = empty($eventId)
|
$url = empty($eventId)
|
||||||
|
@ -140,9 +140,12 @@ class CRM_TwingleCampaign_BAO_TwingleApiCall {
|
||||||
$response = $this->curlGet($url, $params);
|
$response = $this->curlGet($url, $params);
|
||||||
$finished = ($eventId) || count($response['data']) < $this->limit;
|
$finished = ($eventId) || count($response['data']) < $this->limit;
|
||||||
$offset = $offset + $this->limit;
|
$offset = $offset + $this->limit;
|
||||||
if ($response['data']) {
|
if ($response['data'] && !$eventId) {
|
||||||
$result = array_merge($result, $response['data']);
|
$result = array_merge($result, $response['data']);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
$result = $response;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,10 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use CRM_TwingleCampaign_BAO_TwingleApiCall as TwingleApiCall;
|
||||||
|
use CRM_TwingleCampaign_BAO_TwingleProject as TwingleProject;
|
||||||
|
use CRM_TwingleCampaign_BAO_TwingleEvent as TwingleEvent;
|
||||||
|
use CRM_TwingleCampaign_BAO_CampaignType as CampaignType;
|
||||||
|
use CRM_TwingleCampaign_Utils_ExtensionCache as Cache;
|
||||||
use CRM_TwingleCampaign_ExtensionUtil as E;
|
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -9,8 +15,28 @@ use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||||
*
|
*
|
||||||
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
|
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
|
||||||
*/
|
*/
|
||||||
function _civicrm_api3_twingle_sync_Singlesync_spec(&$spec) {
|
function _civicrm_api3_twingle_sync_Singlesync_spec(array &$spec) {
|
||||||
$spec['magicword']['api.required'] = 1;
|
$spec['campaign_id'] = [
|
||||||
|
'name' => '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
|
* @return array
|
||||||
* API result descriptor
|
* API result descriptor
|
||||||
*
|
*
|
||||||
|
* @throws API_Exception
|
||||||
* @see civicrm_api3_create_success
|
* @see civicrm_api3_create_success
|
||||||
*
|
*
|
||||||
* @throws API_Exception
|
|
||||||
*/
|
*/
|
||||||
function civicrm_api3_twingle_sync_Singlesync($params) {
|
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)
|
$result_values = [];
|
||||||
return civicrm_api3_create_success($returnValues, $params, 'TwingleSync', 'Singlesync');
|
|
||||||
|
// 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 {
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
53
tests/phpunit/api/v3/TwingleSync/SyncTest.php
Normal file
53
tests/phpunit/api/v3/TwingleSync/SyncTest.php
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Civi\Test\HeadlessInterface;
|
||||||
|
use Civi\Test\HookInterface;
|
||||||
|
use Civi\Test\TransactionalInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TwingleSync.Sync API Test Case
|
||||||
|
* This is a generic test class implemented with PHPUnit.
|
||||||
|
* @group headless
|
||||||
|
*/
|
||||||
|
class api_v3_TwingleSync_SyncTest extends \PHPUnit\Framework\TestCase implements HeadlessInterface, HookInterface, TransactionalInterface {
|
||||||
|
use \Civi\Test\Api3TestTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up for headless tests.
|
||||||
|
*
|
||||||
|
* Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile().
|
||||||
|
*
|
||||||
|
* See: https://docs.civicrm.org/dev/en/latest/testing/phpunit/#civitest
|
||||||
|
*/
|
||||||
|
public function setUpHeadless() {
|
||||||
|
return \Civi\Test::headless()
|
||||||
|
->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']);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue