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