implement TwingleCampaign.getsingle

This commit is contained in:
Marc Michalsky forumZFD 2021-02-11 18:34:25 +01:00
parent 76adafe66c
commit c48928052a
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9

View file

@ -10,36 +10,90 @@ 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_campaign_Getsingle_spec(&$spec) { function _civicrm_api3_twingle_campaign_Getsingle_spec(&$spec) {
$spec['magicword']['api.required'] = 1; $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['name'] = [
'name' => 'name',
'title' => E::ts('Campaign Name'),
'type' => CRM_Utils_Type::T_STRING,
'api.required' => 0,
'description' => E::ts('Name of the Campaign'),
];
$spec['title'] = [
'name' => 'title',
'title' => E::ts('Campaign Title'),
'type' => CRM_Utils_Type::T_STRING,
'api.required' => 0,
'description' => E::ts('Title of the Campaign'),
];
$spec['start_date'] = [
'name' => 'start_date',
'title' => E::ts('Campaign Start Date'),
'type' => CRM_Utils_Type::T_STRING,
'api.required' => 0,
'description' => E::ts('Date and Time that Campaign starts.'),
];
$spec['last_modified_id'] = [
'name' => 'last_modified_id',
'title' => E::ts('Campaign Modified By'),
'type' => CRM_Utils_Type::T_INT,
'api.required' => 0,
'description' => E::ts('FK ti civicrm_contact, who recently edited this campaign'),
'FKClassName' => 'CRM_Contact_DAO_Contact',
];
$spec['last_modified_date'] = [
'name' => 'last_modified_date',
'title' => E::ts('Campaign Modified Date'),
'type' => CRM_Utils_Type::T_STRING,
'api.required' => 0,
'description' => E::ts('FK ti civicrm_contact, who recently edited this campaign'),
];
$spec['is_active'] = [
'name' => 'is_active',
'title' => E::ts('Campaign is active'),
'type' => CRM_Utils_Type::T_BOOLEAN,
'api.required' => 0,
'description' => E::ts('Is this Campaign enabled or disabled/cancelled?'),
];
} }
/** /**
* TwingleCampaign.Getsingle API * # TwingleCampaign.Getsingle API
* *
* @param array $params * @param array $params
* *
* @return array * @return array
* API result descriptor * API result descriptor
* *
* @throws \CiviCRM_API3_Exception
* @see civicrm_api3_create_success * @see civicrm_api3_create_success
* *
* @throws API_Exception
*/ */
function civicrm_api3_twingle_campaign_Getsingle($params) { function civicrm_api3_twingle_campaign_Getsingle(array $params): array {
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) // filter parameters
return civicrm_api3_create_success($returnValues, $params, 'TwingleCampaign', 'Getsingle'); $allowed_params = [];
} _civicrm_api3_twingle_campaign_Getsingle_spec($allowed_params);
else { $params = array_intersect_key($params, $allowed_params);
throw new API_Exception(/*error_message*/ 'Everyone knows that the magicword is "sesame"', /*error_code*/ 'magicword_incorrect');
// Get TwingleCampaign by provided parameters
$returnValues = civicrm_api3('TwingleCampaign', 'get', $params);
$count = $returnValues['count'];
// Check whether only a single TwingleCampaign is found
if ($count != 1) {
return civicrm_api3_create_error(
"Expected one TwingleCampaign but found $count"
);
} }
return civicrm_api3_create_success(
$returnValues['values'][$returnValues['id']],
$params, 'TwingleCampaign',
'Getsingle'
);
} }