f336b513ef78d078039c3d31ee450d785406025c
[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                 if ( angular.isUndefined( $scope.user ) || angular.isUndefined( $scope.registration ) ) return false;
181                 return (
182                         angular.isUndefined( $scope.user.registration_type ) // new registration
183                         || angular.isUndefined( $scope.registration.state ) // not saved yet
184                         || $scope.registration.state == 'draft'
185                         || $scope.registration.state == 'confirmation'
186                 ) ? false : true;
187         }
188         $scope.show_registration_type = function() {
189                 return true;
190         }
191
192         $scope.reset();
193 }
194 RegistrationCtrl.$inject = [ '$scope', '$log', 'Registration', '$routeParams', '$location', '$route', 'View', 'ValidStates' ];
195
196 function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter, ValidStates ) {
197
198         $scope.list = [];
199         $scope.list_results = 0;
200         $scope.pager = {
201                 page: 1,
202                 limit: 10,
203                 results: 1,
204                 last_page: 1,
205                 show_all: false
206         };
207         $scope.search = {};
208         $scope.ready = false;
209         $scope.filters = [
210                 { select: 'user.student', label: 'student', name: 'filter_student' },
211                 { select: 'user.hpd_member', label: 'HPD', name: 'filter_hpd' },
212                 { select: 'user.reception', label: 'reception', name: 'filter_reception' },
213                 { select: 'user.dinner', label: 'dinner', name: 'filter_dinner' },
214                 { select: 'payment.received', label: 'payment', name: 'filter_payment' },
215                 { select: 'user.org_member', label: 'organisation', name: 'filter_org' }
216         ];
217         $scope.show = {
218                 registration_type: true,
219                 filters: true,
220                 search: true,
221                 states: true,
222                 pager_numeric: false
223         };
224         $scope.ValidStates = ValidStates();
225
226         $scope.create_counts = function( array ) {
227                 $log.info('create_counts', array.length );
228                 var Counts = {};
229                 var inc_count = function(type) {
230                         if ( ! angular.isNumber( Counts[type] ) ) {
231                                 Counts[type] = 1;
232                         } else {
233                                 Counts[type]++;
234                         }
235                 };
236                 angular.forEach( array, function(value, key) {
237                         if ( ! value.user ) {
238                                 $log.error("create_counts user corrupted for registration", key, value);
239                                 return;
240                         }
241
242                         var type = value.user.registration_type;
243                         inc_count( type );
244                         inc_count( '' ); // total
245                         angular.forEach( $scope.filters, function(filter) {
246                                 var s = filter.select.split(/\./);
247                                 var v = value[s[0]];
248                                 if ( angular.isDefined( v ) ) {
249                                         v = v[s[1]];
250                                 }
251                                 if ( v === 'yes' || v === true ) {
252                                         inc_count( filter.name );
253                                         inc_count( type + '+' + filter.name );
254                                         inc_count( '+' + filter.name ); // total
255                                         value[filter.name] = true;
256                                 } else {
257                                         value[filter.name] = false;
258                                 }
259                         });
260
261                         // for filter
262                         value.registration_type = type;
263
264                         // count registration state
265                         inc_count( 'state+' + value.state );
266
267 //                      $log.info( key, value );
268                 });
269                 $log.info('Counts', Counts);
270                 $scope.Counts = Counts;
271                 return Counts;
272         };
273
274         $scope.all_registrations = Registration.query( function(result) {
275                 $scope.ready = true;
276                 $log.info('Reqistration.query callback', result);
277                 $scope.reset();
278         });
279
280         $scope.filter_list = function(newVal, oldVal) {
281                 if ( newVal == oldVal ) return;
282                 $log.info('filter_list', newVal, oldVal, 'search', $scope.search);
283                 var filtered =
284                         $filter('filter')($scope.all_registrations, $scope.search);
285
286                 $scope.create_counts( filtered );
287
288                 $log.info('pager before apply', $scope.pager);
289
290                 $scope.pager.results = filtered.length;
291                 $scope.pager.last_page = Math.ceil( $scope.pager.results / $scope.pager.limit );
292                 if ( $scope.pager.page < 1 ) $scope.pager.page = 1;
293                 if ( $scope.pager.page > $scope.pager.last_page ) $scope.pager.page = $scope.pager.last_page;
294                 $log.info('pager', $scope.pager);
295
296                 var from = ( $scope.pager.page - 1 ) * $scope.pager.limit;
297                 $scope.list = [];
298                 angular.forEach( filtered, function(v,k) {
299                         if ( k >= from && k < from + $scope.pager.limit || $scope.pager.show_all ) {
300                                 v.nr = k + 1;
301                                 this.push(v);
302                         }
303                 }, $scope.list );
304                 $log.info('list length=', $scope.list.length, "offset=", from);
305         };
306         $scope.$watch('search', $scope.filter_list, true);
307         $scope.$watch('search.$', $scope.filter_list); // FIXME not included in search true because of $
308         $scope.$watch('pager', $scope.filter_list, true);
309
310         $scope.RegistrationTypes = RegistrationTypes;
311         $log.info( "RegistrationTypes", RegistrationTypes );
312
313         $scope.reset = function() {
314                 $scope.search = { registration_type: '' };
315                 $log.info('reset', $scope.search );
316                 $scope.pager.page = 1;
317         }
318
319         $scope.print = {
320                 layout: false,
321                 center: '',
322                 bold: '',
323                 toggle: function(v) {
324                         $log.info('print_hide', v);
325                         $scope.print.layout = ! $scope.print.layout;
326                         if ( v ) {
327                                 $scope.print.center = 'print-center';
328                                 $scope.print.bold = 'print-bold';
329                         } else {
330                                 $scope.print.center = '';
331                                 $scope.print.bold = '';
332                         }
333                 }
334         }
335 }
336
337 ListCtrl.$inject = [ '$scope', '$log', 'Registration', 'RegistrationTypes', '$filter', 'ValidStates' ];
338
339
340 function AuthorsCtrl($scope, $log, View ) {
341         $scope.message = 'Loading data...';
342         $scope.ready = false;
343         View.getPromise('authors?format=key_distinct').then(function(data) {
344                 if ( angular.isArray(data.rows) ) {
345                         $scope.data = data;
346                         $scope.authors = $scope.data.rows;
347                         $scope.ready = true;
348                         $log.info('authors promise ', data);
349                 } else {
350                         $log.info('no rows in', data);
351                         $scope.message = 'No rows found';
352                 }
353         });
354 }
355
356 AuthorsCtrl.$inject = [ '$scope', '$log', 'View' ];
357
358