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().
*
--- /dev/null
+<?php
+use CRM_Afform_ExtensionUtil as E;
+
+/**
+ * Afform.Get API specification (optional)
+ * This is used for documentation and validation.
+ *
+ * @param array $spec description of fields supported by this API call
+ * @return void
+ * @see http://wiki.civicrm.org/confluence/display/CRMDOC/API+Architecture+Standards
+ */
+function _civicrm_api3_afform_get_spec(&$spec) {
+ $spec['name'] = array(
+ 'name' => '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());
+}
--- /dev/null
+<?php
+
+use Civi\Test\HeadlessInterface;
+use Civi\Test\HookInterface;
+use Civi\Test\TransactionalInterface;
+
+/**
+ * Afform.Get API Test Case
+ * This is a generic test class implemented with PHPUnit.
+ * @group headless
+ */
+class api_v3_Afform_GetTest extends \PHPUnit_Framework_TestCase implements HeadlessInterface, HookInterface, TransactionalInterface {
+
+ /**
+ * Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile().
+ * See: https://github.com/civicrm/org.civicrm.testapalooza/blob/master/civi-test.md
+ */
+ public function setUpHeadless() {
+ return \Civi\Test::headless()
+ ->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']);
+ }
+
+}