From 64d7c64ee26d829ff3cc0f733e40278a89eb1d55 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Thu, 5 Oct 2006 23:36:05 +0000 Subject: [PATCH] When changing item type, reset any creator types that don't exist in the target item type to 'contributor', which exists in all --- chrome/content/zotero/xpcom/data_access.js | 25 +++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/chrome/content/zotero/xpcom/data_access.js b/chrome/content/zotero/xpcom/data_access.js index c39d61185..825cf31f9 100644 --- a/chrome/content/zotero/xpcom/data_access.js +++ b/chrome/content/zotero/xpcom/data_access.js @@ -161,8 +161,9 @@ Zotero.Item.prototype.setType = function(itemTypeID){ return true; } - // If existing type, clear fields from old type that aren't in new one + // If existing type if (this.getType()){ + // Clear fields from old type that aren't in new one var sql = 'SELECT fieldID FROM itemTypeFields ' + 'WHERE itemTypeID=' + this.getType() + ' AND fieldID NOT IN ' + '(SELECT fieldID FROM itemTypeFields WHERE itemTypeID=' @@ -174,6 +175,19 @@ Zotero.Item.prototype.setType = function(itemTypeID){ this.setField(obsoleteFields[i],false); } } + + // And reset custom creator types to the default + var creators = this.getCreators(); + if (creators){ + for each(var creator in creators){ + if (!Zotero.CreatorTypes.isValidForItemType(creator['creatorTypeID'], itemTypeID)) + { + // Reset to contributor (creatorTypeID 2), which exists in all + this.setCreator(orderIndex, creator['firstName'], + creator['lastName'], 2, creator['fieldMode']); + } + } + } } this._data['itemTypeID'] = itemTypeID; @@ -2910,6 +2924,7 @@ Zotero.CreatorTypes = new function(){ this.constructor.prototype = new Zotero.CachedTypes(); this.getTypesForItemType = getTypesForItemType; + this.isValidForItemType = isValidForItemType; this.getPrimaryIDForType = getPrimaryIDForType; this._typeDesc = 'creator type'; @@ -2926,6 +2941,14 @@ Zotero.CreatorTypes = new function(){ return Zotero.DB.query(sql, itemTypeID); } + + function isValidForItemType(creatorTypeID, itemTypeID){ + var sql = "SELECT COUNT(*) FROM itemTypeCreatorTypes " + + "WHERE itemTypeID=? AND creatorTypeID=?"; + return !!Zotero.DB.valueQuery(sql, [itemTypeID, creatorTypeID]); + } + + function getPrimaryIDForType(itemTypeID){ var sql = "SELECT creatorTypeID FROM itemTypeCreatorTypes " + "WHERE itemTypeID=? AND primaryField=1";