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
		
47
orgsModule.directive('orgTabsMenu', function($http) {
48
	return {
49
		restrict: 'E',
50
		scope: {
51
			'orgId'    : '@',
52
			'info'     : '=',
53
			'selected' : '=',
54
			'org'      : '=',
55
			'events'   : '=',
56
		},
57
		templateUrl: 'resources/html/menu/org_tabs_menu.html',
58
		link: function(scope, element, attrs, ctrl) {
59

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

    
74
				$http.get('api/organizations/conflicts?id=' + scope.orgId).then(function successCallback(res) {
75
					if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
76
					scope.events.conflicts = res.data;
77
					$http.get('api/organizations/duplicates?id=' + scope.orgId).then(function successCallback(res) {
78
						if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
79
						scope.events.duplicates = res.data;
80
					}, function errorCallback(res) {
81
						alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
82
					});
83
				}, function errorCallback(res) {
84
					alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
85
				});
86
				scope.selected = 2;
87
			};
88
								
89
			scope.loadOrg();
90
		}
91
     }
92
});
93

    
94

    
95
orgsModule.directive('orgFormMetadata', function($http, $location, $route, $routeParams) {
96
	return {
97
		restrict: 'E',
98
		scope: {
99
			'orgId'        : '@',
100
			'org'          : '=',
101
			'vocabularies' : '=',
102
			'mode'         : '@',  // insert or update
103
			'infoMethod'   : '&'
104
		},
105
		templateUrl: 'resources/html/forms/org_metadata.html',
106
		link: function(scope, element, attrs, ctrl) {
107
			
108
			scope.newRelation = {};
109
			scope.newRelType = '';
110
							
111
			scope.resetSelectedRelation = function() {
112
				scope.newRelation = {};
113
			}
114
		
115
			scope.addNewRelation = function(r) {
116
				scope.org.relations.push({
117
					'relatedOrgId'   : scope.newRelation.id,
118
					'relatedOrgName' : scope.newRelation.name,
119
					'type'           : scope.newRelType
120
				});
121
				scope.newRelation = {};
122
				scope.newRelType = '';
123
			}
124
			
125
			scope.save = function() {
126
				$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
127
				$http.post('api/organizations/save', scope.org).then(function successCallback(res) {
128
					if      ((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
129
					else if (scope.mode == 'insert')        { $location.url('/edit/1/' + res.data[0]);    }
130
					else if ($routeParams.msg == 2)         { $route.reload(); }
131
					else                                    { $location.url('/edit/2/' + res.data[0]); }
132
				}, function errorCallback(res) {
133
					alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
134
				}); 
135
			}
136
		}
137
     }
138
});
139

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

    
180

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

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

    
212

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

    
227
orgsModule.directive('orgFormDuplicates', function($http, $location, $route) {
228
	return {
229
		restrict: 'E',
230
		scope: {
231
			'duplicates'     : '=',
232
			'showSaveButton' : '@',
233
			'saveFunction'   : '&'
234
		},
235
		templateUrl: 'resources/html/forms/org_duplicates.html',
236
		link: function(scope, element, attrs, ctrl) {}
237
     }
238
});
239

    
240
orgsModule.directive('orgFormConflicts', function($http, $location, $route) {
241
	return {
242
		restrict: 'E',
243
		scope: {
244
			'conflicts'      : '=',
245
			'showSaveButton' : '@',
246
			'saveFunction'   : '&'
247
		},
248
		templateUrl: 'resources/html/forms/org_conflicts.html',
249
		link: function(scope, element, attrs, ctrl) {}
250
     }
251
});
252

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

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

    
323
orgsModule.filter('escape', function() {
324
	return function(input) {
325
		return encodeURIComponent(encodeURIComponent(input));
326
	}; 
327
});
328

    
329
orgsModule.filter('unescape', function() {
330
	return function(input) {
331
		return decodeURIComponent(input);
332
	};
333
});
334

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

    
358
orgsModule.controller('searchCtrl', function ($scope, $location) {
359
	$scope.searchText = '';
360
	$scope.search = function() {
361
		$location.url('/searchResults/0/50/' + encodeURIComponent(encodeURIComponent($scope.searchText)));
362
	}
363
});
364

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

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

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

    
418
});
419

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

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

    
456

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

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

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

    
546

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