function add_saved_search_items($collection)
{
    # Adds resources from a search to the collection.
    $results = do_search(getvalescaped("addsearch", ""), getvalescaped("restypes", ""), "relevance", getvalescaped("archive", "", true), -1, '', false, getvalescaped("starsearch", ""), false, false, getvalescaped("daylimit", ""));
    # Check if this collection has already been shared externally. If it has, we must add a further entry
    # for this specific resource, and warn the user that this has happened.
    $keys = get_collection_external_access($collection);
    $resourcesnotadded = array();
    # record the resources that are not added so we can display to the user
    if (count($keys) > 0) {
        # Set the flag so a warning appears.
        global $collection_share_warning, $collection_allow_not_approved_share;
        $collection_share_warning = true;
        for ($n = 0; $n < count($keys); $n++) {
            # Insert a new access key entry for this resource/collection.
            global $userref;
            for ($r = 0; $r < count($results); $r++) {
                $resource = $results[$r]["ref"];
                $archivestatus = $results[$r]["archive"];
                if ($archivestatus < 0 && !$collection_allow_not_approved_share) {
                    $resourcesnotadded[$resource] = $results[$r];
                    continue;
                }
                sql_query("insert into external_access_keys(resource,access_key,user,collection,date) values ('{$resource}','" . escape_check($keys[$n]["access_key"]) . "','{$userref}','{$collection}',now())");
                #log this
                collection_log($collection, "s", $resource, $keys[$n]["access_key"]);
            }
        }
    }
    if (is_array($results)) {
        $modifyNotAdded = hook('modifynotaddedsearchitems', '', array($results, $resourcesnotadded));
        if (is_array($modifyNotAdded)) {
            $resourcesnotadded = $modifyNotAdded;
        }
        for ($n = 0; $n < count($results); $n++) {
            $resource = $results[$n]["ref"];
            if (!isset($resourcesnotadded[$resource])) {
                sql_query("delete from collection_resource where resource='{$resource}' and collection='{$collection}'");
                sql_query("insert into collection_resource(resource,collection) values ('{$resource}','{$collection}')");
            }
        }
    }
    if (!empty($resourcesnotadded)) {
        # Translate to titles only for displaying them to the user
        global $view_title_field;
        $titles = array();
        foreach ($resourcesnotadded as $resource) {
            $titles[] = i18n_get_translated($resource['field' . $view_title_field]);
        }
        return $titles;
    }
    return array();
}
			}
			?>
			</textarea>
			<?php if ($unapproved_collection){?><script>alert('<?php echo $lang['notapprovedsharetheme']?>');</script><?php } ?>
			</p>
			<?php
			}
		}
	//Display existing shares for collections in theme

	if ($access=="")
		{
		foreach($collectionstoshare as $collection)
			{			
			$ref=$collection["ref"];
			$keys=get_collection_external_access($ref);
			?>
			<p>&nbsp;</p>
			<h2><?php echo str_replace("%collectionname%", i18n_get_collection_name($collection), $lang["externalusersharing-name"]);?></h2>
			<div class="Question">
			<?php
			if (count($keys)==0)
				{
				?>
				<p><?php echo $lang["noexternalsharing"] ?></p>
				<?php
				}
			else
				{
				?>
				<div class="Listview">
function add_saved_search_items($collection)
	{
	# Adds resources from a search to the collection.
	$results=do_search(getvalescaped("addsearch",""), getvalescaped("restypes",""), "relevance", getvalescaped("archive","",true));

	# Check if this collection has already been shared externally. If it has, we must add a further entry
	# for this specific resource, and warn the user that this has happened.
	$keys=get_collection_external_access($collection);
	$resourcesnotadded=array(); # record the resources that are not added so we can display to the user
	if (count($keys)>0)
		{
		# Set the flag so a warning appears.
		global $collection_share_warning;
		$collection_share_warning=true;
		
		for ($n=0;$n<count($keys);$n++)
			{
			# Insert a new access key entry for this resource/collection.
			global $userref;
			
			for ($r=0;$r<count($results);$r++)
				{
				$resource=$results[$r]["ref"];
				$archivestatus=$results[$r]["archive"];
				if ($archivestatus<0 && !$collection_allow_not_approved_share) {array_push($resourcesnotadded,$resource);continue;}
				sql_query("insert into external_access_keys(resource,access_key,user,collection,date) values ('$resource','" . escape_check($keys[$n]["access_key"]) . "','$userref','$collection',now())");
				#log this
				collection_log($collection,"s",$resource, $keys[$n]["access_key"]);
				}
			}
		}

	if (is_array($results))
                {
                for ($n=0;$n<count($results);$n++)
                        {
                        $resource=$results[$n]["ref"];
       			if (!in_array($resource,$resourcesnotadded))
				{
				sql_query("delete from collection_resource where resource='$resource' and collection='$collection'");
				sql_query("insert into collection_resource(resource,collection) values ('$resource','$collection')");
				}
                        }
                }
	return $resourcesnotadded;
	}
function add_saved_search_items($collection)
{
    # Adds resources from a search to the collection.
    $results = do_search(getvalescaped("addsearch", ""), getvalescaped("restypes", ""), "relevance", getvalescaped("archive", "", true));
    if (is_array($results)) {
        for ($n = 0; $n < count($results); $n++) {
            $resource = $results[$n]["ref"];
            sql_query("delete from collection_resource where resource='{$resource}' and collection='{$collection}'");
            sql_query("insert into collection_resource(resource,collection) values ('{$resource}','{$collection}')");
        }
    }
    # Check if this collection has already been shared externally. If it has, we must add a further entry
    # for this specific resource, and warn the user that this has happened.
    $keys = get_collection_external_access($collection);
    if (count($keys) > 0) {
        # Set the flag so a warning appears.
        global $collection_share_warning;
        $collection_share_warning = true;
        for ($n = 0; $n < count($keys); $n++) {
            # Insert a new access key entry for this resource/collection.
            global $userref;
            for ($r = 0; $r < count($results); $r++) {
                $resource = $results[$r]["ref"];
                sql_query("insert into external_access_keys(resource,access_key,user,collection,date) values ('{$resource}','" . escape_check($keys[$n]["access_key"]) . "','{$userref}','{$collection}',now())");
                #log this
                collection_log($collection, "s", $resource, $keys[$n]["access_key"]);
            }
        }
    }
}
                }
            });
                
            uploader.start();
        } else {
            alert('You must queue at least one file.');
        }

        return false;
    });
		
});
	
