<?
if (defined("_DB_PAGENUGGETS.PHP")) { return; }
define("_DB_PAGENUGGETS.PHP", "1");


require_once $_SERVER['DOCUMENT_ROOT'] . '/cms/db_connection.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/cms/db_logon.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/cms/db_breadcrumbs.php';
require_once  rtrim($_SERVER['DOCUMENT_ROOT'],DIRECTORY_SEPARATOR) . '/cms/protected/_userupload.inc';
require_once $_SERVER['DOCUMENT_ROOT'] . '/cms/symlink.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/cms/downloadkey.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/cms/url_builder.php';


//
// these values MUST match what's in nugget_type in the DB
//
define("PN_PICTURE", "1");
define("PN_MULTI_LINE", "2");
define("PN_LINK", "3");
define("PN_SINGLE_LINE", "4");
define("PN_USER_PICTURE", "5");
define("PN_USER_FILE", "6");		

define("PN_STATUS_DELETED", "0");
define("PN_STATUS_NORMAL", "1");
define("PN_STATUS_WAITING", "2");

//======================
// public interface
//======================


//
// for caching
//

function PageNuggets_NewestNuggetId($cid) {
	$cid = CleanInput($cid);

	$s = 
	"select ni_sortID from nugget_item ".
	"where ni_crumbID = $cid ".
	"group by ni_sortID ". 
	"order by ni_sortID desc limit 0,1";

	$r = ExecuteDBReadQuery($s);

	return $r[0]['ni_sortID'];
}


//---------------------------
// for RSS feed
//---------------------------
function PageNuggets_NewestPublic($limit) {
	$s = 
	"select ni_crumbID, ni_sortID, ni_authorID, display_name from nugget_item, bread_crumbs ".
	"where ni_status=1 and ni_crumbID = bread_crumbs.ID and bc_perm_restricted_to_groupID=0 ".
	"group by ni_crumbID, ni_sortID, ni_authorID ". 
	"order by ni_sortID desc limit 0,$limit";

	return ExecuteDBReadQuery($s);
}

function PageNuggets_NewestPublicPage($limit, $cid) {
	$cid = CleanInput($cid);

	$s = 
	"select ni_crumbID, ni_sortID, ni_authorID, display_name from nugget_item, bread_crumbs ".
	"where ni_status=1 and ni_crumbID = $cid and ni_crumbID = bread_crumbs.ID and bc_perm_restricted_to_groupID=0 ".
	"group by ni_crumbID, ni_sortID, ni_authorID ". 
	"order by ni_sortID desc limit 0,$limit";

	return ExecuteDBReadQuery($s);
}


function PageNuggets_NewestRestricted($limit) {
	$mask = Logon_GetCurrentUserGroupMask();
	
	

	$s = 
	"select ni_crumbID, ni_sortID, ni_authorID, display_name from nugget_item, bread_crumbs ".
	"where ni_status=1 and ni_crumbID = bread_crumbs.ID  ";

	if (!Logon_IsAdmin()) {
		$s .= " and (((bc_perm_restricted_to_groupID & $mask) > 0) or (bc_perm_restricted_to_groupID = 0)) ";
	}

	$s .= " group by ni_crumbID, ni_sortID, ni_authorID ". 
	"order by ni_sortID desc limit 0,$limit";

	return ExecuteDBReadQuery($s);
	
}

function PageNuggets_TitleGuess($crumbID, $userID, $sortID) {
	$crumbID = CleanInput($crumbID);
	$userID = CleanInput($userID);
	$sortID = CleanInput($sortID);
	

	$s = 
	"select ni_datastring ni_status, ni_crumbID, ni_authorID, ni_datastring, ni_sortID, ntm_html_file, ntm_type ".
	"from nugget_item, nugget_template, logon_user ".
	"WHERE ni_crumbID = $crumbID AND ".
	"ni_authorID = $userID and ".
	"ni_sortID = $sortID and ".
	"ni_authorID = logon_user.ID and ".
	"ni_templateID = nugget_template.ID ".
	"order by ntm_sortID limit 0,1";


	$r = ExecuteDBReadQuery($s);
	return PageNuggets_ItemToParagraph($r[0],$r[0]['ni_datastring']);
}

//---------------------------
// content Admin Approvals
//---------------------------
function PageNuggets_HasApprovals() {
	return count(PageNuggets_FindApprovals()) != 0;
}

