Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Afform - Remove ngRoute from afformStandalone page #19703

Merged
merged 2 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Civi/Angular/AngularLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function __construct() {
$this->res = \CRM_Core_Resources::singleton();
$this->angular = \Civi::service('angular');
$this->region = \CRM_Utils_Request::retrieve('snippet', 'String') ? 'ajax-snippet' : 'html-header';
$this->pageName = $_GET['q'] ?? NULL;
$this->pageName = \CRM_Utils_System::currentPath();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Joomla does not use $_GET['q'] which is why we have a function for this.

$this->modules = [];
}

Expand Down
19 changes: 6 additions & 13 deletions ext/afform/core/CRM/Afform/Page/AfformBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,18 @@ public function run() {
// To avoid php complaints about the number of args passed to this function vs the base function
[$pagePath, $pageArgs] = func_get_args();

// The api will throw an exception if afform is not found (because of the index 0 param)
$afform = civicrm_api4('Afform', 'get', [
'checkPermissions' => FALSE,
'where' => [['name', '=', $pageArgs['afform']]],
'select' => ['title', 'module_name', 'directive_name'],
], 0);

$this->set('afModule', $afform['module_name']);

$loader = new \Civi\Angular\AngularLoader();
$loader->setModules([$afform['module_name'], 'afformStandalone']);
$loader->setPageName(implode('/', $pagePath));
$loader->getRes()->addSetting([
'afform' => [
'open' => $afform['directive_name'],
],
])
// TODO: Allow afforms to declare their own theming requirements
->addBundle('bootstrap3');
$loader->load();
$this->assign('directive', $afform['directive_name']);

(new \Civi\Angular\AngularLoader())
->setModules([$afform['module_name'], 'afformStandalone'])
->load();

if (!empty($afform['title'])) {
$title = strip_tags($afform['title']);
Expand Down
2 changes: 2 additions & 0 deletions ext/afform/core/afform.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ function afform_civicrm_angularModules(&$angularModules) {
'basePages' => [],
'partialsCallback' => '_afform_get_partials',
'_afform' => $afform['name'],
// TODO: Allow afforms to declare their own theming requirements
'bundles' => ['bootstrap3'],
'exports' => [
$afform['directive_name'] => 'E',
],
Expand Down
8 changes: 6 additions & 2 deletions ext/afform/core/ang/afCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
angular.module('afCore', CRM.angRequires('afCore'));

// Use `afCoreDirective(string name)` to generate an AngularJS directive.
angular.module('afCore').service('afCoreDirective', function($routeParams, crmApi4, crmStatus, crmUiAlert) {
angular.module('afCore').service('afCoreDirective', function($location, crmApi4, crmStatus, crmUiAlert) {
return function(camelName, meta, d) {
d.restrict = 'E';
d.scope = {};
d.scope.options = '=';
d.link = {
pre: function($scope, $el, $attr) {
$scope.ts = CRM.ts(camelName);
$scope.routeParams = $routeParams;
$scope.meta = meta;
$scope.crmApi4 = crmApi4;
$scope.crmStatus = crmStatus;
$scope.crmUiAlert = crmUiAlert;
$scope.crmUrl = CRM.url;

// Afforms do not use routing, but some forms get input from search params
$scope.$watch(function() {return $location.search();}, function(params) {
$scope.routeParams = params;
});
}
};
return d;
Expand Down
2 changes: 1 addition & 1 deletion ext/afform/core/ang/afformStandalone.ang.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
],
'css' => [],
'settings' => [],
'requires' => ['ngRoute'],
'requires' => [],
];
15 changes: 2 additions & 13 deletions ext/afform/core/ang/afformStandalone.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
(function(angular, $, _) {
// Declare a list of dependencies.
angular.module('afformStandalone', CRM.angRequires('afformStandalone'));

angular.module('afformStandalone', CRM.angular.modules)
.config(function($routeProvider) {
$routeProvider.when('/', {
controller: 'AfformStandalonePageCtrl',
template: function() {
return '<div id="bootstrap-theme"><' + CRM.afform.open + '></' + CRM.afform.open + '></div>';
}
});
})
.controller('AfformStandalonePageCtrl', function($scope) {});
// Empty module just loads all available modules.
angular.module('afformStandalone', CRM.angular.modules);

})(angular, CRM.$, CRM._);
10 changes: 5 additions & 5 deletions ext/afform/core/templates/CRM/Afform/Page/AfformBase.tpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{literal}
<div ng-app="afformStandalone">
<form ng-view></form>
</div>
{/literal}
<div ng-app="afformStandalone">
<form id="bootstrap-theme">
<{$directive}></{$directive}>
</form>
</div>
13 changes: 7 additions & 6 deletions ext/afform/mock/tests/phpunit/api/v4/AfformRoutingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testChangingPermissions() {
};

$result = $http->get($url('civicrm/mock-page'));
$this->assertNotAuthorized($result);
$this->assertNotAuthorized($result, 'mock-page');

Civi\Api4\Afform::update()
->setCheckPermissions(FALSE)
Expand All @@ -62,26 +62,27 @@ public function testChangingPath() {
->execute();

$this->assertOpensPage($http->get($url('civicrm/mock-page')), 'mock-page');
$this->assertNotAuthorized($http->get($url('civicrm/mock-page-renamed')));
$this->assertNotAuthorized($http->get($url('civicrm/mock-page-renamed')), 'mock-page');

Civi\Api4\Afform::update()
->setCheckPermissions(FALSE)
->addWhere('name', '=', $this->formName)
->addValue('server_route', 'civicrm/mock-page-renamed')
->execute();

$this->assertNotAuthorized($http->get($url('civicrm/mock-page')));
$this->assertNotAuthorized($http->get($url('civicrm/mock-page')), 'mock-page');
$this->assertOpensPage($http->get($url('civicrm/mock-page-renamed')), 'mock-page');
}

/**
* @param $result
* @param string $directive
*/
private function assertNotAuthorized(Psr\Http\Message\ResponseInterface $result) {
private function assertNotAuthorized(Psr\Http\Message\ResponseInterface $result, $directive) {
$contents = $result->getBody()->getContents();
$this->assertEquals(403, $result->getStatusCode());
$this->assertRegExp(';You are not authorized to access;', $contents);
$this->assertNotRegExp(';afform":\{"open":".*"\};', $contents);
$this->assertNotRegExp(';' . preg_quote("<$directive>", ';') . ';', $contents);
}

/**
Expand All @@ -93,7 +94,7 @@ private function assertOpensPage(Psr\Http\Message\ResponseInterface $result, $di
$contents = $result->getBody()->getContents();
$this->assertEquals(200, $result->getStatusCode());
$this->assertNotRegExp(';You are not authorized to access;', $contents);
$this->assertRegExp(';afform":\{"open":"' . preg_quote($directive, ';') . '"\};', $contents);
$this->assertRegExp(';' . preg_quote("<$directive>", ';') . ';', $contents);
}

}