added RegistrationStatus array
[angular-drzb] / app / js / controllers.js
1 'use strict';
2
3 /* Controllers */
4
5 function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $route, Organizations, RegistrationStatus) {
6
7         $scope.$routeParams = $routeParams;
8         $scope.$location = $location;
9         $scope.organizations = [];
10         Organizations.getArrayPromise().then(function(data) {
11                 $scope.organizations = data;
12                 $log.info('organizations promise ', $scope.organizations);
13         });
14         $scope.RegistrationStatus = RegistrationStatus;
15
16         $scope.update = function(registration, state) {
17
18                 registration.state = state;
19
20                 if ( ! $scope.has_work && registration.work ) {
21                         delete( registration.work );
22                         $log.info("removed work");
23                 }
24
25                 registration.$save( function(registration) {
26                         $log.info("saved", registration);
27                         $log.info('id =', registration.id, 'state = ', registration.state);
28                         if ( registration.id && registration.state ) {
29                                 $location.path( '/' + registration.state + '/' + registration.id );
30                         } else {
31                                 $log.error("can't find id in ", registration);
32                         }
33                 });
34         }
35
36         $scope.reset = function() {
37                 if ( $routeParams.registrationId ) {
38                         $scope.registration = new Registration();
39                         $scope.registration.$get({ registrationId: $routeParams.registrationId }, function(registration) {
40                                 $log.info("get Registration", registration);
41                                 $scope.user = registration.user;
42                                 $scope.work = registration.work;
43                         });
44                 } else {
45                         $scope.registration = new Registration({ user: {}, work: { persons: [] } });
46                         $scope.user = $scope.registration.user;
47                         $scope.work = $scope.registration.work;
48                         $scope.work.persons = [ $scope.user ]; // first author is person submitting work
49                         $log.info("new Registration", $scope.registration);
50                 }
51
52                 $log.info( $routeParams.registrationId );
53
54         }
55
56         $scope.$watch('user.registration_type', function( oldValue, newValue ) {
57                 $log.info("registration_type watch", oldValue, newValue );
58
59                 if ( oldValue === newValue ) return; // triggers on resource, so ignore it
60                 if ( ! $scope.user ) { // resource triggers watch before loading json
61                         $log.error("no user in scope");
62                         return;
63                 }
64
65                 if (
66                         $scope.user.registration_type == 'lecture' ||
67                         $scope.user.registration_type == 'poster' ||
68                         $scope.user.registration_type == 'symposium' ||
69                         $scope.user.registration_type == 'round'
70                 ) {
71                         $scope.work.type = $scope.user.registration_type;
72                         $scope.has_work = true;
73                         $log.info( $scope.user.registration_type, " type updated");
74
75                         if ( $scope.user.registration_type == 'symposium' && ! $scope.work.symposium_works ) {
76                                 // create 4 empty symposium works
77                                 $scope.work.symposium_works = [
78                                         { persons: [{}] },
79                                         { persons: [{}] },
80                                         { persons: [{}] },
81                                         { persons: [{}] }
82                                 ];
83                                 $log.info('created symposium_works');
84                         }
85
86                 } else {
87                         $log.info( $scope.user.registration_type, "NO work" );
88                         $scope.has_work = false;
89                 }
90         });
91
92         $scope.change_student = function() {
93                 if ( $scope.user.student ) {
94                         $scope.user.hpd_member = false;
95                         $scope.user.dinner = false;
96                 }
97         }
98
99         $scope.addPerson = function(persons) {
100                 $log.info('addPerson', persons);
101                 persons.push({ firstname: '' });
102         };
103  
104         $scope.removePerson = function(persons,person) {
105                 for (var i = 0, ii = persons.length; i < ii; i++) {
106                         if (person === persons[i]) {
107                                 persons.splice(i, 1);
108                                 $log.info('removePerson', i, person);
109                         }
110                 }
111         };
112
113         $scope.add_symposium_work = function(works) {
114                 works.push({ persons: [{}] });
115         }
116
117         $scope.symposium_work_remove = function(work_index) {
118                 if ( ! angular.isNumber(work_index) ) {
119                         $log.error("work_index", work_index);
120                         return;
121                 }
122                 var works = $scope.work.symposium_works;
123                 $log.info('symposium_work_remove', works, work_index);
124                 var removed = works.splice( work_index, 1 );
125                 if ( angular.isArray(removed) && removed[0] ) {
126                         removed = removed[0];
127                         removed.deleted = true;
128                         if ( ! angular.isArray( $scope.work.symposium_works_deleted ) )
129                                 $scope.work.symposium_works_deleted = [];
130                         $scope.work.symposium_works_deleted.push( removed );
131                         $log.info("work deleted", works);
132                 } else {
133                         $log.warn("symposium_work_remove no work to remove", works, work);
134                 }
135         }
136
137         $scope.abstract_class = function(work) {
138                 if ( work === undefined ) return;
139                 var abstract = work.abstract;
140                 return angular.isString(abstract) && abstract.length <= 2000 ? 'ok' : 'ng-invalid';
141         }
142         $scope.abstract_length = function(work) {
143                 if ( work === undefined ) return;
144                 var abstract = work.abstract;
145                 if ( ! abstract ) return 0;
146                 return abstract.length <= 2000 ? abstract.length : 2000 - abstract.length;
147         }
148
149         $scope.reset();
150 }
151 RegistrationCtrl.$inject = [ '$scope', '$log', 'Registration', '$routeParams', '$location', '$route', 'Organizations' ];
152
153 function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter) {
154
155         $scope.list = [];
156         $scope.list_results = 0;
157         $scope.pager = {
158                 page: 1,
159                 limit: 10,
160                 results: 0,
161                 last_page: 0
162         };
163         $scope.search = {};
164         $scope.ready = false;
165         $scope.filters = [ 'student', 'hpd_member', 'reception', 'dinner' ];
166
167         $scope.all_registrations = Registration.query( function() {
168                 var Counts = {};
169                 var inc_count = function(type) {
170                         if ( ! angular.isNumber( Counts[type] ) ) {
171                                 Counts[type] = 1;
172                         } else {
173                                 Counts[type]++;
174                         }
175                 };
176                 angular.forEach( $scope.all_registrations, function(value, key) {
177                         if ( ! value.user ) {
178                                 $log.error("all_registrations user corrupted for registration", key, value);
179                                 return;
180                         }
181
182                         var type = value.user.registration_type;
183                         inc_count( type );
184                         inc_count( '' ); // total
185                         angular.forEach( $scope.filters, function(subtype) {
186                                 var v = value.user[subtype];
187                                 if ( v === 'yes' || v === true ) {
188                                         inc_count( subtype );
189                                         inc_count( type + '+' + subtype );
190                                         inc_count( '+' + subtype ); // total
191                                 }
192                         });
193
194                         // for filter
195                         value.registration_type = type;
196                         angular.forEach( $scope.filters, function(f) {
197                                 var v = value.user[f];
198                                 value[f] = v == true || v == 'yes' ? true : false;
199                         });
200
201 //                      $log.info( key, value, Counts[type]  );
202                 });
203                 $log.info('Counts', Counts);
204                 $scope.Counts = Counts;
205
206 //              $scope.list = $scope.all_registrations; // FIXME show all registrations on page loadyy
207                 $scope.ready = true;
208
209         });
210
211         $scope.filter_list = function(newVal, oldVal) {
212                 if ( newVal == oldVal ) return;
213                 $log.info('filter_list', newVal, oldVal, 'search', $scope.search);
214                 var filtered =
215                         $filter('filter')($scope.all_registrations, $scope.search);
216
217 //              $scope.pager.page = 1;
218                 $scope.pager.results = filtered.length;
219                 $scope.pager.last_page = Math.ceil( $scope.pager.results / $scope.pager.limit );
220                 if ( $scope.pager.page > $scope.pager.last_page ) {
221                         $scope.pager.page = 1;
222                 }
223                 $log.info('pager', $scope.pager);
224
225                 var from = ( $scope.pager.page - 1 ) * $scope.pager.limit;
226                 $scope.list = [];
227                 angular.forEach( filtered, function(v,k) {
228                         if ( k >= from && k < from + $scope.pager.limit ) {
229                                 v.nr = k + 1;
230                                 this.push(v);
231                         }
232                 }, $scope.list );
233                 $log.info('list length=', $scope.list.length, "offset=", from);
234         };
235         angular.forEach( $scope.filters, function(f) {
236                 $scope.$watch('search.'+f, $scope.filter_list);
237                 $log.info('watch search.'+f);
238         });
239         $scope.$watch('search.registration_type', $scope.filter_list);
240         $scope.$watch('search.$', $scope.filter_list);
241         $scope.$watch('pager.page', $scope.filter_list);
242         $scope.$watch('pager.limit', $scope.filter_list);
243
244         $scope.RegistrationTypes = RegistrationTypes;
245         $log.info( "RegistrationTypes", RegistrationTypes );
246 }
247
248
249 ListCtrl.$inject = [ '$scope', '$log', 'Registration', 'RegistrationTypes', '$filter' ];