Collections functions
Encryption functions
General functions
Render functions
Theme permission functions
Video functions
Resource 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

Return

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

Location

include/render_functions.php lines 3353 to 3697

Definition

 
function check_display_condition($n, array $field, array $fields$render_js)
    {
    global 
$required_fields_exempt$blank_edit_template$ref$use$FIXED_LIST_FIELD_TYPES;

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

    
// 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);
        }

    
// On upload, check against the posted nodes as save_resource_data() saves nodes after going through all the fields
    
$user_set_values getval('nodes', array());

    foreach (
$conditions as $condition# Check each 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']];
                }
            else if(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']];
                }

            
// 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));

                
$node_options extract_node_options($display_check_data[$cf]['nodes']);

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

                
$checkvalues=$s[1];
                
// Break down values delimited with pipe characters
                
$validvalues=explode("|",mb_strtoupper($checkvalues));
                
$scriptconditions[$condref]['valid'] = array();
                
$v trim_array(get_resource_nodes($ref$display_check_data[$cf]['ref']));

                if(
count($ui_selected_node_values) > 0)
                    {
                    
$v $ui_selected_node_values;
                    }

                
// 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'][] = $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;
                        }
                    else if(
FIELD_TYPE_DYNAMIC_KEYWORDS_LIST == $display_check_data[$cf]['type'])
                        {
                        
?>
                        <script>
                        jQuery(document).ready(function()
                            {
                            
                            
if($GLOBALS["multiple"] === false)
                                {
                                
?>
                                checkDisplayCondition echo $field['ref']; ?>();
                                
                                
}
                            
?>
                            jQuery('#CentralSpace').on('dynamicKeywordChanged', function(e,node)
                                {
                                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)
        {
        
?>
        <script type="text/javascript">
        function checkDisplayCondition echo $field["ref"];?>()
            {
            // Get current display status
            field echo $field['ref']; ?>status    = jQuery('#question_ echo $n?>').css('display');
            // Assume field will not be displayed
            newfield echo $field['ref']; ?>status = 'none';
            newfield echo $field['ref']; ?>show   = false;
            newfield echo $field['ref']; ?>provisional = true;
            
            
foreach($scriptconditions as $scriptcondition)
                {
                
/*
                Example of $scriptcondition:
                Array
                    (
                    [field] => 73
                    [type] => 2
                    [valid] => Array
                        (
                            [0] => 267
                            [1] => 266
                        )
                    )
                */
                
?>
                newfield echo $field['ref']; ?>subcheck = false;
                fieldokvalues echo $scriptcondition['field']; ?> =  echo json_encode($scriptcondition['valid']); ?>;
                
                
############################
                ### Field type specific
                ############################
                
if(in_array($scriptcondition['type'], $FIXED_LIST_FIELD_TYPES))