remove work data if there is no need for one
[angular-drzb] / app / js / controllers.js
1 'use strict';
2
3 /* Controllers */
4
5
6 function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $route) {
7
8         $scope.$routeParams = $routeParams;
9         $scope.$location = $location;
10
11         $scope.update = function(registration) {
12
13                 if ( ! $scope.has_work && registration.work ) {
14                         delete( registration.work );
15                         $log.info("removed work");
16                 }
17
18                 registration.$save( function(registration) {
19                         $log.info("saved", registration);
20                         $log.info('id =', registration.id);
21                         if ( registration.id ) {
22                                 $location.path( '/registration-finished/' + registration.id );
23                         } else {
24                                 $log.error("can't find id in ", registration);
25                         }
26                 });
27         }
28
29         $scope.reset = function() {
30                 if ( $routeParams.registrationId ) {
31                         $scope.registration = new Registration();
32                         $scope.registration.$get({ registrationId: $routeParams.registrationId }, function(registration) {
33                                 $log.info("get Registration", registration);
34                                 $scope.user = registration.user;
35                                 $scope.work = registration.work;
36                         });
37                 } else {
38                         $scope.registration = new Registration({ user: {} });
39                         $log.info("new Registration", $scope.user);
40                         $scope.user = $scope.registration.user;
41                         $scope.work = $scope.registration.work;
42                 }
43
44                 $log.info( $routeParams.registrationId );
45         }
46
47         $scope.$watch('user.registration_type', function( oldValue, newValue ) {
48                 $log.info("registration_type watch", oldValue, newValue );
49
50                 if (
51                         $scope.user.registration_type == 'lecture' ||
52                         $scope.user.registration_type == 'poster' ||
53                         $scope.user.registration_type == 'symposium' ||
54                         $scope.user.registration_type == 'round'
55                 ) {
56                         if ( $scope.work ) {
57                                 $scope.work.type = $scope.user.registration_type;
58                                 $log.info( $scope.user.registration_type, " type updated");
59                         } else {
60                                 $scope.work = {
61                                         type: $scope.user.registration_type,
62                                         persons: [ $scope.user ],
63                                 };
64                                 $log.info( $scope.user.registration_type, " type created");
65                         }
66                         $scope.has_work = true;
67                 } else {
68                         $log.info( $scope.user.registration_type, "NO work" );
69                         $scope.has_work = false;
70                 }
71         });
72
73         $scope.change_student = function() {
74                 if ( $scope.user.student ) {
75                         $scope.user.hpd_member = false;
76                         $scope.user.reception = false;
77                         $scope.user.dinner = false;
78                 }
79         }
80
81         $scope.addPerson = function() {
82                 $scope.work.persons.push({ firstname: '' });
83         };
84  
85         $scope.removePerson = function(person) {
86                 var persons = $scope.work.persons;
87                 for (var i = 0, ii = persons.length; i < ii; i++) {
88                         if (person === persons[i]) {
89                                 persons.splice(i, 1);
90                         }
91                 }
92         };
93
94         $scope.reset();
95 }
96 //RegistrationCtrl.$inject = [ '$scope', '$log' ];
97
98
99 function MyCtrl2() {
100 }
101 MyCtrl2.$inject = [];