create_resource_type_field()

Description

Create a new resource type field with the specified name of the required type

Parameters

ColumnTypeDefaultDescription
$name string - name of new field
$restype integer|array 0 - resource type - resource type(s) that field applies to (0 = global, single value = one type, array = multiple types)
$type integer FIELD_TYPE_TEXT_BOX_SINGLE_LINE - field type - refer to include/definitions.php
$shortname string "" - shortname of new field
$index boolean false - should new field be indexed?

Return

boolean|integer - ref of new field, false if unsuccessful

Location

include/resource_functions.php lines 8032 to 8075

Definition

 
function create_resource_type_field($name$restype 0$type FIELD_TYPE_TEXT_BOX_SINGLE_LINE$shortname ""$index false)
{
    if ((
trim($name) == "") || !is_numeric($type) || !(is_numeric($restype) || is_array($restype))) {
        return 
false;
    }

    if (
trim($shortname) == "") {
        
$shortname mb_substr(mb_strtolower(str_replace(array("_""-"), ""safe_file_name($name))), 020);
    }

    
$duplicate = (bool) ps_value("SELECT count(ref) AS `value` FROM resource_type_field WHERE `name` = ?", array("s",$shortname), 0"schema");
    
$order_by_current ps_value("SELECT MAX(order_by) AS `value` FROM resource_type_field", [], 0"schema");
    
$default_tab ps_value("SELECT MIN(ref) value FROM tab", [], 0"schema");

    
// Global type?
    
if ($restype == 0) {
        
$global 1;
        
$restypes = [];
    } else {
        
$global 0;
        
$restypes is_array($restype) ? $restype : [$restype];
    }

    
ps_query(
        
"INSERT INTO resource_type_field (title, global, type, `name`, keywords_index, order_by, tab) VALUES (?, ?, ?, ?, ?, ?, ?)",
        array(
"s",$name,"i",$global,"i",$type,"s",$shortname,"i",($index "1" "0"),"i",$order_by_current 10,"i",$default_tab)
    );
    
$new sql_insert_id();

    
// Add joins
    
foreach ($restypes as $resinsert) {
        
ps_query("INSERT INTO resource_type_field_resource_type (resource_type_field,resource_type) VALUES (?, ?)", array("i",$new,"i",$resinsert));
    }

    if (
$duplicate) {
        
ps_query("UPDATE resource_type_field SET `name` = ? WHERE ref = ?", array("s",$shortname $new,"i",$new));
    }

    
log_activity(nullLOG_CODE_CREATED$name'resource_type_field''title'$newnull'');

    
clear_query_cache("schema");

    return 
$new;
}

This article was last updated 3rd June 2025 21:35 Europe/London time based on the source file dated 28th May 2025 12:00 Europe/London time.