store everything in registration resource
[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                 registration.$save( function(registration) {
14                         $log.info("saved", registration);
15                         $log.info('id =', registration.id);
16                         if ( registration.id ) {
17                                 $location.path( '/registration-finished/' + registration.id );
18                         } else {
19                                 $log.error("can't find id in ", registration);
20                         }
21                 });
22         }
23
24         $scope.reset = function() {
25                 if ( $routeParams.registrationId ) {
26                         $scope.registration = new Registration();
27                         $scope.registration.$get({ registrationId: $routeParams.registrationId }, function(registration) {
28                                 $log.info("get Registration", registration);
29                                 $scope.user = registration.user;
30                                 $scope.work = registration.work;
31                         });
32                 } else {
33                         $scope.registration = new Registration({ user: {} });
34                         $log.info("new Registration", $scope.user);
35                         $scope.user = $scope.registration.user;
36                         $scope.work = $scope.registration.work;
37                 }
38
39                 $log.info( $routeParams.registrationId );
40         }
41
42         $scope.$watch('user.registration_type', function( oldValue, newValue ) {
43                 $log.info("registration_type watch", oldValue, newValue );
44
45                 if (
46                         $scope.user.registration_type == 'lecture' ||
47                         $scope.user.registration_type == 'poster' ||
48                         $scope.user.registration_type == 'symposium' ||
49                         $scope.user.registration_type == 'round'
50                 ) {
51                         if ( $scope.work ) {
52                                 $scope.work.type = $scope.user.registration_type;
53                                 $log.info( $scope.user.registration_type, " type updated");
54                         } else {
55                                 $scope.work = {
56                                         type: $scope.user.registration_type,
57                                         persons: [ $scope.user ],
58                                 };
59                                 $log.info( $scope.user.registration_type, " type created");
60                         }
61                         $scope.has_work = true;
62                 } else {
63                         $log.info( $scope.user.registration_type, "NO work" );
64                         $scope.has_work = false;
65                 }
66         });
67
68         $scope.change_student = function() {
69                 if ( $scope.user.student ) {
70                         $scope.user.hpd_member = false;
71                         $scope.user.reception = false;
72                         $scope.user.dinner = false;
73                 }
74         }
75
76         $scope.addPerson = function() {
77                 $scope.work.persons.push({ firstname: '' });
78         };
79  
80         $scope.removePerson = function(person) {
81                 var persons = $scope.work.persons;
82                 for (var i = 0, ii = persons.length; i < ii; i++) {
83                         if (person === persons[i]) {
84                                 persons.splice(i, 1);
85                         }
86                 }
87         };
88
89         $scope.reset();
90 }
91 //RegistrationCtrl.$inject = [ '$scope', '$log' ];
92
93
94 function MyCtrl2() {
95 }
96 MyCtrl2.$inject = [];