From 7c166c4757279cfdc5f85e10cc6f5aa786b82209 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 12 Jun 2018 18:40:18 -0700 Subject: [PATCH] Proof-of-concept - Afform.create --- ext/afform/api/v3/Afform/Create.php | 66 +++++++++++++++++++ .../phpunit/api/v3/Afform/CreateTest.php | 49 ++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 ext/afform/api/v3/Afform/Create.php create mode 100644 ext/afform/tests/phpunit/api/v3/Afform/CreateTest.php diff --git a/ext/afform/api/v3/Afform/Create.php b/ext/afform/api/v3/Afform/Create.php new file mode 100644 index 0000000000..8381482432 --- /dev/null +++ b/ext/afform/api/v3/Afform/Create.php @@ -0,0 +1,66 @@ +createSiteLocalPath($name, 'layout.html'); + // printf("[%s] Update layout %s\n", $name, $layoutPath); + CRM_Utils_File::createDir(dirname($layoutPath)); + file_put_contents($layoutPath, $converter->convertArrayToHtml($updates['layout'])); + // FIXME check for writability then success. Report errors. + } + + // Create or update meta.json. + $orig = civicrm_api('afform', 'get', ['name' => $name, 'sequential' => 1]); + if (is_array($orig['values'][0])) { + $meta = _afform_fields_filter(array_merge($orig['values'][0], $updates)); + } + else { + $meta = $updates; + } + unset($meta['definition']); + unset($meta['name']); + if (!empty($meta)) { + $metaPath = $scanner->createSiteLocalPath($name, CRM_Afform_AfformScanner::METADATA_FILE); + // printf("[%s] Update meta %s: %s\n", $name, $metaPath, print_R(['updates'=>$updates, 'meta'=>$meta], 1)); + CRM_Utils_File::createDir(dirname($metaPath)); + file_put_contents($metaPath, json_encode($meta, JSON_PRETTY_PRINT)); + // FIXME check for writability then success. Report errors. + } + + return civicrm_api3_create_success($updates, $params, 'Afform', 'create'); +} diff --git a/ext/afform/tests/phpunit/api/v3/Afform/CreateTest.php b/ext/afform/tests/phpunit/api/v3/Afform/CreateTest.php new file mode 100644 index 0000000000..5fe2c9f8c8 --- /dev/null +++ b/ext/afform/tests/phpunit/api/v3/Afform/CreateTest.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', 'Create', array('magicword' => 'sesame')); + $this->assertEquals('Twelve', $result['values'][12]['name']); + } + +} -- 2.25.1