// The function wrapper keeps global scope clear and avoids global naming collisions (function () { // define your controller and its logic in a local function variable var publicProjectController = function ($scope, $http, $window, WebApiUrlService, ServerValues, ModalConfirmService, CallRestApiService, $translatePartialLoader, $translate) { $scope.projectID = ServerValues.projectID; $scope.organizationID = ServerValues.organizationID; $scope.personID = ServerValues.personID; // personid of logged in person $scope.userSignedUp = ServerValues.userSignedUp; $scope.project = null; $scope.projectPerson = null; $scope.zonePerson = null; $scope.projectZones = []; $scope.loading = false; $scope.hasRecentlyUpdatedPersonInfo = true; $scope.anyFamProjectPersons = false; $scope.anyPositionSelectionPersons = false; $scope.accessPersons = []; $scope.famProjectFiles = []; $scope.$parent.$root.enableLocalization = true; // Show locale change flags $translatePartialLoader.addPart('public/projectDetails'); $scope.alertClass = 'hidden'; // hidden $scope.statusText = ''; $scope.buttonClass = 'hidden'; // hidden $scope.buttonText = ''; $scope.init = function () { $scope.getProject(); } $scope.checkIfPersonInfoIsRecentlyUpdated = function () { var lastModified = null; var d = new Date(); d.setMonth(d.getMonth() - 6); if ($scope.projectPerson.projectApplication.person.modified != null) { lastModified = moment($scope.projectPerson.projectApplication.person.modified).toDate(); } else { lastModified = moment($scope.projectPerson.projectApplication.person.created).toDate(); } $scope.hasRecentlyUpdatedPersonInfo = (lastModified > d) ? true : false; } $scope.displayUserSignedUpStatusText = function () { $scope.buttonClass = 'btn-primary'; $scope.buttonText = 'Change'; $scope.projectPerson.famProjectPersons.forEach(function (projPerson) { if (projPerson.status == 1) { // Registered projPerson.statusText = 'Registered'; projPerson.statusClass = "label applicationstatus-registered"; } else if (projPerson.status == 2) { // Approved projPerson.statusText = 'Approved'; projPerson.statusClass = "label applicationstatus-approved"; } else if (projPerson.status == 3) { // Processing projPerson.statusText = 'Processing'; projPerson.statusClass = "label applicationstatus-processing"; } else if (projPerson.status == 4) { // Signed off projPerson.statusText = 'Signed off'; projPerson.statusClass = "label applicationstatus-signedoff"; } else if (projPerson.status == 7) { // PositionSelection projPerson.statusText = $translate.instant('Shiftselection'); projPerson.statusClass = "label applicationstatus-processing"; $scope.buttonText = $translate.instant('Select shifts'); $scope.anyPositionSelectionPersons = true; } else { projPerson.statusText = 'Unknown'; projPerson.statusClass = ''; } }) } $scope.displayUserNotSignedUpStatus = function () { var d = new Date(); var projectEndDate = moment($scope.project.endDate).toDate(); if (projectEndDate < d) { $scope.statusText = 'The event has ended (it is no longer possible to enroll)'; $scope.alertClass = 'alert-danger'; $scope.buttonClass = 'hidden'; } else if ($scope.project.isArchived) { $scope.statusText = 'The event has been archived (it is not possible to enroll)'; $scope.alertClass = 'alert-danger'; $scope.buttonClass = 'hidden'; } else { $scope.statusText = ''; $scope.alertClass = 'alert-info'; $scope.buttonClass = 'btn-primary btn-xlarge'; $scope.buttonText = 'Enroll'; } } $scope.getProject = function () { $scope.loading = true; var url = '/rest/projects/' + $scope.projectID; return CallRestApiService(function () { return $http.get(url) }) .then(function (data) { $scope.project = data; if ($scope.personID > 0) { // user is logged in $scope.getProjectPersonWithFamily(); } else { $scope.displayUserNotSignedUpStatus(); $scope.loading = false; } }, function (error) { ModalConfirmService.confirm("Error", error.toString(), "OK"); $scope.project = null; $scope.loading = false; }) }; $scope.signupProject = function () { $window.location.href = '/Public/Projects/' + $scope.projectID + "/Signup"; } $scope.downloadFile = function (filePath) { window.open(filePath); } $scope.addPositionsForProjectPersonDay = function () { $scope.projectPerson.famProjectPersonDays.forEach(function (day) { day.positionsForDay = []; var projPersonDayDate = new Date(day.projectDay.dayDate).toDateString(); $scope.projectPerson.projectPositionPersons.forEach(function (pos) { var posStartDate = new Date(pos.startDate).toDateString(); if (posStartDate === projPersonDayDate) { day.positionsForDay.push(pos); } }); }); } $scope.confirmPosition = function (pos) { var url = '/rest/organizations/' + $scope.organizationID + '/projects/' + $scope.projectID + '/positionpersons/'+pos.projectPositionPersonID+'/confirm/' + pos.personID; return CallRestApiService(function () { return $http.patch(url) }) .then(function (data) { pos.confirmedByUser = new Date(); }, function (error) { ModalConfirmService.confirm("Error", error.toString(), "OK"); }) } $scope.getProjectPersonWithFamily = function () { $scope.loading = true; CallRestApiService(function () { return $http.get("/rest/public/projects/" + $scope.projectID + "/familyProjectMember/" + $scope.personID); }) .then( function (data) { $scope.projectPerson = data; $scope.anyFamProjectPersons = $scope.projectPerson.famProjectPersons.length > 0; if ($scope.userSignedUp) { $scope.checkIfPersonInfoIsRecentlyUpdated(); $scope.getZonePerson(); $scope.calculateRewardProgressForUser(); } $scope.addPositionsForProjectPersonDay(); $scope.displayUserSignedUpStatusText(); $scope.getAccessPersons(); $scope.getProjectFunctionFiles(); if (!$scope.userSignedUp && !$scope.anyFamProjectPersons) { $scope.displayUserNotSignedUpStatus(); } }, function (error) { } ) .finally(function () { $scope.loading = false; }); } $scope.getAccessPersons = function () { $scope.project.projectAccess.forEach(function (access) { if (!access.deleted) { if (access.accessType == 1) { access.desc = "Rewards manager"; $scope.accessPersons.push(access); } else if (access.accessType == 2) { access.desc = "Volunteer manager"; $scope.accessPersons.push(access); } else { $scope.projectPerson.famProjectPersonDays.forEach(function (day) { if (day.positionsForDay.length == 0) { if (day.projectFunctioncategoryID && day.projectFunctioncategoryID === access.functionCategoryID && access.functionID == null) { access.desc = day.projectFunctionCategory.categoryName; } else if (day.projectFunctionID && day.projectFunctionID === access.functionID) { access.desc = day.projectFunction.functionName; } if (access.desc) { if ($scope.accessPersons.indexOf(access) === -1) $scope.accessPersons.push(access); } } else { day.positionsForDay.forEach(function (pos) { if (pos.projectPosition.projectFunctionCategoryID === access.functionCategoryID && access.functionID == null) access.desc = pos.projectPosition.projectFunction.functionCategory.categoryName; if (pos.projectPosition.projectFunctionID === access.functionID) access.desc = pos.projectPosition.projectFunction.functionName; if ($scope.accessPersons.indexOf(access) === -1 && access.desc) $scope.accessPersons.push(access); }); } }); } } }); } $scope.calculateRewardProgressForUser = function () { $scope.project.projectRewardRule.forEach(function (rule) { if (!rule.deleted) { rule.numberOfUnit = 0; if (rule.rewardThresholdType == 1) { // NumberOfApprovedDays rule.unit = $translate.instant('Days'); rule.numberOfUnit = $scope.projectPerson.projectApplication.projectPersonDays.length + $scope.projectPerson.projectApplication.unspecifiedWork; } else { var myPositions = []; $scope.projectPerson.projectPositionPersons.forEach(function (pos) { if (pos.personID.toString() === $scope.personID) { myPositions.push(pos) } }) if (rule.rewardThresholdType == 2) { rule.unit = $translate.instant('Shifts'); rule.numberOfUnit = myPositions.length; } else { rule.unit = $translate.instant('Hours'); myPositions.forEach(function (pos) { rule.numberOfUnit += $scope.getDuration(pos); }) } } rule.usedOfUnit = 0; $scope.projectPerson.famProjectPersons[0].projectRewardTransactions.forEach(function (transaction) { if (transaction.rewardThresholdType == rule.rewardThresholdType && transaction.deleted == null) { rule.usedOfUnit += transaction.transactionAmount; } }) rule.leftOfUnit = rule.numberOfUnit - rule.usedOfUnit; rule.progressBarStyle = { "width": (rule.numberOfUnit / rule.threshold) * 100 + "%", "max-width": "100%" }; rule.class = (rule.numberOfUnit >= rule.threshold) ? "success" : "warning"; } }); } $scope.getDuration = function (position) { var start = moment(position.startDate); var end = moment(position.endDate); var duration = moment.duration(end.diff(start)); return duration.hours(); }; $scope.getProjectFunctionFiles = function () { $scope.projectPerson.projectPositionPersons.forEach(function (posPers) { var functionName = posPers.projectPosition.projectFunction.functionName; posPers.projectPosition.projectFunction.projectFunctionFiles.forEach(function (file) { if (!file.deleted) { if (!$scope.famProjectFiles.some(f => f.projectFunctionID === file.projectFunctionID)) file.functionName = functionName; if (!$scope.famProjectFiles.some(f => f.projectFunctionFileID === file.projectFunctionFileID)) $scope.famProjectFiles.push(file); console.log($scope.famProjectFiles); } }) }) $scope.projectPerson.famProjectPersonDays.forEach(function (personDay) { if (personDay.projectFunction) { var functionName = personDay.projectFunction.functionName; personDay.projectFunction.projectFunctionFiles.forEach(function (file) { if (!file.deleted) { if (!$scope.famProjectFiles.some(f => f.projectFunctionID === file.projectFunctionID)) file.functionName = functionName; if (!$scope.famProjectFiles.some(f => f.projectFunctionFileID === file.projectFunctionFileID)) $scope.famProjectFiles.push(file); console.log($scope.famProjectFiles); } }) } }) } $scope.getZonePerson = function () { CallRestApiService(function () { return $http.get("/rest/public/projects/" + $scope.projectID + "/zoneperson"); }) .then( function (zonePerson) { $scope.zonePerson = zonePerson; }, function (error) { // ignore error } ); } $scope.isActive = function (rule) { return !rule.deleted; } $scope.init(); }; // Set up dependecy injections for dependencies used by controller function publicProjectController.$inject = ['$scope', '$http', '$window', 'WebApiUrlService', 'ServerValues', 'ModalConfirmService', 'CallRestApiService', '$translatePartialLoader', '$translate']; // Register the controller angular.module('rubicApp').controller('publicProjectController', publicProjectController); }());;