b98fb99848a4bee33c8426403eccd2f9b8b11da2
[angular-drzb] / app / js / controllers.js
1 'use strict';
2
3 /* Controllers */
4
5 function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $route, Organizations, ValidStates) {
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.ValidStates = ValidStates();
15
16         $scope.info = { message: '', css_class: '' } ; // alert box
17
18         $scope.update = function(registration, state, info_message) {
19
20                 if ( $scope.ValidStates.indexOf( state ) > $scope.ValidStates.indexOf( registration.state ) ) {
21                         registration.state = state;
22                         $log.info( 'registration.state updated to ',state);
23                 } else {
24                         $log.info('registration.state NOT changed');
25                 }
26
27                 if ( ! $scope.has_work && registration.work ) {
28                         delete( registration.work );
29                         $log.info("removed work");
30                 }
31
32                 registration.$save( function(registration) {
33                         $log.info('id =', registration.id, 'state = ', registration.state);
34                         if ( registration.id && registration.state ) {
35                                 $log.info( 'have id && state', $location.path(), registration.state, $routeParams );
36                                 if ( $location.path().split(/\//)[1] != registration.state ) {
37                                         $location.path( '/' + registration.state + '/' + registration.id );
38                                 } else {
39                                         $scope.info = { message: info_message, css_class: 'alert-success' };
40                                 }
41                         } else {
42                                 $log.error("can't find id in ", registration);
43                         }
44                 });
45         }
46
47         $scope.reset = function() {
48                 if ( $routeParams.registrationId ) {
49                         $scope.registration = new Registration();
50                         $scope.registration.$get({ registrationId: $routeParams.registrationId }, function(registration) {
51                                 $log.info("get Registration", registration);
52                                 $scope.user = registration.user;
53                                 $scope.work = registration.work;
54                         });
55                 } else {
56                         $scope.registration = new Registration({ user: {}, work: { persons: [] } });
57                         $scope.user = $scope.registration.user;
58                         $scope.work = $scope.registration.work;
59                         $scope.work.persons = [ $scope.user ]; // first author is person submitting work
60                         $log.info("new Registration", $scope.registration);
61                 }
62
63                 $log.info( $routeParams.registrationId );
64
65         }
66
67         $scope.$watch('user.registration_type', function( oldValue, newValue ) {
68                 $log.info("registration_type watch", oldValue, newValue );
69
70                 if ( oldValue === newValue ) return; // triggers on resource, so ignore it
71                 if ( ! $scope.user ) { // resource triggers watch before loading json
72                         $log.error("no user in scope");
73                         return;
74                 }
75
76                 if (
77                         $scope.user.registration_type == 'lecture' ||
78                         $scope.user.registration_type == 'poster' ||
79                         $scope.user.registration_type == 'symposium' ||
80                         $scope.user.registration_type == 'round'
81                 ) {
82                         if ( angular.isUndefined( $scope.registration.work ) ) {
83                                 $log.info('create empty work');
84                                 $scope.registration.work = { persons: [ $scope.user ] };
85                                 if ( angular.isUndefined( $scope.work ) )
86                                         $scope.work = $scope.registration.work;
87                         }
88
89
90                         $scope.work.type = $scope.user.registration_type;
91                         $scope.has_work = true;
92                         $log.info( $scope.user.registration_type, " type updated");
93
94                         if ( $scope.user.registration_type == 'symposium' && ! $scope.work.symposium_works ) {
95                                 // create 4 empty symposium works
96                                 $scope.work.symposium_works = [
97                                         { persons: [{}] },
98                                         { persons: [{}] },
99                                         { persons: [{}] },
100                                         { persons: [{}] }
101                                 ];
102                                 $log.info('created symposium_works');
103                         }
104
105                 } else {
106                         $log.info( $scope.user.registration_type, "NO work" );
107                         $scope.has_work = false;
108                 }
109         });
110
111         $scope.change_student = function() {
112                 if ( $scope.user.student ) {
113                         $scope.user.hpd_member = false;
114                         $scope.user.dinner = false;
115                 }
116         }
117
118         $scope.addPerson = function(persons) {
119                 $log.info('addPerson', persons);
120                 persons.push({ firstname: '' });
121         };
122  
123         $scope.removePerson = function(persons,person) {
124                 for (var i = 0, ii = persons.length; i < ii; i++) {
125                         if (person === persons[i]) {
126                                 persons.splice(i, 1);
127                                 $log.info('removePerson', i, person);
128                         }
129                 }
130         };
131
132         $scope.add_symposium_work = function(works) {
133                 works.push({ persons: [{}] });
134         }
135
136         $scope.symposium_work_remove = function(work_index) {
137                 if ( ! angular.isNumber(work_index) ) {
138                         $log.error("work_index", work_index);
139                         return;
140                 }
141                 var works = $scope.work.symposium_works;
142                 $log.info('symposium_work_remove', works, work_index);
143                 var removed = works.splice( work_index, 1 );
144                 if ( angular.isArray(removed) && removed[0] ) {
145                         removed = removed[0];
146                         removed.deleted = true;
147                         if ( ! angular.isArray( $scope.work.symposium_works_deleted ) )
148                                 $scope.work.symposium_works_deleted = [];
149                         $scope.work.symposium_works_deleted.push( removed );
150                         $log.info("work deleted", works);
151                 } else {
152                         $log.warn("symposium_work_remove no work to remove", works, work);
153                 }
154         }
155
156         $scope.abstract_class = function(work) {
157                 if ( work === undefined ) return;
158                 var abstract = work.abstract;
159                 return angular.isString(abstract) && abstract.length <= 2000 ? 'ok' : 'ng-invalid';
160         }
161         $scope.abstract_length = function(work) {
162                 if ( work === undefined ) return;
163                 var abstract = work.abstract;
164                 if ( ! abstract ) return 0;
165                 return abstract.length <= 2000 ? abstract.length : 2000 - abstract.length;
166         }
167
168         $scope.change_state = function(new_state) {
169                 if ( new_state == $scope.registration.state ) return;
170                 $scope.info = { css_class: 'alert-warning', message: 'Changed state from "'+$scope.registration.state+'" to "'+new_state+'"' };
171                 $scope.registration.state = new_state;
172         }
173
174         $scope.show_submission_type = function() {
175                 // FIXME check if all submission are allowed by date and allow them!
176                 if ( angular.isUndefined( $scope.user ) || angular.isUndefined( $scope.registration ) ) return false;
177                 return (
178                         angular.isUndefined( $scope.user.registration_type ) // new registration
179                         || angular.isUndefined( $scope.registration.state ) // not saved yet
180                         || $scope.registration.state == 'draft'
181                         || $scope.registration.state == 'confirmation'
182                 ) ? false : true;
183         }
184         $scope.show_registration_type = function() {
185                 return true;
186         }
187
188         $scope.reset();
189 }
190 RegistrationCtrl.$inject = [ '$scope', '$log', 'Registration', '$routeParams', '$location', '$route', 'Organizations', 'ValidStates' ];
191
192 function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter, ValidStates ) {
193
194         $scope.list = [];
195         $scope.list_results = 0;
196         $scope.pager = {
197                 page: 1,
198                 limit: 10,
199                 results: 1,
200                 last_page: 1,
201                 show_all: false
202         };
203         $scope.search = {};
204         $scope.ready = false;
205         $scope.filters = [
206                 { select: 'user.student', label: 'student', name: 'filter_student' },
207                 { select: 'user.hpd_member', label: 'HPD', name: 'filter_hpd' },
208                 { select: 'user.reception', label: 'reception', name: 'filter_reception' },
209                 { select: 'user.dinner', label: 'dinner', name: 'filter_dinner' },
210                 { select: 'payment.received', label: 'payment', name: 'filter_payment' }
211         ];
212         $scope.show = {
213                 registration_type: true,
214                 filters: true,
215                 search: true,
216                 states: true,
217                 pager_numeric: false
218         };
219         $scope.ValidStates = ValidStates();
220
221         $scope.create_counts = function( array ) {
222                 $log.info('create_counts', array.length );
223                 var Counts = {};
224                 var inc_count = function(type) {
225                         if ( ! angular.isNumber( Counts[type] ) ) {
226                                 Counts[type] = 1;
227                         } else {
228                                 Counts[type]++;
229                         }
230                 };
231                 angular.forEach( array, function(value, key) {
232                         if ( ! value.user ) {
233                                 $log.error("create_counts user corrupted for registration", key, value);
234                                 return;
235                         }
236
237                         var type = value.user.registration_type;
238                         inc_count( type );
239                         inc_count( '' ); // total
240                         angular.forEach( $scope.filters, function(filter) {
241                                 var s = filter.select.split(/\./);
242                                 var v = value[s[0]];
243                                 if ( angular.isDefined( v ) ) {
244                                         v = v[s[1]];
245                                 }
246                                 if ( v === 'yes' || v === true ) {
247                                         inc_count( filter.name );
248                                         inc_count( type + '+' + filter.name );
249                                         inc_count( '+' + filter.name ); // total
250                                         value[filter.name] = true;
251                                 } else {
252                                         value[filter.name] = false;
253                                 }
254                         });
255
256                         // for filter
257                         value.registration_type = type;
258
259                         // count registration state
260                         inc_count( 'state+' + value.state );
261
262 //                      $log.info( key, value );
263                 });
264                 $log.info('Counts', Counts);
265                 $scope.Counts = Counts;
266                 return Counts;
267         };
268
269         $scope.all_registrations = Registration.query( function(result) {
270                 $scope.ready = true;
271                 $log.info('Reqistration.query callback', result);
272                 $scope.reset();
273         });
274
275         $scope.filter_list = function(newVal, oldVal) {
276                 if ( newVal == oldVal ) return;
277                 $log.info('filter_list', newVal, oldVal, 'search', $scope.search);
278                 var filtered =
279                         $filter('filter')($scope.all_registrations, $scope.search);
280
281                 $scope.create_counts( filtered );
282
283                 $log.info('pager before apply', $scope.pager);
284
285                 $scope.pager.results = filtered.length;
286                 $scope.pager.last_page = Math.ceil( $scope.pager.results / $scope.pager.limit );
287                 if ( $scope.pager.page < 1 ) $scope.pager.page = 1;
288                 if ( $scope.pager.page > $scope.pager.last_page ) $scope.pager.page = $scope.pager.last_page;
289                 $log.info('pager', $scope.pager);
290
291                 var from = ( $scope.pager.page - 1 ) * $scope.pager.limit;
292                 $scope.list = [];
293                 angular.forEach( filtered, function(v,k) {
294                         if ( k >= from && k < from + $scope.pager.limit || $scope.pager.show_all ) {
295                                 v.nr = k + 1;
296                                 this.push(v);
297                         }
298                 }, $scope.list );
299                 $log.info('list length=', $scope.list.length, "offset=", from);
300         };
301         $scope.$watch('search', $scope.filter_list, true);
302         $scope.$watch('search.$', $scope.filter_list); // FIXME not included in search true because of $
303         $scope.$watch('pager', $scope.filter_list, true);
304
305         $scope.RegistrationTypes = RegistrationTypes;
306         $log.info( "RegistrationTypes", RegistrationTypes );
307
308         $scope.reset = function() {
309                 $scope.search = { registration_type: '' };
310                 $log.info('reset', $scope.search );
311                 $scope.pager.page = 1;
312         }
313
314         $scope.print = {
315                 layout: false,
316                 center: '',
317                 toggle: function(v) {
318                         $log.info('print_hide', v);
319                         $scope.print.layout = ! $scope.print.layout;
320                         if ( v ) {
321                                 $scope.print.center = 'print-center';
322                         } else {
323                                 $scope.print.center = '';
324                         }
325                 }
326         }
327 }
328
329 ListCtrl.$inject = [ '$scope', '$log', 'Registration', 'RegistrationTypes', '$filter', 'ValidStates' ];
330