added RegistrationStatus array
[angular-drzb] / app / js / controllers.js
index e20938e..0576a11 100644 (file)
@@ -2,13 +2,20 @@
 
 /* Controllers */
 
-
-function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $route) {
+function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $route, Organizations, RegistrationStatus) {
 
        $scope.$routeParams = $routeParams;
        $scope.$location = $location;
+       $scope.organizations = [];
+       Organizations.getArrayPromise().then(function(data) {
+               $scope.organizations = data;
+               $log.info('organizations promise ', $scope.organizations);
+       });
+       $scope.RegistrationStatus = RegistrationStatus;
+
+       $scope.update = function(registration, state) {
 
-       $scope.update = function(registration) {
+               registration.state = state;
 
                if ( ! $scope.has_work && registration.work ) {
                        delete( registration.work );
@@ -17,9 +24,9 @@ function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $
 
                registration.$save( function(registration) {
                        $log.info("saved", registration);
-                       $log.info('id =', registration.id);
-                       if ( registration.id ) {
-                               $location.path( '/registration-finished/' + registration.id );
+                       $log.info('id =', registration.id, 'state = ', registration.state);
+                       if ( registration.id && registration.state ) {
+                               $location.path( '/' + registration.state + '/' + registration.id );
                        } else {
                                $log.error("can't find id in ", registration);
                        }
@@ -43,6 +50,7 @@ function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $
                }
 
                $log.info( $routeParams.registrationId );
+
        }
 
        $scope.$watch('user.registration_type', function( oldValue, newValue ) {
@@ -65,7 +73,13 @@ function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $
                        $log.info( $scope.user.registration_type, " type updated");
 
                        if ( $scope.user.registration_type == 'symposium' && ! $scope.work.symposium_works ) {
-                               $scope.work.symposium_works = [{ persons: [{}] }]; // create first empty symposium work
+                               // create 4 empty symposium works
+                               $scope.work.symposium_works = [
+                                       { persons: [{}] },
+                                       { persons: [{}] },
+                                       { persons: [{}] },
+                                       { persons: [{}] }
+                               ];
                                $log.info('created symposium_works');
                        }
 
@@ -78,7 +92,6 @@ function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $
        $scope.change_student = function() {
                if ( $scope.user.student ) {
                        $scope.user.hpd_member = false;
-                       $scope.user.reception = false;
                        $scope.user.dinner = false;
                }
        }
@@ -97,11 +110,140 @@ function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $
                }
        };
 
+       $scope.add_symposium_work = function(works) {
+               works.push({ persons: [{}] });
+       }
+
+       $scope.symposium_work_remove = function(work_index) {
+               if ( ! angular.isNumber(work_index) ) {
+                       $log.error("work_index", work_index);
+                       return;
+               }
+               var works = $scope.work.symposium_works;
+               $log.info('symposium_work_remove', works, work_index);
+               var removed = works.splice( work_index, 1 );
+               if ( angular.isArray(removed) && removed[0] ) {
+                       removed = removed[0];
+                       removed.deleted = true;
+                       if ( ! angular.isArray( $scope.work.symposium_works_deleted ) )
+                               $scope.work.symposium_works_deleted = [];
+                       $scope.work.symposium_works_deleted.push( removed );
+                       $log.info("work deleted", works);
+               } else {
+                       $log.warn("symposium_work_remove no work to remove", works, work);
+               }
+       }
+
+       $scope.abstract_class = function(work) {
+               if ( work === undefined ) return;
+               var abstract = work.abstract;
+               return angular.isString(abstract) && abstract.length <= 2000 ? 'ok' : 'ng-invalid';
+       }
+       $scope.abstract_length = function(work) {
+               if ( work === undefined ) return;
+               var abstract = work.abstract;
+               if ( ! abstract ) return 0;
+               return abstract.length <= 2000 ? abstract.length : 2000 - abstract.length;
+       }
+
        $scope.reset();
 }
-//RegistrationCtrl.$inject = [ '$scope', '$log' ];
+RegistrationCtrl.$inject = [ '$scope', '$log', 'Registration', '$routeParams', '$location', '$route', 'Organizations' ];
+
+function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter) {
+
+       $scope.list = [];
+       $scope.list_results = 0;
+       $scope.pager = {
+               page: 1,
+               limit: 10,
+               results: 0,
+               last_page: 0
+       };
+       $scope.search = {};
+       $scope.ready = false;
+       $scope.filters = [ 'student', 'hpd_member', 'reception', 'dinner' ];
+
+       $scope.all_registrations = Registration.query( function() {
+               var Counts = {};
+               var inc_count = function(type) {
+                       if ( ! angular.isNumber( Counts[type] ) ) {
+                               Counts[type] = 1;
+                       } else {
+                               Counts[type]++;
+                       }
+               };
+               angular.forEach( $scope.all_registrations, function(value, key) {
+                       if ( ! value.user ) {
+                               $log.error("all_registrations 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];
+                               if ( v === 'yes' || v === true ) {
+                                       inc_count( subtype );
+                                       inc_count( type + '+' + subtype );
+                                       inc_count( '+' + subtype ); // total
+                               }
+                       });
+
+                       // 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]  );
+               });
+               $log.info('Counts', Counts);
+               $scope.Counts = Counts;
 
+//             $scope.list = $scope.all_registrations; // FIXME show all registrations on page loadyy
+               $scope.ready = true;
 
-function MyCtrl2() {
+       });
+
+       $scope.filter_list = function(newVal, oldVal) {
+               if ( newVal == oldVal ) return;
+               $log.info('filter_list', newVal, oldVal, 'search', $scope.search);
+               var filtered =
+                       $filter('filter')($scope.all_registrations, $scope.search);
+
+//             $scope.pager.page = 1;
+               $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;
+               }
+               $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 ) {
+                               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.RegistrationTypes = RegistrationTypes;
+       $log.info( "RegistrationTypes", RegistrationTypes );
 }
-MyCtrl2.$inject = [];
+
+
+ListCtrl.$inject = [ '$scope', '$log', 'Registration', 'RegistrationTypes', '$filter' ];