finish list of registrations with search and filter
[angular-drzb] / app / js / controllers.js
1 'use strict';
2
3 /* Controllers */
4
5
6 function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $route) {
7
8         $scope.$routeParams = $routeParams;
9         $scope.$location = $location;
10
11         $scope.update = function(registration) {
12
13                 if ( ! $scope.has_work && registration.work ) {
14                         delete( registration.work );
15                         $log.info("removed work");
16                 }
17
18                 registration.$save( function(registration) {
19                         $log.info("saved", registration);
20                         $log.info('id =', registration.id);
21                         if ( registration.id ) {
22                                 $location.path( '/registration-finished/' + registration.id );
23                         } else {
24                                 $log.error("can't find id in ", registration);
25                         }
26                 });
27         }
28
29         $scope.reset = function() {
30                 if ( $routeParams.registrationId ) {
31                         $scope.registration = new Registration();
32                         $scope.registration.$get({ registrationId: $routeParams.registrationId }, function(registration) {
33                                 $log.info("get Registration", registration);
34                                 $scope.user = registration.user;
35                                 $scope.work = registration.work;
36                         });
37                 } else {
38                         $scope.registration = new Registration({ user: {}, work: { persons: [] } });
39                         $scope.user = $scope.registration.user;
40                         $scope.work = $scope.registration.work;
41                         $scope.work.persons = [ $scope.user ]; // first author is person submitting work
42                         $log.info("new Registration", $scope.registration);
43                 }
44
45                 $log.info( $routeParams.registrationId );
46         }
47
48         $scope.$watch('user.registration_type', function( oldValue, newValue ) {
49                 $log.info("registration_type watch", oldValue, newValue );
50
51                 if ( oldValue === newValue ) return; // triggers on resource, so ignore it
52                 if ( ! $scope.user ) { // resource triggers watch before loading json
53                         $log.error("no user in scope");
54                         return;
55                 }
56
57                 if (
58                         $scope.user.registration_type == 'lecture' ||
59                         $scope.user.registration_type == 'poster' ||
60                         $scope.user.registration_type == 'symposium' ||
61                         $scope.user.registration_type == 'round'
62                 ) {
63                         $scope.work.type = $scope.user.registration_type;
64                         $scope.has_work = true;
65                         $log.info( $scope.user.registration_type, " type updated");
66
67                         if ( $scope.user.registration_type == 'symposium' && ! $scope.work.symposium_works ) {
68                                 // create 4 empty symposium works
69                                 $scope.work.symposium_works = [
70                                         { persons: [{}] },
71                                         { persons: [{}] },
72                                         { persons: [{}] },
73                                         { persons: [{}] }
74                                 ];
75                                 $log.info('created symposium_works');
76                         }
77
78                 } else {
79                         $log.info( $scope.user.registration_type, "NO work" );
80                         $scope.has_work = false;
81                 }
82         });
83
84         $scope.change_student = function() {
85                 if ( $scope.user.student ) {
86                         $scope.user.hpd_member = false;
87                         $scope.user.reception = false;
88                         $scope.user.dinner = false;
89                 }
90         }
91
92         $scope.addPerson = function(persons) {
93                 $log.info('addPerson', persons);
94                 persons.push({ firstname: '' });
95         };
96  
97         $scope.removePerson = function(persons,person) {
98                 for (var i = 0, ii = persons.length; i < ii; i++) {
99                         if (person === persons[i]) {
100                                 persons.splice(i, 1);
101                                 $log.info('removePerson', i, person);
102                         }
103                 }
104         };
105
106         $scope.add_symposium_work = function(works) {
107                 works.push({ persons: [{}] });
108         }
109
110         $scope.reset();
111 }
112 //RegistrationCtrl.$inject = [ '$scope', '$log' ];
113
114 function ListCtrl($scope, $log, Registration, RegistrationTypes) {
115
116         $scope.list = Registration.query( function() {
117                 var RegistrationTypeCount = { '': 0 };
118                 angular.forEach( $scope.list, function(value, key) {
119                         var type = value.user.registration_type;
120                         if ( ! angular.isNumber( RegistrationTypeCount[type] ) ) {
121                                 RegistrationTypeCount[type] = 1;
122                         } else {
123                                 RegistrationTypeCount[type]++;
124                         }
125                         RegistrationTypeCount['']++;
126
127                         value.registration_type = type; // for search
128                         
129 //                      $log.info( key, value.user.registration_type, RegistrationTypeCount[type]  );
130                 });
131                 $log.info('RegistrationTypeCount', RegistrationTypeCount);
132                 $scope.RegistrationTypeCount = RegistrationTypeCount;
133         });
134
135
136         $scope.RegistrationTypes = RegistrationTypes;
137         $log.info( "RegistrationTypes", RegistrationTypes );
138         $scope.search = { '$': '' };
139 }
140
141
142 function MyCtrl2() {
143 }
144 MyCtrl2.$inject = [];