/*
function PageNuggets_ApproveList($list) {
	$s =
	"update nugget_item set ni_status = 1 where ID in ($list)";

	return ExecuteDBWriteQuery($s);
}
*/
function PageNuggest_ApproveNugget($crumbID, $userID, $sortID) {
	$crumbID = CleanInput($crumbID);
	$userID = CleanInput($userID);
	$sortID = CleanInput($sortID);

	$s =
	"update nugget_item set ni_status = 1 where ni_crumbID = $crumbID and ni_authorID = $userID and ni_sortID = $sortID";
	
	return ExecuteDBWriteQuery($s);
	
}
function PageNuggets_FindApprovals() {
	$isadmin = Logon_IsAdmin();


	$s = 
	"select nugget_item.ID, display_name, bc_catagoryID, ni_crumbID, ni_authorID, lu_name, ni_datastring, ni_sortID, ntm_html_file, ntm_type " .
	"from nugget_item, nugget_template, bread_crumbs, logon_user " .
	"WHERE ni_status = 2 and " .
	"ni_authorID = logon_user.ID and ".
	"ni_templateID = nugget_template.ID and ".
	"ni_crumbID = bread_crumbs.ID order by ni_sortID ";
	if (!$isadmin) {
		$mask = Logon_GetCurrentUserGroupMask();
    	$s = 
    	"select nugget_item.ID, display_name, ni_crumbID, ni_authorID, lu_name, ni_datastring, ni_sortID, ntm_html_file, ntm_type " .
    	"from nugget_item, nugget_template, bread_crumbs, logon_user " .
    	"WHERE ni_status = 2 and " .
		"ni_crumbID = bread_crumbs.ID and ".
		"((bc_perm_admin_content_groupID & $mask) > 0) and ".
    	"ni_authorID = logon_user.ID and ".
    	"ni_templateID = nugget_template.ID order by ni_sortID  ";
	}

	return ExecuteDBReadQuery($s);
}

function PageNuggets_FormatDatastring($userID, $ntm_type, $ds) {

	switch ($ntm_type) {
	case PN_PICTURE:
	case PN_SINGLE_LINE:
	case PN_LINK:
		return $ds;
		break;
	case PN_MULTI_LINE:
		return str_replace("\n", "<br />\n", $ds);
		break;
	case PN_USER_PICTURE:
		$key = GetDownloadKey("static");
		AddDownloadFolder($key);

		return  Upload_GetSymHREF($key, $userID, $ds);

	case PN_USER_FILE:
		return  GetDownloadHREF(Upload_GetSubFolder($userID, $ds));
		break;
	}	

}



//---------------------------
// HTML generators
//---------------------------

function PageNuggets_GetNuggetHTML($crumbID, $userID, $sortID, $allowdeleted=false, $stripcontenttags=false) {
	$crumbID = CleanInput($crumbID);
	$userID = CleanInput($userID);
	$sortID = CleanInput($sortID);

	if (!($crumbID && $userID && sortID)) {
		return 'Item not found';
	}
	$s = 
	"select nugget_item.ID, ni_status, ni_crumbID, ni_authorID, lu_name, ni_datastring, ni_sortID, ntm_html_file, ntm_type " .
	"from nugget_item, nugget_template, logon_user " .
	"WHERE ni_crumbID = $crumbID AND " .
	"ni_authorID = $userID and " .
	"ni_sortID = $sortID and " .
	"ni_authorID = logon_user.ID and ".
	"ni_templateID = nugget_template.ID ";
	if (!$allowdeleted) {
		$s .= "and ni_status != 0 ";
	}
	$s .= "order by ntm_sortID ";
	
 
	$r = ExecuteDBReadQuery($s);
	if (!$r) { die('Item not found'); } 

	$s = PageNuggets_RecordToHTML($r, Logon_GetCurrentUserID(), 0, Logon_IsAdmin(), $stripcontenttags);
	return $s;
}


function PageNuggets_GetMaxNuggets($crumbID) {
	$r = ExecuteReadParams(
		"select bc_max_nuggets from bread_crumbs where bread_crumbs.ID = ?",
		"i",
		[$crumbID]
	);
	if (!$r) { die('Breadcrumb failed to retreive max nugget count'); }
	
	return $r[0]['bc_max_nuggets'];

}
function PageNuggets_GetSortIDBlock($crumbID, $maxnugget, $offset, $desc=false) {
	$s = "SELECT ni_sortID FROM `nugget_item` WHERE ni_crumbID = ?" . 
	" and ni_status != 0 " .
	" group by ni_sortID order by ni_sortID ";
	if ($desc) {
		$s .= ' DESC ';
	}
	if ($maxnugget != 0) {
		$s .= "limit ". $maxnugget ." offset " . $offset;
	}
	
	$r = ExecuteReadParams(
		$s,
		"i",
		[$crumbID]
	);
		
	return $r;
}

