$usercollection = $c[0]["ref"];
        set_user_collection($userref, $usercollection);
    }
    # User has deleted their last collection? add a new one.
    if (count($c) == 0) {
        # No collections to select. Create them a new collection.
        $name = get_mycollection_name($userref);
        $usercollection = create_collection($userref, $name);
        set_user_collection($userref, $usercollection);
    }
    refresh_collection_frame($usercollection);
}
hook('customcollectionmanage');
$removeall = getvalescaped("removeall", "");
if ($removeall != "") {
    remove_all_resources_from_collection($removeall);
    refresh_collection_frame($usercollection);
}
include "../include/header.php";
?>
  <div class="BasicsBox">
    <h2>&nbsp;</h2>
    <h1><?php 
echo $lang["managemycollections"];
?>
</h1>
    <p class="tight"><?php 
echo text("introtext");
?>
</p><br>
<div class="BasicsBox">
function save_collection($ref)
	{
	global $theme_category_levels;
	
	
	$allow_changes=(getval("allow_changes","")!=""?1:0);
	
	# Next line disabled as it seems incorrect to override the user's setting here. 20071217 DH.
	#if ($theme!="") {$allow_changes=0;} # lock allow changes to off if this is a theme
	
	# Update collection with submitted form data
	if (!hook('modifysavecollection')) {
	$sql="update collection set
				name='" . getvalescaped("name","") . "',
				keywords='" . getvalescaped("keywords","") . "',
				public='" . getvalescaped("public","",true) . "',";
		
		for($n=1;$n<=$theme_category_levels;$n++){
			if ($n==1){$themeindex="";} else {$themeindex=$n;}
			$themes[$n]=getvalescaped("theme$themeindex","");
			if (getval("newtheme$themeindex","")!="") {
				$themes[$n]=trim(getvalescaped("newtheme$themeindex",""));
				}
			if (isset($themes[$n])){
				$sql.="theme".$themeindex."='" . $themes[$n]. "',";
				}
		}

	$sql.="allow_changes='" . $allow_changes . "'";
	
	if (checkperm("h"))
		{	
		$sql.="
			,home_page_publish='" . (getvalescaped("home_page_publish","")!=""?"1":"0") . "'
			,home_page_text='" . getvalescaped("home_page_text","") . "'";
		if (getval("home_page_image","")!="")
			{
			$sql.=",home_page_image='" . getvalescaped("home_page_image","") . "'";
			}
		}
		
	    $sql.=" where ref='$ref'";
	
	sql_query($sql);
	} # end replace hook - modifysavecollection
	
	index_collection($ref);
		
	# If 'users' is specified (i.e. access is private) then rebuild users list
	$users=getvalescaped("users",false);
	if ($users!==false)
		{
		sql_query("delete from user_collection where collection='$ref'");
		#log this
		collection_log($ref,"T",0, '#all_users');

		if (($users)!="")
			{
			# Build a new list and insert
			$users=resolve_userlist_groups($users);
			$ulist=array_unique(trim_array(explode(",",$users)));
			$urefs=sql_array("select ref value from user where username in ('" . join("','",$ulist) . "')");
			if (count($urefs)>0)
				{
				sql_query("insert into user_collection(collection,user) values ($ref," . join("),(" . $ref . ",",$urefs) . ")");
				}
			#log this
			collection_log($ref,"S",0, join(", ",$ulist));
			}
		}
		
	# Relate all resources?
	if (getval("relateall","")!="")
		{
		$rlist=get_collection_resources($ref);
		for ($n=0;$n<count($rlist);$n++)
			{
			for ($m=0;$m<count($rlist);$m++)
				{
				if ($rlist[$n]!=$rlist[$m]) # Don't relate a resource to itself
					{
					sql_query("delete from resource_related where resource='" . $rlist[$n] . "' and related='" . $rlist[$m] . "'");
					sql_query("insert into resource_related (resource,related) values ('" . $rlist[$n] . "','" . $rlist[$m] . "')");
					}
				}
			}
		}
	
	
	# Remove all resources?
	if (getval("removeall","")!="")
		{
		remove_all_resources_from_collection($ref);
		}
		
	# Delete all resources?
	if (getval("deleteall","")!="" && !checkperm("D"))
		{
		$resources=do_search("!collection" . $ref);
		for ($n=0;$n<count($resources);$n++)
			{
			if (checkperm("e" . $resources[$n]["archive"]))
				{
				delete_resource($resources[$n]["ref"]);	
				collection_log($ref,"D",$resources[$n]["ref"]);
				}
			}
		}
		
	# Update limit count for saved search
	if (isset($_POST["result_limit"]))
		{
		sql_query("update collection_savedsearch set result_limit='" . getvalescaped("result_limit","") . "' where collection='$ref'");
		
		}
	
	refresh_collection_frame();
	}
