From fc54326ff8d77d396f269fca5bb6fb02239b99f6 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 25 Oct 2019 18:21:40 -0700 Subject: [PATCH] Split "Utils" into "AHQ" and "FormDataModel" --- ext/afform/core/Civi/Afform/AHQ.php | 47 ++++++++++++ ext/afform/core/Civi/Afform/FormDataModel.php | 64 +++++++++++++++++ ext/afform/core/Civi/Afform/Utils.php | 72 ------------------- .../Api4/Action/Afform/AbstractProcessor.php | 10 +-- .../core/Civi/Api4/Action/Afform/Prefill.php | 2 +- .../core/Civi/Api4/Action/Afform/Submit.php | 4 +- .../phpunit/Civi/Afform/FormDataModelTest.php | 69 ++++++++++++++++++ 7 files changed, 188 insertions(+), 80 deletions(-) create mode 100644 ext/afform/core/Civi/Afform/AHQ.php create mode 100644 ext/afform/core/Civi/Afform/FormDataModel.php delete mode 100644 ext/afform/core/Civi/Afform/Utils.php create mode 100644 ext/afform/core/tests/phpunit/Civi/Afform/FormDataModelTest.php diff --git a/ext/afform/core/Civi/Afform/AHQ.php b/ext/afform/core/Civi/Afform/AHQ.php new file mode 100644 index 0000000000..7eb8877fa1 --- /dev/null +++ b/ext/afform/core/Civi/Afform/AHQ.php @@ -0,0 +1,47 @@ +entities = $entities; + return $self; + } + + /** + * @param array $layout + * The root element of the layout, in shallow/deep format. + * @param array $entities + * A list of entities, keyed by named. + * This will be updated to include 'fields'. + * Ex: $entities['spouse']['type'] = 'Contact'; + */ + protected static function parseFields($layout, &$entities) { + foreach ($layout['#children'] as $child) { + if ($child['#tag'] == 'af-fieldset' && !empty($child['#children'])) { + $entities[$child['model']]['fields'] = array_merge($entities[$child['model']]['fields'] ?? [], AHQ::getTags($child, 'af-field')); + } + elseif (!empty($child['#children'])) { + self::parseFields($child['#children'], $entities); + } + } + } + + /** + * @return array + * Ex: $entities['spouse']['type'] = 'Contact'; + */ + public function getEntities() { + return $this->entities; + } + +} diff --git a/ext/afform/core/Civi/Afform/Utils.php b/ext/afform/core/Civi/Afform/Utils.php deleted file mode 100644 index 45054f631a..0000000000 --- a/ext/afform/core/Civi/Afform/Utils.php +++ /dev/null @@ -1,72 +0,0 @@ -_afform = (array) civicrm_api4('Afform', 'get', ['checkPermissions' => FALSE, 'where' => [['name', '=', $this->name]]], 0); - $this->_afformEntities = Utils::getEntities($this->_afform['layout']); + $this->_formDataModel = FormDataModel::create($this->_afform['layout']); $this->validateArgs(); $result->exchangeArray($this->processForm()); } @@ -47,7 +47,7 @@ abstract class AbstractProcessor extends \Civi\Api4\Generic\AbstractAction { $rawArgs = $this->args; $this->args = []; foreach ($rawArgs as $arg => $val) { - if (!empty($this->_afformEntities[$arg]['af-url-autofill'])) { + if (!empty($this->_formDataModel[$arg]['af-url-autofill'])) { $this->args[$arg] = $val; } } diff --git a/ext/afform/core/Civi/Api4/Action/Afform/Prefill.php b/ext/afform/core/Civi/Api4/Action/Afform/Prefill.php index f7c64d77ca..5ce5918c9a 100644 --- a/ext/afform/core/Civi/Api4/Action/Afform/Prefill.php +++ b/ext/afform/core/Civi/Api4/Action/Afform/Prefill.php @@ -11,7 +11,7 @@ class Prefill extends AbstractProcessor { protected $_data = []; protected function processForm() { - foreach ($this->_afformEntities as $entityName => $entity) { + foreach ($this->_formDataModel->getEntities() as $entityName => $entity) { // Load entities from args if (!empty($this->args[$entityName])) { $this->loadEntity($entity, $this->args[$entityName]); diff --git a/ext/afform/core/Civi/Api4/Action/Afform/Submit.php b/ext/afform/core/Civi/Api4/Action/Afform/Submit.php index 809b7ace6c..85b88ea449 100644 --- a/ext/afform/core/Civi/Api4/Action/Afform/Submit.php +++ b/ext/afform/core/Civi/Api4/Action/Afform/Submit.php @@ -21,12 +21,12 @@ class Submit extends AbstractProcessor { protected function processForm() { $entityValues = []; - foreach ($this->_afformEntities as $entityName => $entity) { + foreach ($this->_formDataModel->getEntities() as $entityName => $entity) { // Predetermined values override submitted values $entityValues[$entity['type']][$entityName] = ($entity['af-values'] ?? []) + ($this->values[$entityName] ?? []); } - $event = new AfformSubmitEvent($this->_afformEntities, $entityValues); + $event = new AfformSubmitEvent($this->_formDataModel->getEntities(), $entityValues); \Civi::dispatcher()->dispatch(self::EVENT_NAME, $event); foreach ($event->entityValues as $entityType => $entities) { if (!empty($entities)) { diff --git a/ext/afform/core/tests/phpunit/Civi/Afform/FormDataModelTest.php b/ext/afform/core/tests/phpunit/Civi/Afform/FormDataModelTest.php new file mode 100644 index 0000000000..df5687028b --- /dev/null +++ b/ext/afform/core/tests/phpunit/Civi/Afform/FormDataModelTest.php @@ -0,0 +1,69 @@ +installMe(__DIR__)->apply(); + } + + public function getEntityExamples() { + $cases = []; + + //$cases[] = [ + // 'html' => 'Hello world', + // 'entities' => [], + //]; + // + //$cases[] = [ + // 'html' => '
', + // 'entities' => [], + //]; + // + //$cases[] = [ + // 'html' => '
Hello world
', + // 'entities' => [], + //]; + + $cases[] = [ + 'html' => '', + 'entities' => [ + 'foobar' => [ + 'type' => 'Foo', + 'name' => 'foobar', + 'fields' => [ + ['name' => 'propA'], + ['name' => 'propB', 'defn' => ['title' => 'Whiz']], + ], + ], + ], + ]; + + return $cases; + } + + /** + * @param $html + * @param $expectEntities + * @dataProvider getEntityExamples + */ + public function testGetEntities($html, $expectEntities) { + $parser = new \CRM_Afform_ArrayHtml(); + $fdm = FormDataModel::create($parser->convertHtmlToArray($html)); + $this->assertEquals($expectEntities, $fdm->getEntities()); + } + +} -- 2.25.1