var sesfControllers = angular.module('sesfControllers', []); sesfControllers.factory('ImageUpload', ['$fileUploader', '$rootScope', function($fileUploader, $rootScope){ function formDataForUpload(data) { if (!data || !angular.isArray(data)) { return []; } var out = []; angular.forEach(data, function (row) { if (!row || typeof row !== 'object') { return; } var o = {}; angular.forEach(row, function (v, k) { if (k.charAt(0) === '$') { return; } if (v === null || v === void 0) { return; } if (typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean') { o[k] = v; } else { o[k] = String(v); } }); out.push(o); }); return out; } return { init: function(config, scope, callback){ var uploader = $fileUploader.create({ url: config.url, scope: scope, headers: {}, formData: formDataForUpload(config.data), removeAfterUpload: config.removeAfterUpload }).bind( 'afteraddingfile', function( event, item ) { $rootScope.log('added file', item); scope.item = item; }).bind( 'complete', function( event, xhr, item ) { $rootScope.log( 'Complete: ' + JSON.parse(xhr.response) ); item.progress = null; item.file = null; item.response = JSON.parse(xhr.response); if(!!item.response) { item.status = 'error'; } else { item.response = 'Success'; item.status = 'success'; } if(typeof(callback) == 'function') { callback.call(item.response); } }); scope.$on('$destroy', function() { uploader.destroy(); }); return uploader; } }; }]); sesfControllers.factory( 'HomeService', [ '$rootScope', '$http', '$resource', '$route', function( rootScope, $http, $resource, $route) { return { Sponsors: function () { return $resource('event/sponsors', {limit: 6, all: true}); }, Events: function() { return $resource('home/events', {limit: 1}); }, Report: function() { return $resource('home/report'); } }; }]); sesfControllers.controller('homeController', ['$scope', 'HomeService', '$timeout', '$log', '$window', function homeController(scope, HomeService, $timeout, $log, $window) { $log.info('loading home page'); scope.sponsors = HomeService.Sponsors().get(); scope.events = HomeService.Events().query(); scope.carouselIndex = 0; function syncPastVideosCarouselLayout() { var $carousel = $('.carousel.my-slider.standard'); if (!$carousel.length) { return; } var $container = $carousel.parent('.rn-carousel-container'); var $slides = $carousel.children('li'); var idx = scope.carouselIndex; if (idx < 0 || idx >= $slides.length) { return; } var narrow = $window.matchMedia && $window.matchMedia('(max-width: 767px)').matches; if (narrow) { $container.css('height', ''); $carousel.css('height', ''); } else { var slideEl = $slides[idx]; var h = slideEl.scrollHeight + 40; $container.height(h); $carousel.height(slideEl.scrollHeight); } } scope.$watch('carouselIndex', function() { $timeout(syncPastVideosCarouselLayout, 0); }); angular.element($window).on('resize.sesfPastVideos', syncPastVideosCarouselLayout); scope.$on('$destroy', function() { angular.element($window).off('resize.sesfPastVideos'); }); $timeout(syncPastVideosCarouselLayout, 0); $timeout(syncPastVideosCarouselLayout, 150); try{ FB.XFBML.parse(); } catch(err) {} }]); sesfControllers.controller('emptyController', function() { $('html,body').animate({ scrollTop: 0 }, 1); }); sesfControllers.controller('reportPage', ['$scope', '$modal', function(scope, $modal) { scope.reportPage = function(){ var reportModal = $modal.open({ templateUrl: '/template/home?only=report_page', controller: 'reportPageModal' }); }; }]); sesfControllers.controller('reportPageModal', ['$scope', '$modalInstance', 'HomeService', '$timeout', '$log', '$rootScope', 'LoginService', '$location', function(scope, modalInstance, HomeService, $timeout, $log, $rootScope, LoginService, $location) { scope.message = {}; scope.isLoggedIn = LoginService.is_logged_in; scope.ok = function () { var data = scope.message; data.user_id = LoginService.user_id; data.log = JSON.stringify($log.exportLogs()); data.location = $location.path(); var message = HomeService.Report(); message = new message(data); message.$save(null, function(response){ scope.report_page = { response: 'Message Sent', status: 'success' }; $timeout(function() { modalInstance.close(); }, 2000); },function(response) { $log.error('failed to send', scope.message, response); scope.report_page = { response: response.data, status: 'error' }; }); }; scope.cancel = function () { modalInstance.dismiss(); }; }]); sesfControllers.service( 'EventsService', [ '$resource', function( $resource ) { var service = { Events: function(){ return $resource('events/car-shows', {is_active: 1}); } }; return service; }]); sesfControllers.controller('eventsController', ['$scope', 'EventsService', function (scope, EventsService) { scope.events = {}; scope.events.active = EventsService.Events().query(); scope.events.inactive = EventsService.Events().query({is_active: 0}); }]); sesfControllers.factory( 'EventService', [ '$rootScope', '$http', '$resource', '$route', function( rootScope, $http, $resource, $route) { var service = { // EventInfo uses show_id from eventId only; gallery/manage routes have no eventId — eventController skips the HTTP call. EventInfo: function () { if(!$route.current.params) { return FALSE; } return $resource('event/info', {show_id: $route.current.params.eventId}); }, Agenda: function() { return $resource('event/agenda', {show_id: $route.current.params.eventId, type_id: 1}); }, Locations: function() { return $resource('event/locations', {show_id: $route.current.params.eventId}); }, Registration: function() { return $resource('event/registration', {show_id: $route.current.params.eventId}); }, Extra: function() { return $resource('event/extra', {extra_id: $route.current.params.extraId}); }, Cars: function() { return $resource('event/cars', {show_id: $route.current.params.eventId}); }, Sponsors: function() { return $resource('event/sponsors', {show_id: $route.current.params.eventId}); }, Schedule: function() { return $resource('event/schedule', {show_id: $route.current.params.eventId}); } }; return service; }]); sesfControllers.controller('eventScheduleController', ['$scope', 'EventService', function(scope, EventService) { scope.schedule = EventService.Schedule().get(); scope.sponsors = EventService.Sponsors().get(); }]); sesfControllers.controller('eventController', ['$scope', 'EventService', 'LoginService', '$route', function (scope, EventService, LoginService, $route) { if ($route.current.params.eventId) { scope.event_info = EventService.EventInfo().get(); } else { scope.event_info = {}; } scope.user_data = LoginService.user_data; }]); sesfControllers.controller('eventRegisterController', ['$scope', '$location', 'EventService', 'appStorage', '$location', 'registration', 'cars', 'EventService', '$timeout', function (scope, location, EventService, appStorage, location, registration, cars, EventService, $timeout) { breadCrumb = appStorage('sesfStorage', 'breadCrumb', scope); if(scope.breadCrumb.registrationData && scope.breadCrumb.lastPage == 'newCar') { scope.registration = scope.breadCrumb.registrationData; breadCrumb.clear(); } else scope.registration = registration; scope.registration.cars = cars; var event_info = EventService.EventInfo().get(); breadCrumb.set('lastPage', 'eventRegistration'); breadCrumb.set('page', location.path()); breadCrumb.set('registrationData', scope.registration); scope.register = function(){ breadCrumb.clear(); scope.registrationResponse = {}; scope.registration.$save(null, function(response){ $('html,body').animate({ scrollTop: 0 }, 100); location.url(event_info.memberProfileURL.replace('#','') + '/billing/' + response.registration.id); return; scope.registrationResponse.all = { response: 'Successfully submitted', status: 'success' }; },function(response){ scope.registrationResponse = { response: response.data, status: 'error' }; }); } }]); sesfControllers.controller('eventExtraController', ['$scope', 'EventService', 'registration', function (scope, EventService, registration) { scope.registration = registration; scope.submit_extra = function(){ scope.registrationResponse = {}; extra = EventService.Extra(); console.log('extras', {extras: scope.registration.registration.extras}); extra.save({extras: scope.registration.registration.extras}, function(){ scope.registrationResponse.all = { response: 'Successfully submitted', status: 'success' }; },function(response){ scope.registrationResponse.all = { response: response.data, status: 'error' }; }); } }]); sesfControllers.controller('eventSponsorController', ['$scope', 'sponsors', function(scope, sponsors) { console.log(sponsors) sponsors.$promise.then(function(val){ console.log(val.sponsors); if(typeof val.sponsors != 'undefined') { var temp = []; val.sponsors.forEach(function(sponsor){ if(typeof temp[sponsor.type_id] == 'undefined') { temp[sponsor.type_id] = [sponsor]; } else { temp[sponsor.type_id].push(sponsor); } }); var sponsorTypes = []; temp.forEach(function(type){ sponsorTypes.push(type); }); console.log(sponsorTypes); scope.sponsors = sponsorTypes; } }) //scope.sponsors = sponsors; }]); sesfControllers.controller('eventAgendaController', ['$scope', 'agenda', 'sponsors', function (scope, agenda, sponsors) { scope.agenda = agenda; scope.sponsors = sponsors; }]); sesfControllers.controller('eventJudgingController', ['$scope', 'agenda', function (scope, agenda) { scope.agenda = agenda; }]); sesfControllers.controller('eventAttendeesController', ['$scope', 'EventService', function (scope, EventService) { }]); sesfControllers.controller('eventDirectionsController', ['$scope', 'EventService', 'LoginService', 'locations', function (scope, EventService, LoginService, locations) { scope.locations = locations; scope.user_data = LoginService.user_data; for (var i = 0; i < scope.locations.length; i++) { scope.locations[i].zoom = 14; scope.locations[i].id = 'angularMap' + i; scope.locations[i].options = { map: { center: new google.maps.LatLng(scope.locations[i].address.lattitude, scope.locations[i].address.longitude), zoom: 14, /* mapTypeId: google.maps.MapTypeId.STEET */ }, volcanoes: { icon: 'https://maps.gstatic.com/mapfiles/ms2/micons/red-dot.png', } }; scope.locations[i].markers = [ { name: scope.locations[i].name, location: { lat: scope.locations[i].address.lattitude, lng: scope.locations[i].address.longitude } } ]; } scope.getMarkerOpts = function(marker) { return angular.extend( { title: marker.name } ); }; }]); sesfControllers.factory('RegistrationService', ['$resource', '$route', function($resource, $route){ return { Registration: function(){ return $resource('/member/register', null); } }; }]); sesfControllers.controller('registerController', ['$rootScope', '$scope', 'RegistrationService', '$location', 'LoginService', function (rootScope, scope, RegistrationService, $location, LoginService) { scope.registrationResponse = {}; var reg = RegistrationService.Registration(); var reg = new reg(); scope.reg = reg; scope.isSubmitting = false; scope.submit = function(){ if (scope.isSubmitting) return; scope.isSubmitting = true; rootScope.log('reg', scope.reg); $('html,body').animate({ scrollTop: 0 }, 100); reg.$save(null,function(response){ // Stay disabled on success — page is about to redirect via LoginService. scope.registrationResponse = { response: 'Your account has been successfully created', status: 'success' }; LoginService.Authenticate(); scope.user_info = response; },function(response){ scope.isSubmitting = false; rootScope.log('error', response); scope.registrationResponse = { response: response.data, status: 'error' }; // Turnstile tokens are single-use; refresh after every failed submit // so the next click sends a fresh token instead of a stale one. if (window.sesfTurnstileReset) { window.sesfTurnstileReset(); } }); } if(LoginService.is_logged_in === true) { $location.path(LoginService.user_data.profileURL.replace('#', '')); } }]); sesfControllers.factory( 'MembersService', [ '$resource', '$location', function($resource, $location ) { var service = { filters: function () { return $resource('members/list-filters', $location.search()); }, paginate: function () { return $resource('members/users', $location.search()); } }; return service; }]); sesfControllers.controller('membersController', ['$scope', 'MembersService', '$location', 'appStorage', '$route', function (scope, MembersService, $location, appStorage, $route) { breadCrumb = appStorage('sesfStorage', 'breadCrumb', scope); breadCrumb.set('lastPage', 'memberList'); scope.isEventPage = $route.current.$$route.eventPage; var show_id = $route.current.$$route.eventPage ? $route.current.params.eventId : $location.search().show_id; scope.baseUrl = $route.current.$$route.eventPage ? '#' + $location.path().replace('/members', '').replace('/cars', '') : '#'; scope.filters = MembersService.filters().query({eventPage: $route.current.$$route.eventPage}); scope.members = MembersService.paginate().get({limit: $route.current.$$route.eventPage ? 25 : 20, show_id: show_id}); }]); sesfControllers.factory('MemberService', ['$resource', '$route', function($resource, $route){ return { Profile: function(){ return $resource('/member/profile', {user_id: $route.current.params.memberId}); }, Password: function(){ return $resource('/login/update-password', {user_id: $route.current.params.memberId}); }, AccountInfo: function(){ return $resource('/member/account-info', {user_id: $route.current.params.memberId}); }, Preferences: function(){ return $resource('/member/private-preferences', { user_id: $route.current.params.memberId }); }, Location: function(){ return $resource('/member/location', {user_id: $route.current.params.memberId}); }, InvoiceList: function() { return $resource('/member/invoice-list', {user_id: $route.current.params.memberId}); }, Invoice: function(){ return $resource('/member/invoice', { registration_id: $route.current.params.registrationId }); }, Cars: function(){ return $resource('/member/cars', { user_id: $route.current.params.memberId }); }, Shows: function(){ return $resource('/member/shows', { user_id: $route.current.params.memberId }); }, Tutorial: function(){ return $resource('/tutorial/tutorial', null); }, Message: function(){ return $resource('/member/message', { user_id: $route.current.params.memberId }); }, SellCar: function(){ return $resource('/member/sell-car', { user_id: $route.current.params.memberId }); }, DeleteAccount: function(){ return $resource('/member/delete-account', { user_id: $route.current.params.memberId }); } }; }]); sesfControllers.controller('memberController', ['$scope', 'MemberService', '$location', 'appStorage', 'LoginService', '$modal', '$log', function (scope, MemberService, $location, appStorage, LoginService, $modal, $log) { breadCrumb = appStorage('sesfStorage', 'breadCrumb', scope); breadCrumb.set('lastPage', 'profile'); scope.user = MemberService.Profile().get(function(){ scope.is_private = scope.user.is_private; }); scope.addCar = function(){ $location.path('/car/new'); }; scope.isLoggedIn = function(){ return LoginService.is_logged_in; }; scope.sendEmail = function(){ var modalInstance = $modal.open({ templateUrl: '/template/member?only=send_email_modal', controller: 'memberSendEmail' }); }; }]); sesfControllers.controller('memberSendEmail', ['$scope', '$modalInstance', 'MemberService', '$timeout', '$log', function(scope, modalInstance, MemberService, $timeout, $log) { scope.message = {}; scope.isSubmitting = false; scope.ok = function () { if (scope.isSubmitting) return; scope.isSubmitting = true; var message = MemberService.Message(); message = new message(scope.message); message.$save(null, function(response){ $log.info('sent private email'); scope.message_response = { response: 'Message Sent', status: 'success' }; $timeout(function() { modalInstance.close(); }, 2000); },function(response) { $log.error('failed to send private email ' + JSON.stringify(scope.message) + ' ' + JSON.stringify(response)); scope.isSubmitting = false; scope.message_response = { response: response.data, status: 'error' }; }); }; scope.cancel = function () { modalInstance.dismiss(); }; }]); sesfControllers.controller('memberSettingsPreferencesController', ['$scope', 'MemberService', '$log', function (scope, MemberService, $log) { scope.preferences = MemberService.Preferences().get(); scope.submit = function(){ scope.loc_response = {}; scope.preferences.$save(null, function(){ $log.info('saved member settings preferences') scope.preferences_res = { response: 'Preferences Updated', status: 'success' }; },function(response){ $log.error('failed to save member settings preferences ' + JSON.stringify(response)); scope.preferences_res = { response: response.data, status: 'error' }; }); }; }]); sesfControllers.controller('memberBillingController', ['$scope', 'MemberService', 'invoiceList', '$route', '$log', function(scope, MemberService, invoiceList, $route, $log) { scope.invoiceList = invoiceList; scope.selectedInvoice = {}; scope.$watch('selectedInvoice', function(newVal, oldVal){ if(newVal != oldVal) scope.invoice = MemberService.Invoice().get({registration_id: newVal.id}); }); if($route.current.params.registrationId) { scope.invoice = MemberService.Invoice().get(); scope.$watch('invoiceList', function(){ angular.forEach(invoiceList, function(item){ if(item.id == $route.current.params.registrationId) { scope.selectedInvoice = item; } }); }); } else if(scope.invoiceList.length > 0) { scope.selectedInvoice = invoiceList[0]; scope.invoice = MemberService.Invoice().get({registration_id: scope.selectedInvoice.id}); } }]); sesfControllers.controller('memberSettingsAccountInfoController', ['$scope', 'MemberService', '$log', function (scope, MemberService, $log) { scope.account_info = {}; scope.submit = function(){ var data = { first: scope.user.member_info.first, last: scope.user.member_info.last, email: scope.user.member_info.email, phone: scope.user.member_info.phone } var AccountInfo = MemberService.AccountInfo(); var accountInfo = new AccountInfo(data); accountInfo.$save(null, function(){ $log.info('saved account info'); scope.account_info = { response: 'Account Info Updated', status: 'success' }; },function(response){ $log.error('failed to save account info ' + JSON.stringify(response)); scope.account_info = { response: response.data, status: 'error' }; }); }; }]); sesfControllers.controller('memberSettingsUpdatePasswordController', ['$scope', 'MemberService', '$log', function (scope, MemberService, $log) { scope.password = {}; scope.password_response = {}; scope.submit = function(){ var Password = MemberService.Password(); var password = new Password(scope.password); password.$save(null, function(){ $log.info('updated password'); scope.password = {}; scope.password_response = { response: 'Password Updated', status: 'success' }; },function(response){ $log.error('failed to update password ' + JSON.stringify(response)); scope.password_response = { response: response.data, status: 'error' }; }); }; }]); sesfControllers.controller('memberSettingsAddressController', ['$scope', 'MemberService', '$log', function (scope, MemberService, $log) { scope.location = MemberService.Location().get(); scope.submit = function(){ scope.loc_response = {}; scope.location.$save(null, function(){ $log.info('saved address'); scope.loc_response = { response: 'Address Updated', status: 'success' }; },function(response){ $log.error('failed to save address ' + JSON.stringify(response)); scope.loc_response = { response: response.data, status: 'error' }; }); }; }]); sesfControllers.controller('memberSettingsProfileImageController', ['$scope', 'ImageUpload', '$routeParams', '$log', function (scope, ImageUpload, $routeParams, $log){ scope.timestamp = new Date().getTime(); scope.uploader = ImageUpload.init({ url: '/member/profile-image', data: [{user_id: $routeParams.memberId}], removeAfterUpload: true }, scope, function() { $log.info('loaded new image info'); scope.timestamp = new Date().getTime(); scope.user.$get(); }); }]); sesfControllers.controller('memberSettingsDeleteAccountController', ['$scope', 'MemberService', '$window', '$log', function (scope, MemberService, $window, $log) { scope.delete_data = {}; scope.delete_response = {}; scope.submit = function(){ if(!scope.delete_data.password) { scope.delete_response = { response: 'Please enter your password', status: 'error' }; return; } if(!confirm('Are you sure you want to delete your account? This action can only be undone by an administrator.')) { return; } var DeleteAccount = MemberService.DeleteAccount(); var req = new DeleteAccount(scope.delete_data); req.$save(null, function(){ $log.info('account deleted'); $window.location.href = '/'; }, function(response){ $log.error('failed to delete account ' + JSON.stringify(response)); scope.delete_response = { response: response.data, status: 'error' }; }); }; }]); sesfControllers.controller('memberCarsController', ['$scope', 'cars', '$log', 'MemberService', function(scope, cars, $log, MemberService) { scope.cars = cars; scope.sellCar = function(car){ if(confirm('Are you sure you want to mark this car as SOLD? This will permanently remove the selected car from your garage.')) { var sellCar = MemberService.SellCar(); sellCar = new sellCar(car); sellCar.$save(null, function(response){ $log.info('marked as sold' + JSON.stringify(car)); car.is_sold = true; scope.message_response = { response: 'Marked as Sold', status: 'success' }; },function(response) { $log.error('failed to mark as sold: ' + JSON.stringify(car) + ' ' + JSON.stringify(response)); scope.message_response = { response: response.data, status: 'error' }; }); } }; }]); sesfControllers.controller('memberTutorialController', ['$scope', 'MemberService', 'LoginService', '$location', '$log', function(scope, MemberService, LoginService, $location, $log) { scope.$on('login.update', function(){ if(LoginService.user_id) { scope.tutorial = MemberService.Tutorial().get({id: LoginService.user_id, current_url: $location.path()}); scope.hide = function(){ $log.info('hiding tutorial'); scope.tutorial.show_login_welcome = 0; scope.tutorial.$save(null , function(response) { scope.show_tutorial = false; }); }; } }); }]); sesfControllers.factory( 'CarsService', [ '$resource', '$location', function($resource, $location ) { var service = { filters: function () { return $resource('cars/list-filters', $location.search()); }, paginate: function () { return $resource('cars/list', $location.search()); } }; return service; }]); sesfControllers.controller('carsController', ['$scope', 'CarsService', '$location', 'appStorage', '$route', function (scope, CarsService, $location, appStorage, $route) { breadCrumb = appStorage('sesfStorage', 'breadCrumb', scope); breadCrumb.set('lastPage', 'memberList'); scope.isEventPage = $route.current.$$route.eventPage; var show_id = $route.current.$$route.eventPage ? $route.current.params.eventId : $location.search().show_id; scope.baseUrl = $route.current.$$route.eventPage ? '#' + $location.path().replace('/members', '').replace('/cars', '') : '#'; scope.filters = CarsService.filters().query({eventPage: $route.current.$$route.eventPage}); scope.members = CarsService.paginate().get({limit: $route.current.$$route.eventPage ? 25 : 20, show_id: show_id}); }]); sesfControllers.factory('CarService', ['$resource', '$routeParams', '$route', function($resource, $routeParams, $route){ var service = { Profile: function(){ return $resource('/car/profile', {car_id: $routeParams.carId}, {'new': {method: 'PUT'}}); }, Body: function(){ return $resource('/car/body'); }, Type: function(){ return $resource('/car/type'); } }; return service; }]); sesfControllers.controller('carController', ['$scope', 'CarService', 'appStorage', function (scope, CarService, appStorage) { sesfStorage = appStorage('sesfStorage', 'breadCrumb', scope); scope.car = CarService.Profile().get(function(){ scope.is_private = scope.car.is_private; }); }]); sesfControllers.controller('carNewController', ['$scope', 'CarService', '$location', function (scope, CarService, $location) { var car = CarService.Profile(); scope.carInfo = new car(); scope.bodies = CarService.Body().query(); scope.types = CarService.Type().query(); scope.submit = function(){ scope.carInfo.$new(null,function(response){ $location.path('/car/' + response.id + '/new-image'); },function(response){ scope.carInfoResponse = { response: response.data, status: 'error' }; }); }; }]); sesfControllers.controller('carNewImageController', ['$scope', 'CarService', 'ImageUpload', '$location', 'appStorage', '$routeParams', function (scope, CarService, ImageUpload, $location, appStorage, $routeParams) { breadCrumb = appStorage('sesfStorage', 'breadCrumb', scope); scope.car = CarService.Profile().get(); scope.timestamp = new Date().getTime(); scope.uploader = ImageUpload.init({ url: '/car/profile-image', data: [{car_id: $routeParams.carId}], removeAfterUpload: true }, scope, function() { scope.timestamp = new Date().getTime(); scope.car.$get(); }); scope.submit = function(){ if(scope.breadCrumb.lastPage == 'eventRegistration') { scope.breadCrumb.registrationData.registration.cars.push(scope.car); $location.path(scope.breadCrumb.page); breadCrumb.set('lastPage', 'newCar'); } else $location.path('/car/' + $routeParams.carId + '/settings'); }; }]); sesfControllers.controller('carSettingsCarInfoController', ['$scope', '$q', 'CarService', function (scope, $q, CarService) { scope.carData = {}; $q.all({ profile: CarService.Profile().get(), bodies: CarService.Body().query().$promise, types: CarService.Type().query().$promise }).then(function(result){ scope.carData = result; }); scope.submit = function(){ /* store the body and type selections for after put */ body = scope.carData.profile.body; type = scope.carData.profile.type; scope.carData.profile.$new(null,function(response){ scope.carData.profile.body = body; scope.carData.profile.type = type; scope.carInfoResponse = { response: 'Successfully updated', status: 'success' }; },function(response){ scope.carInfoResponse = { response: response.data, status: 'error' }; }); }; }]); sesfControllers.controller('carSettingsProfileImageController', ['$scope', '$routeParams', 'ImageUpload', function (scope, $routeParams, ImageUpload){ scope.timestamp = new Date().getTime(); scope.uploader = ImageUpload.init({ url: '/car/profile-image', data: [{car_id: $routeParams.carId}], removeAfterUpload: true }, scope, function() { scope.timestamp = new Date().getTime(); scope.car.$get(); }); }]); sesfControllers.service('LoginService', ['$rootScope', '$http', '$routeParams', '$route', '$location', '$log', function($rootScope, $http, $routeParams, $route, $location, $log){ var service = { is_logged_in: false, auth_resolved: false, user_id: null, user_level: 0, nextUrl: '/', emailSigninRedirectPath: null, email_signin: { step: 1, email: '', code: '', status: null, response: null, verify_inflight: false, request_inflight: false }, login_data: { status: null, response: null }, user_data: {}, Authenticate: function(){ service.login_data = { status: null, response: null }; $http.post('/login/authenticate', { }).success(function(data, status){ $log.info('authenticated'); service.user_data = data; service.is_logged_in = true; service.auth_resolved = true; $rootScope.$broadcast('login.auth.success'); $rootScope.$broadcast('login.update'); }).error(function(data){ $log.info('not authenticated'); service.is_logged_in = false; service.auth_resolved = true; service.user_data = {user_id: null, user_level: 0}; $rootScope.$broadcast('login.update'); }); }, Login: function(email, password, remember){ service.login_data = { status: null, response: { response: null } }; $http.post('/login/login', { email: email, password: password, remember: remember }).success(function(data, status){ $log.info('logged in ' + JSON.stringify(data)); service.user_data = data; service.is_logged_in = true; service.login_data.status = 'success'; $rootScope.$broadcast('login.auth.success'); $rootScope.$broadcast('login.update'); }).error(function(data){ $log.error('failed login ' + JSON.stringify(data)); service.is_logged_in = false; service.user_data = null; service.login_data.status = 'error'; service.login_data.response = data; $rootScope.$broadcast('login.update'); }); }, Logout: function(){ service.login_data = { status: null, response: null }; return $http.post('/login/logout').success(function(data){ service.user_data = {user_id: null, user_level: 0}; service.is_logged_in = false; document.cookie = 'laravel_session' + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;'; $rootScope.$broadcast('login.update'); }).error(function(data){ service.user_data = {user_id: null, user_level: 0}; service.is_logged_in = false; $rootScope.$broadcast('login.update'); }); }, forgot_password: { email: null, status: null, response: null }, ForgotPassword: function(email){ service.forgot_password.email = email; $http.post('/login/forgot-password', { email: email }).success(function(data){ $log.info('Forgot Password ' + JSON.stringify(data)); service.forgot_password.email = null; service.forgot_password.status = 'success'; service.forgot_password.response = 'Password Reset Emailed'; $rootScope.$broadcast('forgot_password.update'); }).error(function(data){ $log.error('Forgot Password fail ' + JSON.stringify(data)); service.forgot_password.status = 'error'; service.forgot_password.response = data; $rootScope.$broadcast('forgot_password.update'); }); }, reset_password: { token: $location.search().token, email: null, password: null, password_confirm: null, status: null, response: null }, ResetPassword: function(email, password, password_confirm){ service.reset_password.email = email; service.reset_password.password = password; service.reset_password.password_confirm = password_confirm; $http.post('/login/reset-password', { token: service.reset_password.token, email: email, password: password, password_confirmation: password_confirm }).success(function(data){ service.reset_password.email = null; service.reset_password.status = 'success'; service.reset_password.response = 'Password Successfully Reset'; service.user_data = data; service.is_logged_in = true; $rootScope.$broadcast('reset_password.update'); $rootScope.$broadcast('login.update'); }).error(function(data){ service.reset_password.status = 'error'; service.reset_password.response = data; $rootScope.$broadcast('reset_password.update'); }); }, RequestEmailSignin: function(email, redirectPath){ if (service.email_signin.request_inflight) { return; } service.email_signin.status = null; service.email_signin.response = null; service.email_signin.request_inflight = true; $rootScope.$broadcast('email_signin.update'); var body = { email: email }; if (redirectPath) { body.redirect_path = redirectPath; } $http.post('/login/email-signin/request', body).success(function(){ service.email_signin.request_inflight = false; service.email_signin.step = 2; service.email_signin.status = 'success'; service.email_signin.response = 'Check your email. We sent you a code.'; $rootScope.$broadcast('email_signin.update'); }).error(function(data){ service.email_signin.request_inflight = false; service.email_signin.status = 'error'; service.email_signin.response = data; $rootScope.$broadcast('email_signin.update'); }); }, VerifyEmailSigninCode: function(email, code){ if (service.email_signin.verify_inflight) { return; } service.email_signin.verify_inflight = true; service.login_data = { status: null, response: { response: null } }; $http.post('/login/email-signin/verify-code', { email: email, code: code }).success(function(data){ service.email_signin.verify_inflight = false; var rp = data.redirect_path; delete data.redirect_path; service.user_data = data; service.emailSigninRedirectPath = rp || null; service.is_logged_in = true; service.login_data.status = 'success'; service.email_signin.step = 1; service.email_signin.code = ''; service.email_signin.status = null; service.email_signin.response = null; $rootScope.$broadcast('login.auth.success'); $rootScope.$broadcast('login.update'); $rootScope.$broadcast('email_signin.update'); }).error(function(data){ service.email_signin.verify_inflight = false; service.email_signin.status = 'error'; service.email_signin.response = data; $rootScope.$broadcast('email_signin.update'); }); } }; $rootScope.$on('login.update', function(event) { if(service.user_data != null) { service.user_id = service.user_data.user_id; service.user_level = service.user_data.user_level; } else { service.user_id = null; service.user_level = 0; } }); service.Authenticate(); return service; }]); sesfControllers.controller('loginController', ['$scope', 'LoginService', '$location', '$timeout', '$window', function (scope, LoginService, $location, $timeout, $window) { scope.ready = false; scope.loginMethod = 'password'; scope.email_signin = LoginService.email_signin; scope.emailSigninExpiredMessage = null; if ($location.search().email_signin === 'invalid') { scope.emailSigninExpiredMessage = 'This sign-in link is no longer valid. Use Email and request a new code.'; } scope.setLoginMethod = function(method){ scope.loginMethod = method; if (method === 'password') { LoginService.email_signin.step = 1; LoginService.email_signin.status = null; LoginService.email_signin.response = null; LoginService.email_signin.verify_inflight = false; LoginService.email_signin.request_inflight = false; } }; scope.emailSigninRedirectPath = function(){ var r = $location.search().redirect; if (r && r.length) { return r; } if (LoginService.nUrl) { return LoginService.nUrl; } if (LoginService.nextUrl && LoginService.nextUrl !== '/login') { return LoginService.nextUrl; } return null; }; scope.submitLoginForm = function(){ if (scope.loginMethod === 'password') { scope.submit(); return; } if (LoginService.email_signin.step === 1) { if (LoginService.email_signin.request_inflight) { return; } LoginService.RequestEmailSignin(LoginService.email_signin.email, scope.emailSigninRedirectPath()); return; } LoginService.VerifyEmailSigninCode(LoginService.email_signin.email, LoginService.email_signin.code); }; scope.maybeAutoSubmitEmailCode = function(){ if (LoginService.email_signin.step !== 2 || LoginService.email_signin.verify_inflight) { return; } var email = (LoginService.email_signin.email || '').trim(); if (!email) { return; } var digits = String(LoginService.email_signin.code || '').replace(/\D/g, ''); if (digits.length !== 6) { return; } $timeout(function(){ if (LoginService.email_signin.verify_inflight) { return; } var d = String(LoginService.email_signin.code || '').replace(/\D/g, ''); if (d.length !== 6) { return; } LoginService.VerifyEmailSigninCode(email, d); }, 0); }; scope.restartEmailSignin = function(){ LoginService.email_signin.step = 1; LoginService.email_signin.status = null; LoginService.email_signin.response = null; LoginService.email_signin.code = ''; LoginService.email_signin.verify_inflight = false; LoginService.email_signin.request_inflight = false; scope.email_signin = LoginService.email_signin; }; scope.$on('email_signin.update', function(){ scope.email_signin = LoginService.email_signin; }); scope.isMainNavActive = function (section) { var p = $location.path() || ''; switch (section) { case 'events': if (p.indexOf('/events') === 0) return true; if (p.indexOf('/gallery') === 0) return false; return /^\/[^\/]+\/e\d+/.test(p); case 'members': if (p.indexOf('/members') === 0) return true; return /^\/[^\/]+\/u\d+/.test(p); case 'gallery': return p.indexOf('/gallery') === 0; default: return false; } }; scope.submit = function(){ LoginService.Login(this.email, this.password, this.remember); }; scope.logout = function(e){ if (e) e.preventDefault(); LoginService.Logout().finally(function(){ $location.path('/'); }); }; if ($location.search().r) { LoginService.nUrl = $location.search().r; } scope.$on('login.update', function(event) { scope.login_data = LoginService.login_data; scope.log('login', scope.login_data); scope.user_data = LoginService.user_data; scope.is_logged_in = LoginService.is_logged_in; if(LoginService.user_data != null) scope.user_id = LoginService.user_data.user_id; if(LoginService.is_logged_in === true && $location.path() == '/login') { if (LoginService.emailSigninRedirectPath) { var erPath = LoginService.emailSigninRedirectPath; LoginService.emailSigninRedirectPath = null; if (erPath.match(/^\/waiver\/[0-9]+$/)) { $window.location.href = erPath; LoginService.nextUrl = '/'; return; } if (erPath.indexOf('/judging/') === 0 && erPath.length > 9) { $window.location.href = erPath; LoginService.nextUrl = '/'; return; } $location.path(erPath); LoginService.nextUrl = '/'; return; } var nextUrl; if($location.search().redirect == "forum") { $window.location.href = "https://forum.bmwsharkfest.org"; LoginService.nextUrl = '/'; return; } if($location.search().redirect && $location.search().redirect.length) { $window.location.href = $location.search().redirect; LoginService.nextUrl = '/'; return; } if(LoginService.nUrl) { $window.location.href = LoginService.nUrl; LoginService.nextUrl = '/'; return; } if(LoginService.nextUrl == '/') nextUrl = LoginService.user_data.profileURL; else nextUrl = LoginService.nextUrl; $location.path(nextUrl); LoginService.nextUrl = '/'; } scope.ready = true; }); scope.slideOut = false; scope.$on('login.auth.success', function(event) { if(LoginService.user_data.slideOutName == 1) { scope.slideOut = true; $timeout(function() { scope.slideOut = false; }, 3000); } }); }]); sesfControllers.controller('forgotPasswordController', ['$scope', 'LoginService', '$location', function (scope, LoginService, $location) { scope.forgot_password = LoginService.forgot_password; scope.submit = function(){ LoginService.ForgotPassword(this.email); }; scope.$on('forgot_password.update', function(event) { scope.forgot_password = LoginService.forgot_password; scope.log(LoginService.forgot_password); }); }]); sesfControllers.controller('resetPasswordController', ['$scope', 'LoginService', '$location', '$timeout', function (scope, LoginService, $location, $timeout) { scope.reset_password = LoginService.reset_password; scope.submit = function(){ LoginService.ResetPassword(this.email, this.password, this.password_confirm); }; scope.$on('reset_password.update', function(event) { scope.reset_password = LoginService.reset_password; scope.log(LoginService.reset_password); if(LoginService.reset_password.status === 'success') { $timeout(function() { $location.path('/'); }, 5000); } }); }]); sesfControllers.service( 'GoogleService', [ '$rootScope', function( rootScope) { var geocoder = new google.maps.Geocoder(); var service = { addressData: {}, geoCode: function(address) { geocoder.geocode({ address: address}, function (result, status) { if (status === google.maps.GeocoderStatus.OK) { service.addressData = result[0]; } }); }, getLatLong: function() { return { lattitude: service.addressData.geometry.location.ob, longitude: service.addressData.geometry.location.pb }; } }; return service; }]); ; sesfControllers.factory('GalleryService', ['$resource', '$route', function($resource, $route){ return { EventList: function(){ return $resource('/gallery/car-show-list', null); }, Gallery: function(){ return $resource('/gallery/info', {gallery_id: $route.current.params.galleryId}); }, GalleryList: function(){ return $resource('/gallery/list', {show_id: $route.current.params.eventId, gallery_id: $route.current.params.galleryId}); }, Items: function(get_subcategory_items, skip){ return $resource('/gallery/items', {show_id: $route.current.params.eventId, gallery_id: $route.current.params.galleryId, skip: skip, get_subcategory_items: get_subcategory_items}); }, RemoveItem: function(){ return $resource('/gallery/remove-item', null); }, Create: function(){ return $resource('/gallery/create', null); }, DefaultItem: function() { return $resource('/gallery/default-item', null); }, ShowHideGallery: function() { return $resource('/gallery/show-hide', null); }, Breadcrumb: function(){ return $resource('/gallery/breadcrumb', {show_id: $route.current.params.eventId, gallery_id: $route.current.params.galleryId}); }, CheckedIn: function(){ return $resource('/gallery/checked-in', null); } }; }]); sesfControllers.controller('galleryEventListController', ['$scope', 'events', 'GalleryService', '$location', 'LoginService', '$log', function (scope, events, GalleryService, $location, LoginService, $log) { scope.user_data = LoginService.user_data; scope.events = events; }]); sesfControllers.controller('galleryListController', ['$scope', 'galleries', 'galleryInfo', 'galleryItems', 'breadcrumb', 'GalleryService', '$location', 'LoginService', '$log', '$route', '$modal', '$window', '$document', '$rootScope', function (scope, galleries, galleryInfo, galleryItems, breadcrumb, GalleryService, $location, LoginService, $log, $route, $modal, $window, $document, $rootScope) { scope.galleries = galleries; scope.galleryInfo = galleryInfo; scope.galleryItems = galleryItems; scope.breadcrumb = breadcrumb; scope.user_data = LoginService.user_data; scope.show_id = $route.current.params.eventId; scope.currentSkip = 0; scope.loading = false; scope.memberCheckedIn = false; scope.memberGalleryId = null; function updateMemberCheckedIn() { scope.memberCheckedIn = false; scope.memberGalleryId = null; if (!LoginService.is_logged_in) { return; } if (LoginService.user_data && LoginService.user_data.type_id == $rootScope.USER_TYPE_ADMIN) { return; } GalleryService.CheckedIn().get({show_id: scope.show_id}, function (r) { scope.memberCheckedIn = r.checked_in === true; scope.memberGalleryId = r.member_gallery_id != null ? r.member_gallery_id : null; }); } scope.showEditMyPhotos = function() { if (!scope.memberGalleryId) { return false; } if (!scope.galleryInfo || !scope.galleryInfo.id) { return true; } return parseInt(scope.galleryInfo.id, 10) === parseInt(scope.memberGalleryId, 10); }; updateMemberCheckedIn(); scope.$on('login.update', updateMemberCheckedIn); $('html,body').animate({ scrollTop: 0 }, 100); angular.element($window).bind('scroll', function() { if(document.body.clientHeight - (document.body.scrollTop + window.innerHeight) < 300 && !scope.loading) { scope.loading = true; scope.loadMore(); } }); scope.loadMore = function(){ scope.currentSkip += scope.galleryItems.per_page; if(scope.currentSkip >= scope.galleryItems.count) { return false; } newItems = GalleryService.Items(true, scope.currentSkip).get(function(data){ scope.galleryItems.items = scope.galleryItems.items.concat(newItems.items); scope.loading = false; }); }; scope.viewItem = function($item, $galleryInfo){ var pageItems = scope.galleryItems.items || []; var itemModal = $modal.open({ templateUrl: '/template/gallery?only=view_item', controller: 'viewItemModal', windowClass: 'itemView', backdrop: true, animation: false, resolve: { item: function(){ return $item; }, gallery: function() { return $galleryInfo; }, items: ['GalleryService', '$q', function(GalleryService, $q) { return GalleryService.Items(true).get({ all_items: 1 }).$promise.then( function(data) { var items = (data && data.items) ? data.items : []; if (items.length) { return items; } return pageItems; }, function() { return $q.when(pageItems); } ); }] } }); }; }]); sesfControllers.controller('viewItemModal', ['$scope', '$modalInstance', 'GalleryService', '$timeout', '$log', '$rootScope', 'LoginService', '$location', 'item', 'gallery', 'items', '$window', function(scope, modalInstance, GalleryService, $timeout, $log, $rootScope, LoginService, $location, $item, $gallery, $items, $window) { scope.items = $items; function indexOfItemInItems(items, item) { if (!items || !item) { return -1; } if (item.id != null) { for (var i = 0; i < items.length; i++) { if (items[i].id == item.id) { return i; } } } return items.indexOf(item); } scope.currentIndex = indexOfItemInItems($items, $item); if (scope.currentIndex >= 0) { scope.item = $items[scope.currentIndex]; } else if ($items.length) { scope.currentIndex = 0; scope.item = $items[0]; } else { scope.item = $item; } scope.gallery = $gallery; scope.imageLoading = true; scope.onImageLoaded = function() { scope.imageLoading = false; }; scope.$watch(function() { return scope.item && scope.item.id; }, function(newId, oldId) { if (newId !== oldId) { scope.imageLoading = true; } }); scope.hasPrev = function() { return scope.currentIndex > 0; }; scope.hasNext = function() { return scope.currentIndex < scope.items.length - 1; }; scope.prev = function() { if (scope.imageLoading) { return; } if (scope.hasPrev()) { scope.currentIndex--; scope.item = scope.items[scope.currentIndex]; } }; scope.next = function() { if (scope.imageLoading) { return; } if (scope.hasNext()) { scope.currentIndex++; scope.item = scope.items[scope.currentIndex]; } }; scope.close = function(){ modalInstance.dismiss('cancel'); }; function onKeyDown(e) { if (scope.imageLoading) { return; } var key = e.which || e.keyCode; if (key === 37 || key === 38) { scope.$apply(scope.prev); e.preventDefault(); } else if (key === 39 || key === 40 || key === 32) { scope.$apply(scope.next); e.preventDefault(); } else if (key === 27) { scope.$apply(scope.close); } } angular.element($window).on('keydown', onKeyDown); scope.$on('$destroy', function() { angular.element($window).off('keydown', onKeyDown); }); }]); sesfControllers.controller('galleryCreateController', ['$scope', 'event', 'gallery', 'GalleryService', '$location', 'LoginService', '$log', '$route', '$rootScope', '$timeout', function (scope, event, gallery, GalleryService, $location, LoginService, $log, $route, $rootScope, $timeout) { scope.event = event; scope.gallery = gallery; scope.showId = $route.current.params.eventId; scope.galleryId = $route.current.params.galleryId; scope.isAdmin = LoginService.user_data && LoginService.user_data.type_id == $rootScope.USER_TYPE_ADMIN; scope.create = function(){ var create = new GalleryService.Create(); var payload = { show_id: $route.current.params.eventId }; if ($route.current.params.galleryId && scope.isAdmin) { payload.gallery_id = $route.current.params.galleryId; } if (scope.isAdmin) { payload.title = scope.title; } create.save( payload, function(data){ $location.path("/gallery/manage/" + data.id); }, function(response){ scope.response = { response: response.data, status: 'error' }; }); }; if (!scope.isAdmin) { $timeout(function() { scope.create(); }, 0); } }]); sesfControllers.controller('galleryManageController', ['$scope', 'galleryManage', 'GalleryService', '$location', '$route', 'LoginService', '$log', '$rootScope', '$window', function (scope, galleryManage, GalleryService, $location, $route, LoginService, $log, $rootScope, $window) { scope.gallery = galleryManage.gallery; scope.galleryItems = galleryManage.galleryItems; scope.galleryId = $route.current.params.galleryId; scope.isGalleryAdmin = LoginService.user_data && LoginService.user_data.type_id == $rootScope.USER_TYPE_ADMIN; scope.isGalleryOwner = LoginService.user_data && scope.gallery && (parseInt(scope.gallery.user_id, 10) === parseInt(LoginService.user_data.user_id, 10)); scope.canManageThisGallery = !!(scope.isGalleryAdmin || scope.isGalleryOwner); scope.dragControlListeners = { accept: function (sourceItemHandleScope, destSortableScope) {//override to determine drag is allowed or not. default is true. return true; }, itemMoved: function (event) { }, orderChanged: function(event) {//Do what you want $.each(scope.galleryItems.items, function(index, value){ value.sort_order = index + 1; }); } }; scope.saveItems = function(){ scope.saveResponse = {}; scope.galleryItems.$save( { gallery_id: scope.galleryId }, function(data){ scope.saveResponse = { response: 'Saved', status: 'success' }; return true; }, function(data){ scope.saveResponse = { response: 'Failed to save', status: 'error' }; return false; } ); } scope.showHideGallery = function(){ var newIsEnabled = (scope.gallery.is_enabled == 1 ? 0 : 1); GalleryService.ShowHideGallery().save( { gallery_id: scope.galleryId, is_enabled: newIsEnabled }, function(data){ scope.gallery.is_enabled = newIsEnabled; } ); } scope.deleteItem = function(item){ if (!$window.confirm('Delete this photo? This cannot be undone.')) { return; } GalleryService.RemoveItem().save( { gallery_id: scope.galleryId, item_id: item.id }, function(data){ scope.galleryItems.items.splice(scope.galleryItems.items.indexOf(item), 1); } ); } scope.defaultItem = function(item){ if (scope.gallery.default_item && item.id == scope.gallery.default_item.id) { return; } if (!$window.confirm('Use this image as the gallery cover?')) { return; } GalleryService.DefaultItem().save( { gallery_id: scope.galleryId, item_id: item.id }, function(data){ if(scope.gallery.default_item === null) { scope.gallery.default_item = {}; } scope.gallery.default_item.id = item.id; } ); } }]); sesfControllers.factory( 'AdminService', [ '$rootScope', '$http', '$resource', '$route', function( rootScope, $http, $resource, $route) { var service = { getUsers: function (show_id) { return $resource('/admin2/judging_list/getUsersWithoutJudging/:showId', {showId: '@showId'}, { get: {method: 'GET', isArray: true} }); }, getJudging: function() { return $resource('/admin2/judging_list/getJudgingTypes/:showId',{}, { get: {method: 'GET', isArray: true} }); }, add: function() { return $resource('/admin2/judging_list/addJudging', {}); }, remove: function() { return $resource('/admin2/judging_list/removeJudging', {}); }, getEventJudges: function() { return $resource('/admin2/judging_list/getEventJudges/:showId', {showId: '@showId'}, { get: { method: 'GET', isArray: true } }); }, getJudgeUsersForShow: function() { return $resource('/admin2/judging_list/getJudgeUsersForShow/:showId', {showId: '@showId'}, { get: { method: 'GET', isArray: true } }); }, addEventJudge: function() { return $resource('/admin2/judging_list/addEventJudge', {}); }, removeEventJudge: function() { return $resource('/admin2/judging_list/removeEventJudge', {}); }, Registration: function() { return $resource('event/registration', {show_id: $route.current.params.eventId}); }, Extra: function() { return $resource('event/extra', {extra_id: $route.current.params.extraId}); }, Cars: function() { return $resource('event/cars', {show_id: $route.current.params.eventId}); }, Sponsors: function() { return $resource('event/sponsors', {show_id: $route.current.params.eventId}); }, Schedule: function() { return $resource('event/schedule', {show_id: $route.current.params.eventId}); } }; return service; }]); sesfControllers.controller('eventJudges', ['$scope', 'AdminService', function($scope, AdminService) { $scope.eventJudges = []; $scope.eligibleJudges = []; $scope.judgeUserRaw = null; $scope.reload = function() { if (!$scope.showId) { return; } AdminService.getEventJudges().get({ showId: $scope.showId }).$promise.then(function(rows) { $scope.eventJudges = rows; }); AdminService.getJudgeUsersForShow().get({ showId: $scope.showId }).$promise.then(function(rows) { $scope.eligibleJudges = rows; }); }; $scope.init = function(showId) { $scope.showId = showId; $scope.reload(); }; $scope.addJudge = function() { if (!$scope.judgeUserRaw) { return; } var Row = AdminService.addEventJudge(); (new Row({ show_id: $scope.showId, user_id: $scope.judgeUserRaw.id })).$save(function() { $scope.judgeUserRaw = null; $scope.reload(); }); }; $scope.removeJudge = function(userId) { if (!confirm('Remove this event judge?')) { return; } var Row = AdminService.removeEventJudge(); (new Row({ show_id: $scope.showId, user_id: userId })).$save(function() { $scope.reload(); }); }; }]); sesfControllers.controller('newJudging', ['$scope', 'AdminService', '$window', function($scope, AdminService, $window) { $scope.selectUser = function(){ $scope.user = JSON.parse($scope.userRaw); console.log('user', $scope.user); } $scope.selectCar = function(){ $scope.car = JSON.parse($scope.carRaw); console.log('car', $scope.car); AdminService.getJudging() .get({showId: $scope.showId, body_id: $scope.car.body_id}) .$promise .then(function(response){ $scope.judgingTypes = response; console.log($scope.judging); }); } $scope.selectJudging = function(){ $scope.judging = JSON.parse($scope.judgingRaw); } $scope.init = function(showId){ $scope.showId = null; $scope.users = []; $scope.judgingTypes = []; $scope.user = {}; $scope.car = {}; $scope.showId = showId; AdminService.getUsers() .get({showId: showId}) .$promise .then(function(response){ $scope.users = response; console.log($scope.users); }); } $scope.add = function(){ console.log($scope.car, $scope.user, $scope.judging); var Judging = AdminService.add(); var judging = new Judging({registration_id: $scope.user.id, car_id: $scope.car.id, judging_type_id: $scope.judging.id}); console.log(judging); judging.$save(null, function(){ $scope.init($scope.showId); $window.location.reload(); }); } $scope.remove = function(){ var Judging = AdminService.remove(); var judging = new Judging({registration_id: $scope.user.id, car_id: $scope.car.id}); judging.$save(null, function(){ $scope.init($scope.showId); $window.location.reload(); }); } }]); sesfControllers.controller('judgingAdmin', ['$scope', 'AdminService', '$window', function($scope, AdminService, $window) { $scope.remove = function(car_id, registration_id){ if(confirm('are you sure?')) { console.log('here'); var Judging = AdminService.remove(); var judging = new Judging({registration_id: registration_id, car_id: car_id}); judging.$save(null, function(){ $window.location.reload(); }); } } }]); sesfControllers.controller('adminRegisterUserController', ['$scope', '$routeParams', '$http', '$location', 'events', function(scope, $routeParams, $http, $location, events) { scope.userId = $routeParams.userId; scope.showId = $routeParams.showId; scope.events = events || []; scope.selectedEvent = null; if(scope.showId) { for(var i = 0; i < scope.events.length; i++) { if(String(scope.events[i].id) === String(scope.showId)) { scope.selectedEvent = scope.events[i]; break; } } } if(!scope.selectedEvent && scope.events.length) { scope.selectedEvent = scope.events[0]; } scope.registration = null; scope.event_info = null; scope.user_data = null; scope.targetUserName = ''; scope.loading = false; scope.registrationResponse = {}; scope.loadRegistration = function() { if(!scope.selectedEvent) return; scope.loading = true; scope.registration = null; scope.event_info = null; scope.user_data = null; var showId = scope.selectedEvent.id; var userId = scope.userId; $http.get('event/info', { params: { show_id: showId } }).then(function(infoResp) { scope.event_info = infoResp.data; return $http.get('event/registration', { params: { show_id: showId, user_id: userId } }); }).then(function(regResp) { scope.registration = regResp.data; scope.user_data = regResp.data.targetUser || null; scope.targetUserName = scope.user_data ? (scope.user_data.first + ' ' + scope.user_data.last) : ''; }).catch(function() { scope.registration = null; }).finally(function() { scope.loading = false; }); }; scope.register = function() { if(!scope.selectedEvent || !scope.registration) return; scope.registrationResponse = {}; var showId = scope.selectedEvent.id; var userId = scope.userId; $http.post('event/registration', { registration: scope.registration.registration }, { params: { show_id: showId, user_id: userId } }).then(function(resp) { var regId = resp.data.registration ? resp.data.registration.id : null; if(regId) { window.location.href = '/admin/CarShowRegistration/' + regId; } else { scope.registration = resp.data; scope.registrationResponse.all = { response: 'Registration saved.', status: 'success' }; } }).catch(function(resp) { scope.registrationResponse = { response: resp.data, status: 'error' }; }); }; if(scope.selectedEvent) scope.loadRegistration(); }]);