]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/z3950/app.js
Merge branch 'master' of git.evergreen-ils.org:Evergreen
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / cat / z3950 / app.js
1 /*
2  * Z39.50 search and import
3  */
4
5 angular.module('egCatZ3950Search',
6     ['ngRoute', 'ui.bootstrap', 'ngOrderObjectBy', 'egCoreMod', 'egUiMod', 'egGridMod', 'egZ3950Mod', 'egMarcMod'])
7
8 .config(function($routeProvider, $locationProvider, $compileProvider) {
9     $locationProvider.html5Mode(true);
10     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/); // grid export
11
12     var resolver = {delay : function(egStartup) {return egStartup.go()}};
13
14     // search page shows the list view by default
15     $routeProvider.when('/cat/z3950/search', {
16         templateUrl: './cat/z3950/t_list',
17         controller: 'Z3950SearchCtrl',
18         resolve : resolver
19     });
20
21     // default page / bucket view
22     $routeProvider.otherwise({redirectTo : '/cat/z3950/search'});
23 })
24
25 /**
26  * List view - grid stuff
27  */
28 .controller('Z3950SearchCtrl',
29        ['$scope','$q','$location','$timeout','$window','egCore','egGridDataProvider','egZ3950TargetSvc','$uibModal',
30         'egConfirmDialog',
31 function($scope , $q , $location , $timeout , $window,  egCore , egGridDataProvider,  egZ3950TargetSvc,  $uibModal,
32          egConfirmDialog) {
33
34     // get list of targets
35     egZ3950TargetSvc.loadTargets();
36     egZ3950TargetSvc.loadActiveSearchFields();
37
38     $scope.field_strip_groups = [];
39     egCore.startup.go().then(function() {
40         // and list of field strip groups; need to ensure
41         // that enough of the startup has happened so that
42         // we have the user WS
43         egCore.pcrud.search('vibtg',
44             {
45                 always_apply : 'f',
46                 owner : {
47                     'in' : {
48                         select : {
49                             aou : [{
50                                 column : 'id',
51                                 transform : 'actor.org_unit_ancestors',
52                                 result_field : 'id'
53                             }]
54                         },
55                         from : 'aou',
56                         where : {
57                             id : egCore.auth.user().ws_ou()
58                         }
59                     }
60                 }
61             },
62             { order_by : { vibtq : ['label'] } }
63         ).then(null, null, function(strip_group) {
64             strip_group.selected = false;
65             $scope.field_strip_groups.push(strip_group);
66         });
67     });
68
69     $scope.total_hits = 0;
70
71     var provider = egGridDataProvider.instance({});
72
73     provider.get = function(offset, count) {
74         var deferred = $q.defer();
75
76         var query = egZ3950TargetSvc.currentQuery();
77         if (!query.raw_search && Object.keys(query.search).length == 0) {
78             return $q.when();
79         }
80
81         var method = query.raw_search ?
82                        'open-ils.search.z3950.search_service' :
83                        'open-ils.search.z3950.search_class';
84
85         if (query.raw_search) {
86             query.query = query.raw_search;
87             delete query['search'];
88             delete query['raw_search'];
89             query.service = query.service[0];
90             query.username = query.username[0];
91             query.password = query.password[0];
92         }
93
94         query['limit'] = count;
95         query['offset'] = offset;
96
97         var resultIndex = offset;
98         $scope.total_hits = 0;
99         $scope.searchInProgress = true;
100         egCore.net.request(
101             'open-ils.search',
102             method,
103             egCore.auth.token(),
104             query
105         ).then(
106             function() { $scope.searchInProgress = false; deferred.resolve() },
107             null, // onerror
108             function(result) {
109                 // FIXME when the search offset is > 0, the
110                 // total hits count can be wrong if one of the
111                 // Z39.50 targets has fewer than $offset hits; in that
112                 // case, result.count is not supplied.
113                 $scope.total_hits += (result.count || 0);
114                 for (var i in result.records) {
115                     result.records[i].mvr['service'] = result.service;
116                     result.records[i].mvr['index'] = resultIndex++;
117                     result.records[i].mvr['marcxml'] = result.records[i].marcxml;
118                     deferred.notify(result.records[i].mvr);
119                 }
120             }
121         );
122
123         return deferred.promise;
124     };
125
126     $scope.z3950SearchGridProvider = provider;
127     $scope.gridControls = {};
128
129     $scope.search = function() {
130         $scope.z3950SearchGridProvider.refresh();
131     };
132     $scope.clearForm = function() {
133         egZ3950TargetSvc.clearSearchFields();
134     };
135
136     $scope.saveDefaultZ3950Targets = function() {
137         egZ3950TargetSvc.saveDefaultZ3950Targets();
138     }
139
140     var display_form = true;
141     $scope.show_search_form = function() {
142         return display_form;
143     }
144     $scope.toggle_search_form = function() {
145         display_form = !display_form;
146     }
147
148     $scope.raw_search_impossible = function() {
149         return egZ3950TargetSvc.rawSearchImpossible();
150     }
151     $scope.showRawSearchForm = function() {
152         $uibModal.open({
153             templateUrl: './cat/z3950/t_raw_search',
154             size: 'md',
155             controller:
156                 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
157                 egZ3950TargetSvc.setRawSearch('');
158                 $scope.focusMe = true;
159                 $scope.ok = function(args) { $uibModalInstance.close(args) }
160                 $scope.cancel = function () { $uibModalInstance.dismiss() }
161             }]
162         }).result.then(function (args) {
163             if (!args || !args.raw_search) return;
164             $scope.clearForm();
165             egZ3950TargetSvc.setRawSearch(args.raw_search);
166             $scope.z3950SearchGridProvider.refresh();
167         });
168     }
169
170     $scope.showInCatalog = function() {
171         var items = $scope.gridControls.selectedItems();
172         // relying on cant_showInCatalog to protect us
173         var url = egCore.env.basePath +
174                   'cat/catalog/record/' + items[0].tcn();
175         $timeout(function() { $window.open(url, '_blank') });        
176     };
177     $scope.cant_showInCatalog = function() {
178         var items = $scope.gridControls.selectedItems();
179         if (items.length != 1) return true;
180         if (items[0]['service'] == 'native-evergreen-catalog') return false;
181         return true;
182     };
183
184     $scope.local_overlay_target = egCore.hatch.getLocalItem('eg.cat.marked_overlay_record') || 0;
185     $scope.mark_as_overlay_target = function() {
186         var items = $scope.gridControls.selectedItems();
187         if ($scope.local_overlay_target == items[0].tcn()) {
188             $scope.local_overlay_target = 0;
189         } else {
190             $scope.local_overlay_target = items[0].tcn();
191         }
192         egCore.hatch.setLocalItem('eg.cat.marked_overlay_record',$scope.local_overlay_target);
193     }
194     $scope.cant_overlay = function() {
195         if (!$scope.local_overlay_target) return true;
196         var items = $scope.gridControls.selectedItems();
197         if (items.length != 1) return true;
198         if (
199                 items[0]['service'] == 'native-evergreen-catalog' &&
200                 items[0].tcn() == $scope.local_overlay_target
201            ) return true;
202         return false;
203     }
204
205     $scope.selectFieldStripGroups = function() {
206         var groups = [];
207         angular.forEach($scope.field_strip_groups, function(grp, idx) {
208             if (grp.selected) {
209                 groups.push(grp.id());
210             }
211         });
212         return groups;
213     };
214     $scope.import = function() {
215         var deferred = $q.defer();
216         var items = $scope.gridControls.selectedItems();
217         egCore.net.request(
218             'open-ils.cat',
219             'open-ils.cat.biblio.record.xml.import',
220             egCore.auth.token(),
221             items[0]['marcxml'],
222             null, // FIXME bib source
223             null,
224             null,
225             $scope.selectFieldStripGroups()
226         ).then(
227             function() { deferred.resolve() },
228             null, // onerror
229             function(result) {
230                 egConfirmDialog.open(
231                     egCore.strings.IMPORTED_RECORD_FROM_Z3950,
232                     egCore.strings.IMPORTED_RECORD_FROM_Z3950_AS_ID,
233                     { id : result.id() },
234                     egCore.strings.GO_TO_RECORD,
235                     egCore.strings.GO_BACK
236                 ).result.then(function() {
237                     // NOTE: $location.path('/cat/catalog/record/' + result.id()) did not work
238                     // for some reason
239                     $window.location.href = egCore.env.basePath + 'cat/catalog/record/' + result.id();
240                 });
241             }
242         );
243
244         return deferred.promise;
245     };
246     $scope.need_one_selected = function() {
247         var items = $scope.gridControls.selectedItems();
248         if (items.length == 1) return false;
249         return true;
250     };
251
252     $scope.spawn_editor = function() {
253         var items = $scope.gridControls.selectedItems();
254         var recId = 0;
255         $uibModal.open({
256             templateUrl: './cat/z3950/t_marc_edit',
257             size: 'lg',
258             controller:
259                 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
260                 $scope.focusMe = true;
261                 $scope.record_id = recId;
262                 $scope.dirty_flag = false;
263                 $scope.marc_xml = items[0]['marcxml'];
264                 $scope.ok = function(args) { $uibModalInstance.close(args) }
265                 $scope.cancel = function () { $uibModalInstance.dismiss() }
266                 $scope.save_label = egCore.strings.IMPORT_BUTTON_LABEL;
267                 $scope.import_record_callback = function (record_id) {
268                     recId = record_id;
269                     $scope.save_label = egCore.strings.SAVE_BUTTON_LABEL;
270                 };
271             }]
272         }).result.then(function () {
273             if (recId) {
274                 $window.location.href = egCore.env.basePath + 'cat/catalog/record/' + recId;
275             }
276         });
277     }
278
279     $scope.view_marc = function() {
280         var items = $scope.gridControls.selectedItems();
281         $uibModal.open({
282             templateUrl: './cat/z3950/t_marc_html',
283             size: 'lg',
284             controller:
285                 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
286                 $scope.focusMe = true;
287                 $scope.marc_xml = items[0]['marcxml'];
288                 $scope.isbn = (items[0].isbn() || '').replace(/ .*/, '');
289                 $scope.ok = function(args) { $uibModalInstance.close(args) }
290                 $scope.cancel = function () { $uibModalInstance.dismiss() }
291             }]
292         }).result.then(function (args) {
293             if (!args || !args.name) return;
294         });
295     }
296
297     $scope.overlay_record = function() {
298         var items = $scope.gridControls.selectedItems();
299         var overlay_target = $scope.local_overlay_target;
300         var args = {
301             'marc_xml' : items[0]['marcxml']
302         };
303         $uibModal.open({
304             templateUrl: './cat/z3950/t_overlay',
305             size: 'lg',
306             controller:
307                 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
308                 $scope.focusMe = true;
309                 $scope.merge_profile = null;
310                 $scope.overlay_target = {
311                     id : overlay_target,
312                     merged : false
313                 };
314
315                 egCore.pcrud.retrieve('bre', $scope.overlay_target.id)
316                 .then(function(rec) {
317                     $scope.overlay_target.orig_marc_xml = rec.marc();
318                     $scope.overlay_target.marc_xml = rec.marc();
319                     $scope.merge_marc(); // in case a sticky value was already set
320                 });
321
322                 $scope.merge_marc = function() {
323                     if (!$scope.merge_profile) return;
324                     egCore.net.request(
325                         'open-ils.cat',
326                         'open-ils.cat.merge.marc.per_profile',
327                         egCore.auth.token(),
328                         $scope.merge_profile,
329                         [ args.marc_xml, $scope.overlay_target.orig_marc_xml ]
330                     ).then(function(merged) {
331                         if (merged) {
332                             $scope.overlay_target.marc_xml = merged;
333                             $scope.overlay_target.merged = true;
334                         }
335                     });
336                 }
337                 $scope.$watch('merge_profile', function(newVal, oldVal) {
338                     if (newVal && newVal !== oldVal) {
339                         $scope.merge_marc();
340                     }
341                 });
342
343                 $scope.args = args;
344                 args.overlay_target = $scope.overlay_target;
345                 $scope.ok = function(args) { $uibModalInstance.close(args) };
346                 $scope.cancel = function () { $uibModalInstance.dismiss() };
347                 $scope.editOverlayRecord = function() {
348                     $uibModal.open({
349                         templateUrl: './cat/z3950/t_edit_overlay_record',
350                         size: 'lg',
351                         controller:
352                             ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
353                             $scope.focusMe = true;
354                             $scope.record_id = 0;
355                             $scope.dirty_flag = false;
356                             $scope.args = args;
357                             $scope.ok = function(args) { $uibModalInstance.close(args) }
358                             $scope.cancel = function () { $uibModalInstance.dismiss() }
359                         }]
360                     }).result.then(function (args) {
361                         $scope.merge_marc();
362                         if (!args || !args.name) return;
363                     });
364                 };
365             }]
366         }).result.then(function (args) {
367             egCore.net.request(
368                 'open-ils.cat',
369                 'open-ils.cat.biblio.record.marc.replace',
370                 egCore.auth.token(),
371                 overlay_target,
372                 (args.overlay_target.merged ? args.overlay_target.marc_xml : args.marc_xml),
373                 null, // FIXME bib source
374                 null,
375                 $scope.selectFieldStripGroups()
376             ).then(
377                 function(result) {
378                     console.debug('overlay complete');
379                 }
380             );            
381         });
382     }
383 }])