don't die on undefined registration or user
[angular-drzb] / app / js / controllers.js
index 977758c..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, RegistrationState ) {
+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, Regist
                page: 1,
                limit: 10,
                results: 1,
-               last_page: 1
+               last_page: 1,
+               show_all: false
        };
        $scope.search = {};
        $scope.ready = false;
@@ -181,7 +197,7 @@ function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter, Regist
                states: true,
                pager_numeric: false
        };
-       $scope.RegistrationState = RegistrationState();
+       $scope.ValidStates = ValidStates();
 
        $scope.create_counts = function( array ) {
                $log.info('create_counts', array.length );
@@ -218,6 +234,9 @@ function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter, Regist
                                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);
@@ -250,7 +269,7 @@ function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter, Regist
                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);
                        }
@@ -271,5 +290,5 @@ function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter, Regist
        }
 }
 
-ListCtrl.$inject = [ '$scope', '$log', 'Registration', 'RegistrationTypes', '$filter', 'RegistrationState' ];
+ListCtrl.$inject = [ '$scope', '$log', 'Registration', 'RegistrationTypes', '$filter', 'ValidStates' ];