}
set_time_limit(60 * 60 * 5);
echo "<pre>";
$resources = sql_query("select r.ref,u.username,u.fullname from \ncollection_resource left join resource r on collection_resource.resource = r.ref \nleft outer join user u on r.created_by=u.ref where collection_resource.collection = '{$collection}' order by ref");
for ($n = 0; $n < count($resources); $n++) {
    $ref = $resources[$n]["ref"];
    # Delete existing keywords
    sql_query("delete from resource_keyword where resource='{$ref}'");
    # Index fields
    $data = get_resource_field_data($ref);
    for ($m = 0; $m < count($data); $m++) {
        if ($data[$m]["keywords_index"] == 1) {
            #echo $data[$m]["value"];
            $value = $data[$m]["value"];
            if ($data[$m]["type"] == 3 || $data[$m]["type"] == 2) {
                # Prepend a comma when indexing dropdowns
                $value = "," . $value;
            }
            # Date field? These need indexing differently.
            $is_date = $data[$m]["type"] == 4 || $data[$m]["type"] == 6;
            add_keyword_mappings($ref, i18n_get_indexable($value), $data[$m]["ref"], $data[$m]["partial_index"], $is_date);
        }
    }
    # Also index contributed by field.
    add_keyword_mappings($ref, $resources[$n]["username"] . " " . $resources[$n]["fullname"], -1);
    # Always index the resource ID as a keyword
    remove_keyword_mappings($ref, $ref, -1);
    add_keyword_mappings($ref, $ref, -1);
    $words = sql_value("select count(*) value from resource_keyword where resource='{$ref}'", 0);
    echo "Done {$ref} ({$n}/" . count($resources) . ") - {$words} words<br />\n";
}
function reindex_resource($ref)
	{
	global $index_contributed_by;
	# Reindex a resource. Delete all resource_keyword rows and create new ones.
	
	# Delete existing keywords
	sql_query("delete from resource_keyword where resource='$ref'");

	# Index fields
	$data=get_resource_field_data($ref,false,false); # Fetch all fields and do not use permissions.
	for ($m=0;$m<count($data);$m++)
		{
		if ($data[$m]["keywords_index"]==1)
			{
			#echo $data[$m]["value"];
			$value=$data[$m]["value"];
			if ($data[$m]["type"]==3 || $data[$m]["type"]==2)
				{
				# Prepend a comma when indexing dropdowns
				$value="," . $value;
				}
			
			# Date field? These need indexing differently.
			$is_date=($data[$m]["type"]==4 || $data[$m]["type"]==6);
			
			add_keyword_mappings($ref,i18n_get_indexable($value),$data[$m]["ref"],$data[$m]["partial_index"],$is_date);		
			}
		}
	
	# Also index contributed by field, unless disabled
	if ($index_contributed_by)
		{
		$resource=get_resource_data($ref);
		$userinfo=get_user($resource["created_by"]);
		add_keyword_mappings($ref,$userinfo["username"] . " " . $userinfo["fullname"],-1);
		}

	# Always index the resource ID as a keyword
	remove_keyword_mappings($ref, $ref, -1);
	add_keyword_mappings($ref, $ref, -1);
	
	hook("afterreindexresource","all",array($ref));
	
	}
