3c43f53ab1a889f2a01f598f223c3838a796ecda
[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                 return (
164                         angular.isUndefined( $scope.user.registration_type ) // new registration
165                         || angular.isUndefined( $scope.registration.state ) // not saved yet
166                         || $scope.registration.state == 'draft'
167                         || $scope.registration.state == 'confirmation'
168                 ) ? false : true;
169         }
170         $scope.show_registration_type = function() {
171                 return true;
172         }
173
174         $scope.reset();
175 }
176 RegistrationCtrl.$inject = [ '$scope', '$log', 'Registration', '$routeParams', '$location', '$route', 'Organizations', 'ValidStates' ];
177
178 function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter, ValidStates ) {
179
180         $scope.list = [];
181         $scope.list_results = 0;
182         $scope.pager = {
183                 page: 1,
184                 limit: 10,
185                 results: 1,
186                 last_page: 1,
187                 show_all: false
188         };
189         $scope.search = {};
190         $scope.ready = false;
191         $scope.filters = [ 'student', 'hpd_member', 'reception', 'dinner' ];
192         $scope.show = {
193                 registration_type: true,
194                 filters: true,
195                 search: true,
196                 states: true,
197                 pager_numeric: false
198         };
199         $scope.ValidStates = ValidStates();
200
201         $scope.create_counts = function( array ) {
202                 $log.info('create_counts', array.length );
203                 var Counts = {};
204                 var inc_count = function(type) {
205                         if ( ! angular.isNumber( Counts[type] ) ) {
206                                 Counts[type] = 1;
207                         } else {
208                                 Counts[type]++;
209                         }
210                 };
211                 angular.forEach( array, function(value, key) {
212                         if ( ! value.user ) {
213                                 $log.error("create_counts user corrupted for registration", key, value);
214                                 return;
215                         }
216
217                         var type = value.user.registration_type;
218                         inc_count( type );
219                         inc_count( '' ); // total
220                         angular.forEach( $scope.filters, function(subtype) {
221                                 var v = value.user[subtype];
222                                 if ( v === 'yes' || v === true ) {
223                                         inc_count( subtype );
224                                         inc_count( type + '+' + subtype );
225                                         inc_count( '+' + subtype ); // total
226                                 }
227                         });
228
229                         // for filter
230                         value.registration_type = type;
231                         angular.forEach( $scope.filters, function(f) {
232                                 var v = value.user[f];
233                                 value[f] = v == true || v == 'yes' ? true : false;
234                         });
235
236                         // count registration state
237                         inc_count( 'state+' + value.state );
238
239 //                      $log.info( key, value, Counts[type]  );
240                 });
241                 $log.info('Counts', Counts);
242                 $scope.Counts = Counts;
243                 return Counts;
244         };
245
246         $scope.all_registrations = Registration.query( function(result) {
247                 $scope.ready = true;
248                 $log.info('Reqistration.query callback', result);
249                 $scope.reset();
250         });
251
252         $scope.filter_list = function(newVal, oldVal) {
253                 if ( newVal == oldVal ) return;
254                 $log.info('filter_list', newVal, oldVal, 'search', $scope.search);
255                 var filtered =
256                         $filter('filter')($scope.all_registrations, $scope.search);
257
258                 $scope.create_counts( filtered );
259
260                 $log.info('pager before apply', $scope.pager);
261
262                 $scope.pager.results = filtered.length;
263                 $scope.pager.last_page = Math.ceil( $scope.pager.results / $scope.pager.limit );
264                 if ( $scope.pager.page < 1 ) $scope.pager.page = 1;
265                 if ( $scope.pager.page > $scope.pager.last_page ) $scope.pager.page = $scope.pager.last_page;
266                 $log.info('pager', $scope.pager);
267
268                 var from = ( $scope.pager.page - 1 ) * $scope.pager.limit;
269                 $scope.list = [];
270                 angular.forEach( filtered, function(v,k) {
271                         if ( k >= from && k < from + $scope.pager.limit || $scope.pager.show_all ) {
272                                 v.nr = k + 1;
273                                 this.push(v);
274                         }
275                 }, $scope.list );
276                 $log.info('list length=', $scope.list.length, "offset=", from);
277         };
278         $scope.$watch('search', $scope.filter_list, true);
279         $scope.$watch('search.$', $scope.filter_list); // FIXME not included in search true because of $
280         $scope.$watch('pager', $scope.filter_list, true);
281
282         $scope.RegistrationTypes = RegistrationTypes;
283         $log.info( "RegistrationTypes", RegistrationTypes );
284
285         $scope.reset = function() {
286                 $scope.search = { registration_type: '' };
287                 $log.info('reset', $scope.search );
288                 $scope.pager.page = 1;
289         }
290 }
291
292 ListCtrl.$inject = [ '$scope', '$log', 'Registration', 'RegistrationTypes', '$filter', 'ValidStates' ];
293