controllers.js
5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
angular.module('confjuvapp.controllers', [])
.controller('ProposalCtrl', function($scope, $ionicModal, $http, $ionicPopup) {
$scope.loading = false;
/******************************************************************************
L O G I N
******************************************************************************/
$scope.loginFormDisplayed = false;
$scope.displayLoginForm = function() {
$scope.loginFormDisplayed = true;
$scope.registerFormDisplayed = false;
};
// Function to open the modal
$scope.openModal = function() {
if ($scope.modal) {
$scope.modal.show();
}
else {
// Initiate the modal
$ionicModal.fromTemplateUrl('html/_login.html', {
scope: $scope,
animation: 'slide-in-up'
}).then(function(modal) {
$scope.modal = modal;
$scope.modal.show();
});
}
};
// Function to close the modal
$scope.closeModal = function() {
$scope.modal.hide();
};
// Cleanup the modal when we're done with it!
$scope.$on('$destroy', function() {
$scope.modal.remove();
});
// Function to login
$scope.Login = function(data) {
if (!data || !data.login || !data.password) {
return;
}
$scope.loading = true;
var config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
},
timeout: 10000
}
$http.post(ConfJuvAppUtils.pathTo('login'), jQuery.param(data), config)
.then(function(resp) {
$scope.closeModal();
var popup = $ionicPopup.alert({ title: 'Login', template: 'Login efetuado com sucesso!' });
$scope.loggedIn = true;
$scope.user = resp.data.person;
popup.then(function() {
$scope.loadDiscussions(resp.data.private_token);
});
}, function(err) {
$scope.closeModal();
var popup = $ionicPopup.alert({ title: 'Login', template: 'Erro ao efetuar login. Verifique usuário e senha e conexão com a internet.' });
$scope.loggedIn = false;
$scope.loading = false;
popup.then(function() {
$scope.openModal();
});
});
};
// Function to retrieve password
$scope.forgotPassword = function(email) {
if (!email) {
var popup = $ionicPopup.alert({ title: 'Esqueceu a senha?', template: 'Digite seu e-mail no campo "Usuário" e clique novamente neste link' });
return;
}
$scope.loading = true;
var config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
},
timeout: 10000
}
var data = { value: email };
$http.post(ConfJuvAppUtils.pathTo('account/forgot_password', true), jQuery.param(data), config)
.then(function(resp) {
$ionicPopup.alert({ title: 'Esqueceu a senha?', template: 'Um link para redefinição de senha foi enviado para o seu e-mail.' });
$scope.loading = false;
}, function(err) {
$ionicPopup.alert({ title: 'Esqueceu a senha?', template: 'Erro ao requisitar redefinição de senha.' });
$scope.loading = false;
});
};
// Register as a new user
$scope.registerFormDisplayed = false;
$scope.displayRegisterForm = function() {
$scope.registerFormDisplayed = true;
$scope.loginFormDisplayed = false;
};
// Function to register
$scope.Register = function(data) {
if (!data || !data.login || !data.email || !data.password || !data.password_confirmation) {
$ionicPopup.alert({ title: 'Registrar', template: 'Por favor preencha todos os campos' });
return;
}
else if (data.password != data.password_confirmation) {
$ionicPopup.alert({ title: 'Registrar', template: 'Senhas não conferem' });
return;
}
$scope.loading = true;
var config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
},
timeout: 10000
}
$http.post(ConfJuvAppUtils.pathTo('register'), jQuery.param(data), config)
.then(function(resp) {
var popup = $ionicPopup.alert({ title: 'Registrar', template: 'Usuário registrado com sucesso!' });
popup.then(function() {
$scope.registerFormDisplayed = false;
});
$scope.loading = false;
}, function(err) {
var msg = '';
try {
var errors = JSON.parse(err.data.message);
for (var field in errors) {
msg += 'Campo "' + field + '" ' + errors[field][0] + '. ';
}
} catch(e) {
// Do nothing
}
$ionicPopup.alert({ title: 'Registrar', template: 'Erro ao registrar usuário. ' + msg });
$scope.loading = false;
});
};
$scope.backToLoginHome = function() {
$scope.registerFormDisplayed = false;
$scope.loginFormDisplayed = false;
};
/******************************************************************************
D I S C U S S I O N S
******************************************************************************/
$scope.discussionsList = [];
$scope.loadDiscussions = function(token) {
$scope.discussionsList = [];
var path = '?private_token=' + token + '&fields=title,image,body,abstract&content_type=ProposalsDiscussionPlugin::Proposal';
if (ConfJuvAppConfig.noosferoCommunity == '') {
path = 'articles' + path;
}
else {
path = 'communities/' + ConfJuvAppConfig.noosferoCommunity + '/articles' + path;
}
$http.get(ConfJuvAppUtils.pathTo(path))
.then(function(resp) {
$scope.loading = false;
$scope.discussionsList = resp.data.articles;
}, function(err) {
var popup = $ionicPopup.alert({ title: 'Discussões', template: 'Não foi possível carregar as discussões' });
$scope.loading = false;
});
};
}); // Ends controller