implement TwingleForm API

This commit is contained in:
Marc Michalsky forumZFD 2020-12-07 17:25:11 +01:00
parent 4b6b1556d5
commit fc545b7f1c
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9
3 changed files with 181 additions and 0 deletions

110
api/v3/TwingleForm/Get.php Normal file
View file

@ -0,0 +1,110 @@
<?php
use CRM_TwingleCampaign_ExtensionUtil as E;
use CRM\TwingleCampaign\Utils\ExtensionCache as Cache;
include_once E::path() . '/CRM/TwingleCampaign/Utils/ExtensionCache.php';
/**
* TwingleForm.Get 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_form_Get_spec(array &$spec) {
$spec['id'] = [
'name' => 'id',
'title' => E::ts('TwingleProject ID'),
'type' => CRM_Utils_Type::T_INT,
'api.required' => 0,
'description' => E::ts('ID of the TwingleProject campaign'),
];
$spec['name'] = [
'name' => 'name',
'title' => E::ts('TwingleProject name'),
'type' => CRM_Utils_Type::T_STRING,
'api.required' => 0,
'description' => E::ts('Name of the TwingleProject campaign'),
];
$spec['title'] = [
'name' => 'title',
'title' => E::ts('TwingleProject title'),
'type' => CRM_Utils_Type::T_STRING,
'api.required' => 0,
'description' => E::ts('Title of the TwingleProject campaign'),
];
$spec['twingle_project_type'] = [
'name' => 'twingle_project_type',
'title' => E::ts('TwingleProject type'),
'type' => CRM_Utils_Type::T_STRING,
'api.required' => 0,
'description' => E::ts('Project type can be default, event or membership'),
];
}
/**
* TwingleForm.Get API
*
* @param array $params
*
* @return array
* API result descriptor
*
* @throws API_Exception
* @see civicrm_api3_create_success
*
*/
function civicrm_api3_twingle_form_Get(array $params) {
// Get custom fields
$custom_field_mapping = Cache::getInstance()->getCustomFieldMapping();
$cf_twingle_project_id = $custom_field_mapping['twingle_project_id'];
$cf_twingle_project_type = $custom_field_mapping['twingle_project_type'];
$cf_twingle_project_widget = $custom_field_mapping['twingle_project_widget'];
$cf_twingle_project_counter = $custom_field_mapping['twingle_project_counter'];
// Replace twingle_project_type key with custom field name
if (key_exists('twingle_project_type', $params)) {
$params[$cf_twingle_project_type] = $params['twingle_project_type'];
unset($params['twingle_project_type']);
}
$request = [
'sequential' => 1,
'campaign_type_id' => "twingle_project"
];
foreach($params as $param) {
array_push($request, $param);
}
try {
$result = civicrm_api3('Campaign', 'get', $request);
if ($result['is_error'] == 0) {
$returnValues = [];
foreach($result['values'] as $value) {
array_push(
$returnValues,
[
'id' => $value['id'],
'twingle_project_id' => $value[$cf_twingle_project_id],
'title' => $value['title'],
'name' => $value['name'],
'project_type' => $value[$cf_twingle_project_type],
'embed_code' => $value[$cf_twingle_project_widget],
'counter' => $value[$cf_twingle_project_counter]
]
);
}
return civicrm_api3_create_success($returnValues, $params, 'TwingleForm', 'Get');
}
else {
// TODO: Edit error message
throw new API_Exception(/*error_message*/ 'Everyone knows that the magicword is "sesame"', /*error_code*/ 'magicword_incorrect');
}
} catch (Exception $e) {
throw new API_Exception(/*error_message*/ 'Everyone knows that the magicword is "sesame"', /*error_code*/ 'magicword_incorrect');
}
}