From 2d4bfef17400f129975d5c3b6bf0122e7161a3ef Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 19 Dec 2019 21:24:08 -0500 Subject: [PATCH] Toward #6 - use API instead of scanner to retrieve afforms --- ext/afform/core/CRM/Afform/AfformScanner.php | 2 +- .../core/Civi/Api4/Action/Afform/Get.php | 2 +- ext/afform/core/Civi/Api4/Afform.php | 2 +- .../core/Civi/Api4/Utils/AfformSaveTrait.php | 2 +- ext/afform/core/afform.php | 59 +++++++++++-------- .../templates/afform/AfformAngularModule.tpl | 3 +- ext/afform/gui/ang/afGuiList.aff.html | 6 +- ext/afform/html/ang/afHtmlList.aff.html | 6 +- .../mock/tests/phpunit/api/v4/AfformTest.php | 6 +- 9 files changed, 47 insertions(+), 41 deletions(-) diff --git a/ext/afform/core/CRM/Afform/AfformScanner.php b/ext/afform/core/CRM/Afform/AfformScanner.php index 61ac85c95ef6..bce6323a63d3 100644 --- a/ext/afform/core/CRM/Afform/AfformScanner.php +++ b/ext/afform/core/CRM/Afform/AfformScanner.php @@ -163,7 +163,7 @@ public function getComputedFields($name) { $fields = []; $fields['has_local'] = file_exists($localLayoutPath) || file_exists($localMetaPath); - $fields['has_packaged'] = ($fields['has_local'] && count($allPaths) > 1) + $fields['has_base'] = ($fields['has_local'] && count($allPaths) > 1) || (!$fields['has_local'] && count($allPaths) > 0); return $fields; } diff --git a/ext/afform/core/Civi/Api4/Action/Afform/Get.php b/ext/afform/core/Civi/Api4/Action/Afform/Get.php index 0f16028e9786..a752f5a8c1dd 100644 --- a/ext/afform/core/Civi/Api4/Action/Afform/Get.php +++ b/ext/afform/core/Civi/Api4/Action/Afform/Get.php @@ -19,7 +19,7 @@ public function getRecords() { $values = []; foreach ($names as $name) { $record = $scanner->getMeta($name); - if ($record) { + if ($record && ($this->_isFieldSelected('has_local') || $this->_isFieldSelected('has_base'))) { $record = array_merge($record, $scanner->getComputedFields($name)); } $layout = $this->_isFieldSelected('layout') ? $scanner->getLayout($name) : NULL; diff --git a/ext/afform/core/Civi/Api4/Afform.php b/ext/afform/core/Civi/Api4/Afform.php index cca20d19d381..3f84a8042c1d 100644 --- a/ext/afform/core/Civi/Api4/Afform.php +++ b/ext/afform/core/Civi/Api4/Afform.php @@ -120,7 +120,7 @@ public static function getFields() { 'name' => 'has_local', ]; $fields[] = [ - 'name' => 'has_packaged', + 'name' => 'has_base', ]; } diff --git a/ext/afform/core/Civi/Api4/Utils/AfformSaveTrait.php b/ext/afform/core/Civi/Api4/Utils/AfformSaveTrait.php index eae07c5ee449..8581583e34fc 100644 --- a/ext/afform/core/Civi/Api4/Utils/AfformSaveTrait.php +++ b/ext/afform/core/Civi/Api4/Utils/AfformSaveTrait.php @@ -36,7 +36,7 @@ protected function writeRecord($item) { unset($fields[array_search('layout', $fields)]); unset($fields[array_search('name', $fields)]); unset($fields[array_search('has_local', $fields)]); - unset($fields[array_search('has_packaged', $fields)]); + unset($fields[array_search('has_base', $fields)]); $orig = \Civi\Api4\Afform::get()->setCheckPermissions(FALSE)->addWhere('name', '=', $item['name'])->setSelect($fields)->execute()->first(); } diff --git a/ext/afform/core/afform.php b/ext/afform/core/afform.php index 3ec331b376b1..deac5cf41bb0 100644 --- a/ext/afform/core/afform.php +++ b/ext/afform/core/afform.php @@ -160,27 +160,30 @@ function afform_civicrm_caseTypes(&$caseTypes) { function afform_civicrm_angularModules(&$angularModules) { _afform_civix_civicrm_angularModules($angularModules); - /** @var CRM_Afform_AfformScanner $scanner */ - $scanner = Civi::service('afform_scanner'); - $names = array_keys($scanner->findFilePaths()); - foreach ($names as $name) { - $meta = $scanner->getMeta($name); - $angularModules[_afform_angular_module_name($name, 'camel')] = [ + $afforms = \Civi\Api4\Afform::get() + ->setCheckPermissions(FALSE) + ->setSelect(['name', 'requires']) + ->execute(); + + foreach ($afforms as $afform) { + $angularModules[_afform_angular_module_name($afform['name'], 'camel')] = [ 'ext' => E::LONG_NAME, - 'js' => ['assetBuilder://afform.js?name=' . urlencode($name)], - 'requires' => $meta['requires'], + 'js' => ['assetBuilder://afform.js?name=' . urlencode($afform['name'])], + 'requires' => $afform['requires'], 'basePages' => [], 'partialsCallback' => '_afform_get_partials', - '_afform' => $name, + '_afform' => $afform['name'], 'exports' => [ - _afform_angular_module_name($name, 'dash') => 'AE', + _afform_angular_module_name($afform['name'], 'dash') => 'AE', ], ]; } } /** - * Construct a list of partials for a given afform/angular module. + * Callback to retrieve partials for a given afform/angular module. + * + * @see afform_civicrm_angularModules * * @param string $moduleName * The module name. @@ -188,12 +191,17 @@ function afform_civicrm_angularModules(&$angularModules) { * The module definition. * @return array * Array(string $filename => string $html). + * @throws API_Exception */ function _afform_get_partials($moduleName, $module) { - /** @var CRM_Afform_AfformScanner $scanner */ - $scanner = Civi::service('afform_scanner'); + $afform = civicrm_api4('Afform', 'get', [ + 'where' => [['name', '=', $module['_afform']]], + 'select' => ['layout'], + 'layoutFormat' => 'html', + 'checkPermissions' => FALSE, + ], 0); return [ - "~/$moduleName/$moduleName.aff.html" => $scanner->getLayout($module['_afform']), + "~/$moduleName/$moduleName.aff.html" => $afform['layout'], ]; } @@ -418,17 +426,11 @@ function afform_civicrm_buildAsset($asset, $params, &$mimeType, &$content) { throw new RuntimeException("Missing required parameter: afform.js?name=NAME"); } - $name = $params['name']; - /** @var \CRM_Afform_AfformScanner $scanner */ - $scanner = Civi::service('afform_scanner'); - $meta = $scanner->getMeta($name); - $moduleName = _afform_angular_module_name($name, 'camel'); - + $moduleName = _afform_angular_module_name($params['name'], 'camel'); $smarty = CRM_Core_Smarty::singleton(); $smarty->assign('afform', [ 'camel' => $moduleName, - 'meta' => $meta, - 'metaJson' => json_encode($meta), + 'meta' => ['name' => $params['name']], 'templateUrl' => "~/$moduleName/$moduleName.aff.html", ]); $mimeType = 'text/javascript'; @@ -480,10 +482,15 @@ function afform_civicrm_permission_check($permission, &$granted, $contactId) { if (preg_match('/^@afform:(.*)/', $permission, $m)) { $name = $m[1]; - /** @var CRM_Afform_AfformScanner $scanner */ - $scanner = \Civi::container()->get('afform_scanner'); - $meta = $scanner->getMeta($name); - $granted = CRM_Core_Permission::check($meta['permission'], $contactId); + $afform = \Civi\Api4\Afform::get() + ->setCheckPermissions(FALSE) + ->addWhere('name', '=', $name) + ->setSelect(['permission']) + ->execute() + ->first(); + if ($afform) { + $granted = CRM_Core_Permission::check($afform['permission'], $contactId); + } } } diff --git a/ext/afform/core/templates/afform/AfformAngularModule.tpl b/ext/afform/core/templates/afform/AfformAngularModule.tpl index 3af54b317898..1e96eb5a7bde 100644 --- a/ext/afform/core/templates/afform/AfformAngularModule.tpl +++ b/ext/afform/core/templates/afform/AfformAngularModule.tpl @@ -2,14 +2,13 @@ @param string $afform.camel The full camel-case name of the AngularJS module being created @param array $afform.meta The full metadata record of the form - @param string $afform.metaJson The full metadata record of the form @param string $afform.layout The template content (HTML) *} {literal} (function(angular, $, _) { angular.module('{/literal}{$afform.camel}{literal}', CRM.angRequires('{/literal}{$afform.camel}{literal}')); angular.module('{/literal}{$afform.camel}{literal}').directive('{/literal}{$afform.camel}{literal}', function(afCoreDirective) { - return afCoreDirective({/literal}{$afform.camel|json},{$afform.metaJson}{literal}, { + return afCoreDirective({/literal}{$afform.camel|json}, {$afform.meta|@json_encode}{literal}, { templateUrl: {/literal}{$afform.templateUrl|json}{literal} }); }); diff --git a/ext/afform/gui/ang/afGuiList.aff.html b/ext/afform/gui/ang/afGuiList.aff.html index f51bdf19fcfa..763ca8547f63 100644 --- a/ext/afform/gui/ang/afGuiList.aff.html +++ b/ext/afform/gui/ang/afGuiList.aff.html @@ -2,7 +2,7 @@ {{ ts('New Form') }}
@@ -38,14 +38,14 @@ af-api4-success-msg="ts('Reverted')" af-api4-success="listCtrl.refresh()" class="btn btn-xs btn-default" - ng-if="availForm.has_local && availForm.has_packaged" + ng-if="availForm.has_local && availForm.has_base" >{{ts('Revert')}} {{ts('Delete')}} diff --git a/ext/afform/html/ang/afHtmlList.aff.html b/ext/afform/html/ang/afHtmlList.aff.html index e306b3f2ee01..4f70a552bccb 100644 --- a/ext/afform/html/ang/afHtmlList.aff.html +++ b/ext/afform/html/ang/afHtmlList.aff.html @@ -1,5 +1,5 @@
@@ -35,14 +35,14 @@ af-api4-success-msg="ts('Reverted')" af-api4-success="listCtrl.refresh()" class="btn btn-xs btn-default" - ng-if="availForm.has_local && availForm.has_packaged" + ng-if="availForm.has_local && availForm.has_base" >{{ts('Revert')}} {{ts('Delete')}} diff --git a/ext/afform/mock/tests/phpunit/api/v4/AfformTest.php b/ext/afform/mock/tests/phpunit/api/v4/AfformTest.php index 392e9f95010f..19de132180d3 100644 --- a/ext/afform/mock/tests/phpunit/api/v4/AfformTest.php +++ b/ext/afform/mock/tests/phpunit/api/v4/AfformTest.php @@ -60,7 +60,7 @@ public function testGetUpdateRevert($formName, $originalMetadata) { $this->assertEquals($get($originalMetadata, 'server_route'), $get($result[0], 'server_route'), $message); $this->assertEquals($get($originalMetadata, 'permission'), $get($result[0], 'permission'), $message); $this->assertTrue(is_array($result[0]['layout']), $message); - $this->assertEquals(TRUE, $get($result[0], 'has_packaged'), $message); + $this->assertEquals(TRUE, $get($result[0], 'has_base'), $message); $this->assertEquals(FALSE, $get($result[0], 'has_local'), $message); $message = 'After updating with Afform.create, the revised data should be returned'; @@ -80,7 +80,7 @@ public function testGetUpdateRevert($formName, $originalMetadata) { $this->assertEquals($get($originalMetadata, 'server_route'), $get($result[0], 'server_route'), $message); $this->assertEquals('access foo', $get($result[0], 'permission'), $message); $this->assertTrue(is_array($result[0]['layout']), $message); - $this->assertEquals(TRUE, $get($result[0], 'has_packaged'), $message); + $this->assertEquals(TRUE, $get($result[0], 'has_base'), $message); $this->assertEquals(TRUE, $get($result[0], 'has_local'), $message); Civi\Api4\Afform::revert()->addWhere('name', '=', $formName)->execute(); @@ -92,7 +92,7 @@ public function testGetUpdateRevert($formName, $originalMetadata) { $this->assertEquals($get($originalMetadata, 'server_route'), $get($result[0], 'server_route'), $message); $this->assertEquals($get($originalMetadata, 'permission'), $get($result[0], 'permission'), $message); $this->assertTrue(is_array($result[0]['layout']), $message); - $this->assertEquals(TRUE, $get($result[0], 'has_packaged'), $message); + $this->assertEquals(TRUE, $get($result[0], 'has_base'), $message); $this->assertEquals(FALSE, $get($result[0], 'has_local'), $message); }