Don't include deleted items in collection-based operations (export, create bib)

This commit is contained in:
Dan Stillman 2009-12-30 21:08:45 +00:00
parent 9996fd09a3
commit bdc34103a9

View file

@ -306,11 +306,12 @@ Zotero.Collection.prototype.getChildCollections = function (asIDs) {
/** /**
* Returns child items of this collection * Returns child items of this collection
* *
* @param bool asIDs Return as itemIDs * @param {Boolean} asIDs Return as itemIDs
* @return array Array of Zotero.Item instances or itemIDs, * @param {Boolean} includeDeleted Include items in Trash
* or FALSE if none * @return {Zotero.Item[]|Integer[]|FALSE} Array of Zotero.Item instances or itemIDs,
* or FALSE if none
*/ */
Zotero.Collection.prototype.getChildItems = function (asIDs) { Zotero.Collection.prototype.getChildItems = function (asIDs, includeDeleted) {
if (!this._childItemsLoaded) { if (!this._childItemsLoaded) {
this._loadChildItems(); this._loadChildItems();
} }
@ -319,10 +320,18 @@ Zotero.Collection.prototype.getChildItems = function (asIDs) {
return false; return false;
} }
// Remove deleted items if necessary
var childItems = [];
for each(var item in this._childItems) {
if (includeDeleted || !item.deleted) {
childItems.push(item);
}
}
// Return itemIDs // Return itemIDs
if (asIDs) { if (asIDs) {
var ids = []; var ids = [];
for each(var item in this._childItems) { for each(var item in childItems) {
ids.push(item.id); ids.push(item.id);
} }
return ids; return ids;
@ -330,7 +339,7 @@ Zotero.Collection.prototype.getChildItems = function (asIDs) {
// Return Zotero.Item objects // Return Zotero.Item objects
var objs = []; var objs = [];
for each(var item in this._childItems) { for each(var item in childItems) {
objs.push(item); objs.push(item);
} }
return objs; return objs;
@ -546,6 +555,8 @@ Zotero.Collection.prototype.save = function () {
orderStatement.reset(); orderStatement.reset();
Zotero.debug("Adding item " + itemID + " to collection " + collectionID, 4);
insertStatement.bindInt32Parameter(0, collectionID); insertStatement.bindInt32Parameter(0, collectionID);
insertStatement.bindInt32Parameter(1, itemID); insertStatement.bindInt32Parameter(1, itemID);
insertStatement.bindInt32Parameter(2, insertStatement.bindInt32Parameter(2,
@ -702,7 +713,9 @@ Zotero.Collection.prototype.addItems = function(itemIDs) {
insertStatement.execute(); insertStatement.execute();
} }
catch(e) { catch(e) {
throw (e + ' [ERROR: ' + Zotero.DB.getLastErrorString() + ']'); var errMsg = Zotero.DB.getLastErrorString()
+ " (" + this.id + "," + itemID + "," + nextOrderIndex + ")";
throw (e + ' [ERROR: ' + errMsg + ']');
} }
notifierPairs.push(this.id + '-' + itemID); notifierPairs.push(this.id + '-' + itemID);
@ -726,7 +739,7 @@ Zotero.Collection.prototype.addItems = function(itemIDs) {
* Remove an item from the collection (does not delete item from library) * Remove an item from the collection (does not delete item from library)
**/ **/
Zotero.Collection.prototype.removeItem = function(itemID) { Zotero.Collection.prototype.removeItem = function(itemID) {
var childItems = this.getChildItems(true); var childItems = this.getChildItems(true, true);
if (childItems) { if (childItems) {
var index = childItems.indexOf(itemID); var index = childItems.indexOf(itemID);
if (index == -1) { if (index == -1) {
@ -873,12 +886,13 @@ Zotero.Collection.prototype.diff = function (collection, includeMatches, ignoreO
Zotero.Collection.prototype.erase = function(deleteItems) { Zotero.Collection.prototype.erase = function(deleteItems) {
Zotero.DB.beginTransaction(); Zotero.DB.beginTransaction();
var descendents = this.getDescendents(); var descendents = this.getDescendents(false, null, true);
var collections = [this.id]; var collections = [this.id];
var items = []; var items = [];
var notifierData = {}; var notifierData = {};
notifierData[this.id] = { old: this.serialize() }; notifierData[this.id] = { old: this.serialize() };
var del = [];
for(var i=0, len=descendents.length; i<len; i++) { for(var i=0, len=descendents.length; i<len; i++) {
// Descendent collections // Descendent collections
if (descendents[i].type == 'collection') { if (descendents[i].type == 'collection') {
@ -890,12 +904,15 @@ Zotero.Collection.prototype.erase = function(deleteItems) {
} }
// Descendent items // Descendent items
else { else {
// Delete items from DB
if (deleteItems) { if (deleteItems) {
// Delete items from DB del.push(descendents[i].id);
Zotero.Items.get(descendents[i].id).erase();
} }
} }
} }
if (del.length) {
Zotero.Items.erase(del);
}
var placeholders = collections.map(function () '?').join(); var placeholders = collections.map(function () '?').join();
@ -959,15 +976,16 @@ Zotero.Collection.prototype.serialize = function(nested) {
/** /**
* Returns an array of descendent collections and items * Returns an array of descendent collections and items
* *
* @param bool recursive Descend into subcollections * @param {Boolean} [recursive=false] Descend into subcollections
* @param bool nested Return multidimensional array with 'children' * @param {Boolean} [nested=false] Return multidimensional array with 'children'
* nodes instead of flat array * nodes instead of flat array
* @param string type 'item', 'collection', or FALSE for both * @param {String} [type] 'item', 'collection', or NULL for both
* @param {Boolean} [includeDeletedItems=false] Include items in Trash
* @return {Object[]} Array of objects with 'id', 'key', * @return {Object[]} Array of objects with 'id', 'key',
* 'type' ('item' or 'collection'), 'parent', * 'type' ('item' or 'collection'), 'parent',
* and, if collection, 'name' and the nesting 'level' * and, if collection, 'name' and the nesting 'level'
*/ */
Zotero.Collection.prototype.getChildren = function(recursive, nested, type, level) { Zotero.Collection.prototype.getChildren = function(recursive, nested, type, includeDeletedItems, level) {
if (!this.id) { if (!this.id) {
throw ('Zotero.Collection.getChildren() cannot be called on an unsaved item'); throw ('Zotero.Collection.getChildren() cannot be called on an unsaved item');
} }
@ -978,14 +996,6 @@ Zotero.Collection.prototype.getChildren = function(recursive, nested, type, leve
level = 1; level = 1;
} }
// 0 == collection
// 1 == item
var children = Zotero.DB.query('SELECT collectionID AS id, '
+ "0 AS type, collectionName AS collectionName, key "
+ 'FROM collections WHERE parentCollectionID=?1'
+ ' UNION SELECT itemID AS id, 1 AS type, NULL AS collectionName, key '
+ 'FROM collectionItems JOIN items USING (itemID) WHERE collectionID=?1', this.id);
if (type) { if (type) {
switch (type) { switch (type) {
case 'item': case 'item':
@ -996,6 +1006,20 @@ Zotero.Collection.prototype.getChildren = function(recursive, nested, type, leve
} }
} }
// 0 == collection
// 1 == item
var sql = 'SELECT collectionID AS id, '
+ "0 AS type, collectionName AS collectionName, key "
+ 'FROM collections WHERE parentCollectionID=?1';
if (!type || type == 'item') {
sql += ' UNION SELECT itemID AS id, 1 AS type, NULL AS collectionName, key '
+ 'FROM collectionItems JOIN items USING (itemID) WHERE collectionID=?1';
if (!includeDeletedItems) {
sql += " AND itemID NOT IN (SELECT itemID FROM deletedItems)";
}
}
var children = Zotero.DB.query(sql, this.id);
for(var i=0, len=children.length; i<len; i++) { for(var i=0, len=children.length; i<len; i++) {
// This seems to not work without parseInt() even though // This seems to not work without parseInt() even though
// typeof children[i]['type'] == 'number' and // typeof children[i]['type'] == 'number' and
@ -1017,7 +1041,7 @@ Zotero.Collection.prototype.getChildren = function(recursive, nested, type, leve
if (recursive) { if (recursive) {
var descendents = var descendents =
Zotero.Collections.get(children[i].id). Zotero.Collections.get(children[i].id).
getChildren(true, nested, type, level+1); getChildren(true, nested, type, includeDeletedItems, level+1);
if (nested) { if (nested) {
toReturn[toReturn.length-1].children = descendents; toReturn[toReturn.length-1].children = descendents;
@ -1050,8 +1074,8 @@ Zotero.Collection.prototype.getChildren = function(recursive, nested, type, leve
/** /**
* Alias for the recursive mode of getChildren() * Alias for the recursive mode of getChildren()
*/ */
Zotero.Collection.prototype.getDescendents = function(nested, type, level) { Zotero.Collection.prototype.getDescendents = function(nested, type, includeDeletedItems) {
return this.getChildren(true, nested, type); return this.getChildren(true, nested, type, includeDeletedItems);
} }