function PageNuggets_GetPageHTML($crumbID, $userID, $catagoryID, $isadmin, $archive=false, $archivepage=0) {
	$maxnugget = PageNuggets_GetMaxNuggets($crumbID);
	$offset = $archivepage * $maxnugget;
	
	$r2 = PageNuggets_GetSortIDBlock(
		$crumbID, $maxnugget, $offset, 
		(int)$catagoryID == (int)BC_PREPENDABLE_PAGE
		);
	if (!$r2) { return ''; } // it's OK to find nothing
	
	$cnt = count($r2);
	$sortList = '';
	for ($i=0; $i < $cnt; $i++) {
		if ($sortList != '') { $sortList .= ','; }
		$sortList .= $r2[$i]['ni_sortID'];
	}
	
	$s = 
	"select nugget_item.ID, ni_status, ni_crumbID, ni_authorID, lu_name, ".
	"ni_datastring, ni_sortID, ntm_html_file, ntm_type, ntm_friendly_name " .
	"from nugget_item, nugget_template, logon_user " .
	"WHERE ni_crumbID = ? AND " .
	"ni_sortID in (".$sortList.") and " .
	"ni_authorID = logon_user.ID and ".
	"ni_templateID = nugget_template.ID ";


	switch((int)$catagoryID) {
	case (int)BC_APPENDABLE_PAGE:
		$s .= " order by ni_sortID, ntm_sortID, nugget_template.ID ";
		break;
	case (int)BC_PREPENDABLE_PAGE:
		$s .= " order by ni_sortID DESC, ntm_sortID, nugget_template.ID ";
		break;
	default:
		$s .= " order by ni_sortID, ntm_sortID ";
		break;
	}
	
	$r = ExecuteReadParams($s,"i",[$crumbID]);
	if (!$r) { return ''; } // it's OK for a breadcrumb to be empty

	

	$s = PageNuggets_RecordToHTML($r, $userID, $maxnugget, $isadmin, false, $archive,$archivepage);
	return $s;
}


function PageNuggets_RecordToHTML($r, $userID, $maxnug, $isadmin=false, $stripcontenttags=false, $archive=false, $archivepage=0) {
	$cnt = count($r);
	$s = "";
	$lastsortID = "";
	$x = $maxnug-1;
	$lastsortID = '';
	if ($cnt < $x) { $x=$cnt-1; }

	// echo "<br /> max: $maxnug cnt: $cnt x: $x archive: $archive <br /> ";
	
	for ($i = 0; $i < $cnt; $i++) {
		$crumbID = $r[$i]["ni_crumbID"];
		$ds = $r[$i]["ni_datastring"];
		if ($ds == '') { continue; }

		// skip not-normal nuggets unless this is the author
		$uid = $r[$i]["ni_authorID"];
		$statusOK = ($r[$i]['ni_status'] == 1);
		if (!$statusOK) {
			if ($uid != $userID) {
				if (!$isadmin) {
					continue;
				}
			}
		}
		$un = $r[$i]["lu_name"];
		$oldds = $ds;
		$ds = PageNuggets_FormatDatastring(
			$uid,
			$r[$i]["ntm_type"],
			$ds
		);
		$h = $r[$i]["ntm_html_file"];
		$sortID = $r[$i]["ni_sortID"];
		$fn = str_replace(' ', '_', $r[$i]['ntm_friendly_name']);

		$date = date('M-d-y g:i A T', $sortID); 
		$isauthor = ($uid == $userID);

		$maxedout = false;
		
		if ($lastsortID != $sortID) {
			if ($lastsortID != '' ) {
       			$s .= "\n</div>\n";
				
				if ($maxnug != 0) {
					if ($x > 1) { 
						$x--; 
					} else {
						$maxedout = true;
						//$s .= "<a class=\"mybutton\" href=\"\?arc=1\"> Older Items</a>";
						$s .= "<a class=\"bigbuttonsize mybutton\" href=\"?parc=".($archivepage+1)."&arc=1&amp;cid=" .$crumbID. "\"> Older Items</a>";
						break;
					}
				}
				

			}
			$lastsortID = $sortID;


			$s .= '<div class="nuggetheader">';
			$s .= '<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td>';
			$s .= ' <div class="nuggetinfo">';
			$s .= " <b><a class=\"authorlink\" href=\"" . PToURL('author',  "aid=$uid") . "\">$un</a></b> $date";
			$s .= "</div>\n";
			$s .= '</td><td align="right">';
			$s .= '<div class="nuggetlinks">';
    		if (($isadmin) || ($isauthor)) {
    			$s .= "<a class=\"mybutton\" target=\"_blank\" href=\"" . ProtectedToURL('editpage', "cid=$crumbID&amp;sid=$sortID&amp;aid=$uid") . "\">Edit</a>";
				$s .= "&nbsp;<a class=\"mybutton\" target=\"_blank\" href=\"" . ProtectedToURL('deletepage', "cid=$crumbID&amp;sid=$sortID&amp;aid=$uid") . "\">Delete</a>&nbsp;";
    		}

			$s .= "<a class=\"nuggetheaderlink\" href=\"#PageTop\">^top</a>&nbsp;";
			$s .= "<a class=\"nuggetheaderlink\"  href=\"/cms/showpage.php?cid=$crumbID#$sortID\">link</a> <a name=\"$sortID\"></a>";				
			if (!$statusOK) {
				$s .= '&nbsp;[waiting approval]';				
			}
			$s .= '</div>';
			$s .= '</td></tr></table>';
			$s .= "</div>\n";


			$s .= "<div class=\"nugget\">\n";
		}
		

   		if ($stripcontenttags) {
   			$s .= strip_tags(PageNuggets_ItemToParagraph($r[$i]));
   		} else {
   			$s .=  PageNuggets_ItemToParagraph($r[$i]);
   		}


	}
	if (!$maxedout) { $s .= "</div>\n"; }
	return $s;


}

