(function () { 'use strict'; angular .module('app') .controller('UserCreateController', userCreate); function userCreate($scope, $state, userRepository, apiMediaUploaderRepository, convertHelper, errorHandler, uiNotifications, $translate, localizationHelper) { var vm = this; vm.translationObject = {}; vm.loadTranslation = function(){ $translate(['Info', 'Success', 'Warning', 'Error', 'ClientCreated', 'ClientPhotoIsEmpty']).then(function (tr) { vm.translationObject = tr; }); }; vm.stgCountryId = -1; vm.stgSecretQuestion1 = -1; vm.stgSecretQuestion2 = -1; 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.user.countryUI){ vm.stgCountryId = vm.profile.user.countryUI.id; } vm.loadTranslation(); refreshGenderSelectComponent(); getCountries(); getSecurityQuestions(true); }); vm.loadTranslation(); vm.genders = [ { id: 1, value: 'male', key: "Male" }, { id: 2, value: 'female', key: 'Female' } ]; vm.selectedGender = null; vm.defaultAvatar = "images/default-user.png"; vm.profile = { user: { firstName: "", lastName: "", email: "", birthDate: "", imageThumbnailUrl: null, city: "", countryId: null, countryUI: null, address: "", state: "", postalCode: "", imageUrl: "", phoneNumber: "", gender: "" }, verification: { securityQuestion1: { securityQuestionId: 1, answer: "" }, securityQuestion2: { securityQuestionId: 2, answer: "" } }, password: "", confirmPassword: "" }; vm.countries = []; vm.questions = []; vm.questionList1 = []; vm.questionList2 = []; vm.questionPair1 = {}; vm.questionPair2 = {}; vm.isLoaded = false; // vm.loadingCounter = 0; // vm.maxLoadingCounter = 2; vm.bankInfoForm = null; // Bind jQuery datePicker to html-element. userRepository.updateDate("#BirthDate", null, true, new Date()); vm.tempFormData = { data: null, isImageSelected: false }; $("#user-image").on('change', function(){ transformImage(); }); //Methods. vm.createUser = function() { vm.profile.user.gender = vm.selectedGender.value; vm.profile.verification.securityQuestion1.securityQuestionId = vm.questionPair1.id; vm.profile.verification.securityQuestion2.securityQuestionId = vm.questionPair2.id; vm.profile.user.imageThumbnailUrl = null; vm.profile.user.countryId = vm.countries.find(function (c) { return c.name == vm.profile.user.countryUI.name }).id; apiMediaUploaderRepository.uploadImageFormData(vm.tempFormData.data, true).then(function (response) { if (response.success) { vm.profile.user.imageUrl = response.data[0].fileUrl; vm.profile.user.imageThumbnailUrl = response.data[0].fileThumbnailUrl; userRepository.add(vm.profile).success(function (response, status, headers, config) { if (errorHandler.check(response, status, config) == errorHandler.STATUS.SUCCESS) { uiNotifications.inform(vm.translationObject['ClientCreated'], 'success'); $state.go('main.users',{"status":0}); } else if (response && !response.success) { uiNotifications.inform(response.message, 'warning'); } }); } }); }; vm.removeImage = function(){ vm.profile.user.imageThumbnailUrl = null; $("#user-image").val(null) }; 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(); }; function getCountries() { 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}; }); if(vm.stgCountryId && vm.stgCountryId != -1){ vm.profile.user.countryUI = vm.countries.find(function(c){ return c.id == vm.stgCountryId; }); } resetBrowserAutocompletedFields(); refreshCountrySelectComponent(); vm.isLoaded = true; } }); } 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; vm.questionList2 = vm.questions; 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; }); } } resetBrowserAutocompletedFields(); refreshSecurityQuestionsSelectComponent(); vm.isLoaded = true; } }); } function resetBrowserAutocompletedFields(){ vm.profile.user.birthDate = ""; vm.profile.password = ""; } function transformImage(newImage){ setTimeout(function(){ try { var fd = new FormData(); var imgBlob = convertHelper.dataURIToBlob(newImage || vm.profile.user.imageThumbnailUrl); fd.append('file', imgBlob, convertHelper.renderImageName(imgBlob.type)); vm.tempFormData.data = fd; vm.tempFormData.isImageSelected = true; } catch(error) { console.log(error); } }, 10); } // Main. getCountries(); getSecurityQuestions(); 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); } $scope.$on("$destroy", function(){ vm.unsubscribe(); }); window.CREATE_USER = vm; refreshGenderSelectComponent(); var counter = 400; } // IoC container. userCreate.$inject = [ "$scope", "$state", "repository.user", "repository.apiMediaUploader", "helpers.convert", "appUtils.errorHandler", "appUtils.uiNotifications", "$translate", "helpers.localization" ]; })();