Collections functions
General functions
Render functions
Theme permission 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 8299 to 8360

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);
        }
    else if(!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
    // 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");
    
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 30th September 2022 18:05 Europe/London time based on the source file dated 30th September 2022 14:20 Europe/London time.