'use strict'; /** * controllers for ng-table * Simple table with sorting and filtering on AngularJS */ app.controller('universityCtrl', ["$scope", "toaster", "$filter", "ngTableParams", "$http", "API_POINTS", "$state", '$rootScope', '$cookies', 'md5', 'ipCookie', '$timeout', '$window', function ($scope, toaster, $filter, ngTableParams, $http, apiPoint, $state, $rootScope, $cookies, md5, ipCookie, $timeout, $window) { /** * university details capture * by : kdk */ // get local cliend details var localDetail = JSON.parse(localStorage.getItem('localObj')); var localDetails = ipCookie('cookiechk'); //alert(JSON.stringify(localDetail)); $scope.myModel = { 'UniversityID': '', 'UniversityName': '', 'UniversityShortName': '', 'MobileNumber': '', 'EmailID': '', 'Address': '', 'switchsetting': true, }; $scope.init = function() { // alert(); const LOCALTYPE = localDetail.localType; if (localDetail != null && (LOCALTYPE == 'R003' || LOCALTYPE == 'R004' || LOCALTYPE == 'R002' || LOCALTYPE == 'R005')) { $scope.getUniversityList(); }else { if(localDetail != null && LOCALTYPE != 'R001') { $state.go('app.dashboard'); } else { ipCookie.remove('cookiechk'); window.localStorage.clear(); $state.go('login.signin'); } } } // sorting function for table params $scope.sort = function (keyname) { $scope.sortKey = keyname; //set the sortKey to the param passed $scope.reverse = !$scope.reverse; //if true make it false and vice versa } $scope.getUniversityList = function () { $scope.loader = true; var empListreq = { method: 'POST', url: apiPoint.url + 'getUniversity/', headers: { 'Content-Type': 'application/json' }, data: { data: localDetails } }; $http(empListreq).then(function (response) { if (response.data.univList) { $scope.loader = false; $scope.emptyUniversity = true; $scope.univ = response.data.university_details; } else { $scope.loader = false; $scope.emptyUniversity = false; } }); } // check employee Id existing $scope.checkUnivIdExist = function () { var checkReq = { method: 'POST', url: apiPoint.url + 'chkUnivIdExistDetail/', headers: { 'Content-Type': 'application/json' }, data: { data: $scope.myModel.UniversityID, check: 'univId', branchCode: localDetails.localBranchID, } }; $http(checkReq).then(function (response) { if (response.data.existUniv === true) { swal("", "University Id already exists", "warning"); $scope.myModel.UniversityID = ''; } else { } }); } //--------------------------------------------------------------> // getting for image to show //--------------------------------------------------------------> $scope.stepsModel = []; $scope.imageUpload = function (event) { var files = event.target.files; //FileList object for (var i = 0; i < files.length; i++) { var file = files[i]; var reader = new FileReader(); reader.onload = $scope.imageIsLoaded; reader.readAsDataURL(file); } } $scope.imageIsLoaded = function (e) { $scope.$apply(function () { // $scope.stepsModel.push(e.target.result); $scope.stepsModel[0] = e.target.result; }); } //an array of files selected $scope.filesimage = []; //listen for the file selected event $scope.$on("fileSelected", function (event, args) { $scope.$apply(function () { //add the file object to the scope's files collection $scope.filesimage[0] = args.file; // console.log($scope.filesimage); }); }); // reset image $scope.resetImage = function () { $scope.filesimage.length = 0; $scope.stepsModel.length = 0; } //------------------image functions over ----------> $scope.setEditId = function (id) { // alert(id); $scope.editId = id; } $scope.cancel = function () { $scope.getUniversityList(); $scope.editId = -1; } $scope.updateUniversityDetails = function (form, p) { //alert() var firstError = null; if (form.$invalid) { var field = null, firstError = null; for (field in form) { if (field[0] != '$') { if (firstError === null && !form[field].$valid) { firstError = form[field].$name; } if (form[field].$pristine) { form[field].$dirty = true; } } } angular.element('.ng-invalid[name=' + firstError + ']').focus(); // swal("The form cannot be submitted because it contains validation errors!", "Errors are marked with a red, dashed border!", "error"); } else { p.IsActive = p.IsActive === true ? 1 : 0; var idProof = $scope.filesimage[0]; if(idProof === undefined) { //alert('if') // alert(JSON.stringify(p)) var req = { method: 'POST', url: apiPoint.url + 'updateUniversityDetail/', headers: { 'Content-Type': 'application/json' }, data: { data: JSON.stringify(p), updatedBy: localDetails.localUserID, branchCode: localDetails.localBranchID } }; $http(req).then(function (response) { if (response.data.updateUniversityStatus = true) { swal(" ", "Updated successfully.", "success"); $scope.getUniversityList(); $scope.editId = -1; } else { // $scope.ldloading1[loading.replace('-', '_')] = false; swal(" ", "Something went wrong, please try again.", "error"); } }); } else { //alert('else') var formData = new FormData(); var idProof = $scope.filesimage[0]; //alert(idProof) formData.append("data", JSON.stringify(p)); formData.append("updatedBy", localDetails.localUserID); formData.append("branchCode", localDetails.localBranchID); formData.append('idProof', idProof); $http.post(apiPoint.url + 'updateUniversityDetail/', formData, { transformRequest: angular.identity, headers: { 'Content-Type': undefined, 'Process-Data': false }, }).success(function (data) { if (data.updateUniversityStatus == true) { swal("Success!", data.message, "success"); $state.go($state.current, {}, { reload: true }); } else { // $scope.ldloading1[loading.replace('-', '_')] = false; swal("Failed!", data.message, "error"); } }); } } } // add university $scope.universityForm = { submit: function (form, myModel) { var firstError = null; if (form.$invalid) { var field = null, firstError = null; for (field in form) { if (field[0] != '$') { if (firstError === null && !form[field].$valid) { firstError = form[field].$name; } if (form[field].$pristine) { form[field].$dirty = true; } } } angular.element('.ng-invalid[name=' + firstError + ']').focus(); // swal("The form cannot be submitted because it contains validation errors!", "Errors are marked with a red, dashed border!", "error"); } else { $scope.myModel.switchsetting = $scope.myModel.switchsetting === true ? 1 : 0; var idProof = $scope.filesimage[0]; var formData = new FormData(); //var idProof = $scope.filesimage[0]; //alert(idProof); // var req = { // method: 'POST', // url: apiPoint.url + 'insertUniversity/', // headers: { // 'Content-Type': 'application/json' // }, // data: { // data: $scope.myModel, // branchCode: localDetails.localBranchID, // createdBy: localDetails.localUserID, // } // }; formData.append("data", JSON.stringify($scope.myModel)); formData.append("branchCode", localDetails.localBranchID); formData.append("createdBy", localDetails.localUserID); formData.append('idProof', idProof); $http.post(apiPoint.url + 'insertUniversity/', formData, { transformRequest: angular.identity, headers: { 'Content-Type': undefined, 'Process-Data': false }, }).success(function (data) { if (data.addUniv == true) { // swal("Success!", data.message, "success"); // $state.go($state.current, {}, { reload: true }); swal("Success!", data.message, "success"); $state.go($state.current, {}, { reload: true }); } else { // $scope.ldloading1[loading.replace('-', '_')] = false; swal("Failed!", data.message, "error"); } }); // $http(req).then(function (response) { // if (response.data.addUniv == true) { // swal("", "University details added successfully.", "success"); // $state.go($state.current, {}, { reload: true }); // } // else if(response.data.addUniv == false){ // swal("","This University Id is already exist!", "error"); // } // else { // // $scope.ldloading1[loading.replace('-', '_')] = false; // swal("","Something went wrong, please try again.", "error"); // } // }); } }, reset: function (form) { // $scope.myModel = angular.copy($scope.master); form.$setPristine(true); $scope.myModel = { 'UniversityID': '', 'UniversityName': '', 'UniversityShortName': '', 'MobileNumber': '', 'EmailID': '', 'Address': '', }; } } }]); // file upload directive app.directive('fileUpload', function () { return { scope: true, //create a new scope link: function (scope, el, attrs) { el.bind('change', function (event) { var files = event.target.files; //iterate files since 'multiple' may be specified on the element for (var i = 0; i < files.length; i++) { //emit event upward scope.$emit("fileSelected", { file: files[i] }); } }); } }; });