Add APIs for TwingleProject and TwingleEvent
This commit is contained in:
parent
86639cfe42
commit
e97ed21937
8 changed files with 425 additions and 0 deletions
45
api/v3/TwingleEvent/Create.php
Normal file
45
api/v3/TwingleEvent/Create.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||
|
||||
/**
|
||||
* TwingleEvent.Create 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_event_Create_spec(&$spec) {
|
||||
$spec['magicword']['api.required'] = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* TwingleEvent.Create API
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return array
|
||||
* API result descriptor
|
||||
*
|
||||
* @see civicrm_api3_create_success
|
||||
*
|
||||
* @throws API_Exception
|
||||
*/
|
||||
function civicrm_api3_twingle_event_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
|
||||
|
||||
// Spec: civicrm_api3_create_success($values = 1, $params = [], $entity = NULL, $action = NULL)
|
||||
return civicrm_api3_create_success($returnValues, $params, 'TwingleEvent', 'Create');
|
||||
}
|
||||
else {
|
||||
throw new API_Exception(/*error_message*/ 'Everyone knows that the magicword is "sesame"', /*error_code*/ 'magicword_incorrect');
|
||||
}
|
||||
}
|
45
api/v3/TwingleEvent/Get.php
Normal file
45
api/v3/TwingleEvent/Get.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||
|
||||
/**
|
||||
* TwingleEvent.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_event_Get_spec(&$spec) {
|
||||
$spec['magicword']['api.required'] = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* TwingleEvent.Get API
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return array
|
||||
* API result descriptor
|
||||
*
|
||||
* @see civicrm_api3_create_success
|
||||
*
|
||||
* @throws API_Exception
|
||||
*/
|
||||
function civicrm_api3_twingle_event_Get($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)
|
||||
return civicrm_api3_create_success($returnValues, $params, 'TwingleEvent', 'Get');
|
||||
}
|
||||
else {
|
||||
throw new API_Exception(/*error_message*/ 'Everyone knows that the magicword is "sesame"', /*error_code*/ 'magicword_incorrect');
|
||||
}
|
||||
}
|
78
api/v3/TwingleProject/Create.php
Normal file
78
api/v3/TwingleProject/Create.php
Normal file
|
@ -0,0 +1,78 @@
|
|||
<?php
|
||||
|
||||
use CRM_TwingleCampaign_BAO_TwingleProject as TwingleProject;
|
||||
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||
|
||||
include_once E::path() . '/CRM/TwingleCampaign/BAO/TwingleProject.php';
|
||||
|
||||
/**
|
||||
* TwingleProject.Create 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_project_Create_spec(array &$spec) {
|
||||
$spec['title'] = [
|
||||
'name' => 'title',
|
||||
'title' => E::ts('Campaign Title'),
|
||||
'type' => CRM_Utils_Type::T_STRING,
|
||||
'api.required' => 1,
|
||||
'description' => E::ts('Title of the Campaign'),
|
||||
];
|
||||
$spec['project_type'] = [
|
||||
'name' => 'project_type',
|
||||
'title' => E::ts('Twingle Project Type'),
|
||||
'type' => CRM_Utils_Type::T_STRING,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('The type of the Twingle Project'),
|
||||
];
|
||||
$spec['allow_more'] = [
|
||||
'name' => 'allow_more',
|
||||
'title' => E::ts('Allow more'),
|
||||
'type' => CRM_Utils_Type::T_BOOLEAN,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('Allow to donate more than is defined in the target'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* TwingleProject.Create API
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return array
|
||||
* API result descriptor
|
||||
*
|
||||
* @throws \Exception
|
||||
* @see civicrm_api3_create_success
|
||||
*
|
||||
*/
|
||||
function civicrm_api3_twingle_project_Create($params) {
|
||||
|
||||
$title = $params['title'];
|
||||
$name = strtolower(preg_replace('/[^A-Za-z0-9]/', '_', $title));
|
||||
$type = $params['project_type'] ?? 'default';
|
||||
$allow_more = $params['allow_more'] ?? 1;
|
||||
|
||||
$values = [
|
||||
'title' => $title,
|
||||
'name' => $name,
|
||||
'type' => $type,
|
||||
'allow_more' => $allow_more,
|
||||
];
|
||||
|
||||
$project = new TwingleProject($values, 'CIVICRM');
|
||||
|
||||
$project->create();
|
||||
|
||||
$sync = $result = civicrm_api3('TwingleSync', 'post');
|
||||
|
||||
if ($sync['is_error'] == 0) {
|
||||
$returnValues = civicrm_api3();
|
||||
}
|
||||
|
||||
$returnValues = null;
|
||||
return civicrm_api3_create_success($returnValues, $params, 'TwingleProject', 'Create');
|
||||
}
|
45
api/v3/TwingleProject/Get.php
Normal file
45
api/v3/TwingleProject/Get.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||
|
||||
/**
|
||||
* TwingleProject.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_project_Get_spec(&$spec) {
|
||||
$spec['magicword']['api.required'] = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* TwingleProject.Get API
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return array
|
||||
* API result descriptor
|
||||
*
|
||||
* @see civicrm_api3_create_success
|
||||
*
|
||||
* @throws API_Exception
|
||||
*/
|
||||
function civicrm_api3_twingle_project_Get($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)
|
||||
return civicrm_api3_create_success($returnValues, $params, 'TwingleProject', 'Get');
|
||||
}
|
||||
else {
|
||||
throw new API_Exception(/*error_message*/ 'Everyone knows that the magicword is "sesame"', /*error_code*/ 'magicword_incorrect');
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue