Project

General

Profile

1
var orgsModule = angular.module('orgs', ['ngRoute', 'checklist-model']);
2

    
3
orgsModule.service('vocabulariesService', function($http) {
4
    this.vocs = {};
5
    this.getVocs = function(f) {
6
   		if (Object.keys(this.vocs).length === 0) {
7
   			$http.get('api/vocabularies').then(function successCallback(res) {
8
   				if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
9
	    		this.vocs = res.data;
10
	    		f(angular.copy(this.vocs));
11
			}, function errorCallback(res) {
12
				alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
13
			});
14
   		} else {
15
   			f(this.vocs);
16
   		}
17
    };
18
});
19

    
20
orgsModule.directive('selectOrgModal', function($http) {
21
	return {
22
		restrict: 'E',
23
		scope: {
24
			'modalId'     : '@',
25
			'selectedOrg' : '='
26
		},
27
		templateUrl: 'resources/html/modals/select_org.html',
28
		link: function(scope, element, attrs, ctrl) {
29
			scope.searchOrgs = {};
30
			scope.searchValue = '';
31
			
32
			scope.search = function(text, page, size) {
33
				scope.searchOrgs = {};
34
				$http.get('api/organizations/search/' + page + '/' + size + '?q=' + text).then(function successCallback(res) {
35
					if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
36
					scope.searchValue = text;
37
					scope.searchOrgs = res.data;
38
				}, function errorCallback(res) {
39
					alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
40
				});
41
			}
42
		}
43
	}
44
});
45

    
46
orgsModule.directive('resolveConflictsModal', function($http) {
47
	return {
48
		restrict: 'E',
49
		scope: {
50
			'modalId'     : '@',
51
			'orgs'        : '=',
52
			'selectedOrgs': '='
53
		},
54
		templateUrl: 'resources/html/modals/resolve_conflicts.html',
55
		link: function(scope, element, attrs, ctrl) {
56
			scope.selectOrg = function(org) {
57
				var sel = angular.copy(org);
58
				if (scope.selectedOrgs.length == 0) { sel.show = 'success'; }
59
				else                                { sel.show = 'default'; }
60
				scope.selectedOrgs.push(sel); 
61
				org.show = 'hidden';
62
			}
63

    
64
			scope.reset = function() {
65
				scope.selectedOrgs = [];
66
				angular.forEach(scope.orgs, function(org) { org.show = 'default'; });
67
			}
68
			
69
			scope.createGroup = function() {
70
				alert('todo');
71
			}		
72
		}
73
	}
74
});
75
		
