321d570803eea16492861f93931e1935104c6828
[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.error( $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 = [ 'student', 'hpd_member', 'reception', 'dinner' ];
206         $scope.show = {
207                 registration_type: true,
208                 filters: true,
209                 search: true,
210                 states: true,
211                 pager_numeric: false
212         };
213         $scope.ValidStates = ValidStates();
214
215         $scope.create_counts = function( array ) {
216                 $log.info('create_counts', array.length );
217                 var Counts = {};
218                 var inc_count = function(type) {
219                         if ( ! angular.isNumber( Counts[type] ) ) {
220                                 Counts[type] = 1;
221                         } else {
222                                 Counts[type]++;
223                         }
224                 };
225                 angular.forEach( array, function(value, key) {
226                         if ( ! value.user ) {
227                                 $log.error("create_counts user corrupted for registration", key, value);
228                                 return;
229                         }
230
231                         var type = value.user.registration_type;
232                         inc_count( type );
233                         inc_count( '' ); // total
234                         angular.forEach( $scope.filters, function(subtype) {
235                                 var v = value.user[subtype];
236                                 if ( v === 'yes' || v === true ) {
237                                         inc_count( subtype );
238                                         inc_count( type + '+' + subtype );
239                                         inc_count( '+' + subtype ); // total
240                                 }
241                         });
242
243                         // for filter
244                         value.registration_type = type;
245                         angular.forEach( $scope.filters, function(f) {
246                                 var v = value.user[f];
247                                 value[f] = v == true || v == 'yes' ? true : false;
248                         });
249
250                         // count registration state
251                         inc_count( 'state+' + value.state );
252
253 //                      $log.info( key, value, Counts[type]  );
254                 });
255                 $log.info('Counts', Counts);
256                 $scope.Counts = Counts;
257                 return Counts;
258         };
259
260         $scope.all_registrations = Registration.query( function(result) {
261                 $scope.ready = true;
262                 $log.info('Reqistration.query callback', result);
263                 $scope.reset();
264         });
265
266         $scope.filter_list = function(newVal, oldVal) {
267                 if ( newVal == oldVal ) return;
268                 $log.info('filter_list', newVal, oldVal, 'search', $scope.search);
269                 var filtered =
270                         $filter('filter')($scope.all_registrations, $scope.search);
271
272                 $scope.create_counts( filtered );
273
274                 $log.info('pager before apply', $scope.pager);
275
276                 $scope.pager.results = filtered.length;
277                 $scope.pager.last_page = Math.ceil( $scope.pager.results / $scope.pager.limit );
278                 if ( $scope.pager.page < 1 ) $scope.pager.page = 1;
279                 if ( $scope.pager.page > $scope.pager.last_page ) $scope.pager.page = $scope.pager.last_page;
280                 $log.info('pager', $scope.pager);
281
282                 var from = ( $scope.pager.page - 1 ) * $scope.pager.limit;
283                 $scope.list = [];
284                 angular.forEach( filtered, function(v,k) {
285                         if ( k >= from && k < from + $scope.pager.limit || $scope.pager.show_all ) {
286                                 v.nr = k + 1;
287                                 this.push(v);
288                         }
289                 }, $scope.list );
290                 $log.info('list length=', $scope.list.length, "offset=", from);
291         };
292         $scope.$watch('search', $scope.filter_list, true);
293         $scope.$watch('search.$', $scope.filter_list); // FIXME not included in search true because of $
294         $scope.$watch('pager', $scope.filter_list, true);
295
296         $scope.RegistrationTypes = RegistrationTypes;
297         $log.info( "RegistrationTypes", RegistrationTypes );
298
299         $scope.reset = function() {
300                 $scope.search = { registration_type: '' };
301                 $log.info('reset', $scope.search );
302                 $scope.pager.page = 1;
303         }
304 }
305
306 ListCtrl.$inject = [ '$scope', '$log', 'Registration', 'RegistrationTypes', '$filter', 'ValidStates' ];
307