Merge remote-tracking branch 'MarcMichalsky/issue/43_44'

[#45] Change campaign_id parameter data type and add validation for it
This commit is contained in:
Jens Schuppe 2021-02-23 15:02:45 +01:00
commit 549916a0bc
2 changed files with 25 additions and 1 deletions

View file

@ -117,6 +117,30 @@ class CRM_Twingle_Submission {
); );
} }
} }
// Validate campaign_id, if given.
if (!empty($params['campaign_id'])) {
// Check whether campaign_id is a numeric string and cast it to an integer.
if (is_numeric($params['campaign_id'])) {
$params['campaign_id'] = intval($params['campaign_id']);
}
else {
throw new CiviCRM_API3_Exception(
E::ts('campaign_id must be a numeric string. '),
'invalid_format'
);
}
// Check whether given campaign_id exists and if not, unset the parameter.
try {
civicrm_api3(
'Campaign',
'getsingle',
['id' => $params['campaign_id']]
);
} catch (CiviCRM_API3_Exception $e) {
unset($params['campaign_id']);
}
}
} }
/** /**

View file

@ -234,7 +234,7 @@ function _civicrm_api3_twingle_donation_Submit_spec(&$params) {
$params['campaign_id'] = array( $params['campaign_id'] = array(
'name' => 'campaign_id', 'name' => 'campaign_id',
'title' => E::ts('Campaign ID'), 'title' => E::ts('Campaign ID'),
'type' => CRM_Utils_Type::T_INT, 'type' => CRM_Utils_Type::T_STRING,
'api.required' => 0, 'api.required' => 0,
'description' => E::ts('The CiviCRM ID of a campaign to assign the contribution.'), 'description' => E::ts('The CiviCRM ID of a campaign to assign the contribution.'),
); );