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