X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=app%2Fjs%2Fcontrollers.js;h=c7a043caba2419712860bc35847546620f71cd98;hb=39e4203cab6bea4279c604afb76a18a9ec44ef8e;hp=8b9cf1d1c23720f7eda4360a7503ecc680df6332;hpb=6743cdd1dd1ad27ae1f07a3257a53c162c28a333;p=angular-drzb diff --git a/app/js/controllers.js b/app/js/controllers.js index 8b9cf1d..c7a043c 100644 --- a/app/js/controllers.js +++ b/app/js/controllers.js @@ -2,20 +2,27 @@ /* Controllers */ -function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $route, Organizations, RegistrationState) { +function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $route, View, ValidStates) { $scope.$routeParams = $routeParams; $scope.$location = $location; $scope.organizations = []; - Organizations.getArrayPromise().then(function(data) { + View.getArrayPromise('organizations?group=true;format=key_array').then(function(data) { $scope.organizations = data; $log.info('organizations promise ', $scope.organizations); }); - $scope.RegistrationState = RegistrationState; + $scope.ValidStates = ValidStates(); - $scope.update = function(registration, state) { + $scope.info = { message: '', css_class: '' } ; // alert box - registration.state = state; + $scope.update = function(registration, state, info_message) { + + if ( $scope.ValidStates.indexOf( state ) > $scope.ValidStates.indexOf( registration.state ) ) { + registration.state = state; + $log.info( 'registration.state updated to ',state); + } else { + $log.info('registration.state NOT changed'); + } if ( ! $scope.has_work && registration.work ) { delete( registration.work ); @@ -23,10 +30,14 @@ function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $ } registration.$save( function(registration) { - $log.info("saved", registration); $log.info('id =', registration.id, 'state = ', registration.state); if ( registration.id && registration.state ) { - $location.path( '/' + registration.state + '/' + registration.id ); + $log.info( 'have id && state', $location.path(), registration.state, $routeParams ); + if ( $location.path().split(/\//)[1] != registration.state ) { + $location.path( '/' + registration.state + '/' + registration.id ); + } else { + $scope.info = { message: info_message, css_class: 'alert-success' }; + } } else { $log.error("can't find id in ", registration); } @@ -68,6 +79,14 @@ function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $ $scope.user.registration_type == 'symposium' || $scope.user.registration_type == 'round' ) { + if ( angular.isUndefined( $scope.registration.work ) ) { + $log.info('create empty work'); + $scope.registration.work = { persons: [ $scope.user ] }; + if ( angular.isUndefined( $scope.work ) ) + $scope.work = $scope.registration.work; + } + + $scope.work.type = $scope.user.registration_type; $scope.has_work = true; $log.info( $scope.user.registration_type, " type updated"); @@ -146,25 +165,61 @@ function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $ return abstract.length <= 2000 ? abstract.length : 2000 - abstract.length; } + $scope.change_state = function(new_state) { + if ( new_state == $scope.registration.state ) return; + $scope.info = { css_class: 'alert-warning', message: 'Changed state from "'+$scope.registration.state+'" to "'+new_state+'"' }; + $scope.registration.state = new_state; + } + + $scope.show_submission_type = function() { + // FIXME check if all submission are allowed by date and allow them! + if ( angular.isUndefined( $scope.user ) || angular.isUndefined( $scope.registration ) ) return false; + return ( + angular.isUndefined( $scope.user.registration_type ) // new registration + || angular.isUndefined( $scope.registration.state ) // not saved yet + || $scope.registration.state == 'draft' + || $scope.registration.state == 'confirmation' + ) ? false : true; + } + $scope.show_registration_type = function() { + return true; + } + $scope.reset(); } -RegistrationCtrl.$inject = [ '$scope', '$log', 'Registration', '$routeParams', '$location', '$route', 'Organizations', 'RegistrationState' ]; +RegistrationCtrl.$inject = [ '$scope', '$log', 'Registration', '$routeParams', '$location', '$route', 'View', 'ValidStates' ]; -function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter) { +function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter, ValidStates ) { $scope.list = []; $scope.list_results = 0; $scope.pager = { page: 1, limit: 10, - results: 0, - last_page: 0 + results: 1, + last_page: 1, + show_all: false }; $scope.search = {}; $scope.ready = false; - $scope.filters = [ 'student', 'hpd_member', 'reception', 'dinner' ]; + $scope.filters = [ + { select: 'user.student', label: 'student', name: 'filter_student' }, + { select: 'user.hpd_member', label: 'HPD', name: 'filter_hpd' }, + { select: 'user.reception', label: 'reception', name: 'filter_reception' }, + { select: 'user.dinner', label: 'dinner', name: 'filter_dinner' }, + { select: 'payment.received', label: 'payment', name: 'filter_payment' } + ]; + $scope.show = { + registration_type: true, + filters: true, + search: true, + states: true, + pager_numeric: false + }; + $scope.ValidStates = ValidStates(); - $scope.all_registrations = Registration.query( function() { + $scope.create_counts = function( array ) { + $log.info('create_counts', array.length ); var Counts = {}; var inc_count = function(type) { if ( ! angular.isNumber( Counts[type] ) ) { @@ -173,39 +228,48 @@ function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter) { Counts[type]++; } }; - angular.forEach( $scope.all_registrations, function(value, key) { + angular.forEach( array, function(value, key) { if ( ! value.user ) { - $log.error("all_registrations user corrupted for registration", key, value); + $log.error("create_counts user corrupted for registration", key, value); return; } var type = value.user.registration_type; inc_count( type ); inc_count( '' ); // total - angular.forEach( $scope.filters, function(subtype) { - var v = value.user[subtype]; + angular.forEach( $scope.filters, function(filter) { + var s = filter.select.split(/\./); + var v = value[s[0]]; + if ( angular.isDefined( v ) ) { + v = v[s[1]]; + } if ( v === 'yes' || v === true ) { - inc_count( subtype ); - inc_count( type + '+' + subtype ); - inc_count( '+' + subtype ); // total + inc_count( filter.name ); + inc_count( type + '+' + filter.name ); + inc_count( '+' + filter.name ); // total + value[filter.name] = true; + } else { + value[filter.name] = false; } }); // for filter value.registration_type = type; - angular.forEach( $scope.filters, function(f) { - var v = value.user[f]; - value[f] = v == true || v == 'yes' ? true : false; - }); -// $log.info( key, value, Counts[type] ); + // count registration state + inc_count( 'state+' + value.state ); + +// $log.info( key, value ); }); $log.info('Counts', Counts); $scope.Counts = Counts; + return Counts; + }; -// $scope.list = $scope.all_registrations; // FIXME show all registrations on page loadyy + $scope.all_registrations = Registration.query( function(result) { $scope.ready = true; - + $log.info('Reqistration.query callback', result); + $scope.reset(); }); $scope.filter_list = function(newVal, oldVal) { @@ -214,36 +278,56 @@ function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter) { var filtered = $filter('filter')($scope.all_registrations, $scope.search); -// $scope.pager.page = 1; + $scope.create_counts( filtered ); + + $log.info('pager before apply', $scope.pager); + $scope.pager.results = filtered.length; $scope.pager.last_page = Math.ceil( $scope.pager.results / $scope.pager.limit ); - if ( $scope.pager.page > $scope.pager.last_page ) { - $scope.pager.page = 1; - } + if ( $scope.pager.page < 1 ) $scope.pager.page = 1; + if ( $scope.pager.page > $scope.pager.last_page ) $scope.pager.page = $scope.pager.last_page; $log.info('pager', $scope.pager); var from = ( $scope.pager.page - 1 ) * $scope.pager.limit; $scope.list = []; angular.forEach( filtered, function(v,k) { - if ( k >= from && k < from + $scope.pager.limit ) { + if ( k >= from && k < from + $scope.pager.limit || $scope.pager.show_all ) { v.nr = k + 1; this.push(v); } }, $scope.list ); $log.info('list length=', $scope.list.length, "offset=", from); }; - angular.forEach( $scope.filters, function(f) { - $scope.$watch('search.'+f, $scope.filter_list); - $log.info('watch search.'+f); - }); - $scope.$watch('search.registration_type', $scope.filter_list); - $scope.$watch('search.$', $scope.filter_list); - $scope.$watch('pager.page', $scope.filter_list); - $scope.$watch('pager.limit', $scope.filter_list); + $scope.$watch('search', $scope.filter_list, true); + $scope.$watch('search.$', $scope.filter_list); // FIXME not included in search true because of $ + $scope.$watch('pager', $scope.filter_list, true); $scope.RegistrationTypes = RegistrationTypes; $log.info( "RegistrationTypes", RegistrationTypes ); + + $scope.reset = function() { + $scope.search = { registration_type: '' }; + $log.info('reset', $scope.search ); + $scope.pager.page = 1; + } + + $scope.print = { + layout: false, + center: '', + bold: '', + toggle: function(v) { + $log.info('print_hide', v); + $scope.print.layout = ! $scope.print.layout; + if ( v ) { + $scope.print.center = 'print-center'; + $scope.print.bold = 'print-bold'; + } else { + $scope.print.center = ''; + $scope.print.bold = ''; + } + } + } } +ListCtrl.$inject = [ '$scope', '$log', 'Registration', 'RegistrationTypes', '$filter', 'ValidStates' ]; -ListCtrl.$inject = [ '$scope', '$log', 'Registration', 'RegistrationTypes', '$filter' ];