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));
}
Esempio n. 2
0
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)

	# Fetch some information about the field
	$fieldinfo=sql_query("select keywords_index,resource_column,partial_index,type from resource_type_field where ref='$field'");
	if (count($fieldinfo)==0) {return false;} else {$fieldinfo=$fieldinfo[0];}
	
	if ($fieldinfo["keywords_index"])
		{
		# Fetch previous value and 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"]);
			}
		
		if (($fieldinfo['type'] == 2 || $fieldinfo['type'] == 3 || $fieldinfo['type'] == 7) && 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"]);
		}
		
	# 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' limit 1");
	$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)){
		sql_query("update resource set field".$field."=".$value." where ref='$resource'");
		}			
		
	}
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)
    # Fetch some information about the field
    $fieldinfo = sql_query("select keywords_index,resource_column,partial_index,type, onchange_macro from resource_type_field where ref='{$field}'");
    if (count($fieldinfo) == 0) {
        return false;
    } else {
        $fieldinfo = $fieldinfo[0];
    }
    # 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 (($fieldinfo['type'] == 2 || $fieldinfo['type'] == 3 || $fieldinfo['type'] == 7 || $fieldinfo['type'] == 9) && 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)) {
        global $resource_field_column_limit;
        sql_query("update resource set field" . $field . "=" . trim($value, $resource_field_column_limit) . " 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));
}