(function () { 'use strict'; angular .module('app') .controller('UserProfileEditController', userProfileEdit); function userProfileEdit($scope, $state, userRepository, $filter, convertHelper, errorHandler, uiNotifications, $translate, localizationHelper, apiMediaUploaderRepository) { var vm = this; vm.translationObject = {}; vm.loadTranslation = function(){ $translate(['Info', 'Success', 'Warning', 'Error', 'ClientLocked', 'ClientUnlocked','ClientPhotoIsEmpty', 'ClientUpdated']).then(function (tr) { vm.translationObject = tr; }); }; vm.tempFormData = {data: null, isImageSelected: false}; vm.stgSecretQuestion1 = -1; vm.stgSecretQuestion2 = -1; vm.stgCountryId = null; vm.unsubscribe = localizationHelper.eventEmitter.subscribe('onLanguageChangedEvent', function (lang) { if(vm.questionPair1){ vm.stgSecretQuestion1 = vm.questionPair1.id } if(vm.questionPair2){ vm.stgSecretQuestion2 = vm.questionPair2.id } if(vm.profile.countryUI){ vm.stgCountryId = vm.profile.countryUI.id; } vm.loadTranslation(); if(vm.stgCountryId){ getCountries(vm.stgCountryId); }else{ getCountries(); } getSecurityQuestions(true); refreshGenderSelectComponent(); }); vm.loadTranslation(); vm.itemBorder = { Default: "1px solid #ccc", Danger: "2px solid #aa0000" }; // Variables. vm.profile = { firstName: null, lastName: null, birthDate: null, email: null, phoneNumber: null, imageThumbnailUrl: "", city:"", countryId: null, countryUI: null, address: "", state:"", gender: null, postalCode: "", imageUrl: "", securityQuestions: [], securityQuestion1: { "answer": "" }, securityQuestion2: { "answer": "" }, verification: { securityQuestion1: { securityQuestionId: 0, answer: "" }, securityQuestion2: { securityQuestionId: 0, answer: "" } } }; vm.genders = [ { id:1, value:'male', key:"Male" }, { id:2, value:'female', key:'Female' } ]; vm.selectedGender = null; vm.currentProfileId = $state.params.id; vm.userIsActive = false; vm.isActiveButtonLoaded = false; vm.questions = []; vm.questionList1 = []; vm.questionList2 = []; vm.questionPair1 = {}; vm.questionPair2 = {}; vm.countries = []; vm.defaultAvatar = "images/default-user.png"; vm.isLoaded = false; vm.request = null; $("#user-image-input").on('change', function(){ setTimeout(function () { transformImage(); }, 10); }); //Methods. vm.updateProfile = function(){ vm.request = { user: { firstName: vm.profile.firstName, lastName: vm.profile.lastName, birthDate: vm.profile.birthDate || $("#birthDate").val(), phoneNumber: vm.profile.phoneNumber, countryId: vm.countries.find(function (c) {return c.name == vm.profile.countryUI.name}).id, city:vm.profile.city, gender:vm.selectedGender.value, address:vm.profile.address, postalCode:vm.profile.postalCode, state:vm.profile.state }, verification: vm.profile.verification }; vm.request.verification.securityQuestion1.securityQuestionId = vm.questionPair1.id; vm.request.verification.securityQuestion2.securityQuestionId = vm.questionPair2.id; if(vm.tempFormData.data){ apiMediaUploaderRepository.uploadImageFormData(vm.tempFormData.data, true).then(function (response) { if (response.success) { vm.request.user.imageUrl = response.data[0].fileUrl; vm.request.user.imageThumbnailUrl = response.data[0].fileThumbnailUrl; vm.sendRequestToUpdate(); } }); }else{ vm.request.user.imageThumbnailUrl = vm.profile.imageThumbnailUrl; vm.request.user.imageUrl = vm.profile.imageUrl; vm.sendRequestToUpdate(); } }; vm.sendRequestToUpdate = function () { userRepository.update(vm.request, vm.currentProfileId).success(function(response, status, headers, config){ if (errorHandler.check(response, status, config) == errorHandler.STATUS.SUCCESS) { uiNotifications.inform(vm.translationObject['ClientUpdated'], 'info'); $state.reload(); } }); } vm.getUserById = function(id){ userRepository.get(id).success(function(response, status, headers, config){ if (errorHandler.check(response, status, config) == errorHandler.STATUS.SUCCESS) { userRepository.updateObjectKeys(vm.profile, response.data.user, ['birthDate']); userRepository.updateDate("#birthDate", response.data.user.birthDate, true, new Date()); vm.profile.birthDate = $filter('date')(response.data.user.birthDate, "yyyy-MM-dd"); vm.userIsActive = response.data.user.isActive; vm.profile.imageThumbnailUrl = response.data.user.imageThumbnailUrl; vm.profile.imageUrl = response.data.user.imageUrl; vm.profile.countryId = response.data.user.countryId; vm.profile.city = response.data.user.city; vm.profile.address = response.data.user.address; vm.profile.postalCode = response.data.user.postalCode; vm.profile.gender = response.data.user.gender; vm.isActiveButtonLoaded = true; if (response.data.securityQuestions.length) { vm.questionPair1.id = response.data.securityQuestions[0].questionId; vm.questionPair1.question = response.data.securityQuestions[0].question; vm.profile.verification.securityQuestion1.answer = response.data.securityQuestions[0].answer; vm.profile.verification.securityQuestion1.id = response.data.securityQuestions[0].id; vm.profile.verification.securityQuestion1.securityQuestionId = response.data.securityQuestions[0].questionId; vm.questionPair2.id = response.data.securityQuestions[1].questionId; vm.questionPair2.question = response.data.securityQuestions[1].question; vm.profile.verification.securityQuestion2.answer = response.data.securityQuestions[1].answer; vm.profile.verification.securityQuestion2.id = response.data.securityQuestions[1].id; vm.profile.verification.securityQuestion2.securityQuestionId = response.data.securityQuestions[1].questionId; } if(vm.questionPair1){ vm.stgSecretQuestion1 = vm.questionPair1.id } if(vm.questionPair2){ vm.stgSecretQuestion2 = vm.questionPair2.id } if(vm.profile.countryUI){ vm.stgCountryId = vm.profile.countryUI.id; } getSecurityQuestions(true); getCountries(response.data.user.countryId); vm.selectedGender = response.data.user.gender ? vm.genders.find(function(item){ return item.value == response.data.user.gender; }) : null; refreshGenderSelectComponent(); vm.isLoaded = true; } }); }; vm.updateUserStatus = function(status){ if (status == "block") { userRepository.lockById(vm.currentProfileId).success(function (response, status, headers, config) { if (errorHandler.check(response, status, config) == errorHandler.STATUS.SUCCESS) { uiNotifications.inform(vm.translationObject['ClientLocked'], 'info'); $state.reload(); } }); } if (status == "unblock") { userRepository.unlockById(vm.currentProfileId).success(function (response, status, headers, config) { if (errorHandler.check(response, status, config) == errorHandler.STATUS.SUCCESS) { uiNotifications.inform(vm.translationObject['ClientUnlocked'], 'success'); $state.reload(); } }); } }; vm.questionBoxChanged = function(boxId){ if (boxId == 1 && vm.questionPair1) { vm.questionList2 = vm.questions.filter(function(q){ return q.id !== vm.questionPair1.id; }); } if (boxId == 2 && vm.questionPair2) { vm.questionList1 = vm.questions.filter(function(q){ return q.id !== vm.questionPair2.id; }); } refreshSecurityQuestionsSelectComponent(); }; vm.removeImage = function(){ vm.profile.imageThumbnailUrl = null; }; function getSecurityQuestions(isUpdate){ userRepository.getSecurityQuestions().success(function(response, status, headers, config){ if (errorHandler.check(response, status, config) == errorHandler.STATUS.SUCCESS) { vm.questions = response.data; vm.questionList1 = vm.questions.filter(function (q) { return q.id != vm.profile.verification.securityQuestion2.securityQuestionId; }); vm.questionList2 = vm.questions.filter(function (q) { return q.id != vm.profile.verification.securityQuestion1.securityQuestionId; }); if(isUpdate){ if(vm.stgSecretQuestion1 && vm.stgSecretQuestion1 != -1){ vm.questionPair1 = vm.questionList1.find(function(q){ return vm.stgSecretQuestion1 == q.id; }); } if(vm.stgSecretQuestion2 && vm.stgSecretQuestion2 != -1){ vm.questionPair2 = vm.questionList2.find(function(q){ return vm.stgSecretQuestion2 == q.id; }); } } vm.isLoaded = true; } refreshSecurityQuestionsSelectComponent(); }); } function getCountries(userCountryId) { userRepository.getCountries().success(function (response, status, headers, config) { if (errorHandler.check(response, status, config) == errorHandler.STATUS.SUCCESS) { vm.countries = response.data.map(function (c) { return {"id": c.id, "name": c.name, "code": c.iso2Code}; }); vm.profile.countryUI = vm.countries.find(function (c) { return c.id == userCountryId; }); } refreshCountrySelectComponent(); }); } function refreshCountrySelectComponent(){ setTimeout(function(){ $('#countrySelectPicker').selectpicker('refresh'); },1); } function refreshGenderSelectComponent(){ setTimeout(function(){ $('#selectGender').selectpicker('refresh'); },5); } function refreshSecurityQuestionsSelectComponent(){ setTimeout(function(){ $('#securityQuestions1SelectPicker').selectpicker('refresh'); $('#securityQuestions2SelectPicker').selectpicker('refresh'); },1); } //Uploader logic function transformImage(newImage) { setTimeout(function () { try{ var fd = new FormData(); var imgBlob = convertHelper.dataURIToBlob(newImage || vm.profile.imageThumbnailUrl); fd.append('file', imgBlob, convertHelper.renderImageName(imgBlob.type)); vm.tempFormData.data = fd; vm.tempFormData.isImageSelected = true; }catch(error){ console.log(error); } }, 10); } // Main. vm.getUserById(vm.currentProfileId); $scope.$on("$destroy", function(){ vm.unsubscribe(); }); window.UPDATE_PROFILE = vm; } // IoC container. userProfileEdit.$inject = [ "$scope", '$state', "repository.user", "$filter", "helpers.convert", "appUtils.errorHandler", "appUtils.uiNotifications", "$translate", "helpers.localization", "repository.apiMediaUploader" ]; })();