diff --git a/api/v3/TwingleEvent/Create.php b/api/v3/TwingleEvent/Create.php new file mode 100644 index 0000000..0ae5390 --- /dev/null +++ b/api/v3/TwingleEvent/Create.php @@ -0,0 +1,45 @@ + ['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'); + } +} diff --git a/api/v3/TwingleEvent/Get.php b/api/v3/TwingleEvent/Get.php new file mode 100644 index 0000000..8438d31 --- /dev/null +++ b/api/v3/TwingleEvent/Get.php @@ -0,0 +1,45 @@ + ['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'); + } +} diff --git a/api/v3/TwingleProject/Create.php b/api/v3/TwingleProject/Create.php new file mode 100644 index 0000000..ce8df35 --- /dev/null +++ b/api/v3/TwingleProject/Create.php @@ -0,0 +1,78 @@ + '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'); +} diff --git a/api/v3/TwingleProject/Get.php b/api/v3/TwingleProject/Get.php new file mode 100644 index 0000000..9e4f31f --- /dev/null +++ b/api/v3/TwingleProject/Get.php @@ -0,0 +1,45 @@ + ['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'); + } +} diff --git a/tests/phpunit/api/v3/TwingleEvent/CreateTest.php b/tests/phpunit/api/v3/TwingleEvent/CreateTest.php new file mode 100644 index 0000000..59a2daa --- /dev/null +++ b/tests/phpunit/api/v3/TwingleEvent/CreateTest.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('TwingleEvent', 'Create', array('magicword' => 'sesame')); + $this->assertEquals('Twelve', $result['values'][12]['name']); + } + +} diff --git a/tests/phpunit/api/v3/TwingleEvent/GetTest.php b/tests/phpunit/api/v3/TwingleEvent/GetTest.php new file mode 100644 index 0000000..2cd5225 --- /dev/null +++ b/tests/phpunit/api/v3/TwingleEvent/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('TwingleEvent', 'Get', array('magicword' => 'sesame')); + $this->assertEquals('Twelve', $result['values'][12]['name']); + } + +} diff --git a/tests/phpunit/api/v3/TwingleProject/CreateTest.php b/tests/phpunit/api/v3/TwingleProject/CreateTest.php new file mode 100644 index 0000000..95a86fc --- /dev/null +++ b/tests/phpunit/api/v3/TwingleProject/CreateTest.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('TwingleProject', 'Create', array('magicword' => 'sesame')); + $this->assertEquals('Twelve', $result['values'][12]['name']); + } + +} diff --git a/tests/phpunit/api/v3/TwingleProject/GetTest.php b/tests/phpunit/api/v3/TwingleProject/GetTest.php new file mode 100644 index 0000000..da54ddc --- /dev/null +++ b/tests/phpunit/api/v3/TwingleProject/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('TwingleProject', 'Get', array('magicword' => 'sesame')); + $this->assertEquals('Twelve', $result['values'][12]['name']); + } + +}