'use strict'; /** * controllers for ng-table * Simple table with sorting and filtering on AngularJS */ app.controller('studentCtrl', ["$scope", "toaster", "$filter", "ngTableParams", "$http", "API_POINTS", "$state", '$rootScope', '$cookies', 'md5', 'ipCookie', '$timeout', '$window', "flowFactory", function ($scope, toaster, $filter, ngTableParams, $http, apiPoint, $state, $rootScope, $cookies, md5, ipCookie, $timeout, $window, flowFactory) { /** * student details capture * by : kdk */ $scope.myModel = { 'primaryPhone': '', 'firstname': '', 'lastname': '', 'fathername': '', 'mothername': '', 'emailId': '', 'secondaryPhone': '', 'dateofbirth': '', 'gender': '', 'aadharNumber': '', 'otherId': '', 'otherIdDetails': '', 'address1': '', 'address2': '', 'caste': '', 'category': '', 'religion': '', 'nationality': '', 'prequalification': '', 'occupation': '', 'designation': '', 'companyname': '', 'referredby': '', 'referredmobilenumber': '', 'switchsetting': true, 'DeactiveComments': '', 'Comments': '' }; $scope.myModelCourse = { 'university': '', 'course': '', 'batch': '', 'dateofjoining': '', 'enrollmentid': '' }; $scope.myModelCourseAdd = { 'university': '', 'course': '', 'batch': '', 'dateofjoining': '', 'enrollmentid': '' }; $scope.addStudentView = function () { // $scope.myModelCourseAdd = { // 'university': '', // 'course': '', // 'batch': '', // 'dateofjoining': '', // 'enrollmentid': '' // }; // $scope.courseDataAdd = ''; // $scope.batchDataAdd = ''; } // 2017-11-23T00:00:00+0530 // 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 } // get local client details var localDetail = JSON.parse(localStorage.getItem('localObj')); var localDetails = ipCookie('cookiechk'); // load when html page load $scope.init = function () { var nowInMS = new Date().getTime(), sixteenYearsInMS = 1000 * 60 * 60 * 24 * 365 * 16, sixteenYearsBeforeNow = new Date(nowInMS - sixteenYearsInMS); if (localDetail != null) { $scope.currentDate = new Date(sixteenYearsBeforeNow); $scope.getStudentList(); $scope.getUniversityList(); $scope.myModel.dateofbirth=$filter('date')($scope.currentDate, 'dd-MM-yyyy'); } else { } } $scope.getStudentList = function () { $scope.loader = true; var empListreq = { method: 'POST', url: apiPoint.url + 'getStudentList/', headers: { 'Content-Type': 'application/json' }, data: { data: localDetails } }; $http(empListreq).then(function (response) { if (response.data.studentList) { $scope.loader = false; $scope.studentData = true; $scope.data = response.data.student_details; } else { $scope.loader = false; $scope.studentData = false; } }); } $scope.editId = -1; $scope.editCourseId = -1; $scope.setEditId = function (id) { $scope.editId = id; $scope.editCourseId = -1; } $scope.setEditCourseId = function (id) { $scope.list = false; $scope.getStudentCourseList(id); $scope.currentStudentId = id; // $scope.getStudentCourseDetails(id); $scope.editCourseId = id; $scope.editId = -1; $scope.courseEditLoading = true; } $scope.cancel = function () { $scope.getStudentList(); $scope.editId = -1; $scope.editCourseId = -1; } $scope.cancelCourseDetails = function () { $scope.getStudentCourseList($scope.currentStudentId); // $scope.editId = -1; // $scope.editCourseId = -1; } $scope.addStudentCourse = function () { $scope.myModelCourse = { 'university': '', 'course': '', 'batch': '', 'dateofjoining': '', 'enrollmentid': '' }; $scope.list = false; $scope.courseEdit = false; $scope.courseAdd = true; } // add student course $scope.addCourseFORM = { submit: function (form, myModelCourse, 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 { // alert(JSON.stringify(p.StudentID)); var formate = "dd-MM-yyyy"; // $scope.myModel.switchsetting = $scope.myModel.switchsetting === true ? 1 : 0; // $scope.myModel.dateofbirth = $filter('date')(new Date($scope.myModel.dateofbirth), formate); $scope.myModelCourse.dateofjoining = $filter('date')(new Date($scope.myModelCourse.dateofjoining), formate); var req = { method: 'POST', url: apiPoint.url + 'insertStudentCourse/', headers: { 'Content-Type': 'application/json' }, data: { addStudent: p.StudentID, coursedata: $scope.myModelCourse, loginBranch: localDetails.localBranchID, createdBy: localDetails.localUserID, } }; $http(req).then(function (response) { if (response.data.addStuCourseStatus == true) { swal("Success!", response.data.message, "success"); $scope.getStudentCourseList($scope.currentStudentId); } else { swal("Failed!", response.data.message, "error"); $scope.getStudentCourseList($scope.currentStudentId); } }); } } } // get Student Course List $scope.getStudentCourseList = function (studentId) { $scope.courseEditLoading = true; $scope.list = true; $scope.courseEdit = false; $scope.courseAdd = false; var req = { method: 'POST', url: apiPoint.url + 'getStudentCourseList/', headers: { 'Content-Type': 'application/json' }, data: { studentID: studentId } }; $http(req).then(function (response) { if (response.data.stuCourseDetails) { $scope.courseEditLoading = false; $scope.studentCourseList = response.data.course_details; // $scope.stucour = $scope.studentCourse[0]; } else { } }); } // get student particular course details : params : studentCourseID $scope.getStudentCourseDetails = function (studentcourseID) { $scope.courseEditLoading = true; $scope.stucour = ''; $scope.list = false; $scope.courseAdd = false; $scope.courseEdit = true; var req = { method: 'POST', url: apiPoint.url + 'getStudentCourseDetails/', headers: { 'Content-Type': 'application/json' }, data: { studentcourseId: studentcourseID } }; $http(req).then(function (response) { if (response.data.stuCourseDetails) { $scope.courseEditLoading = false; $scope.studentCourse = response.data.course_details; $scope.stucour = $scope.studentCourse[0]; } else { } }); } // update sutdent course details $scope.updateStudentCourseDetails = function (form, datas, hidedoj) { 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 { // alert(datas.DOJ); var formate = "dd-MM-yyyy"; var checkDOJ = $filter('date')(new Date(datas.DOJ), formate); datas.IsActive = datas.IsActive === true ? 1 : 0; datas.DOJ = datas.DOJ == hidedoj ? datas.DOJ : $filter('date')(new Date(datas.DOJ), formate); // alert(datas.DOJ); var req = { method: 'POST', url: apiPoint.url + 'updateStudentCourseDetail/', headers: { 'Content-Type': 'application/json' }, data: { data: datas, updatedBy: localDetails.localUserID, } }; $http(req).then(function (response) { if (response.data.updateStuCourseStatus) { swal("Success!", response.data.message, "success"); $scope.getStudentCourseList($scope.currentStudentId); // $scope.editCourseId = false; } else { // $scope.ldloading1[loading.replace('-', '_')] = false; swal("Failed!", response.data.message, "error"); $scope.getStudentCourseList($scope.currentStudentId); } }); } } $scope.universityData = ''; $scope.getUniversityList = function () { var empListreq = { method: 'POST', url: apiPoint.url + 'getStudentUniversity/', headers: { 'Content-Type': 'application/json' }, data: { data: localDetails } }; $http(empListreq).then(function (response) { if (response.data.univList) { $scope.universityData = response.data.university_details; } else { } }); } $scope.onSlecetUnivEdit = function (id) { $scope.myModelCourse.university = id; $scope.onSlecetUniv(); } $scope.onChangeSlecetUnivEdit = function (id) { $scope.myModelCourse.university = id; $scope.onSlecetUniv(); $scope.stucour.CourseID = ''; $scope.stucour.BatchCode = ''; } $scope.courseLoading = false; $scope.onSlecetUniv = function () { $scope.courseData = ''; $scope.batchData = ''; $scope.myModelCourse.course = ''; $scope.myModelCourse.batch = ''; $scope.courseLoading = true; var univListreq = { method: 'POST', url: apiPoint.url + 'getCourseBatchForUniv/', headers: { 'Content-Type': 'application/json' }, data: { data: $scope.myModelCourse.university } }; $http(univListreq).then(function (response) { if (response.data.courseStatus) { $scope.courseLoading = false; $scope.courseData = response.data.course_details; $scope.batchData = response.data.batch_details; } else { $scope.courseLoading = false; } }); } $scope.onSlecetUnivAdd = function () { $scope.courseDataAdd = ''; $scope.batchDataAdd = ''; $scope.myModelCourseAdd.course = ''; $scope.myModelCourseAdd.batch = ''; $scope.courseLoading = true; var univListreq = { method: 'POST', url: apiPoint.url + 'getCourseBatchForUniv/', headers: { 'Content-Type': 'application/json' }, data: { data: $scope.myModelCourseAdd.university } }; $http(univListreq).then(function (response) { if (response.data.courseStatus) { $scope.courseLoading = false; $scope.courseDataAdd = response.data.course_details; $scope.batchDataAdd = response.data.batch_details; } else { $scope.courseLoading = false; } }); } // check mobile number existing $scope.checkMobileExist = function () { var checkReq = { method: 'POST', url: apiPoint.url + 'chkStudentExistDetail/', headers: { 'Content-Type': 'application/json' }, data: { data: $scope.myModel.primaryPhone, check: 'mobileNumber' } }; $http(checkReq).then(function (response) { if (response.data.existMobile == true) { swal("Warning!", response.data.message, "warning"); $scope.myModel.primaryPhone = ''; } else { } }); } // check emailId existing $scope.checkEmailExist = function () { var checkReq = { method: 'POST', url: apiPoint.url + 'chkStudentExistDetail/', headers: { 'Content-Type': 'application/json' }, data: { data: $scope.myModel.emailId, check: 'emailId' } }; $http(checkReq).then(function (response) { if (response.data.existEmailId == true) { swal("Warning!", response.data.message, "warning"); $scope.myModel.emailId = ''; } 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.files = []; //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.files[0] = args.file; }); }); // reset image $scope.resetImage = function () { $scope.files.length = 0; $scope.stepsModel.length = 0; } $scope.getChangeDate = function() { var formate = "dd-MM-yyyy"; $scope.myModel.dateofbirth = $filter('date')(new Date($scope.myModel.dateofbirth), formate); } // add student $scope.studentFORM = { submit: function (form, myModel, myModelCourseAdd) { 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.disableButton = true; var formate = "dd-MM-yyyy"; $scope.myModel.switchsetting = $scope.myModel.switchsetting === true ? 1 : 0; $scope.myModelCourseAdd.dateofjoining = $filter('date')(new Date($scope.myModelCourseAdd.dateofjoining), formate); var idStuProof = $scope.files[0]; if (idStuProof !== undefined) { var formData = new FormData(); var idProof = $scope.files[0]; // alert(idProof); formData.append("data", JSON.stringify($scope.myModel)); formData.append("coursedata", JSON.stringify($scope.myModelCourseAdd)); formData.append("createdBy", localDetails.localUserID); formData.append("loginBranch", localDetails.localBranchID); formData.append('idStuProof', idStuProof); $http.post(apiPoint.url + 'insertStudentImg/', formData, { transformRequest: angular.identity, headers: { 'Content-Type': undefined, 'Process-Data': false }, }).success(function (data) { if (data.addStudentStatus == true) { swal("Success!", data.message, "success"); $scope.disableButton = false; $state.go($state.current, {}, { reload: true }); } else { // $scope.ldloading1[loading.replace('-', '_')] = false; swal("Failed!", data.message, "error"); } }); } else { var req = { method: 'POST', url: apiPoint.url + 'insertStudent/', headers: { 'Content-Type': 'application/json' }, data: { data: $scope.myModel, coursedata: $scope.myModelCourseAdd, loginBranch: localDetails.localBranchID, createdBy: localDetails.localUserID, } }; $http(req).then(function (response) { if (response.data.addStudentStatus == true) { swal("Success!", response.data.message, "success"); $scope.disableButton = false; $state.go($state.current, {}, { reload: true }); } else { $scope.disableButton = false; swal("Failed!", response.data.message, "error"); } }); } } }, reset: function (form) { // $scope.myModel = angular.copy($scope.master); form.$setPristine(true); $scope.myModel = { 'employeeId': '', 'firstname': '', 'lastname': '', 'fathername': '', 'mothername': '', 'emailId': '', 'primaryPhone': '', 'secondaryPhone': '', 'dateofbirth': '', 'gender': '', 'qualificaion': '', 'aadharNumber': '', 'otherId': '', 'otherIdDetails': '', 'address1': '', 'address2': '', 'homeBranch': '', 'dateofjoining': '', 'dateofjoining': '', 'role': '', 'switchsetting': true, 'DeactiveComments': '', 'Comments': '' }; }, resetCourse: function (form) { form.$setPristine(true); $scope.myModelCourse = { 'university': '', 'course': '', 'batch': '', 'dateofjoining': '', 'enrollmentid': '' }; } } // update check mobile exist $scope.updateCheckMobileExist = function (p) { var checkReq = { method: 'POST', url: apiPoint.url + 'chkUpdateStuExistDetail/', headers: { 'Content-Type': 'application/json' }, data: { exceptId: p.StudentID, datas: p.MobileNumber, check: 'mobileNumber' } }; $http(checkReq).then(function (response) { if (response.data.existMobile == true) { swal("Warning!", response.data.message, "warning"); p.MobileNumber = response.data.resetValue.MobileNumber; } else { } }); } // update check email id exist $scope.updateCheckEmailExist = function (p) { var checkReq = { method: 'POST', url: apiPoint.url + 'chkUpdateStuExistDetail/', headers: { 'Content-Type': 'application/json' }, data: { exceptId: p.StudentID, datas: p.EmailID, check: 'emailId' } }; $http(checkReq).then(function (response) { if (response.data.existEmailId == true) { swal("Warning!", response.data.message, "warning"); p.EmailID = response.data.resetValue.EmailID; } else { } }); } $scope.stepsModelEdit = []; $scope.imageUploadEdit = 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.imageIsLoadedEdit; reader.readAsDataURL(file); } } $scope.imageIsLoadedEdit = function (e) { $scope.$apply(function () { // $scope.stepsModel.push(e.target.result); $scope.stepsModelEdit[0] = e.target.result; }); } //an array of files selected $scope.filesEdit = []; //listen for the file selected event $scope.$on("fileSelectedEdit", function (event, args) { $scope.$apply(function () { //add the file object to the scope's files collection $scope.filesEdit[0] = args.file; }); }); // reset image for Edit $scope.resetImageEdit = function () { $scope.filesEdit.length = 0; $scope.stepsModelEdit.length = 0; } // update student details $scope.updateStuDetails = function (form, p ,hidedob) { 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 { // alert(p.DOB); var idProof = $scope.filesEdit[0]; var formate = "dd-MM-yyyy"; var checkDOB = $filter('date')(new Date(p.DOB), formate); p.IsActive = p.IsActive === true ? 1 : 0; p.DOB = p.DOB == hidedob ? p.DOB : $filter('date')(new Date(p.DOB), formate); if(idProof == undefined) { // alert(idProof); var req = { method: 'POST', url: apiPoint.url + 'updateStudentExistDetail/', headers: { 'Content-Type': 'application/json' }, data: { data: p, updatedBy: localDetails.localUserID, } }; $http(req).then(function (response) { if (response.data.updateStuStatus = true) { swal("Success!", response.data.message, "success"); $scope.getStudentList(); $scope.editId = -1; } else { // $scope.ldloading1[loading.replace('-', '_')] = false; swal("Failed!", response.data.message, "error"); } }); } else { var formData = new FormData(); var idStuUpProof = $scope.filesEdit[0]; // alert(idProof); formData.append("data", JSON.stringify(p)); formData.append("updatedBy", localDetails.localUserID); formData.append('idStuUpProof', idStuUpProof); $http.post(apiPoint.url + 'updateStudentImgDetails/', formData, { transformRequest: angular.identity, headers: { 'Content-Type': undefined, 'Process-Data': false }, }).success(function (data) { if (data.updateStuStatus == true) { swal("Success!", data.message, "success"); $scope.getStudentList(); $scope.editId = -1; } else { // $scope.ldloading1[loading.replace('-', '_')] = false; swal("Failed!", data.message, "error"); } }); } } } }]); // 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] }); } }); } }; }); // file upload directive app.directive('fileUploadEdit', 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("fileSelectedEdit", { file: files[i] }); } }); } }; });