Commit 747b8441 authored by colemanw's avatar colemanw
Browse files

Merge branch 'scanner' into 'master'

Toward #6 - use API instead of scanner to retrieve afforms

See merge request extensions/afform!18
parents 119bd37e 8cc9410e
......@@ -163,7 +163,7 @@ class CRM_Afform_AfformScanner {
$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;
}
......
......@@ -19,7 +19,7 @@ class Get extends \Civi\Api4\Generic\BasicGetAction {
$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;
......
......@@ -120,7 +120,7 @@ class Afform extends AbstractEntity {
'name' => 'has_local',
];
$fields[] = [
'name' => 'has_packaged',
'name' => 'has_base',
];
}
......
......@@ -36,7 +36,7 @@ trait AfformSaveTrait {
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();
}
......
......@@ -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);
}
}
}
......
......@@ -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}
});
});
......
......@@ -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">
......@@ -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>
......
<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">
......@@ -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>
......
......@@ -60,7 +60,7 @@ class api_v4_AfformTest extends api_v4_AfformTestCase {
$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 @@ class api_v4_AfformTest extends api_v4_AfformTestCase {
$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 @@ class api_v4_AfformTest extends api_v4_AfformTestCase {
$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);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment