implement TwingleCampaign
This commit is contained in:
parent
cf6ed7344f
commit
f3d242fce7
3 changed files with 438 additions and 25 deletions
|
@ -1,4 +1,6 @@
|
|||
<?php
|
||||
|
||||
use CRM_TwingleCampaign_BAO_TwingleCampaign as TwingleCampaign;
|
||||
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||
|
||||
/**
|
||||
|
@ -9,37 +11,74 @@ use CRM_TwingleCampaign_ExtensionUtil as E;
|
|||
*
|
||||
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
|
||||
*/
|
||||
function _civicrm_api3_twingle_campaign_Create_spec(&$spec) {
|
||||
$spec['magicword']['api.required'] = 1;
|
||||
function _civicrm_api3_twingle_campaign_Create_spec(array &$spec) {
|
||||
$spec['id'] = [
|
||||
'name' => 'id',
|
||||
'title' => E::ts('Twingle Project ID'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('The Twingle Project ID'),
|
||||
];
|
||||
$spec['name'] = [
|
||||
'name' => 'name',
|
||||
'title' => E::ts('Twingle Campaign Name'),
|
||||
'type' => CRM_Utils_Type::T_STRING,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('Name of the Twingle Project'),
|
||||
];
|
||||
$spec['title'] = [
|
||||
'name' => 'title',
|
||||
'title' => E::ts('Twingle Campaign Title'),
|
||||
'type' => CRM_Utils_Type::T_STRING,
|
||||
'api.required' => 1,
|
||||
'description' => E::ts('Title of the Twingle Campaign'),
|
||||
];
|
||||
$spec['parent_id'] = [
|
||||
'name' => 'parent_id',
|
||||
'title' => E::ts('Parent Campaign'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 1,
|
||||
'description' => E::ts('Optional parent id for this Campaign'),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* TwingleCampaign.Create API
|
||||
* # TwingleCampaign.Create API
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return array
|
||||
* API result descriptor
|
||||
*
|
||||
* @throws CiviCRM_API3_Exception
|
||||
* @see civicrm_api3_create_success
|
||||
*
|
||||
* @throws API_Exception
|
||||
*/
|
||||
function civicrm_api3_twingle_campaign_Create($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
|
||||
function civicrm_api3_twingle_campaign_Create(array $params): array {
|
||||
|
||||
// Spec: civicrm_api3_create_success($values = 1, $params = [], $entity = NULL, $action = NULL)
|
||||
return civicrm_api3_create_success($returnValues, $params, 'TwingleCampaign', 'Create');
|
||||
}
|
||||
else {
|
||||
throw new API_Exception(/*error_message*/ 'Everyone knows that the magicword is "sesame"', /*error_code*/ 'magicword_incorrect');
|
||||
// instantiate TwingleCampaign
|
||||
$campaign = new TwingleCampaign($params);
|
||||
|
||||
// Try to create the TwingleCampaign
|
||||
try {
|
||||
$campaign->create();
|
||||
return civicrm_api3_create_success(
|
||||
$campaign->getResponse('TwingleCampaign created'),
|
||||
$params,
|
||||
'TwingleCampaign',
|
||||
'Create'
|
||||
);
|
||||
} catch(Exception $e){
|
||||
Civi::log()->error(
|
||||
E::LONG_NAME .
|
||||
' could not create TwingleCampaign: ' .
|
||||
$e->getMessage(),
|
||||
$campaign->getResponse()
|
||||
);
|
||||
return civicrm_api3_create_error(
|
||||
'Could not create TwingleCampaign: ' . $e->getMessage(),
|
||||
$campaign->getResponse()
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue