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('organizationInfo', function() {
21
	return {
22
		restrict: 'E',
23
		scope: {
24
			'info'    : '=',
25
			'message' : '='
26
		},
27
		templateUrl: 'resources/html/part/org_info.html'
28
     }
29
});
30

    
31
orgsModule.directive('orgTabsMenu', function($http) {
32
	return {
33
		restrict: 'E',
34
		scope: {
35
			'orgId'    : '@',
36
			'info'     : '=',
37
			'selected' : '=',
38
			'org'      : '=',
39
			'events'   : '=',
40
		},
41
		templateUrl: 'resources/html/part/org_tabs_menu.html',
42
		link: function(scope, element, attrs, ctrl) {
43

    
44
			scope.loadOrg = function() {
45
				scope.org = {};
46
				$http.get('api/organizations/get?id=' + scope.orgId).then(function successCallback(res) {
47
					if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
48
					scope.org = res.data;
49
				}, function errorCallback(res) {
50
					alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
51
				});
52
				scope.selected = 1;
53
			}
54
			
55
			scope.loadDedupEvents = function() {
56
				scope.events = {};
57

    
58
				$http.get('api/organizations/conflicts?id=' + scope.orgId).then(function successCallback(res) {
59
					if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
60
					scope.events.conflicts = res.data;
61
					$http.get('api/organizations/duplicates?id=' + scope.orgId).then(function successCallback(res) {
62
						if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
63
						scope.events.duplicates = res.data;
64
					}, function errorCallback(res) {
65
						alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
66
					});
67
				}, function errorCallback(res) {
68
					alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
69
				});
70
				scope.selected = 2;
71
			};
72
								
73
			scope.loadOrg();
74
		}
75
     }
76
});
77

    
78

    
79
orgsModule.directive('orgFormMetadata', function($http, $location, $route, $routeParams) {
80
	return {
81
		restrict: 'E',
82
		scope: {
83
			'orgId'        : '@',
84
			'org'          : '=',
85
			'vocabularies' : '=',
86
			'mode'         : '@',  // insert or update
87
			'infoMethod'   : '&'
88
		},
89
		templateUrl: 'resources/html/part/org_form_metadata.html',
90
		link: function(scope, element, attrs, ctrl) {
91
			
92
			scope.searchOrgs = {};
93
			scope.searchValue = '';
94
			
95
			scope.newRelation = {};
96
			scope.newRelType = '';
97
							
98
			scope.resetSelectedRelation = function() {
99
				scope.newRelation = {};
100
			}
101
			
102
			scope.selectNewRelation = function(r) {
103
				scope.newRelation = r;
104
			}
105
		
106
			scope.addNewRelation = function(r) {
107
				scope.org.relations.push({
108
					'relatedOrgId'   : scope.newRelation.id,
109
					'relatedOrgName' : scope.newRelation.name,
110
					'type'           : scope.newRelType
111
				});
112
				scope.newRelation = {};
113
				scope.newRelType = '';
114
			}
115

    
116
			scope.search = function(text, page, size) {
117
				scope.searchOrgs = {};
118
				$http.get('api/organizations/search/' + page + '/' + size + '?q=' + text).then(function successCallback(res) {
119
					if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
120
					scope.searchValue = text;
121
					scope.searchOrgs = res.data;
122
				}, function errorCallback(res) {
123
					alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
124
				});
125
			}
126
			
127
			scope.save = function() {
128
				$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
129
				$http.post('api/organizations/save', scope.org).then(function successCallback(res) {
130
					if      ((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
131
					else if (scope.mode == 'insert')        { $location.url('/edit/1/' + res.data[0]);    }
132
					else if ($routeParams.msg == 2)         { $route.reload(); }
133
					else                                    { $location.url('/edit/2/' + res.data[0]); }
134
				}, function errorCallback(res) {
135
					alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
136
				}); 
137
			}
138
		}
139
     }
140
});
141

    
142
orgsModule.directive('orgFormDedupEvents', function($http, $location, $route) {
143
	return {
144
		restrict: 'E',
145
		scope: {
146
			'orgId'        : '@',
147
			'events'       : '=',
148
			'vocabularies' : '=',
149
			'infoMethod'   : '&'
150
		},
151
		templateUrl: 'resources/html/part/org_form_dedup_events.html',
152
		link: function(scope, element, attrs, ctrl) {
153
			
154
			scope.currentOrgDetails = {};
155
			
156
			$http.get('api/organizations/get?id=' + scope.orgId).then(function successCallback(res) {
157
				if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
158
				scope.currentOrgDetails = res.data;
159
			}, function errorCallback(res) {
160
				alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
161
			});
162
			
163
			scope.saveDuplicates = function() {
164
				$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
165
				$http.post('api/organizations/duplicates', scope.events.duplicates).then(function successCallback(res) {
166
					if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
167
					if (scope.infoMethod)             { scope.infoMethod(); }
168
					alert('Events updated !!!');
169
					scope.events.duplicates = res.data;
170
				}, function errorCallback(res) {
171
					alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
172
				});
173
			}
174
			
175
			scope.saveConflicts = function() {
176
				alert('todo');
177
			}
178
		}
179
     }
180
});
181

    
182

    
183
orgsModule.directive('orgDetails', function($http, $location, $route) {
184
	return {
185
		restrict: 'E',
186
		scope: {
187
			'org' : '='
188
		},
189
		templateUrl: 'resources/html/part/org_details.html',
190
		link: function(scope, element, attrs, ctrl) {}
191
     }
192
});
193

    
194
orgsModule.directive('orgResults', function($http, $location, $route) {
195
	return {
196
		restrict: 'E',
197
		scope: {
198
			'searchMessage' : '@',
199
			'orgs'          : '=',
200
			'nextFunction'  : '&',
201
			'prevFunction'  : '&'
202
		},
203
		templateUrl: 'resources/html/part/org_results.html',
204
		link: function(scope, element, attrs, ctrl) {}
205
     }
206
});
207

    
208

    
209
orgsModule.directive('allConflicts', function($http, $location, $route) {
210
	return {
211
		restrict: 'E',
212
		scope: {
213
			'conflicts'    : '=',
214
			'country'      : '@',
215
			'info'         : '=',
216
			'infoMethod'   : '&'
217
		},
218
		templateUrl: 'resources/html/part/all_conflicts.html',
219
		link: function(scope, element, attrs, ctrl) {}
220
     }
221
});
222

    
223
orgsModule.directive('orgDuplicates', function($http, $location, $route) {
224
	return {
225
		restrict: 'E',
226
		scope: {
227
			'duplicates'     : '=',
228
			'showSaveButton' : '@',
229
			'saveFunction'   : '&'
230
		},
231
		templateUrl: 'resources/html/part/org_duplicates.html',
232
		link: function(scope, element, attrs, ctrl) {}
233
     }
234
});
235

    
236
orgsModule.directive('orgConflicts', function($http, $location, $route) {
237
	return {
238
		restrict: 'E',
239
		scope: {
240
			'conflicts'      : '=',
241
			'showSaveButton' : '@',
242
			'saveFunction'   : '&'
243
		},
244
		templateUrl: 'resources/html/part/org_conflicts.html',
245
		link: function(scope, element, attrs, ctrl) {}
246
     }
247
});
248

    
249
orgsModule.directive('allDuplicates', function($http, $location, $route) {
250
	return {
251
		restrict: 'E',
252
		scope: {
253
			'duplicates'   : '=',
254
			'country'      : '@',
255
			'info'         : '=',
256
			'infoMethod'   : '&'
257
		},
258
		templateUrl: 'resources/html/part/all_duplicates.html',
259
		link: function(scope, element, attrs, ctrl) {
260
			scope.currentOrg = {};
261
			scope.currentOrgDetails = {};
262
			scope.currentDuplicates = [];
263
			
264
			scope.prepareDuplicatesModal = function(org) {
265
				scope.currentOrg = org;
266
				scope.currentOrgDetails = {};
267
				scope.currentDuplicates = [];
268
				
269
				$http.get('api/organizations/get?id=' + org.id).then(function successCallback(res) {
270
					if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
271
					scope.currentOrgDetails = res.data;
272
				}, function errorCallback(res) {
273
					alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
274
				});
275
				
276
				$http.get('api/organizations/duplicates?id=' + org.id).then(function successCallback(res) {
277
					if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
278
					scope.currentDuplicates = res.data;
279
				}, function errorCallback(res) {
280
					alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
281
				});
282
			};
283
			
284
			scope.saveCurrentDuplicates = function() {
285
				$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
286
				$http.post('api/organizations/duplicates', scope.currentDuplicates).then(function successCallback(res) {
287
					if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
288
					if (scope.infoMethod)             { scope.infoMethod(); }
289
					scope.currentOrg.numberOfDuplicates = 0;
290
					for (var i=0; i<res.data.length; i++) {
291
						if (res.data[i].relType == 'suggested') {
292
							scope.currentOrg.numberOfDuplicates++;
293
						}
294
					}
295
					scope.currentDuplicates = [];
296
				}, function errorCallback(res) {
297
					alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
298
				});
299
			};
300
		}
301
    }
302
});
303

    
304
orgsModule.config(function($routeProvider) {
305
	$routeProvider
306
		.when('/new',                              { templateUrl: 'resources/html/new.html',              controller: 'newOrgCtrl' })
307
		.when('/search',                           { templateUrl: 'resources/html/search.html',           controller: 'searchCtrl' })
308
		.when('/searchResults/:page/:size/:text*', { templateUrl: 'resources/html/searchResults.html',    controller: 'searchResultsCtrl' })
309
		.when('/countries',                        { templateUrl: 'resources/html/browse.html',           controller: 'countriesCtrl' })
310
		.when('/byCountry/:page/:size/:code*',     { templateUrl: 'resources/html/resultsByCountry.html', controller: 'byCountryCtrl' })
311
		.when('/types',                            { templateUrl: 'resources/html/browse.html',           controller: 'typesCtrl' })
312
		.when('/byType/:page/:size/:type*',        { templateUrl: 'resources/html/resultsByType.html',    controller: 'byTypeCtrl' })
313
		.when('/edit/:msg/:id',                    { templateUrl: 'resources/html/edit.html',             controller: 'showEditCtrl' })
314
		.when('/suggestions/:country/:tab',        { templateUrl: 'resources/html/suggestions.html',      controller: 'showSuggestionsCtrl' })
315
		.when('/users',                            { templateUrl: 'resources/html/users.html',            controller: 'usersCtrl' })
316
		.otherwise({ redirectTo: '/suggestions/_/1' });
317
});
318

    
319
orgsModule.filter('escape', function() {
320
	return function(input) {
321
		return encodeURIComponent(encodeURIComponent(input));
322
	}; 
323
});
324

    
325
orgsModule.filter('unescape', function() {
326
	return function(input) {
327
		return decodeURIComponent(input);
328
	};
329
});
330

    
331
orgsModule.controller('newOrgCtrl', function ($scope, $http, $routeParams, $location, vocabulariesService) {
332
	$scope.org = {
333
		"id": "",
334
		"name": "",
335
		"type": null,
336
		"lat": 0.0,
337
		"lng": 0.0,
338
		"city": "",
339
		"country": "",
340
		"source": "user",
341
		"otherIdentifiers": [],
342
		"otherNames": [],
343
		"relations": [],
344
		"acronyms": [],
345
		"urls": [],
346
		"relations": []
347
	};
348
	
349
	$scope.vocabularies = {};
350
	vocabulariesService.getVocs(function(vocs) { $scope.vocabularies = vocs; });
351
	
352
});
353

    
354
orgsModule.controller('searchCtrl', function ($scope, $location) {
355
	$scope.searchText = '';
356
	$scope.search = function() {
357
		$location.url('/searchResults/0/50/' + encodeURIComponent(encodeURIComponent($scope.searchText)));
358
	}
359
});
360

    
361
orgsModule.controller('searchResultsCtrl', function ($scope, $http, $routeParams, $location) {
362
	$scope.searchText = decodeURIComponent($routeParams.text);
363
	$scope.orgs = {};
364
	
365
	$http.get('api/organizations/search/' + $routeParams.page + '/' + $routeParams.size + '?q=' + $scope.searchText).then(function successCallback(res) {
366
		if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
367
		$scope.orgs = res.data;
368
	}, function errorCallback(res) {
369
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
370
	});
371
	
372
	$scope.prev = function() {
373
		$location.url('/searchResults/' + ($scope.orgs.number - 1) + '/' + $scope.orgs.size + '/' + encodeURIComponent($scope.searchText));
374
	}
375
	
376
	$scope.next = function() {
377
		$location.url('/searchResults/' + ($scope.orgs.number + 1) + '/' + $scope.orgs.size + '/' + encodeURIComponent($scope.searchText));
378
	}
379
});
380

    
381
orgsModule.controller('countriesCtrl', function ($scope, $http, $routeParams) {
382
	$scope.field = 'Country';
383
	$scope.resultsBasePath = '/byCountry'
384
	$scope.entries = [];
385
	
386
	$http.get('api/organizations/browse/countries').then(function successCallback(res) {
387
		if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
388
		$scope.entries = res.data;
389
	}, function errorCallback(res) {
390
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
391
	});
392
	
393
});
394

    
395
orgsModule.controller('byCountryCtrl', function ($scope, $http, $routeParams, $location) {
396
	$scope.fieldValue = decodeURIComponent($routeParams.code);
397
	$scope.orgs = {};
398
	
399
	$http.get('api/organizations/byCountry/' + $routeParams.code + '/' + $routeParams.page + '/' + $routeParams.size).then(function successCallback(res) {
400
		if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
401
		$scope.orgs = res.data;
402
	}, function errorCallback(res) {
403
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
404
	});
405
	
406
	$scope.prev = function() {
407
		$location.url('/byCountry/' + ($scope.orgs.number - 1) + '/' + $scope.orgs.size + '/' + encodeURIComponent($scope.fieldValue));
408
	}
409
	
410
	$scope.next = function() {
411
		$location.url('/byCountry/' + ($scope.orgs.number + 1) + '/' + $scope.orgs.size + '/' + encodeURIComponent($scope.fieldValue));
412
	}
413

    
414
});
415

    
416
orgsModule.controller('typesCtrl', function ($scope, $http, $routeParams) {
417
	$scope.field = 'Organization type';
418
	$scope.resultsBasePath = '/byType'
419
	$scope.entries = [];
420
	
421
	$http.get('api/organizations/browse/types').then(function successCallback(res) {
422
		if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
423
		$scope.entries = res.data;
424
	}, function errorCallback(res) {
425
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
426
	});
427
	
428
});
429

    
430
orgsModule.controller('byTypeCtrl', function ($scope, $http, $routeParams, $location) {
431
	
432
	$scope.fieldValue = $routeParams.type;
433
	
434
	$scope.orgs = {};
435
	
436
	$http.get('api/organizations/byType/' + $routeParams.type + '/' + $routeParams.page + '/' + $routeParams.size).then(function successCallback(res) {
437
		if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
438
		$scope.orgs = res.data;
439
	}, function errorCallback(res) {
440
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
441
	});
442
	
443
	$scope.prev = function() {
444
		$location.url('/byType/' + ($scope.orgs.number - 1) + '/' + $scope.orgs.size + '/' + encodeURIComponent($scope.fieldValue));
445
	}
446
	
447
	$scope.next = function() {
448
		$location.url('/byType/' + ($scope.orgs.number + 1) + '/' + $scope.orgs.size + '/' + encodeURIComponent($scope.fieldValue));
449
	}
450
});
451

    
452

    
453
orgsModule.controller('showEditCtrl', function ($scope, $http, $routeParams, $route, $location, $timeout, vocabulariesService) {
454
	$scope.orgId = $routeParams.id;
455
	$scope.org = {};
456
	$scope.events = {};
457
	$scope.info = {};
458
	
459
	$scope.getInfo = function() {
460
    	$http.get('api/organizations/info?id=' + $scope.orgId).then(function successCallback(res) {
461
    		if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
462
    		$scope.info = res.data;
463
		}, function errorCallback(res) {
464
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
465
		});
466
    };
467
	
468
    $scope.getInfo();
469
	
470
	$scope.vocabularies = {};
471
	vocabulariesService.getVocs(function(vocs) { $scope.vocabularies = vocs; });
472
	
473
	if      ($routeParams.msg == 1) { $scope.message = 'New organization registered !!!'; }
474
	else if ($routeParams.msg == 2) { $scope.message = 'Organization updated !!!';        }
475
	else                            { $scope.message = '';                                }
476
	
477
	$timeout(function() { $scope.message = ''; }, 3000)
478
		
479
});
480

    
481
orgsModule.controller('showSuggestionsCtrl', function ($scope, $http, $routeParams, $location) {
482
	$scope.info = {};
483
	$scope.conflicts = [];
484
	$scope.duplicates = [];
485
	$scope.newGroups = [];
486
	$scope.currentTab = $routeParams.tab;
487
	$scope.country = $routeParams.country;
488
	
489
	$scope.getInfo = function() {
490
    	$http.get('api/organizations/suggestionsInfo').then(function successCallback(res) {
491
    		if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
492
    		$scope.info = res.data;
493
    		if ($scope.country == '_') {
494
    			var found = '';
495
    			
496
    			angular.forEach($scope.info.byCountry, function(values, c) {
497
    				if (!found && (($scope.currentTab == 1 && values.nGroups > 0) || ($scope.currentTab == 2 && values.nDuplicates > 0) || ($scope.currentTab == 3 && values.nConflicts > 0))) {
498
    					found = c;
499
    				}
500
    			});
501
    			if (found) { $location.url('/suggestions/' + found + '/' + $scope.currentTab); }
502
    		}
503
 		}, function errorCallback(res) {
504
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
505
		});
506
	};
507
	
508

    
509
	$scope.refresh = function() {
510
		$scope.conflicts = [];
511
		$scope.duplicates = [];
512
		$scope.newGroups = [];
513
		
514
		if ($scope.country != '_') {
515
			if ($scope.currentTab == 1) {
516
				$scope.groups = []; //TODO
517
			} else if ($scope.currentTab == 2) {
518
				$http.get('api/organizations/duplicates/byCountry/' + $scope.country).then(function successCallback(res) {
519
					if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
520
					$scope.duplicates = res.data;
521
					if ($scope.duplicates.length > 0) {
522
						//$scope.currentCountry = $scope.duplicates.
523
					}
524
				}, function errorCallback(res) {
525
					alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
526
				});
527
			} else if ($scope.currentTab == 3) {
528
				$http.get('api/organizations/conflicts/byCountry/' + $scope.country).then(function successCallback(res) {
529
					if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
530
					$scope.conflicts = res.data;
531
				}, function errorCallback(res) {
532
					alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
533
				});
534
			} else {  }
535
		}
536
		$scope.getInfo();
537
	}
538
	
539
	$scope.refresh();
540
});
541

    
542

    
543
orgsModule.controller('usersCtrl', function ($scope, $http, $timeout, $route, vocabulariesService) {
544
	$scope.users = [];
545
	$scope.vocs = {};
546
	$scope.currentUser = {};
547
	$scope.superAdminMode = superAdminMode();
548
	
549
	$scope.vocabularies = {};
550
	vocabulariesService.getVocs(function(vocs) { $scope.vocabularies = vocs; });
551
	
552
	$http.get('api/users').then(function successCallback(res) {
553
		if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
554
		$scope.users = res.data;
555
	}, function errorCallback(res) {
556
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
557
	});
558
		
559
	$scope.setCurrentUser = function(user) {
560
		angular.copy(user, $scope.currentUser);
561
		if (!$scope.currentUser.role || $scope.currentUser.role == 'PENDING') {		
562
			$scope.currentUser.role = 'USER';
563
		}
564
	}
565
	
566
	$scope.saveUser = function(user) {
567
		$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
568
		$http.post('api/users', user).then(function successCallback(res) {
569
			if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
570
			$scope.users = res.data;
571
		}, function errorCallback(res) {
572
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
573
		});
574
	}
575
	
576
	$scope.deleteUser = function(email) {
577
		if (confirm("Are you sure ?")) {
578
			$http.delete('api/users?email=' + email).then(function successCallback(res) {
579
				if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
580
				$scope.users = res.data;
581
			}, function errorCallback(res) {
582
				alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
583
			});
584
		}
585
	}
586
});
(10-10/12)