function updateResourcesFromContacts($resourcestoupdate,$rs_fields,$resref, $oldvals,$oldvalmatch){
    $resourceupdates = array(); $resources=array(); $rf=array(); $rv=array(); $toindex=array();//empty array vars

    //filter for only resources where this name matches the resource_field_type
    $filteredray = array();
    for ($ru=0; $ru<count($resourcestoupdate); $ru++){
        if($resourcestoupdate[$ru]['resource_type_field']==$resref){
            $filteredray[] = $resourcestoupdate[$ru];
        };
    };
    //loop through the filtered resources
    for($r=0; $r<count($filteredray); $r++){
        //for all of the resources and foreach mapped resource field
        foreach($rs_fields as $k => $v){
            if($v !=""){
                // Check to see if resource data exists for that field and resource
                $exists = sql_query("SELECT * FROM resource_data WHERE resource ='".$filteredray[$r]['resource']."' AND resource_type_field='".$k."'");
                //if it doesn't exist and the value has changed
                if(empty($exists) && !in_array($v,$oldvals[0])){
                    //push the data that needs to be INSERTED
                    $resourceupdates[]="(".$filteredray[$r]['resource'].",".$k.",'".$v."')";
                    $oldkey = matcholdkey($k,$oldvalmatch);
                    $toindex[]=$k.",'".$v."',".$filteredray[$r]['resource'].",'".$oldvals[0][$oldkey]."'";
                    // else if the value does exist and the value has changed
                }else if(!in_array($v,$oldvals[0])){
                    //push the data that needs to be UPDATED
                    $resources[]=$filteredray[$r]['resource'];
                    $rf[]=$k;
                    $rv[]=$v;
                    $oldkey=matcholdkey($k,$oldvalmatch);
                    $toindex[]=$k.",'".$v."',".$filteredray[$r]['resource'].",'".$oldvals[0][$oldkey]."'";
                };
            };
        };
    };//end filtered loop

    $updateq = "";
    //if there is data to INSERT build the statement
    if(!empty($resourceupdates)){
        sql_query("INSERT INTO resource_data (resource, resource_type_field, value) VALUES ". join(",",$resourceupdates));
    }
    //if there is data to UPDATE build the statement
    if(!empty($resources)){
        $updateq = "UPDATE resource_data SET value = CASE resource_type_field";
        foreach($rs_fields as $k => $v){
            if($v !=""){
                $updateq .= " WHEN $k THEN '$v' \n";
            }
        };
        $updateq .= "ELSE value END ";
        $updateq .= "WHERE resource IN (" . join(",",$resources).")";
        sql_query($updateq);//update
    }
    //add remove and index keywords
    for($ti = 0; $ti<count($toindex);$ti++){
        $toind = explode(",", $toindex[$ti]);
        remove_keyword_mappings($toind[2],$toind[3],$toind[0]);
        add_keyword_mappings($toind[2],$toind[1],$toind[0]);
    }
    $results=array();
    for($fr=0; $fr<count($filteredray); $fr++){
        $ref=$filteredray[$fr]['resource'];
        $results[]=get_resource_data($ref);
    }
    //elastic search
    $results=mia_results($results);
    $resource_types=get_resource_types();
    $results=mia_elastic_encode($resource_types,$results,false);
    for($e=0; $e<count($results); $e++){
        $resourcetype=get_resource_type_name($results[$e]['resource_type']);
        $ref=$results[$e]['ref'];
        push_RStoElastic($resourcetype,$ref,json_encode($results[$e]));
    }
};//end function
function update_field($resource, $field, $value)
{
    # Updates a field. Works out the previous value, so this is not efficient if we already know what this previous value is (hence it is not used for edit where multiple fields are saved)
    # accept shortnames in addition to field refs
    if (!is_numeric($field)) {
        $field = sql_value("select ref value from resource_type_field where name='" . escape_check($field) . "'", "");
    }
    # Fetch some information about the field
    $fieldinfo = sql_query("select keywords_index,resource_column,partial_index,type, onchange_macro, options from resource_type_field where ref='{$field}'");
    if (count($fieldinfo) == 0) {
        return false;
    } else {
        $fieldinfo = $fieldinfo[0];
    }
    # If this is a dynamic keyword we need to add it to the field options
    if ($fieldinfo['type'] == 9 && !checkperm('bdk' . $field)) {
        $fieldoptions = explode(",", $fieldinfo['options']);
        $currentoptions = array();
        foreach ($fieldoptions as $fieldoption) {
            $fieldoptiontranslations = explode("~", $fieldoption);
            if (count($fieldoptiontranslations) < 2) {
                $currentoptions[] = trim($fieldoption);
                # Not a translatable field
                debug("update_field: current field option: '" . trim($fieldoption) . "'<br>");
            } else {
                $default = "";
                for ($n = 1; $n < count($fieldoptiontranslations); $n++) {
                    # Not a translated string, return as-is
                    if (substr($fieldoptiontranslations[$n], 2, 1) != ":" && substr($fieldoptiontranslations[$n], 5, 1) != ":" && substr($fieldoptiontranslations[$n], 0, 1) != ":") {
                        $currentoptions[] = trim($fieldoption);
                        debug("update_field: current field option: '" . $fieldoption . "'<br>");
                    } else {
                        # Support both 2 character and 5 character language codes (for example en, en-US).
                        $p = strpos($fieldoptiontranslations[$n], ':');
                        $currentoptions[] = trim(substr($fieldoptiontranslations[$n], $p + 1));
                        debug("update_field: urrent field option: '" . trim(substr($fieldoptiontranslations[$n], $p + 1)) . "'<br>");
                    }
                }
            }
        }
        $newvalues = explode(",", $value);
        foreach ($newvalues as $newvalue) {
            # Check if each new value exists in current options list
            if (!in_array($newvalue, $currentoptions)) {
                # Append the option and update the field
                sql_query("update resource_type_field set options=concat(ifnull(options,''), ', " . escape_check(trim($newvalue)) . "') where ref='{$field}'");
                $currentoptions[] = trim($newvalue);
                debug("update_field: field option added: '" . trim($newvalue) . "'<br>");
            }
        }
    }
    # Fetch previous value
    $existing = sql_value("select value from resource_data where resource='{$resource}' and resource_type_field='{$field}'", "");
    if ($fieldinfo["keywords_index"]) {
        $is_html = $fieldinfo["type"] == 8;
        # If there's a previous value, remove the index for those keywords
        $existing = sql_value("select value from resource_data where resource='{$resource}' and resource_type_field='{$field}'", "");
        if (strlen($existing) > 0) {
            remove_keyword_mappings($resource, i18n_get_indexable($existing), $field, $fieldinfo["partial_index"], false, '', '', $is_html);
        }
        if (in_array($fieldinfo['type'], array(2, 3, 7, 9, 12)) && substr($value, 0, 1) != ',') {
            $value = ',' . $value;
        }
        $value = strip_leading_comma($value);
        # Index the new value
        add_keyword_mappings($resource, i18n_get_indexable($value), $field, $fieldinfo["partial_index"], false, '', '', $is_html);
    }
    # Delete the old value (if any) and add a new value.
    sql_query("delete from resource_data where resource='{$resource}' and resource_type_field='{$field}'");
    $value = escape_check($value);
    sql_query("insert into resource_data(resource,resource_type_field,value) values ('{$resource}','{$field}','{$value}')");
    if ($value == "") {
        $value = "null";
    } else {
        $value = "'" . $value . "'";
    }
    # If this is a 'joined' field we need to add it to the resource column
    $joins = get_resource_table_joins();
    if (in_array($field, $joins)) {
        if ($value != "null") {
            global $resource_field_column_limit;
            $truncated_value = substr($value, 0, $resource_field_column_limit);
            if (substr($truncated_value, -1) !== '\'') {
                $truncated_value .= '\'';
            }
        } else {
            $truncated_value = "null";
        }
        sql_query("update resource set field" . $field . "=" . $truncated_value . " where ref='{$resource}'");
    }
    # Add any onchange code
    if ($fieldinfo["onchange_macro"] != "") {
        eval($fieldinfo["onchange_macro"]);
    }
    # Allow plugins to perform additional actions.
    hook("update_field", "", array($resource, $field, $value, $existing));
}
Example #5
0
if ($ref == "") {
    die("no");
}
$top = getvalescaped('top', '');
$left = getvalescaped('left', '');
$width = getvalescaped('width', '');
$height = getvalescaped('height', '');
$text = getvalescaped('text', '');
$text = str_replace("<br />\n", " ", $text);
// remove the breaks added by get.php
$id = getvalescaped('id', '');
$preview_width = getvalescaped('pw', '');
$preview_height = getvalescaped('ph', '');
$oldtext = sql_value("select note value from annotate_notes where ref='{$ref}' and note_id='{$id}'", "");
if ($oldtext != "") {
    remove_keyword_mappings($ref, i18n_get_indexable($oldtext), -1, false, false, "annotation_ref", $id);
}
sql_query("delete from annotate_notes where ref='{$ref}' and note_id='{$id}'");
if (substr($text, 0, strlen($username)) != $username) {
    $text = $username . ": " . $text;
}
sql_query("insert into annotate_notes (ref,top_pos,left_pos,width,height,preview_width,preview_height,note,user) values ('{$ref}','{$top}','{$left}','{$width}','{$height}','{$preview_width}','{$preview_height}','{$text}','{$userref}') ");
$annotateid = sql_insert_id();
echo $annotateid;
$notes = sql_query("select * from annotate_notes where ref='{$ref}'");
sql_query("update resource set annotation_count=" . count($notes) . " where ref={$ref}");
#Add annotation to keywords
$keywordtext = substr(strstr($text, ": "), 2);
# don't add the username to the keywords
debug("adding annotation to resource keywords. Keywords: " . $keywordtext);
add_keyword_mappings($ref, i18n_get_indexable($keywordtext), -1, false, false, "annotation_ref", $annotateid);