function PageNuggets_ItemToParagraph($r, $data='') {
	$uid = $r["ni_authorID"]; 
	if ($data == '') {
		$ds = $r["ni_datastring"];
	} else {
		$ds = $data;
	}
	$nt = $r['ntm_type'];
	$oldds = $ds;
	$ds =  PageNuggets_FormatDatastring(
			$uid,
			$nt,
			$ds
		);

	$h = $r["ntm_html_file"];
	$fn = str_replace(' ', '_', $r['ntm_friendly_name']);
	switch($nt) {
   	case PN_USER_FILE:
		$final = str_replace("%s", $ds, $h);
		$final = str_replace("%t", 'download '. basename($oldds), $final);
		break;
   	case PN_MULTI_LINE:
   	case PN_SINGLE_LINE:
		$final = str_replace("%s", $ds, $h);
		if (strpos(' '.$ds, '[myimage=') == true) {
    		$key = GetDownloadKey("static");
    		AddDownloadFolder($key);

    		$final =
    		preg_replace(
    			'!(\[myimage=)(.*?)(\])!', 
    			'<img alt="myimage" src="'. Upload_GetSymHREF($key, $uid, 'images/${2}') . '" />',
    			$final
			);
		}

		if (strpos(' '.$ds, '[myfile=') == true) {
    		$key = GetDownloadKey("static");
    		AddDownloadFolder($key);
    		$final =
			
			preg_replace(
				"/\[myfile\=(.*?)\](.*?)\[\/myfile\]/is", 
				"<!--ulink1 --><span class=\"nugget_File\"><a href=\"" . Upload_FileURL($uid,'$1') . "\">$2</a></span><!-- ulink2 -->", 
				$final
			);
			
			/*
    		preg_replace(
    			'!(\[myfile=)(.*?)(\])(.*?)(\[/myfile\])!', 
    			'<a href="'.  Upload_FileURL($uid, '${1}')  . '">${4}</a>',
    			$final
			);
			*/
		}

		break;
	default:
		$final = str_replace("%s", $ds, $h);
		break;
	}
	$s .= '<p class="nugget_' .$fn. '"> '.$final . "</p>\n";

	return $s;
}

/*
	This allows some HTML to be used for user items in a very limited way
*/
function PageNuggets_HTMLToCode($value) {
	$value = preg_replace('!(<strong>)(.*?)(</strong>)!', '[b]${2}[/b]', $value);
	$value = preg_replace('!(<span style="text-decoration: underline;">)(.*?)(</span>)!', '[u]${2}[/u]', $value);
	$value = preg_replace('!(<i>)(.*?)(</i>)!', '[i]${2}[/i]', $value);

	$value = preg_replace('!(<a href=")(.*?)(">)(.*?)(</a>)!', 
		'[url=${2}]${4}[/url]', $value);
	$value = preg_replace('!(<a target="_blank" href=")(.*?)(">)(.*?)(</a>)!', 
		'[url=${2}]${4}[/url]', $value);

	$value = preg_replace('!(<img src=")([^"]+?)(".*?)(" />)!', 
		'[img=${2}]', $value);

	return $value;
}

