diff --git a/api/v3/TwingleForm/Get.php b/api/v3/TwingleForm/Get.php new file mode 100644 index 0000000..a0683cb --- /dev/null +++ b/api/v3/TwingleForm/Get.php @@ -0,0 +1,110 @@ + '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'); + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..0f9f25d --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,18 @@ + + + + + ./tests/phpunit + + + + + ./ + + + + + + + + diff --git a/tests/phpunit/api/v3/TwingleForm/GetTest.php b/tests/phpunit/api/v3/TwingleForm/GetTest.php new file mode 100644 index 0000000..94e83b7 --- /dev/null +++ b/tests/phpunit/api/v3/TwingleForm/GetTest.php @@ -0,0 +1,53 @@ +installMe(__DIR__) + ->apply(); + } + + /** + * The setup() method is executed before the test is executed (optional). + */ + public function setUp() { + parent::setUp(); + } + + /** + * The tearDown() method is executed after the test was executed (optional) + * This can be used for cleanup. + */ + public function tearDown() { + parent::tearDown(); + } + + /** + * Simple example test case. + * + * Note how the function name begins with the word "test". + */ + public function testApiExample() { + $result = civicrm_api3('TwingleForm', 'Get', array('magicword' => 'sesame')); + $this->assertEquals('Twelve', $result['values'][12]['name']); + } + +}