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

check_display_condition()

Description

Check display condition for a field.

Parameters

ColumnTypeDefaultDescription
$n integer Question sequence number on the rendered form
$field array Field on which we check display conditions
$fields array Resource field data and properties as returned by get_resource_field_data()
$render_js boolean Set to TRUE to render the client side code for checking display conditions or FALSE otherwise
$resource_ref integer Resource reference for which the display condition applies

Return

boolean Returns TRUE if no display condition or if field should be displayed or FALSE if field should not be displayed.

Location

include/render_functions.php lines 3832 to 4192

Definition

 
function check_display_condition($n, array $field, array $fields$render_jsint $resource_ref)
    {
    
debug_function_call(__FUNCTION__, [$n$field['ref'], ['ignored on purpose - too verbose'], $render_js]);
    global 
$required_fields_exempt$blank_edit_template$ref$use$FIXED_LIST_FIELD_TYPES;

    if(
trim((string) $field['display_condition']) == "")
        {
        return 
true;  # This field does not have a display condition, so it should be displayed
        
}

    
debug(sprintf('$use = %s'json_encode($use)));

    
// Assume the candidate field is to be displayed    
    
$displaycondition true;
    
// Break down into array of conditions
    
$conditions       explode(';'$field['display_condition']);
    
$condref          0;
    
$scriptconditions = array();
    
    
// Need all field data to check display conditions
    
global $display_check_data;
    if(!
is_array($display_check_data))
        {
        
$display_check_data get_resource_field_data($use,false,false);
        
debug('Loaded $display_check_data');
        }

    
// On upload, check against the posted nodes as save_resource_data() saves nodes after going through all the fields
    
$user_set_values getval('nodes', [], false'is_array');
    
debug(sprintf('$user_set_values = %s'json_encode($user_set_values)));

    foreach (
$conditions as $condition# Check each condition
        
{
        
debug(sprintf('field #%s - checking condition "%s"'$field['ref'], $condition));
        
$displayconditioncheck false;

        
// Break this condition down into fieldname $s[0] and value(s) $s[1]
        
$s explode('='$condition);

        
// Process all fields which are referenced by display condition(s) on the candidate field
        // For each referenced field, render javascript to trigger when the referenced field changes
        
for ($cf=0;$cf<count($display_check_data);$cf++) # Check each field to see if needs to be checked
            
{
            
// Work out nodes submitted by user, if any
            
$ui_selected_node_values = array();
            if(
                isset(
$user_set_values[$display_check_data[$cf]['ref']])
                && !
is_array($user_set_values[$display_check_data[$cf]['ref']])
                && 
$user_set_values[$display_check_data[$cf]['ref']] != ''
                
&& is_numeric($user_set_values[$display_check_data[$cf]['ref']])
            )
                {
                
$ui_selected_node_values[] = $user_set_values[$display_check_data[$cf]['ref']];
                
debug(sprintf('$ui_selected_node_values = %s'json_encode($ui_selected_node_values)));
                }
            elseif(isset(
$user_set_values[$display_check_data[$cf]['ref']]) && is_array($user_set_values[$display_check_data[$cf]['ref']]))
                {
                
$ui_selected_node_values $user_set_values[$display_check_data[$cf]['ref']];
                
debug(sprintf('$ui_selected_node_values = %s'json_encode($ui_selected_node_values)));
                }

            
// Does the fieldname on this condition match the field being processed
            
if($s[0] == $display_check_data[$cf]['name']) # this field needs to be checked
                
{
                
$display_check_data[$cf]['nodes'] = get_nodes($display_check_data[$cf]['ref'], null, (FIELD_TYPE_CATEGORY_TREE == $display_check_data[$cf]['type'] ? true false));

                
$scriptconditions[$condref]['field'] = $display_check_data[$cf]['ref'];
                
$scriptconditions[$condref]['type']  = $display_check_data[$cf]['type'];

                
$checkvalues=$s[1];
                
debug("\$checkvalues = {$checkvalues}");
                
// Break down values delimited with pipe characters
                
$validvalues explode("|",$checkvalues);
                
$validvalues array_map("i18n_get_translated",$validvalues);
                
debug(sprintf('$validvalues = %s'json_encode($validvalues)));
                
$scriptconditions[$condref]['valid'] = array();

                
// Use submitted values if field was shown and user has edit access to it
                
if(getval("field_" $fields[$n]['ref'] . "_displayed","") == "block" && metadata_field_edit_access($fields[$n]['ref']))
                    {
                    
$v $ui_selected_node_values;
                    }
                else
                    {
                    
$v trim_array(get_resource_nodes($ref$display_check_data[$cf]['ref']));
                    }

                
// If blank edit template is used, on upload form the dependent fields should be hidden
                
if($blank_edit_template && $ref && $use == $ref)
                    {
                    
$v = array();
                    }

                foreach(
$validvalues as $validvalue)
                    {
                    
$found_validvalue get_node_by_name($display_check_data[$cf]['nodes'], $validvalue);

                    if(
!= count($found_validvalue))
                        {
                        
$scriptconditions[$condref]['valid'][] = (string)$found_validvalue['ref'];

                        if(
in_array($found_validvalue['ref'], $v))
                            {
                            
$displayconditioncheck true;
                            }
                        }
                    }

                 if(!
$displayconditioncheck)
                    {
                    
$displaycondition false;
                    
$required_fields_exempt[]=$field["ref"];
                    }

                
// Skip rendering the JS calls to checkDisplayCondition functions
                // Skip if user does not have access to the master (parent) field 
                
if(!$render_js || !in_array($display_check_data[$cf]['ref'], array_column($fields,"ref")))
                    {
                    continue;
                    }

                
// Check display conditions
                // Certain fixed list types allow for multiple nodes to be passed at the same time

                // Generate a javascript function specific to the field with the display condition
                // This function will be invoked whenever a field referenced by the display condition changes
                
if(in_array($display_check_data[$cf]['type'], $FIXED_LIST_FIELD_TYPES))
                    {
                    if(
FIELD_TYPE_CATEGORY_TREE == $display_check_data[$cf]['type'])
                        {
                        
?>
                        <script>
                        jQuery(document).ready(function()
                            {
                            
                            
if($GLOBALS["multiple"] === false)
                                {
                                
?>
                                checkDisplayCondition echo $field['ref']; ?>();
                                
                                
}
                            
?>
                            jQuery('#CentralSpace').on('categoryTreeChanged', function(e,node)
                                {
                                checkDisplayCondition echo $field['ref']; ?>();
                                });
                            });
                        </script>
                        

                        
// Move on to the next field now
                        
continue;
                        }
                    elseif(
FIELD_TYPE_DYNAMIC_KEYWORDS_LIST == $display_check_data[$cf]['type'])
                        {
                        
?>
                        <script>
                        jQuery(document).ready(function()
                            {
                            
                            
if($GLOBALS["multiple"] === false)
                                {
                                
?>
                                console.debug('[document.ready] Going to call checkDisplayCondition echo $field['ref']; ?>()');
                                checkDisplayCondition echo $field['ref']; ?>();
                                
                                
}
                            
?>
                            jQuery('#CentralSpace').on('dynamicKeywordChanged', function(e,node)
                                {
                                console.debug('#CentralSpace-on-dynamicKeywordChanged for field # echo $field['ref']; ?>');
                                checkDisplayCondition echo $field['ref']; ?>();
                                });
                            });
                        </script>
                        

                        
// Move on to the next field now
                        
continue;
                        }

                    
$checkname "nodes[{$display_check_data[$cf]['ref']}][]";

                    if(
FIELD_TYPE_RADIO_BUTTONS == $display_check_data[$cf]['type'])
                        {
                        
$checkname "nodes[{$display_check_data[$cf]['ref']}]";
                        }

                    
$jquery_selector "input[name=\"{$checkname}\"]";

                    if(
FIELD_TYPE_DROP_DOWN_LIST == $display_check_data[$cf]['type'])
                        {
                        
$checkname       "nodes[{$display_check_data[$cf]['ref']}]";
                        
$jquery_selector "select[name=\"{$checkname}\"]";
                        }
                    
?>
                    <script type="text/javascript">
                    jQuery(document).ready(function()
                        {
                        
                        
if($GLOBALS["multiple"] === false)
                            {
                            
?>
                            checkDisplayCondition echo $field['ref']; ?>();
                            
                            
}
                        
?>
                        jQuery(' echo $jquery_selector?>').change(function ()
                            {
                            checkDisplayCondition echo $field['ref']; ?>();
                            });
                        });
                    </script>
                    
                    
}
                else
                    {
                    
?>
                    <script type="text/javascript">
                    jQuery(document).ready(function()
                        {
                        
                        
if($GLOBALS["multiple"] === false)
                            {
                            
?>
                            checkDisplayCondition echo $field['ref']; ?>();
                            
                            
}
                        
?>
                        jQuery('#field_ echo $display_check_data[$cf]["ref"]; ?>').change(function ()
                            {
                            checkDisplayCondition echo $field['ref']; ?>();
                            });
                        });
                    </script>
                    
                    
}
                }

            } 
# see if next field needs to be checked
        
$condref++;

        } 
# check next condition

    
if($render_js)
        {
        
$question_id '#question_' $n . ($GLOBALS["multiple"] === true '' '_' $resource_ref);
        
?>
        <script type="text/javascript">
        function checkDisplayCondition echo $field["ref"];?>()
            {
            console.debug('( echo str_replace(dirname(__DIR__), ''__FILE__) . ':' __LINE__?>) checkDisplayCondition echo $field["ref"]; ?>()');

            if (jQuery('#field_ echo (int) $field["ref"]; ?>_displayed').length === 0)
                {
                console.debug('Skipping - field  echo (int) $field["ref"]; ?> not found on form');
                return;
                }

            // Get current display state for governed field ("block" or "none")
            field echo $field['ref']; ?>status    = jQuery(' echo escape($question_id); ?>').css('display');
            newfield echo $field['ref']; ?>status = 'none';

            // Assume visible by default
            field echo $field['ref']; ?>visibility = true;
            
            
foreach($scriptconditions as $scriptcondition)
                {
                
/* Example of $scriptconditions:
                    [{"field":"73","type":"3","display_as_dropdown":"0","valid":["267","266"]}]
                */
                
?>

                field echo $field['ref']; ?>valuefound = false;
                fieldokvalues echo $scriptcondition['field']; ?> =  echo json_encode($scriptcondition['valid']); ?>;
                console.debug('[checkDisplayCondition echo $field["ref"]; ?>] fieldokvalues echo $scriptcondition['field']; ?> = %o', fieldokvalues echo $scriptcondition['field']; ?>);

                
                
############################
                ### Field type specific
                ############################
                
if(in_array($scriptcondition['type'], $FIXED_LIST_FIELD_TYPES))
                    {
                    
$jquery_condition_selector "input[name=\"nodes[{$scriptcondition['field']}][]\"]";
                    
$js_conditional_statement  "fieldokvalues{$scriptcondition['field']}.indexOf(element.value) != -1";

                    if(
FIELD_TYPE_CHECK_BOX_LIST == $scriptcondition['type'])
                        {
                        
$js_conditional_statement "element.checked && {$js_conditional_statement}";
                        }

                    if(
FIELD_TYPE_DROP_DOWN_LIST == $scriptcondition['type'])
                        {
                        
$jquery_condition_selector "select[name=\"nodes[{$scriptcondition['field']}]\"] option:selected";
                        }

                    if(
FIELD_TYPE_RADIO_BUTTONS == $scriptcondition['type'])
                        {
                        
$jquery_condition_selector "input[name=\"nodes[{$scriptcondition['field']}]\"]:checked";
                        }
                    
?>
                        jQuery(' echo $jquery_condition_selector?>').each(function(index, element)
                            {
                            if( echo $js_conditional_statement?>)
                                {
                                field echo $field['ref']; ?>valuefound = true;
                                }
                            });

                    
                    
}
                
?>
                if(!field echo $field['ref']; ?>valuefound)
                    {
                    field echo $field['ref']; ?>visibility = false;
                    }

                
}
                
?>

                // Is field to be displayed
                if(field echo $field['ref']; ?>visibility)
                    {
                    newfield echo $field['ref']; ?>status = 'block';
                    }

                // If display status changed then toggle the visibility
                if(newfield echo $field['ref']; ?>status != field echo $field['ref']; ?>status)
                    {
                    jQuery(' echo escape($question_id); ?>').css("display", newfield echo $field['ref']; ?>status); 
                    // The visibility status (block/none) will be sent to the server in the following field
                    jQuery('#field_ echo $field['ref']; ?>_displayed').attr("value",newfield echo $field['ref']; ?>status);

                
                
// Batch edit mode
                
if($GLOBALS["multiple"] === true)
                    {
                    
?>
                    var batch_edit_editthis = jQuery("# echo "editthis_{$n}"?>");
                    batch_edit_editthis.prop("checked", !batch_edit_editthis.prop("checked"));
                    batch_edit_toggle_edit_multi_checkbox_question( echo (int) $n?>);
                    
                    
}
                    
?>

                    if(jQuery(' echo escape($question_id); ?>').css('display') == 'block')
                        {
                        jQuery(' echo escape($question_id); ?>').css('border-top', '');
                        }
                    else
                        {
                        jQuery(' echo escape($question_id); ?>').css('border-top', 'none');
                        }
                    }
            }
        </script>
        
        
}

    return 
$displaycondition;
    }

This article was last updated 8th December 2024 20:35 Europe/London time based on the source file dated 5th December 2024 17:20 Europe/London time.