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