From bc3b7c5b0a5df5faaf20d0d8e7f2111c674fda82 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 12 Jun 2018 18:13:11 -0700 Subject: [PATCH] Proof-of-concept - Afform.get --- ext/afform/afform.php | 20 ++++++ ext/afform/api/v3/Afform/Get.php | 64 +++++++++++++++++++ .../tests/phpunit/api/v3/Afform/GetTest.php | 49 ++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 ext/afform/api/v3/Afform/Get.php create mode 100644 ext/afform/tests/phpunit/api/v3/Afform/GetTest.php diff --git a/ext/afform/afform.php b/ext/afform/afform.php index 0e8192e7a6..59b22af5a4 100644 --- a/ext/afform/afform.php +++ b/ext/afform/afform.php @@ -3,6 +3,26 @@ require_once 'afform.civix.php'; use CRM_Afform_ExtensionUtil as E; +function _afform_fields() { + return ['name', 'title', 'description', 'requires', 'layout']; +} + +/** + * Filter the content of $params to only have supported afform fields. + * + * @param array $params + * @return array + */ +function _afform_fields_filter($params) { + $result = array(); + foreach (_afform_fields() as $field) { + if (isset($params[$field])) { + $result[$field] = $params[$field]; + } + } + return $result; +} + /** * Implements hook_civicrm_config(). * diff --git a/ext/afform/api/v3/Afform/Get.php b/ext/afform/api/v3/Afform/Get.php new file mode 100644 index 0000000000..a8702e0d17 --- /dev/null +++ b/ext/afform/api/v3/Afform/Get.php @@ -0,0 +1,64 @@ + 'name', + 'type' => CRM_Utils_Type::T_STRING, + 'title' => ts('Form name'), + 'description' => 'Form name', + 'maxlength' => 128, + 'size' => CRM_Utils_Type::HUGE, + 'api.required' => 1, + ); + $spec['description'] = array( + 'name' => 'description', + 'type' => CRM_Utils_Type::T_TEXT, + 'title' => ts('Description'), + 'description' => 'Description', + ); + + // FIXME: title, requires, layout +} + +/** + * Afform.Get API + * + * @param array $params + * @return array API result descriptor + * @see civicrm_api3_create_success + * @see civicrm_api3_create_error + * @throws API_Exception + */ +function civicrm_api3_afform_get($params) { + $scanner = new CRM_Afform_AfformScanner(); + $converter = new CRM_Afform_ArrayHtml(); + $records = []; + + if (isset($params['name']) && is_string($params['name'])) { + $names = [$params['name']]; + } + else { + $names = array_keys($scanner->findFilePaths()); + } + + foreach ($names as $name) { + $record = $scanner->getMeta($name); + $layout = $scanner->findFilePath($name, 'layout.html'); + if ($layout) { + // FIXME check for file existence+substance+validity + $record['layout'] = $converter->convertHtmlToArray(file_get_contents($layout)); + } + $records[$name] = $record; + } + + return _civicrm_api3_basic_array_get('CxnApp', $params, $records, 'name', _afform_fields()); +} diff --git a/ext/afform/tests/phpunit/api/v3/Afform/GetTest.php b/ext/afform/tests/phpunit/api/v3/Afform/GetTest.php new file mode 100644 index 0000000000..6fd87013bc --- /dev/null +++ b/ext/afform/tests/phpunit/api/v3/Afform/GetTest.php @@ -0,0 +1,49 @@ +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('Afform', 'Get', array('magicword' => 'sesame')); + $this->assertEquals('Twelve', $result['values'][12]['name']); + } + +} -- 2.25.1