<?php 
# If adding to a collection that has been externally shared, show a warning.
if ($collection_add != "" && count(get_collection_external_access($collection_add)) > 0) {
    # Show warning.
    ?>
alert("<?php 
    echo $lang["sharedcollectionaddwarningupload"];
    ?>
");<?php 
}
?>
    
		
</script>

<?php 
# Add language support if available
if (file_exists("../lib/plupload/i18n/" . $language . ".js")) {
?>
</div>

<script>usercollection='<?php 
echo htmlspecialchars($collection);
?>
';</script>
<?php 
$add = getvalescaped("add", "");
if ($add != "") {
    $allowadd = true;
    // If we provide a collection ID use that one instead
    $to_collection = getvalescaped('toCollection', '');
    if (checkperm("noex")) {
        // If collection has been shared externally users with this permission can't add resources
        $externalkeys = get_collection_external_access($to_collection === '' ? $usercollection : $to_collection);
        if (count($externalkeys) > 0) {
            $allowadd = false;
            ?>
				<script language="Javascript">alert("<?php 
            echo $lang["sharedcollectionaddblocked"];
            ?>
");</script>
				<?php 
        }
    }
    if ($allowadd) {
        if (strpos($add, ",") > 0) {
            $addarray = explode(",", $add);
        } else {
            $addarray[0] = $add;