}
# Check access
if (!$cinfo["request_feedback"]) {
    exit("Access denied.");
}
# Check that comments have been added.
$comments = get_collection_comments($collection);
if (count($comments) == 0 && $feedback_resource_select == false) {
    $errors = $lang["feedbacknocomments"];
}
if (getval("save", "") != "") {
    # Save comment
    $comment = trim(getvalescaped("comment", ""));
    send_collection_feedback($collection, $comment);
    # Stay on this page for external access users (no access to search)
    refresh_collection_frame();
    $done = true;
}
$headerinsert .= "<script src=\"../lib/lightbox/js/jquery.lightbox-0.5.min.js\" type=\"text/javascript\"></script>";
$headerinsert .= "<link type=\"text/css\" href=\"../lib/lightbox/css/jquery.lightbox-0.5.css?css_reload_key=" . $css_reload_key . "\" rel=\"stylesheet\">";
include "../include/header.php";
?>
<script type="text/javascript">
	jQuery(document).ready(function(){
		jQuery('.lightbox').lightBox(); 
	
	});

</script>
<div class="BasicsBox">
<h1><?php 
        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">
    <form method="post" action="<?php 
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();
}
</option><?php 
                }
                ?>
		
		<?php 
                if ($upload_force_mycollection) {
                    $list = get_user_collections($userref, "My Collection");
                } else {
                    $list = get_user_collections($userref);
                }
                $currentfound = false;
                // make sure it's possible to set the collection with collection_add (compact style "upload to this collection"
                if ($collection_add != "" && getval("resetform", "") == "" && (!isset($save_errors) || !$save_errors)) {
                    # Switch to the selected collection (existing or newly created) and refresh the frame.
                    set_user_collection($userref, $collection_add);
                    refresh_collection_frame($collection_add);
                }
                for ($n = 0; $n < count($list); $n++) {
                    if ($collection_dropdown_user_access_mode) {
                        $colusername = $list[$n]['fullname'];
                        # Work out the correct access mode to display
                        if (!hook('collectionaccessmode')) {
                            if ($list[$n]["public"] == 0) {
                                $accessmode = $lang["private"];
                            } else {
                                if (strlen($list[$n]["theme"]) > 0) {
                                    $accessmode = $lang["theme"];
                                } else {
                                    $accessmode = $lang["public"];
                                }
                            }
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();
	}