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

save_field_options()

Parameters

ColumnTypeDefaultDescription
$field

Location

include/resource_functions.php lines 4337 to 4446

Definition

 
function save_field_options($field)
    {
    
# Save the field options after editing.
    
global $languages,$defaultlanguage;
    
    
$fielddata=get_field($field);
    
$options=get_nodes($field);
    
//$options=trim_array(explode(",",$fielddata["options"]));

    
for ($n=0;$n<count($options);$n++)
        {
        
hook("before_save_field_options","",array($field,$options,$n));
        if (
getval("submit_field_" $n,"")!="")
            {
            
# This option/language combination is being renamed.

            # Construct a new option from the posted languages
            
$new="";$count=0;
            foreach (
$languages as $langcode=>$langname)
                {
                
$val=getvalescaped("field_" $langcode "_" $n,"");
                if (
$val!="") {$new.="~" $langcode ":" $val;$count++;}
                }
            
# Only one language, do not use language syntax.
            
if ($count==1) {$new=getvalescaped("field_" $defaultlanguage "_" $n,"");}
            
            
# Construct a new options value by creating a new array replacing the item in position $n
            
$newoptions=array_merge(array_slice($options,0,$n),array($new),array_slice($options,$n+1));

            foreach (
$newoptions as $no)
                {
                
set_node(null,$field,$no,null,null);
                }

            
# Loop through all matching resources.
            # The matches list uses 'like' so could potentially return values that do not have this option set. However each value list split out and analysed separately.
            
$matching=sql_query("select resource,value from resource_data where resource_type_field='$field' and value like '%" escape_check($options[$n]) . "%'");
            for (
$m=0;$m<count($matching);$m++)
                {
                
$ref=$matching[$m]["resource"];
                                
                
$set=trim_array(explode(",",$matching[$m]["value"]));
                
                
# Construct a new value omitting the old and adding the new.
                
$newval=array();
                for (
$s=0;$s<count($set);$s++)
                    {
                    if (
$set[$s]!==$options[$n]) {$newval[]=$set[$s];}
                    }
                
$newval[]=$new# Set the new value on the end of this string
                
$newval=join(",",$newval);
                
                
#echo "Old value = '" . $matching[$m]["value"] . "', new value = '" . $newval . "'";
                
                
if ($matching[$m]["value"]!== $newval)
                    {
                    
# Value has changed. Update.

                    # Delete existing keywords index for this field.
                    
sql_query("delete from resource_keyword where resource='$ref' and resource_type_field='$field'");
                    
                    
# Store value and reindex
                    
update_field($ref,$field,$newval);
                    }
                }
            
            }


        if (
getval("delete_field_" $n,"")!="")
            {
            
# This field option is being deleted.
            
            # Construct a new options value by creating a new array ommitting the item in position $n
            
$new=array_merge(array_slice($options,0,$n),array_slice($options,$n+1));
            
            foreach (
$new as $new_option)
                {
                
set_node(null,$field,escape_check(trim($new_option)),null,null);
                }
            
            
# Loop through all matching resources.
            # The matches list uses 'like' so could potentially return values that do not have this option set. However each value list split out and analysed separately.
            
$matching=sql_query("select resource,value from resource_data where resource_type_field='$field' and value like '%" escape_check($options[$n]) . "%'");
            for (
$m=0;$m<count($matching);$m++)
                {
                
$ref=$matching[$m]["resource"];
                                
                
$set=trim_array(explode(",",$matching[$m]["value"]));
                
$new=array();
                for (
$s=0;$s<count($set);$s++)
                    {
                    if (
$set[$s]!==$options[$n]) {$new[]=$set[$s];}
                    }
                
$new=join(",",$new);
                
                if (
$matching[$m]["value"]!== $new)
                    {
                    
# Value has changed. Update.

                    # Delete existing keywords index for this field.
                    
sql_query("delete from resource_keyword where resource='$ref' and resource_type_field='$field'");
                    
                    
# Store value and reindex
                    
update_field($ref,$field,$new);
                    }
                }
            }
        }
    }

This article was last updated 8th July 2020 11:35 Europe/London time based on the source file dated 7th July 2020 16:18 Europe/London time.