Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test functions

put_resource_data()

Description

Updates $resource with the name/value pairs in $data - this relates to the resource table column, not metadata.

Parameters

ColumnTypeDefaultDescription
$resource int ID of resource
$data array Array of data to be applied to resource

Return

boolean

Location

include/resource_functions.php lines 522 to 587

Definition

 
function put_resource_data($resource,$data)
    {
    global 
$edit_contributed_by;

    
// Check access
    
if (!get_edit_access($resource)) {return false;}

    
// Get current resource data
    
$currentdata get_resource_data($resource);

    
// Define safe columns
    
$safe_columns = ["resource_type","creation_date","rating","user_rating","archive","access","mapzoom","modified","geo_lat","geo_long","no_file"];
    
$log_columns = [
        
"resource_type" => LOG_CODE_EDITED_RESOURCE,
        
"access" => LOG_CODE_ACCESS_CHANGED,
        
"archive" => LOG_CODE_STATUS_CHANGED,
        
"creation_date" => LOG_CODE_EDITED_RESOURCE,
        
"geo_lat" => LOG_CODE_EDITED_RESOURCE,
        
"geo_long" => LOG_CODE_EDITED_RESOURCE,
        
"no_file" => [=> LOG_CODE_UNSET_NO_FILE=> LOG_CODE_SET_NO_FILE],
        
"locked" => [=> LOG_CODE_UNLOCKED=> LOG_CODE_LOCKED],
    ];
    
$safe_column_types=array("i","s","d","i","i","i","d","s","s","s","i");

    
// Permit the created by column to be changed also
    
if (checkperm("v") && $edit_contributed_by) {$safe_columns[]="created_by";$safe_column_types[]='i';}

    
$sql="";$params=array();
    
$logupdates = [];
    foreach (
$data as $column=>$value) {
        if (!
in_array($column,$safe_columns)) {
            
// Attempted to update a column outside of the expected set
            
return false;
        }
        if (isset(
$currentdata[$column]) && $value == $currentdata[$column]) {
            
// No change
            
continue;
        }
        if (
$sql!="") {$sql.=",";}
        
$sql.=$column "=?";
        
$params[]=$safe_column_types[array_search($column,$safe_columns)]; // Fetch type to use
        
$params[]=$value;
        
// Add to $logupdates
        
if (isset($log_columns[$column])) {
            
// Set log value and type
            
$logupdates[] = [
                
$log_columns[$column][$value] ?? $log_columns[$column], // Log code
                
$column// Log note
                
$currentdata[$column], // From value
                
$value// To value
            
];
        }
    }

    if (
$sql=="") {return false;} // Nothing to do.
    
$params[]="i";$params[]=$resource;
    
ps_query("UPDATE resource SET $sql WHERE ref=?",$params);
    if (
count($logupdates) > 0) {
        
db_begin_transaction("resource_log_updates");
        foreach(
$logupdates as $logupdate) {
            
resource_log($resource,$logupdate[0],0,$logupdate[1],$logupdate[2],$logupdate[3]);
        }
        
db_end_transaction("resource_log_updates");
    }
    return 
true;
    }

This article was last updated 17th November 2024 15:05 Europe/London time based on the source file dated 15th November 2024 18:30 Europe/London time.