76
orgsModule.directive('orgTabsMenu', function($http) {
77
	return {
78
		restrict: 'E',
79
		scope: {
80
			'orgId'    : '@',
81
			'info'     : '=',
82
			'selected' : '=',
83
			'org'      : '=',
84
			'events'   : '=',
85
		},
86
		templateUrl: 'resources/html/menu/org_tabs_menu.html',
87
		link: function(scope, element, attrs, ctrl) {
88

    
89
			scope.loadOrg = function() {
90
				scope.org = {};
91
				$http.get('api/organizations/get?id=' + scope.orgId).then(function successCallback(res) {
92
					if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
93
					scope.org = res.data;
94
				}, function errorCallback(res) {
95
					alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
96
				});
97
				scope.selected = 1;
98
			}
99
			
100
			scope.loadDedupEvents = function() {
101
				scope.events = {};
102

    
103
				$http.get('api/organizations/conflicts?id=' + scope.orgId).then(function successCallback(res) {
104
					if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
105
					scope.events.conflicts = res.data;
106
					$http.get('api/organizations/duplicates?id=' + scope.orgId).then(function successCallback(res) {
107
						if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
108
						scope.events.duplicates = res.data;
109
					}, function errorCallback(res) {
110
						alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
111
					});
112
				}, function errorCallback(res) {
113
					alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
114
				});
115
				scope.selected = 2;
116
			};
117
								
118
			scope.loadOrg();
119
		}
120
     }
121
});
122

    
123

    
124
orgsModule.directive('orgFormMetadata', function($http, $location, $route, $routeParams) {
125
	return {
126
		restrict: 'E',
127
		scope: {
128
			'orgId'        : '@',
129
			'org'          : '=',
130
			'vocabularies' : '=',
131
			'mode'         : '@',  // insert or update
132
			'infoMethod'   : '&'
133
		},
134
		templateUrl: 'resources/html/forms/org_metadata.html',
135
		link: function(scope, element, attrs, ctrl) {
136
			
137
			scope.newRelation = {};
138
			scope.newRelType = '';
139
							
140
			scope.resetSelectedRelation = function() {
141
				scope.newRelation = {};
142
			}
143
		
144
			scope.addNewRelation = function(r) {
145
				scope.org.relations.push({
146
					'relatedOrgId'   : scope.newRelation.id,
147
					'relatedOrgName' : scope.newRelation.name,
148
					'type'           : scope.newRelType
149
				});
150
				scope.newRelation = {};
151
				scope.newRelType = '';
152
			}
153
			
154
			scope.save = function() {
155
				$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
156
				$http.post('api/organizations/save', scope.org).then(function successCallback(res) {
157
					if      ((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
158
					else if (scope.mode == 'insert')        { $location.url('/edit/1/' + res.data[0]);    }
159
					else if ($routeParams.msg == 2)         { $route.reload(); }
160
					else                                    { $location.url('/edit/2/' + res.data[0]); }
161
				}, function errorCallback(res) {
162
					alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
163
				}); 
164
			}
165
		}
166
     }
167
});
168

    
169
orgsModule.directive('orgDedupEvents', function($http, $location, $route) {
170
	return {
171
		restrict: 'E',
172
		scope: {
173
			'orgId'        : '@',
174
			'events'       : '=',
175
			'vocabularies' : '=',
176
			'infoMethod'   : '&'
177
		},
178
		templateUrl: 'resources/html/parts/org_dedup_events.html',
179
		link: function(scope, element, attrs, ctrl) {
180
			
181
			scope.currentOrgDetails = {};
182
			
183
			$http.get('api/organizations/get?id=' + scope.orgId).then(function successCallback(res) {
184
				if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
185
				scope.currentOrgDetails = res.data;
186
			}, function errorCallback(res) {
187
				alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
188
			});
189
			
190
			scope.saveDuplicates = function() {
191
				$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
192
				$http.post('api/organizations/duplicates', scope.events.duplicates).then(function successCallback(res) {
193
					if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
194
					if (scope.infoMethod)             { scope.infoMethod(); }
195
					alert('Events updated !!!');
196
					scope.events.duplicates = res.data;
197
				}, function errorCallback(res) {
198
					alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
199
				});
200
			}
201
			
202
			scope.saveConflicts = function() {
203
				alert('todo');
204
			}
205
		}
206
     }
207
});
208

    
209

    
210
orgsModule.directive('orgDetails', function($http, $location, $route) {
211
	return {
212
		restrict: 'E',
213
		scope: {
214
			'org'      : '=',
215
			'orgTitle' : '@',
216
			'show'     : '@'
217
		},
218
		templateUrl: 'resources/html/parts/org_details.html',
219
		link: function(scope, element, attrs, ctrl) {}
220
     }
221
});
222

    
223
orgsModule.directive('orgResults', function($http, $location, $route) {
224
	return {
225
		restrict: 'E',
226
		scope: {
227
			'searchMessage' : '@',
228
			'orgs'          : '=',
229
			'nextFunction'  : '&',
230
			'prevFunction'  : '&',
231
			'selectedOrg'   : '=',
232
			'mode'          : '@'
233
		},
234
		templateUrl: 'resources/html/parts/org_results.html',
235
		link: function(scope, element, attrs, ctrl) {
236
			scope.selectOrg = function(o) {
237
				scope.selectedOrg = o;
238
			}
239
		}
240
     }
241
});
242

    
243

    
244
orgsModule.directive('allConflicts', function($http, $location, $route, $q) {
245
	return {
246
		restrict: 'E',
247
		scope: {
248
			'conflicts'    : '=',
249
			'country'      : '@',
250
			'info'         : '=',
251
			'infoMethod'   : '&'
252
		},
253
		templateUrl: 'resources/html/forms/all_conflicts.html',
254
		link: function(scope, element, attrs, ctrl) {
255
			scope.orgs = [];
256
			
257
			scope.prepareConflictsModal = function(list) {
258
				scope.orgs = [];
259
				scope.selectedOrgs = [];
260
				
261
				var gets = list.map((o) => $http.get('api/organizations/get?id=' + o.id));
262
				
263
				$q.all(gets).then(function(responses) {
264
					scope.orgs = responses.map((resp) => resp.data);
265
					angular.forEach(scope.orgs, function(org) { org.show = 'default'; });
266
				});
267
			}
268
			
269
		}
270
     }
271
});
272

    
273
orgsModule.directive('orgFormDuplicates', function($http, $location, $route) {
274
	return {
275
		restrict: 'E',
276
		scope: {
277
			'duplicates'     : '=',
278
			'showSaveButton' : '@',
279
			'saveFunction'   : '&'
280
		},
281
		templateUrl: 'resources/html/forms/org_duplicates.html',
282
		link: function(scope, element, attrs, ctrl) {}
283
     }
284
});
285

    
286
orgsModule.directive('orgFormConflicts', function($http, $location, $route) {
287
	return {
288
		restrict: 'E',
289
		scope: {
290
			'conflicts'      : '=',
291
			'showSaveButton' : '@',
292
			'saveFunction'   : '&'
293
		},
294
		templateUrl: 'resources/html/forms/org_conflicts.html',
295
		link: function(scope, element, attrs, ctrl) {}
296
     }
297
});
298

    
299
orgsModule.directive('allDuplicates', function($http, $location, $route) {
300
	return {
301
		restrict: 'E',
302
		scope: {
303
			'duplicates'   : '=',
304
			'country'      : '@',
305
			'info'         : '=',
306
			'infoMethod'   : '&'
307
		},
308
		templateUrl: 'resources/html/forms/all_duplicates.html',
309
		link: function(scope, element, attrs, ctrl) {
310
			scope.currentOrg = {};
311
			scope.currentOrgDetails = {};
312
			scope.currentDuplicates = [];
313
			
314
			scope.prepareDuplicatesModal = function(org) {
315
				scope.currentOrg = org;
316
				scope.currentOrgDetails = {};
317
				scope.currentDuplicates = [];
318
				
319
				$http.get('api/organizations/get?id=' + org.id).then(function successCallback(res) {
320
					if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
321
					scope.currentOrgDetails = res.data;
322
				}, function errorCallback(res) {
323
					alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
324
				});
325
				
326
				$http.get('api/organizations/duplicates?id=' + org.id).then(function successCallback(res) {
327
					if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
328
					scope.currentDuplicates = res.data;
329
				}, function errorCallback(res) {
330
					alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
331
				});
332
			};
333
			
334
			scope.saveCurrentDuplicates = function() {
335
				$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
336
				$http.post('api/organizations/duplicates', scope.currentDuplicates).then(function successCallback(res) {
337
					if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
338
					if (scope.infoMethod)             { scope.infoMethod(); }
339
					scope.currentOrg.numberOfDuplicates = 0;
340
					for (var i=0; i<res.data.length; i++) {
341
						if (res.data[i].relType == 'suggested') {
342
							scope.currentOrg.numberOfDuplicates++;
343
						}
344
					}
345
					scope.currentDuplicates = [];
346
				}, function errorCallback(res) {
347
					alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
348
				});
349
			};
350
		}
351
    }
352
});
353

    
354
orgsModule.config(function($routeProvider) {
355
	$routeProvider
356
		.when('/new',                              { templateUrl: 'resources/html/new.html',              controller: 'newOrgCtrl' })
357
		.when('/search',                           { templateUrl: 'resources/html/search.html',           controller: 'searchCtrl' })
358
		.when('/searchResults/:page/:size/:text*', { templateUrl: 'resources/html/searchResults.html',    controller: 'searchResultsCtrl' })
359
		.when('/countries',                        { templateUrl: 'resources/html/browse.html',           controller: 'countriesCtrl' })
360
		.when('/byCountry/:page/:size/:code*',     { templateUrl: 'resources/html/resultsByCountry.html', controller: 'byCountryCtrl' })
361
		.when('/types',                            { templateUrl: 'resources/html/browse.html',           controller: 'typesCtrl' })
362
		.when('/byType/:page/:size/:type*',        { templateUrl: 'resources/html/resultsByType.html',    controller: 'byTypeCtrl' })
363
		.when('/edit/:msg/:id',                    { templateUrl: 'resources/html/edit.html',             controller: 'showEditCtrl' })
364
		.when('/suggestions/:country/:tab',        { templateUrl: 'resources/html/suggestions.html',      controller: 'showSuggestionsCtrl' })
365
		.when('/users',                            { templateUrl: 'resources/html/users.html',            controller: 'usersCtrl' })
366
		.otherwise({ redirectTo: '/suggestions/_/1' });
367
});
368

    
369
orgsModule.filter('escape', function() {
370
	return function(input) {
371
		return encodeURIComponent(encodeURIComponent(input));
372
	}; 
373
});
374

    
375
orgsModule.filter('unescape', function() {
376
	return function(input) {
377
		return decodeURIComponent(input);
378
	};
379
});
380

    
381
orgsModule.controller('newOrgCtrl', function ($scope, $http, $routeParams, $location, vocabulariesService) {
382
	$scope.org = {
383
		"id": "",
384
		"name": "",
385
		"type": null,
386
		"lat": 0.0,
387
		"lng": 0.0,
388
		"city": "",
389
		"country": "",
390
		"source": "user",
391
		"otherIdentifiers": [],
392
		"otherNames": [],
393
		"relations": [],
394
		"acronyms": [],
395
		"urls": [],
396
		"relations": []
397
	};
398
	
399
	$scope.vocabularies = {};
400
	vocabulariesService.getVocs(function(vocs) { $scope.vocabularies = vocs; });
401
	
402
});
403

    
404
orgsModule.controller('searchCtrl', function ($scope, $location) {
405
	$scope.searchText = '';
406
	$scope.search = function() {
407
		$location.url('/searchResults/0/50/' + encodeURIComponent(encodeURIComponent($scope.searchText)));
408
	}
409
});
410

    
411
orgsModule.controller('searchResultsCtrl', function ($scope, $http, $routeParams, $location) {
412
	$scope.searchText = decodeURIComponent($routeParams.text);
413
	$scope.orgs = {};
414
	
415
	$http.get('api/organizations/search/' + $routeParams.page + '/' + $routeParams.size + '?q=' + $scope.searchText).then(function successCallback(res) {
416
		if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
417
		$scope.orgs = res.data;
418
	}, function errorCallback(res) {
419
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
420
	});
421
	
422
	$scope.prev = function() {
423
		$location.url('/searchResults/' + ($scope.orgs.number - 1) + '/' + $scope.orgs.size + '/' + encodeURIComponent($scope.searchText));
424
	}
425
	
426
	$scope.next = function() {
427
		$location.url('/searchResults/' + ($scope.orgs.number + 1) + '/' + $scope.orgs.size + '/' + encodeURIComponent($scope.searchText));
428
	}
429
});
430

    
431
orgsModule.controller('countriesCtrl', function ($scope, $http, $routeParams) {
432
	$scope.field = 'Country';
433
	$scope.resultsBasePath = '/byCountry'
434
	$scope.entries = [];
435
	
436
	$http.get('api/organizations/browse/countries').then(function successCallback(res) {
437
		if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
438
		$scope.entries = res.data;
439
	}, function errorCallback(res) {
440
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
441
	});
442
	
443
});
444

    
445
orgsModule.controller('byCountryCtrl', function ($scope, $http, $routeParams, $location) {
446
	$scope.fieldValue = decodeURIComponent($routeParams.code);
447
	$scope.orgs = {};
448
	
449
	$http.get('api/organizations/byCountry/' + $routeParams.code + '/' + $routeParams.page + '/' + $routeParams.size).then(function successCallback(res) {
450
		if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
451
		$scope.orgs = res.data;
452
	}, function errorCallback(res) {
453
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
454
	});
455
	
456
	$scope.prev = function() {
457
		$location.url('/byCountry/' + ($scope.orgs.number - 1) + '/' + $scope.orgs.size + '/' + encodeURIComponent($scope.fieldValue));
458
	}
459
	
460
	$scope.next = function() {
461
		$location.url('/byCountry/' + ($scope.orgs.number + 1) + '/' + $scope.orgs.size + '/' + encodeURIComponent($scope.fieldValue));
462
	}
463

    
464
});
465

    
466
orgsModule.controller('typesCtrl', function ($scope, $http, $routeParams) {
467
	$scope.field = 'Organization type';
468
	$scope.resultsBasePath = '/byType'
469
	$scope.entries = [];
470
	
471
	$http.get('api/organizations/browse/types').then(function successCallback(res) {
472
		if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
473
		$scope.entries = res.data;
474
	}, function errorCallback(res) {
475
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
476
	});
477
	
478
});
479

    
480
orgsModule.controller('byTypeCtrl', function ($scope, $http, $routeParams, $location) {
481
	
482
	$scope.fieldValue = $routeParams.type;
483
	
484
	$scope.orgs = {};
485
	
486
	$http.get('api/organizations/byType/' + $routeParams.type + '/' + $routeParams.page + '/' + $routeParams.size).then(function successCallback(res) {
487
		if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
488
		$scope.orgs = res.data;
489
	}, function errorCallback(res) {
490
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
491
	});
492
	
493
	$scope.prev = function() {
494
		$location.url('/byType/' + ($scope.orgs.number - 1) + '/' + $scope.orgs.size + '/' + encodeURIComponent($scope.fieldValue));
495
	}
496
	
497
	$scope.next = function() {
498
		$location.url('/byType/' + ($scope.orgs.number + 1) + '/' + $scope.orgs.size + '/' + encodeURIComponent($scope.fieldValue));
499
	}
500
});
501

    
502

    
503
orgsModule.controller('showEditCtrl', function ($scope, $http, $routeParams, $route, $location, $timeout, vocabulariesService) {
504
	$scope.orgId = $routeParams.id;
505
	$scope.org = {};
506
	$scope.events = {};
507
	$scope.info = {};
508
	
509
	$scope.getInfo = function() {
510
    	$http.get('api/organizations/info?id=' + $scope.orgId).then(function successCallback(res) {
511
    		if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
512
    		$scope.info = res.data;
513
		}, function errorCallback(res) {
514
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
515
		});
516
    };
517
	
518
    $scope.getInfo();
519
	
520
	$scope.vocabularies = {};
521
	vocabulariesService.getVocs(function(vocs) { $scope.vocabularies = vocs; });
522
	
523
	if      ($routeParams.msg == 1) { $scope.message = 'New organization registered !!!'; }
524
	else if ($routeParams.msg == 2) { $scope.message = 'Organization updated !!!';        }
525
	else                            { $scope.message = '';                                }
526
	
527
	$timeout(function() { $scope.message = ''; }, 3000)
528
		
529
});
530

    
531
orgsModule.controller('showSuggestionsCtrl', function ($scope, $http, $routeParams, $location) {
532
	$scope.info = {};
533
	$scope.conflicts = [];
534
	$scope.duplicates = [];
535
	$scope.newGroups = [];
536
	$scope.currentTab = $routeParams.tab;
537
	$scope.country = $routeParams.country;
538
	
539
	$scope.getInfo = function() {
540
    	$http.get('api/organizations/suggestionsInfo').then(function successCallback(res) {
541
    		if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
542
    		$scope.info = res.data;
543
    		if ($scope.country == '_') {
544
    			var found = '';
545
    			
546
    			angular.forEach($scope.info.byCountry, function(values, c) {
547
    				if (!found && (($scope.currentTab == 1 && values.nGroups > 0) || ($scope.currentTab == 2 && values.nDuplicates > 0) || ($scope.currentTab == 3 && values.nConflicts > 0))) {
548
    					found = c;
549
    				}
550
    			});
551
    			if (found) { $location.url('/suggestions/' + found + '/' + $scope.currentTab); }
552
    		}
553
 		}, function errorCallback(res) {
554
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
555
		});
556
	};
557
	
558

    
559
	$scope.refresh = function() {
560
		$scope.conflicts = [];
561
		$scope.duplicates = [];
562
		$scope.newGroups = [];
563
		
564
		if ($scope.country != '_') {
565
			if ($scope.currentTab == 1) {
566
				$scope.groups = []; //TODO
567
			} else if ($scope.currentTab == 2) {
568
				$http.get('api/organizations/duplicates/byCountry/' + $scope.country).then(function successCallback(res) {
569
					if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
570
					$scope.duplicates = res.data;
571
					if ($scope.duplicates.length > 0) {
572
						//$scope.currentCountry = $scope.duplicates.
573
					}
574
				}, function errorCallback(res) {
575
					alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
576
				});
577
			} else if ($scope.currentTab == 3) {
578
				$http.get('api/organizations/conflicts/byCountry/' + $scope.country).then(function successCallback(res) {
579
					if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
580
					$scope.conflicts = res.data;
581
				}, function errorCallback(res) {
582
					alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
583
				});
584
			} else {  }
585
		}
586
		$scope.getInfo();
587
	}
588
	
589
	$scope.refresh();
590
});
591

    
592

    
593
orgsModule.controller('usersCtrl', function ($scope, $http, $timeout, $route, vocabulariesService) {
594
	$scope.users = [];
595
	$scope.vocs = {};
596
	$scope.currentUser = {};
597
	$scope.superAdminMode = superAdminMode();
598
	
599
	$scope.vocabularies = {};
600
	vocabulariesService.getVocs(function(vocs) { $scope.vocabularies = vocs; });
601
	
602
	$http.get('api/users').then(function successCallback(res) {
603
		if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
604
		$scope.users = res.data;
605
	}, function errorCallback(res) {
606
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
607
	});
608
		
609
	$scope.setCurrentUser = function(user) {
610
		angular.copy(user, $scope.currentUser);
611
		if (!$scope.currentUser.role || $scope.currentUser.role == 'PENDING') {		
612
			$scope.currentUser.role = 'USER';
613
		}
614
	}
615
	
616
	$scope.saveUser = function(user) {
617
		$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
618
		$http.post('api/users', user).then(function successCallback(res) {
619
			if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
620
			$scope.users = res.data;
621
		}, function errorCallback(res) {
622
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
623
		});
624
	}
625
	
626
	$scope.deleteUser = function(email) {
627
		if (confirm("Are you sure ?")) {
628
			$http.delete('api/users?email=' + email).then(function successCallback(res) {
629
				if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
630
				$scope.users = res.data;
631
			}, function errorCallback(res) {
632
				alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
633
			});
634
		}
635
	}
636
});
(10-10/12)