diff --git a/chrome/content/zotero/xpcom/data_access.js b/chrome/content/zotero/xpcom/data_access.js index c558f2918..0bd7cd3c4 100644 --- a/chrome/content/zotero/xpcom/data_access.js +++ b/chrome/content/zotero/xpcom/data_access.js @@ -1901,6 +1901,21 @@ Zotero.Item.prototype.addTag = function(tag, type){ } +Zotero.Item.prototype.addTags = function (tags, type) { + Zotero.DB.beginTransaction(); + try { + for each(var tag in tags) { + this.addTag(tag, type); + } + Zotero.DB.commitTransaction(); + } + catch (e) { + Zotero.DB.rollbackTransaction(); + throw (e); + } +} + + Zotero.Item.prototype.addTagByID = function(tagID) { if (!this.getID()) { throw ('Cannot add tag to unsaved item in Item.addTagByID()'); diff --git a/chrome/content/zotero/xpcom/translate.js b/chrome/content/zotero/xpcom/translate.js index 5814b024b..938a80be8 100644 --- a/chrome/content/zotero/xpcom/translate.js +++ b/chrome/content/zotero/xpcom/translate.js @@ -1022,18 +1022,36 @@ Zotero.Translate.prototype._itemTagsAndSeeAlso = function(item, newItem) { } } if(item.tags) { + var tagsToAdd = {}; + tagsToAdd[0] = []; // user tags + tagsToAdd[1] = []; // automatic tags + for each(var tag in item.tags) { if(typeof(tag) == "string") { // accept strings in tag array as automatic tags, or, if // importing, as non-automatic tags - newItem.addTag(tag, (this.type == "import" ? 0 : 1)); + if (this.type == 'import') { + tagsToAdd[0].push(tag); + } + else { + tagsToAdd[1].push(tag); + } } else if(typeof(tag) == "object") { // also accept objects if(tag.tag) { - newItem.addTag(tag.tag, tag.type ? tag.type : 0); + if (!tagsToAdd[tag.type]) { + tagsToAdd[tag.type] = []; + } + tagsToAdd[tag.type].push(tag.tag); } } } + + for (var type in tagsToAdd) { + if (tagsToAdd[type].length) { + newItem.addTags(tagsToAdd[type], type); + } + } } } @@ -1364,23 +1382,42 @@ Zotero.Translate.prototype._itemDone = function(item, attachedTo) { // enabled in the preferences (as it is by default) if(item.tags && (this.type == "import" || Zotero.Prefs.get("automaticTags"))) { + var tagsToAdd = {}; + tagsToAdd[0] = []; // user tags + tagsToAdd[1] = []; // automatic tags + for each(var tag in item.tags) { if(typeof(tag) == "string") { // accept strings in tag array as automatic tags, or, if // importing, as non-automatic tags - newItem.addTag(tag, (this.type == "import" ? 0 : 1)); + if (this.type == 'import') { + tagsToAdd[0].push(tag); + } + else { + tagsToAdd[1].push(tag); + } } else if(typeof(tag) == "object") { // also accept objects if(tag.tag) { - if(this.type == "import") { - newItem.addTag(tag.tag, tag.type ? tag.type : 0); - } else { - // force web imports to automatic - newItem.addTag(tag.tag, 1); + if (this.type == "import") { + if (!tagsToAdd[tag.type]) { + tagsToAdd[tag.type] = []; + } + tagsToAdd[tag.type].push(tag.tag); + } + // force web imports to automatic + else { + tagsToAdd[1].push(tag.tag); } } } } + + for (var type in tagsToAdd) { + if (tagsToAdd[type].length) { + newItem.addTags(tagsToAdd[type], type); + } + } } if(!attachedTo) this.runHandler("itemDone", newItem);