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