Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource functions

delete_resource_type_field()

Description

Delete the specified metadata field. Also delets any node or resource_data rows associated with that field

Parameters

ColumnTypeDefaultDescription
$ref integer Metadata field id (ref from resource_type_field)
$varnames array Array of variable names

Return

boolean|string Returns true on success or text on failure describing error

Location

include/resource_functions.php lines 8635 to 8694

Definition

 
function delete_resource_type_field($ref)
    {
    global 
$lang$corefields$core_field_refs;

    if(
'cli' != php_sapi_name() && !checkperm('a'))
        {
        return 
$lang["error-permissiondenied"];
        }

    
$fieldvars = array();
    foreach (
$corefields as $scope=>$scopevars)
        {
        foreach(
$scopevars as $varname)
            {
            global $
$varname;
            if(isset($
$varname) && ((is_array($$varname) && in_array($ref,$$varname)) || (int)$$varname==$ref))
                {
                
$fieldvars[] = $varname . ($scope != "BASE" " (" $scope ")" "");
                }
            }
        }

    
// Prevent deleting a "core" field required by other parts of the system (e.g plugins)
    
$core_field_scopes = [];
    foreach(
$core_field_refs as $scope => $core_refs)
        {
        if(
in_array($ref$core_refs) && !in_array($scope$core_field_scopes))
            {
            
$core_field_scopes[] = $scope;
            }
        }

    if(
count($fieldvars) > 0)
        {
        return 
$lang["admin_delete_field_error"] . "<br/>\$" implode(", \$",$fieldvars);
        }
    elseif(!empty(
$core_field_scopes))
        {
        return 
sprintf('%s%s'$lang["admin_delete_field_error_scopes"], implode(', '$core_field_scopes));
        }


    
$fieldinfo get_resource_type_field($ref);

    
// Delete the resource type field
    
ps_query("DELETE FROM resource_type_field WHERE ref=?",["i",$ref]);

    
// Remove all nodes and keywords or resources. Always remove nodes last otherwise foreign keys will not work
    
ps_query("DELETE rn.* FROM resource_node rn LEFT JOIN node n ON n.ref=rn.node WHERE n.resource_type_field = ?",["i",$ref]);
    
ps_query("DELETE nk.* FROM node_keyword AS nk LEFT JOIN node AS n ON n.ref = nk.node WHERE n.resource_type_field = ?",["i",$ref]);
    
ps_query("DELETE FROM node WHERE resource_type_field = ?",["i",$ref]);

    
hook("after_delete_resource_type_field");

    
log_activity('Deleted metadata field "' $fieldinfo["title"] . '" (' $fieldinfo["ref"] . ')',LOG_CODE_DELETED,null,'resource_type_field',null,$ref);

    
clear_query_cache("schema");

    return 
true;
    }

This article was last updated 19th March 2024 10:35 Europe/London time based on the source file dated 11th March 2024 14:25 Europe/London time.