function PageNuggets_CodeToHTML($value) {
	$value = preg_replace('!(\[b\])(.*?)(\[/b\])!', '<strong>${2}</strong>', $value);
	$value = preg_replace('!(\[i\])(.*?)(\[/i\])!', '<i>${2}</i>', $value);
	$value = preg_replace('!(\[u\])(.*?)(\[/u\])!', '<span style="text-decoration: underline;">${2}</span>', $value);

	
   	$value = preg_replace('!(\[url=http://arsware)(.*?)(\])(.*?)(\[/url\])!', 
   			'<a href="http://arsware${2}">${4}</a>', $value);
   	$value = preg_replace('!(\[url=http://www.arsware)(.*?)(\])(.*?)(\[/url\])!', 
   			'<a href="http://www.arsware${2}">${4}</a>', $value);

   	$value = preg_replace('!(\[url=)(.*?)(\])(.*?)(\[/url\])!', 
   			'<a target="_blank" href="${2}">${4}</a>', $value);

	$value = preg_replace('!(\[img=)(.*?)(\])!', 
   			'<img src="${2}" alt="${2}" />', $value);
	return $value;
}


function PageNuggets_GetGroupsHTML($crumbID) {
	$s = "select nugget_group.ID, ng_friendly_name, bc_ntm_groupID from nugget_group, bread_crumbs " .
		"where bread_crumbs.ID = $crumbID " .
		"order by ID";

	$r = ExecuteDBReadQuery($s);
	if (!$r) { die('empty nugget_group table'); }
	
	$s = '';

	foreach($r as $rec) {
		$groupID = $rec["bc_ntm_groupID"];
		$id = $rec["ID"];
		$dn = $rec["ng_friendly_name"];
		if ($groupID == $id) {
			$s .= "<input CHECKED type=radio name=\"BreadGroup\" value=\"$id\"> $dn<br>\n";		
		} else {
			$s .= "<input type=radio name=\"BreadGroup\" value=\"$id\"> $dn<br>\n";
		}
	}

	return $s;
}


//---------------------------
// Utility functions
//---------------------------
function PageNuggets_GetNewestAuthorCrumbIDs($limit) {
	$s =
	"select ni_crumbID, ni_sortID from nugget_item, bread_crumbs ".
	"where ni_status = 1 and ni_crumbID = bread_crumbs.ID and ".
	"bc_catagoryID = " . BC_AUTHOR_PAGE . " ".
	"group by ni_crumbID order by ni_sortID desc limit 0, $limit";

	return ExecuteDBReadQuery($s);
}

function PageNuggets_GetAuthorID($crumbID, $sortID) {
	if (!($crumbID && $sortID)) {
		return '';
	}
	$crumbID = CleanInput($crumbID);
	$sortID = CleanInput($sortID);

	$s = "select ni_authorID from nugget_item where ni_crumbID = $crumbID and ni_sortID = $sortID";

	$r = ExecuteDBReadQuery($s);

	return $r[0]['ni_authorID']; 
}


function PageNuggets_FindAllUserItems($userID) {
	$userID = CleanInput($userID);
	
	$s = 
	"select ni_status,ni_crumbID,lu_name,bread_crumbs.display_name,ni_sortID ".
	"from nugget_item,bread_crumbs,logon_user ".
	"where ni_authorID = $userID and ni_crumbID = bread_crumbs.ID and ".
	"ni_authorID = logon_user.ID ".
	"group by ni_crumbID, lu_name, bread_crumbs.display_name, ni_sortID ".
	"order by bc_catagoryID, ni_sortID desc ";

	return ExecuteDBReadQuery($s);
}

