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();
}
<?php

#
# Reindex_collections.php
#
#
# Reindexes the collection index used for public searching.
#
include "../../include/db.php";
include "../../include/authenticate.php";
if (!checkperm("a")) {
    exit("Permission denied");
}
include "../../include/general.php";
include "../../include/resource_functions.php";
include "../../include/collections_functions.php";
include "../../include/image_processing.php";
set_time_limit(60 * 60 * 5);
echo "<pre>";
$collections = sql_query("select * from collection order by ref");
for ($n = 0; $n < count($collections); $n++) {
    $ref = $collections[$n]["ref"];
    $words = index_collection($ref);
    echo "Done {$ref} (" . ($n + 1) . "/" . count($collections) . ") - {$words} words<br />\n";
}