added user.org_member
[angular-drzb] / app / js / controllers.js
index 122746a..f336b51 100644 (file)
 
 /* Controllers */
 
-
-function RegistrationCtrl($scope, $log, Registration, $routeParams) {
+function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $route, View, ValidStates) {
 
        $scope.$routeParams = $routeParams;
+       $scope.$location = $location;
+       $scope.organizations = [];
+       View.getPromise('organizations?group=true;format=key_array').then(function(data) {
+               if ( angular.isArray(data.rows) ) {
+                       $scope.organizations = data.rows;
+                       $log.info('organizations promise ', $scope.organizations);
+               } else {
+                       $log.info('no rows in', data);
+               }
+       });
+       $scope.ValidStates = ValidStates();
+
+       $scope.info = { message: '', css_class: '' } ; // alert box
+
+       $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 );
+                       $log.info("removed work");
+               }
 
-       $scope.update = function(user) {
-               $log.info("saved", user, user.$save());
+               registration.$save( function(registration) {
+                       $log.info('id =', registration.id, 'state = ', registration.state);
+                       if ( registration.id && registration.state ) {
+                               $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);
+                       }
+               });
        }
 
        $scope.reset = function() {
                if ( $routeParams.registrationId ) {
-                       $scope.user = new Registration();
-                       $scope.user.$get({ registrationId: $routeParams.registrationId });
-                       $log.info("get Registration", $scope.user);
+                       $scope.registration = new Registration();
+                       $scope.registration.$get({ registrationId: $routeParams.registrationId }, function(registration) {
+                               $log.info("get Registration", registration);
+                               $scope.user = registration.user;
+                               $scope.work = registration.work;
+                       });
                } else {
-                       $scope.user = new Registration();
-                       $log.info("new Registration", $scope.user);
+                       $scope.registration = new Registration({ user: {}, work: { persons: [] } });
+                       $scope.user = $scope.registration.user;
+                       $scope.work = $scope.registration.work;
+                       $scope.work.persons = [ $scope.user ]; // first author is person submitting work
+                       $log.info("new Registration", $scope.registration);
                }
-               
+
                $log.info( $routeParams.registrationId );
+
+       }
+
+       $scope.$watch('user.registration_type', function( oldValue, newValue ) {
+               $log.info("registration_type watch", oldValue, newValue );
+
+               if ( oldValue === newValue ) return; // triggers on resource, so ignore it
+               if ( ! $scope.user ) { // resource triggers watch before loading json
+                       $log.error("no user in scope");
+                       return;
+               }
+
+               if (
+                       $scope.user.registration_type == 'lecture' ||
+                       $scope.user.registration_type == 'poster' ||
+                       $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");
+
+                       if ( $scope.user.registration_type == 'symposium' && ! $scope.work.symposium_works ) {
+                               // create 4 empty symposium works
+                               $scope.work.symposium_works = [
+                                       { persons: [{}] },
+                                       { persons: [{}] },
+                                       { persons: [{}] },
+                                       { persons: [{}] }
+                               ];
+                               $log.info('created symposium_works');
+                       }
+
+               } else {
+                       $log.info( $scope.user.registration_type, "NO work" );
+                       $scope.has_work = false;
+               }
+       });
+
+       $scope.change_student = function() {
+               if ( $scope.user.student ) {
+                       $scope.user.hpd_member = false;
+                       $scope.user.dinner = false;
+               }
+       }
+
+       $scope.addPerson = function(persons) {
+               $log.info('addPerson', persons);
+               persons.push({ firstname: '' });
+       };
+       $scope.removePerson = function(persons,person) {
+               for (var i = 0, ii = persons.length; i < ii; i++) {
+                       if (person === persons[i]) {
+                               persons.splice(i, 1);
+                               $log.info('removePerson', i, person);
+                       }
+               }
+       };
+
+       $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.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' ];
+RegistrationCtrl.$inject = [ '$scope', '$log', 'Registration', '$routeParams', '$location', '$route', 'View', 'ValidStates' ];
+
+function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter, ValidStates ) {
+
+       $scope.list = [];
+       $scope.list_results = 0;
+       $scope.pager = {
+               page: 1,
+               limit: 10,
+               results: 1,
+               last_page: 1,
+               show_all: false
+       };
+       $scope.search = {};
+       $scope.ready = false;
+       $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' },
+               { select: 'user.org_member', label: 'organisation', name: 'filter_org' }
+       ];
+       $scope.show = {
+               registration_type: true,
+               filters: true,
+               search: true,
+               states: true,
+               pager_numeric: false
+       };
+       $scope.ValidStates = ValidStates();
+
+       $scope.create_counts = function( array ) {
+               $log.info('create_counts', array.length );
+               var Counts = {};
+               var inc_count = function(type) {
+                       if ( ! angular.isNumber( Counts[type] ) ) {
+                               Counts[type] = 1;
+                       } else {
+                               Counts[type]++;
+                       }
+               };
+               angular.forEach( array, function(value, key) {
+                       if ( ! value.user ) {
+                               $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(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( 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;
+
+                       // count registration state
+                       inc_count( 'state+' + value.state );
+
+//                     $log.info( key, value );
+               });
+               $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) {
+               if ( newVal == oldVal ) return;
+               $log.info('filter_list', newVal, oldVal, 'search', $scope.search);
+               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;
+               $scope.pager.last_page = Math.ceil( $scope.pager.results / $scope.pager.limit );
+               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 || $scope.pager.show_all ) {
+                               v.nr = k + 1;
+                               this.push(v);
+                       }
+               }, $scope.list );
+               $log.info('list length=', $scope.list.length, "offset=", from);
+       };
+       $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' ];
 
-function MyCtrl2() {
+
+function AuthorsCtrl($scope, $log, View ) {
+       $scope.message = 'Loading data...';
+       $scope.ready = false;
+       View.getPromise('authors?format=key_distinct').then(function(data) {
+               if ( angular.isArray(data.rows) ) {
+                       $scope.data = data;
+                       $scope.authors = $scope.data.rows;
+                       $scope.ready = true;
+                       $log.info('authors promise ', data);
+               } else {
+                       $log.info('no rows in', data);
+                       $scope.message = 'No rows found';
+               }
+       });
 }
-MyCtrl2.$inject = [];
+
+AuthorsCtrl.$inject = [ '$scope', '$log', 'View' ];
+
+