function save_collection($ref)
{
    global $theme_category_levels, $attach_user_smart_groups;
    if (!collection_writeable($ref)) {
        return false;
    }
    $allow_changes = getval("allow_changes", "") != "" ? 1 : 0;
    # Next line disabled as it seems incorrect to override the user's setting here. 20071217 DH.
    #if ($theme!="") {$allow_changes=0;} # lock allow changes to off if this is a theme
    # Update collection with submitted form data
    if (!hook('modifysavecollection')) {
        $sql = "update collection set\n\t\t\t\tname='" . urldecode(getvalescaped("name", "")) . "',\n\t\t\t\t" . hook('savecollectionadditionalfields') . "\n\t\t\t\tkeywords='" . getvalescaped("keywords", "") . "',\n\t\t\t\tpublic='" . getvalescaped("public", "", true) . "',";
        for ($n = 1; $n <= $theme_category_levels; $n++) {
            if ($n == 1) {
                $themeindex = "";
            } else {
                $themeindex = $n;
            }
            $themes[$n] = getvalescaped("theme{$themeindex}", "");
            if (getval("newtheme{$themeindex}", "") != "") {
                $themes[$n] = trim(getvalescaped("newtheme{$themeindex}", ""));
            }
            if (isset($themes[$n])) {
                $sql .= "theme" . $themeindex . "='" . $themes[$n] . "',";
            }
        }
        $sql .= "allow_changes='" . $allow_changes . "'";
        if (checkperm("h")) {
            $sql .= "\n\t\t\t,home_page_publish='" . (getvalescaped("home_page_publish", "") != "" ? "1" : "0") . "'\n\t\t\t,home_page_text='" . getvalescaped("home_page_text", "") . "'";
            if (getval("home_page_image", "") != "") {
                $sql .= ",home_page_image='" . getvalescaped("home_page_image", "") . "'";
            }
        }
        $modified_sql = hook('morehomepagepublishsave', "", array($sql));
        if (!empty($modified_sql)) {
            $sql = $modified_sql;
        }
        $sql .= " where ref='{$ref}'";
        sql_query($sql);
    }
    # end replace hook - modifysavecollection
    index_collection($ref);
    # If 'users' is specified (i.e. access is private) then rebuild users list
    $users = getvalescaped("users", false);
    if ($users !== false) {
        sql_query("delete from user_collection where collection='{$ref}'");
        if ($attach_user_smart_groups) {
            sql_query("delete from usergroup_collection where collection='{$ref}'");
        }
        #log this
        collection_log($ref, "T", 0, '#all_users');
        if ($users != "") {
            # Build a new list and insert
            $users = resolve_userlist_groups($users);
            $ulist = array_unique(trim_array(explode(",", $users)));
            $urefs = sql_array("select ref value from user where username in ('" . join("','", $ulist) . "')");
            if (count($urefs) > 0) {
                sql_query("insert into user_collection(collection,user) values ({$ref}," . join("),(" . $ref . ",", $urefs) . ")");
            }
            #log this
            collection_log($ref, "S", 0, join(", ", $ulist));
            if ($attach_user_smart_groups) {
                $groups = resolve_userlist_groups_smart($users);
                $groupnames = '';
                if ($groups != '') {
                    $groups = explode(",", $groups);
                    if (count($groups) > 0) {
                        foreach ($groups as $group) {
                            sql_query("insert into usergroup_collection(collection,usergroup) values ({$ref},{$group})");
                            // get the group name
                            if ($groupnames != '') {
                                $groupnames .= ", ";
                            }
                            $groupnames .= sql_value("select name value from usergroup where ref={$group}", "");
                        }
                    }
                    #log this
                    collection_log($ref, "S", 0, $groupnames);
                }
            }
        }
    }
    # Relate all resources?
    if (getval("relateall", "") != "") {
        $rlist = get_collection_resources($ref);
        for ($n = 0; $n < count($rlist); $n++) {
            for ($m = 0; $m < count($rlist); $m++) {
                if ($rlist[$n] != $rlist[$m]) {
                    if (count(sql_query("SELECT 1 FROM resource_related WHERE resource='" . $rlist[$n] . "' and related='" . $rlist[$m] . "' LIMIT 1")) != 1) {
                        sql_query("insert into resource_related (resource,related) values ('" . $rlist[$n] . "','" . $rlist[$m] . "')");
                    }
                }
            }
        }
    }
    # Remove all resources?
    if (getval("removeall", "") != "") {
        remove_all_resources_from_collection($ref);
    }
    # Delete all resources?
    if (getval("deleteall", "") != "" && !checkperm("D")) {
        if (allow_multi_edit($ref)) {
            delete_resources_in_collection($ref);
        }
    }
    # Update limit count for saved search
    if (isset($_POST["result_limit"])) {
        sql_query("update collection_savedsearch set result_limit='" . getvalescaped("result_limit", "") . "' where collection='{$ref}'");
    }
    refresh_collection_frame();
}
	{
	# Add someone else's collection to your My Collections
	add_collection($userref,$addcollection);
	set_user_collection($userref,$addcollection);
	refresh_collection_frame();
	
   	# Log this
	daily_stat("Add public collection",$userref);
	}
/////

#Remove all from collection
$emptycollection = getvalescaped("emptycollection","",true);
if($emptycollection!='' && getvalescaped("submitted","")=='removeall' && getval("removeall","")!="" && collection_writeable($emptycollection))
    {
    remove_all_resources_from_collection($emptycollection);
    }
    
# Disable info box for external access.
if ($k!="") {$infobox=false;} 
# Disable checkboxes for external users.
if ($k!="") {$use_checkboxes_for_selection=false;}

if(!isset($thumbs))
    {
    $thumbs=getval("thumbs","unset");
    if($thumbs == "unset")
        {
        $thumbs = $thumbs_default;
        rs_setcookie("thumbs", $thumbs, 1000,"","",false,false);
        }