function PageNuggets_NewUserItem($crumbID, $userID, $templateID, $sortID, $value) {
	$crumbID = intval($crumbID);
	$userID = intval($userID); 
	$templateID = intval($templateID);
	$sortID = intval($sortID);
	$value = PageNuggets_CodeToHTML($value);
	
	$isadmin = Logon_IsAdmin();
	if ($isadmin) {
		$status = PN_STATUS_NORMAL;
	} else {
		//$status = PN_STATUS_WAITING;
		$status = PN_STATUS_NORMAL;
	} 

	return ExecuteWriteParams(
	"insert into nugget_item (ni_crumbID, ni_status, ni_authorID, ni_templateID, ni_sortID, ni_datastring) values " .
	"(?,?,?,?,?,?)",
	"iiiiis", 
	[$crumbID, $status, $userID, $templateID, $sortID, $value]
	);
	

}
function PageNuggets_SetUserItem($nuggetid, $userID, $value) {
	$value = PageNuggets_CodeToHTML($value);

	return ExecuteWriteParams(
	"update nugget_item set ni_datastring = ? " .
	"where nugget_item.ID = ? and ni_authorID = ? ",
	'sii',[$value, $nuggetid, $userID])
	;
}

function PageNuggets_SetUserItemDEBUG($nuggetid, $userID, $value) {
	$nuggetid = CleanInput($nuggetid);
	$userID = CleanInput($userID);	
	echo 'no clean:' . $value . '<br />';
 
	$value = CleanInput($value);
	echo 'clean:' . $value . '<br />';
	$value = PageNuggets_CodeToHTML($value);
	echo 'codetohtml:' . $value . '<br />';



	

	$s = 
	"update nugget_item set ni_datastring = '$value' " .
	"where nugget_item.ID = $nuggetid and ni_authorID = $userID ";

	return ExecuteDBWriteQuery($s);
}

function PageNuggets_GetMasterTemplate($ntm_groupID) {
	$ntm_groupID = CleanInput($ntm_groupID);

	$s = "SELECT nugget_template.ID, ntm_html_file, ntm_friendly_name, ntm_type, " .
	"nt_html_edit, '' AS ni_templateID, '' AS ni_datastring " .
    "FROM nugget_template,  nugget_type " .
    "WHERE ntm_groupID = $ntm_groupID AND ntm_type = nugget_type.ID " .
	"order by ntm_sortID ";

	$r = ExecuteDBReadQuery($s);
	return $r;

}

function PageNuggets_GetAllItems($crumbID, $ntm_groupID) {
	$crumbID = CleanInput($crumbID);
	$ntm_groupID = CleanInput($ntm_groupID);
	
	// show a nugget_item or nulls for every template belonging to this breadcrumb
	// for the curent author
	//
 
	$s = "SELECT nugget_template.ID, ni_authorID, ntm_html_file, ntm_friendly_name, ntm_type, " .
	"nt_html_edit, COALESCE( ni_templateID, '' ) AS ni_templateID, " . 
	"COALESCE( ni_datastring, '' ) AS ni_datastring " .
    "FROM nugget_template, nugget_type " .
    "LEFT JOIN nugget_item ON ( nugget_item.ni_templateID = nugget_template.ID ) " .
    "WHERE ntm_groupID = $ntm_groupID AND ntm_type = nugget_type.ID " .
	"order by ntm_groupID, ntm_sortID ";

	
	$r = ExecuteDBReadQuery($s);

	return $r;
}

function PageNuggets_GetUserItems($userID, $crumbID, $sortID) {
	$crumbID = CleanInput($crumbID);
	$userID = CleanInput($userID);
	$sortID = CleanInput($sortID);
	
	// show a nugget_item or nulls for every template belonging to this breadcrumb
	// for the curent author
	//
 

	$s =
	"SELECT nugget_template.ID, ni_authorID, ntm_html_file, ntm_friendly_name, ntm_type, " .
	" nt_html_edit, ni_templateID, ni_datastring, nugget_item.ID  " .
	"FROM nugget_item, nugget_template, nugget_type " .
	"WHERE ni_authorID = $userID and " .
	" ni_crumbID = $crumbID and " .
	" ni_sortID = $sortID and " .
	" ni_templateID = nugget_template.ID and " .
	" ntm_type = nugget_type.ID " .
	" order by ni_sortID, ntm_sortID, nugget_item.ID ";

	
	$r = ExecuteDBReadQuery($s);

	return $r;
}


function PageNuggets_DeleteUserItem($crumbID, $userID, $sortID) {
	$crumbID = CleanInput($crumbID);
	$userID = CleanInput($userID);
	$sortID = CleanInput($sortID);
	
	//
	// logically delete an item
	// 

	$s =
	"update nugget_item set ni_status = 0 " .
	"where ni_crumbID = $crumbID and ".
	"ni_authorID = $userID and ".
	"ni_sortID = $sortID";

	return  ExecuteDBWriteQuery($s);
}





?>