LP#1673857: interface for applying tags from copy buckets
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / cat / bucket / copy / app.js
index fc78a77..7eea044 100644 (file)
@@ -656,6 +656,84 @@ function($scope,  $q , $routeParams , $timeout , $window , $uibModal , bucketSvc
         }
     }
 
+    $scope.applyTags = function(copies) {
+        return $uibModal.open({
+            templateUrl: './cat/bucket/copy/t_apply_tags',
+            animation: true,
+            controller:
+                   ['$scope','$uibModalInstance',
+            function($scope , $uibModalInstance) {
+
+                $scope.tag_map = [];
+
+                egCore.pcrud.retrieveAll('cctt', {order_by : { cctt : 'label' }}, {atomic : true}).then(function(list) {
+                    $scope.tag_types = list;
+                    $scope.tag_type = $scope.tag_types[0].code(); // just pick a default
+                });
+
+                $scope.getTags = function(val) {
+                    return egCore.pcrud.search('acpt',
+                        {
+                            owner :  egCore.org.fullPath(egCore.auth.user().ws_ou(), true),
+                            label : { 'startwith' : {
+                                        transform: 'evergreen.lowercase',
+                                        value : [ 'evergreen.lowercase', val ]
+                                    }},
+                            tag_type : $scope.tag_type
+                        },
+                        { order_by : { 'acpt' : ['label'] } }, { atomic: true }
+                    ).then(function(list) {
+                        return list.map(function(item) {
+                            return item.label();
+                        });
+                    });
+                }
+
+                $scope.addTag = function() {
+                    var tagLabel = $scope.selectedLabel;
+                    // clear the typeahead
+                    $scope.selectedLabel = "";
+
+                    egCore.pcrud.search('acpt',
+                        {
+                            owner : egCore.org.fullPath(egCore.auth.user().ws_ou(), true),
+                            label : tagLabel,
+                            tag_type : $scope.tag_type
+                        },
+                        { order_by : { 'acpt' : ['label'] } }, { atomic: true }
+                    ).then(function(list) {
+                        if (list.length > 0) {
+                            var newMap = new egCore.idl.acptcm();
+                            newMap.isnew(1);
+                            newMap.tag(egCore.idl.Clone(list[0]));
+                            $scope.tag_map.push(newMap);
+                        }
+                    });
+                }
+
+                $scope.ok = function() {
+                    var promises = [];
+                    angular.forEach($scope.tag_map, function(map) {
+                        if (map.isdeleted()) return;
+                        angular.forEach(copies, function (cp) {
+                            var m = new egCore.idl.acptcm();
+                            m.isnew(1);
+                            m.copy(cp.id);
+                            m.tag(map.tag().id());
+                            promises.push(egCore.pcrud.create(m));
+                        });
+                    });
+                    return $q.all(promises).then(function(){$uibModalInstance.close()});
+                }
+
+                $scope.cancel = function($event) {
+                    $uibModalInstance.dismiss();
+                    $event.preventDefault();
+                }
+            }]
+        });
+    }
+
     // fetch the bucket;  on error show the not-allowed message
     if ($scope.bucketId) 
         drawBucket()['catch'](function() { $scope.forbidden = true });