(function () { 'use strict'; var controllerId = 'sidebar'; angular.module('app').controller(controllerId, ['$rootScope', '$route', 'config', 'user', '$scope', '$modal', '$location', 'userAuth', 'layoutService', 'branding', 'OrganisationAdminService', 'rolesAdminDataContext', 'rolesAdminService', '$q', 'features', 'cacheManager', 'fullScreenLoaderContext', 'backpackDatacontext', 'userSettingsDataContext', 'auth', 'licencingAdminDataContext', '$interval', '$window', sidebar]); function sidebar($rootScope, $route, config, user, $scope, $modal, $location, userAuth, layoutService, branding, OrganisationAdminService, rolesAdminDataContext, rolesAdminService, $q, features, cacheManager, fsl, backpackDatacontext, userSettingsDataContext, auth, licencingAdminDataContext, $interval, $window) { var vm = this; vm.user = {}; vm.showSettingsMenu = false; var tokenRefreshed = false; var events = config.events; vm.brandOrganisationId = null; vm.showMenu = false; vm.notifications = []; vm.branding = branding; vm.showClaimsMenu = false; activate(); $rootScope.$on('refreshRoleToken', function (event, args) { userAuth.refreshRoleToken(vm.currentRole().roleId).then(function (token) { // Set the new role token userAuth.setRoleToken(token.token, true); }); }); $rootScope.containsValidIssuer = false; function activate() { var noAuth = false; for (var i=0; i < noAuthUrls.length; i++) { if ($route.current.loadedTemplateUrl.indexOf(noAuthUrls[i]) > -1) { noAuth = true; break; } } fsl.show('Loading application'); if (!noAuth) { $q.all([ getUser(true), rolesAdminDataContext.getAllRoleAllocations(config.appCode), OrganisationAdminService.getTopLevelOrg(true), licencingAdminDataContext.getCurrentLicense(true)]) .then(function (data) { if (!data[1]) return; if (!data[2] || data[2].length == 0) { auth.logOut(); userAuth.setAuthToken(null); userAuth.setRoleToken(null); window.location = '#!/signin'; return; } setRolesInitial(data[1], false); vm.licences = data[3]; getBadgeBackpack(); $scope.topLevelOrg = data[2]; $rootScope.currentOrg = data[2].id; features.getCurrentFeatures($scope.topLevelOrg.id).then(function (features) { vm.features = features; $rootScope.features = features; }); fsl.hide(); return; }).catch(function (reason) { rolesAdminDataContext.getAllRoleAllocations(config.appCode).then(function (allocations) { // Organisation endpoints may return invalid token if (!tokenRefreshed && allocations) { setRolesInitial(allocations, true); tokenRefreshed = true; activate(); } }); }); } } if (vm.authToken == null) { vm.authToken = userAuth.getAuthToken(); } vm.go = function (path) { $location.path(path); }; function setRolesInitial(roles, skipOnboarding) { var availableRoles = roles; // Hold all the roles in the service rolesAdminService.setMyRoles(availableRoles); vm.availableRoles = rolesAdminService.getMyRoles(); // Set the current role to the last active role or the first role returned var lastActiveRole = vm.availableRoles.filter(function (role) { return role.lastActive; })[0]; if (!lastActiveRole) rolesAdminService.setActiveRole(vm.availableRoles[0]); else rolesAdminService.setActiveRole(lastActiveRole); vm.currentRole = rolesAdminService.getActiveRole; $rootScope.currentRole = vm.currentRole(); // If the user has no roles sign them out if (!vm.currentRole()) { auth.logOut(); userAuth.setAuthToken(null); userAuth.setRoleToken(null); window.location = '#!/signin'; } else { if ($location.$$path === '/subscription/upgrade') { if (config.adminRoles.indexOf(vm.currentRole().name) !== -1) { // Do nothing } else { if (vm.currentRole().name == "User") { $location.path('/backpack'); } else { if (vm.branding.suppressWelcomePage !== 'yes') $location.path('/overview'); else $location.path('/dashboard'); } } } else { if (vm.currentRole().name == "User") { $location.path('/backpack'); } else { if (vm.branding.suppressWelcomePage !== 'yes') $location.path('/overview'); else $location.path('/dashboard'); } } if (config.adminRoles.indexOf(vm.currentRole().name) != -1) { $rootScope.isSystemAdmin = true; } userAuth.setRoleToken(vm.currentRole().token, true); $interval(function () { $rootScope.$broadcast('refreshRoleToken'); }, 1800000); } } $rootScope.$on('UpdateLicence', function (event, args) { licencingAdminDataContext.getCurrentLicense(true).then(function (data) { vm.licences = data; }); }); vm.clearNotifications = function (role) { vm.notifications = []; vm.notificationsNum = 0; //$scope.$apply(); } vm.setRole = function (role) { // Clear caches cacheManager.bust(); rolesAdminService.setActiveRole(role); $rootScope.isSystemAdmin = false; // Set currentrole vm.currentRole = rolesAdminService.getActiveRole; // We need this to be set on the root scope as the route provider needs it to handle access $rootScope.currentRole = vm.currentRole(); // Set a new role token userAuth.setRoleToken(vm.currentRole().token, true); // Redirect to home page if (config.adminRoles.indexOf(vm.currentRole().name) != -1) { $rootScope.isSystemAdmin = true; } var promises = []; angular.forEach(role.roleAllocationIds, function (allocationId) { promises.push( rolesAdminDataContext.setLastActive(allocationId, config.appCode, vm.user.userId) ); }); $q.all(promises).then(function () { console.log('Last active role set'); $window.location.reload(); }); }; function getUser() { return user.getProfile(true).then(function (theUser) { $rootScope.verifiedEmails = []; vm.avatarUrl = theUser.avatarUrl; vm.user = theUser; for (i in vm.user.emailAddresses) { if (vm.user.emailAddresses[i].verified) { $rootScope.verifiedEmails.push(vm.user.emailAddresses[i].email) } } if ($rootScope.verifiedEmails.length == 0) { vm.emailVerificationStatus(); $rootScope.lockRouter = true; } if (isForgottenPassword || isSetPassword) { vm.resetPassword(); $rootScope.lockRouter = true; } $rootScope.currentUser = vm.user; if (vm.authToken == null) { vm.authToken = userAuth.getAuthToken(); } if (vm.authToken != null) { if (vm.authToken.roles == null) { vm.authToken = userAuth.getAuthToken(); } } if (vm.authToken) { $rootScope.topLevelOrgId = vm.authToken.topLevelOrgId; } return; }); } function getBadgeBackpack() { backpackDatacontext.getBadgeAccount().then(function (data) { if (!data) { backpackDatacontext.createBadgeAccount().then(function (data) { console.log('Badge backpack created'); setBackpackData(data); }); } else { setBackpackData(data); } }); } function setBackpackData(data) { vm.badgeUserConfig = data.mybadgesUserConfig; $rootScope.badgeUserConfig = vm.badgeUserConfig; $rootScope.$broadcast('UpdateDisplay'); for (i in vm.user.emailAddresses) { if (vm.user.emailAddresses[i].verified) { var userBackpack = { emailaddress: vm.user.emailAddresses[i].email, backpackid: vm.badgeUserConfig.backpack.id }; backpackDatacontext.getBadgesForEmail(userBackpack).then(function (data) { $rootScope.$broadcast('UpdateBadges'); vm.badgeUserConfig.badgesRetrieved = true; var model = { userId: vm.badgeUserConfig.id, retrieved: true }; backpackDatacontext.setToRetrieved(model).then(function (data) { }); }); } } if (!vm.badgeUserConfig.backgroundStyle) { vm.badgeUserConfig.backgroundStyle = '/assets/backgrounds/1.jpg'; vm.setTheme = { userId: vm.badgeUserConfig.id, backgroundStyle: vm.badgeUserConfig.backgroundStyle } backpackDatacontext.saveBadgeUserTheme(vm.setTheme).then(function (data) { // Save the default theme }); } $rootScope.$broadcast('UpdateBadges'); } // graphics have been updated $rootScope.$on('UpdateUser', function () { getUser(); }); vm.checkRoles = function(route, role) { if (role) { $location.path(route); } }; vm.getCurrentRoute = function (path) { if ($location.path().substr(1, path.length) === path.substr(1, path.length)) { return 'active-sub'; } else { return ''; } } vm.toggleLeftSideBar = function () { layoutService.setLeftSideBar(!layoutService.getLeftSideBar()); }; vm.leftSideBar = layoutService.getLeftSideBar; vm.openSubNav = function (toOpen) { if (toOpen === 'System') { vm.showSettingsMenu = !vm.showSettingsMenu; } if (toOpen === 'Badges') { $scope.showBadgesMenu = !$scope.showBadgesMenu; } if (toOpen === 'IssuingBadges') { $scope.showIssuingBadgesMenu = !$scope.showIssuingBadgesMenu; } if (toOpen === 'Claims') { $scope.showClaimsMenu = !$scope.showClaimsMenu; } if (toOpen === 'Reporting') { $scope.showReportingMenu = !$scope.showReportingMenu; } }; vm.changeRoute = function (route, roleName) { $location.path(route); }; $rootScope.$on(events.loginSuccess, function (data, data2) { vm.authenticated = true; activate(); } ); vm.emailVerificationStatus = function () { $modal.open({ templateUrl: '/app/layout/verify.html?version=191217', controller: emailVerificationStatusController, size: 'sm', backdrop: 'static', resolve: { theUser: function () { return vm.user; }, branding: function () { return vm.branding; } } }); } //controller for showboards onboarding var emailVerificationStatusController = function ($scope, $modalInstance, theUser, branding) { $scope.user = theUser; $scope.sent = false; $scope.branding = branding; $scope.resendEmailVerification = function (email) { userSettingsDataContext.resendEmailVerification(email, vm.brandOrganisationId).then(function (data) { $scope.sent = true; }); } $scope.signOut = function () { auth.logOut(); userAuth.setAuthToken(null); userAuth.setRoleToken(null); window.location = '#!/signin'; window.location.reload(); } }; vm.resetPassword = function () { $modal.open({ templateUrl: '/app/layout/resetpassword.html?version=191217', controller: resetPasswordController, size: 'sm', backdrop: 'static', resolve: { theUser: function () { return vm.user; } } }); } //controller for showboards onboarding var resetPasswordController = function ($scope, $modalInstance, theUser) { $scope.passwordModel = {}; $scope.passwordModel.applicationId = config.applicationId; $scope.resetPassword = function () { userSettingsDataContext.changePassword($scope.passwordModel).then(function (data) { $scope.done = true; window.location.reload(); }); } }; $scope.$on('createClientOnlyNotification', function (event, args) { vm.user.notificationsNum = vm.user.notificationsNum++; for (i in vm.notifications) { if (vm.notifications[i].id == args.id && vm.notifications[i].notificationType == "badgeIssuingStarted") { vm.issuingInProgress = true; vm.notifications[i].progress = true; vm.notifications[i].issued++; if (vm.notifications[i].issued == vm.notifications[i].total) { vm.notifications[i].finished = true; vm.issuingInProgress = false; vm.notifications[i].id = null; $rootScope.$broadcast('UpdateLicence'); $rootScope.$broadcast('UpdateDashboard'); } vm.notifications[i].percent = (100 / vm.notifications[i].total) * vm.notifications[i].issued if (args.status == false) { vm.notifications[i].failed++ } if (!$scope.$$phase) $scope.$apply(); return; } } vm.notifications.push(args); if (!$scope.$$phase) $scope.$apply(); }); vm.claimBadge = function () { window.location = '#!/myclaims/true'; }; } })();