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