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

process_if_statements()

Description

Process a string (mainly HTML) which contains if statement placeholders and return the processed string

Handles [%if var is set%] [%endif%] type of placeholders

placeholders found in the HTML template

Parameters

ColumnTypeDefaultDescription
$original_string string Full string containing placeholders
$bind_params array A map of all the values that are meant to replace any

Return

string

Location

include/pdf_functions.php lines 296 to 351

Definition

 
function process_if_statements($original_string, array $bind_params)
    {
    
$remove_placeholder_elements = array('[%if '' is set%]');

    
preg_match_all('/\[%if (.*?) is set%\]/'$original_string$if_isset_matches);
    foreach(
$if_isset_matches[0] as $if_isset_match)
        {
        
$var_name str_replace($remove_placeholder_elements''$if_isset_match);

        
$if_isset_match_position strpos($original_string$if_isset_match);
        
$endif_position          strpos($original_string'[%endif%]'$if_isset_match_position);
        
$substr_lenght           $endif_position $if_isset_match_position;

        
$substr_html_one   substr($original_string0$if_isset_match_position);
        
$substr_html_two   substr($original_string$if_isset_match_position$substr_lenght 9);
        
$substr_html_three substr($original_string$endif_position 9);

        
/*
        Make sure we have the correct subset (html2) for our if statement. This means we don't
        stop at the first endif we found unless there are no if statements inside the subset.
        If there are, move passed it and continue looking for other endifs until we reach the
        same number of endifs as we had ifs
        */
        
$endif_count 0;
        do
            {
            
$if_count preg_match_all('/\[%if (.*?) is set%\]/'$substr_html_two$if_isset_matches_subset_two) - 1;
            if(
$if_count)
                {
                
// Move the end of the second subset of HTML to the next endif and then increase endif counter
                
$next_endif_position strpos($substr_html_three'[%endif%]') + 9;
                
$substr_html_two    .= substr($substr_html_three0$next_endif_position);
                
$substr_html_three   substr($substr_html_three$next_endif_position);

                
$endif_count++;
                }
            }
        while(
$if_count !== $endif_count);

        
// Variable is not set, remove that section from the template
        
if(!array_key_exists($var_name$bind_params))
            {
            
$original_string $substr_html_one $substr_html_three;

            continue;
            }

        
// The section stays in, clean it up of the top level if - endif placeholders
        
$substr_html_two substr($substr_html_twostrlen($if_isset_match));
        
$substr_html_two substr($substr_html_two0, -9);

        
$original_string $substr_html_one $substr_html_two $substr_html_three;
        }

    return 
$original_string;
    }

This article was last updated 17th November 2024 15:05 Europe/London time based on the source file dated 8th November 2024 16:00 Europe/London time.