don't die on undefined registration or user
[angular-drzb] / app / js / controllers.js
index fa87a48..4ddca47 100644 (file)
@@ -2,7 +2,7 @@
 
 /* Controllers */
 
-function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $route, Organizations, RegistrationState) {
+function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $route, Organizations, ValidStates) {
 
        $scope.$routeParams = $routeParams;
        $scope.$location = $location;
@@ -11,7 +11,7 @@ function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $
                $scope.organizations = data;
                $log.info('organizations promise ', $scope.organizations);
        });
-       $scope.RegistrationState = RegistrationState;
+       $scope.ValidStates = ValidStates();
 
        $scope.info = { message: '', css_class: '' } ; // alert box
 
@@ -27,7 +27,8 @@ function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $
                registration.$save( function(registration) {
                        $log.info('id =', registration.id, 'state = ', registration.state);
                        if ( registration.id && registration.state ) {
-                               if ( ! info_message ) {
+                               $log.error( $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' };
@@ -157,11 +158,25 @@ function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $
                $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', 'Organizations', 'ValidStates' ];
 
-function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter) {
+function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter, ValidStates ) {
 
        $scope.list = [];
        $scope.list_results = 0;
@@ -169,7 +184,8 @@ function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter) {
                page: 1,
                limit: 10,
                results: 1,
-               last_page: 1
+               last_page: 1,
+               show_all: false
        };
        $scope.search = {};
        $scope.ready = false;
@@ -181,8 +197,10 @@ function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter) {
                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] ) ) {
@@ -191,9 +209,9 @@ 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;
                        }
 
@@ -216,15 +234,20 @@ function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter) {
                                value[f] = v == true || v == 'yes' ? true : false;
                        });
 
+                       // count registration state
+                       inc_count( 'state+' + value.state );
+
 //                     $log.info( key, value, Counts[type]  );
                });
                $log.info('Counts', Counts);
                $scope.Counts = Counts;
+               return Counts;
+       };
 
+       $scope.all_registrations = Registration.query( function(result) {
                $scope.ready = true;
-
+               $log.info('Reqistration.query callback', result);
                $scope.reset();
-
        });
 
        $scope.filter_list = function(newVal, oldVal) {
@@ -233,6 +256,8 @@ function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter) {
                var filtered =
                        $filter('filter')($scope.all_registrations, $scope.search);
 
+               $scope.create_counts( filtered );
+
                $log.info('pager before apply', $scope.pager);
 
                $scope.pager.results = filtered.length;
@@ -244,7 +269,7 @@ function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter) {
                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);
                        }
@@ -265,5 +290,5 @@ function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter) {
        }
 }
 
+ListCtrl.$inject = [ '$scope', '$log', 'Registration', 'RegistrationTypes', '$filter', 'ValidStates' ];
 
-ListCtrl.$inject = [ '$scope', '$log', 'Registration', 'RegistrationTypes', '$filter' ];