From e5b25a966b7984beee1c7f746ed49b3424bcc676 Mon Sep 17 00:00:00 2001
From: Dan Stillman <dstillman@zotero.org>
Date: Tue, 23 May 2006 20:53:09 +0000
Subject: [PATCH] Items.getTreeRows() now takes an optional second param, type,
 which can (for the moment) be 'folders' or 'items' to limit the results

New methods: Folder.hasChildFolders() and Folder.hasChildItems()
---
 .../content/scholar/data_access.js            | 41 +++++++++++++++----
 1 file changed, 34 insertions(+), 7 deletions(-)

diff --git a/chrome/chromeFiles/content/scholar/data_access.js b/chrome/chromeFiles/content/scholar/data_access.js
index 2faf5ed71c..395f60e598 100644
--- a/chrome/chromeFiles/content/scholar/data_access.js
+++ b/chrome/chromeFiles/content/scholar/data_access.js
@@ -904,7 +904,7 @@ Scholar.Items = new function(){
 	 *
 	 * Type can tested with instanceof (e.g. if (obj instanceof Scholar.Folder)) or isFolder()
 	 */
-	function getTreeRows(parent){
+	function getTreeRows(parent, type){
 		var toReturn = new Array();
 		
 		/*
@@ -917,7 +917,18 @@ Scholar.Items = new function(){
 		}
 		
 		var sql = 'SELECT * FROM treeStructure TS '
-			+ 'WHERE parentFolderID=' + parent + ' ORDER BY orderIndex';
+			+ 'WHERE parentFolderID=' + parent;
+		
+		switch (type){
+			case 'folder':
+				sql += ' AND isFolder=1';
+				break;
+			case 'item':
+				sql += ' AND isFolder=0';
+				break;
+		}
+		
+		sql += ' ORDER BY orderIndex';
 		
 		var tree = Scholar.DB.query(sql);
 		
@@ -1074,8 +1085,11 @@ Scholar.Folder = function(){
 Scholar.Folder.prototype.loadFromID = function(id){
 	// Should be same as query in Scholar.Folders, just with folderID
 	var sql = "SELECT folderID, folderName, parentFolderID, "
-		+ "(SELECT COUNT(*) FROM treeStructure WHERE parentFolderID=" +
-		id + ")=0 AS isEmpty FROM folders F "
+		+ "(SELECT COUNT(*) FROM treeStructure WHERE "
+		+ "parentFolderID=TS.id AND isFolder=1)!=0 AS hasChildFolders, "
+		+ "(SELECT COUNT(*) FROM treeStructure WHERE "
+		+ "parentFolderID=TS.id AND isFolder=0)!=0 AS hasChildItems "
+		+ "FROM folders F "
 		+ "JOIN treeStructure TS ON (F.folderID=TS.id AND TS.isFolder=1) "
 		+ "WHERE folderID=" + id;
 	
@@ -1091,7 +1105,8 @@ Scholar.Folder.prototype.loadFromRow = function(row){
 	this._id = row['folderID'];
 	this._name = row['folderName'];
 	this._parent = row['parentFolderID'];
-	this._empty = row['isEmpty'];
+	this._hasChildFolders = row['hasChildFolders'];
+	this._hasChildItems = row['hasChildItems'];
 }
 
 Scholar.Folder.prototype.getID = function(){
@@ -1111,9 +1126,18 @@ Scholar.Folder.prototype.getParent = function(){
 }
 
 Scholar.Folder.prototype.isEmpty = function(){
-	return !!parseInt(this._empty);
+	return !(parseInt(this._hasChildFolders)) && !(parseInt(this._hasChildItems));
 }
 
+Scholar.Folder.prototype.hasChildFolders = function(){
+	return !!(parseInt(this._hasChildFolders));
+}
+
+Scholar.Folder.prototype.hasChildItems = function(){
+	return !!(parseInt(this._hasChildItems));
+}
+
+
 /**
 * Deletes a folder and all descendent folders and items
 **/
@@ -1232,7 +1256,10 @@ Scholar.Folders = new function(){
 	function _load(){
 		var sql = "SELECT folderID, folderName, parentFolderID, "
 			+ "(SELECT COUNT(*) FROM treeStructure WHERE "
-			+ "parentFolderID=TS.id)=0 AS isEmpty FROM folders F "
+			+ "parentFolderID=TS.id AND isFolder=1)!=0 AS hasChildFolders, "
+			+ "(SELECT COUNT(*) FROM treeStructure WHERE "
+			+ "parentFolderID=TS.id AND isFolder=0)!=0 AS hasChildItems "
+			+ "FROM folders F "
 			+ "JOIN treeStructure TS ON (F.folderID=TS.id AND TS.isFolder=1) "
 			+ "WHERE folderID>0"; // skip 'root' folder
 		var result = Scholar.DB.query(sql);