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