Skip to content

Commit

Permalink
Toward civicrm#6 - use API instead of scanner to retrieve afforms
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw authored and CiviCRM committed Sep 16, 2020
1 parent afc0cd7 commit 2d4bfef
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 41 deletions.
2 changes: 1 addition & 1 deletion ext/afform/core/CRM/Afform/AfformScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion ext/afform/core/Civi/Api4/Action/Afform/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion ext/afform/core/Civi/Api4/Afform.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static function getFields() {
'name' => 'has_local',
];
$fields[] = [
'name' => 'has_packaged',
'name' => 'has_base',
];
}

Expand Down
2 changes: 1 addition & 1 deletion ext/afform/core/Civi/Api4/Utils/AfformSaveTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
59 changes: 33 additions & 26 deletions ext/afform/core/afform.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,40 +160,48 @@ 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.
* @param array $module
* 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'],
];
}

Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions ext/afform/core/templates/afform/AfformAngularModule.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
});
});
Expand Down
6 changes: 3 additions & 3 deletions ext/afform/gui/ang/afGuiList.aff.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<i class="crm-i fa-plus"></i> {{ ts('New Form') }}
</a>
<div
af-api4="['Afform', 'get', {select: ['name','title','is_public','server_route','has_local','has_packaged'], orderBy: {name:'ASC'}}]"
af-api4="['Afform', 'get', {select: ['name','title','is_public','server_route','has_local','has_base'], orderBy: {name:'ASC'}}]"
af-api4-ctrl="listCtrl">

<div ng-if="apiData.result.length == 0">
Expand Down Expand Up @@ -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')}}</a>
<a af-api4-action="['Afform', 'revert', {where: [['name','=', availForm.name]]}]"
af-api4-start-msg="ts('Deleting...')"
af-api4-success-msg="ts('Deleted')"
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('Delete')}}</a>
</td>
</tr>
Expand Down
6 changes: 3 additions & 3 deletions ext/afform/html/ang/afHtmlList.aff.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div
af-api4="['Afform', 'get', {select: ['name','title','is_public','server_route', 'has_local', 'has_packaged'], orderBy: {name:'ASC'}}]"
af-api4="['Afform', 'get', {select: ['name','title','is_public','server_route', 'has_local', 'has_base'], orderBy: {name:'ASC'}}]"
af-api4-ctrl="listCtrl">

<div ng-if="apiData.result.length == 0">
Expand Down Expand Up @@ -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')}}</a>
<a af-api4-action="['Afform', 'revert', {where: [['name','=', availForm.name]]}]"
af-api4-start-msg="ts('Deleting...')"
af-api4-success-msg="ts('Deleted')"
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('Delete')}}</a>
</td>
</tr>
Expand Down
6 changes: 3 additions & 3 deletions ext/afform/mock/tests/phpunit/api/v4/AfformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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();
Expand All @@ -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);
}

Expand Down

0 comments on commit 2d4bfef

Please sign in to comment.