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

migrate_filter()

Parameters

ColumnTypeDefaultDescription
$filtertext
$allowpartialmigration false

Location

include/migration_functions.php lines 108 to 230

Definition

 
function migrate_filter($filtertext,$allowpartialmigration=false)
    {
    global 
$FIXED_LIST_FIELD_TYPES;
    if(
trim($filtertext) == "")
        {
        return 
false;
        }

    
$all_fields=get_resource_type_fields();

    
// Don't migrate if already migrated
    
$existingrules ps_query("SELECT ref, name FROM filter");
   
    
$logtext "FILTER MIGRATION: Migrating filter rule. Current filter text: '" $filtertext "'\n";
    
    
// Check for existing rule (will only match if name hasn't been changed)
    
$filterid array_search($filtertextarray_column($existingrules'name'));
    if(
$filterid !== false)
        {
        
$logtext .= "FILTER MIGRATION: - Filter already migrated. ID = " $existingrules[$filterid]["ref"] . "\n";
        return 
$existingrules[$filterid]["ref"];
        }
    else
        {
        
$truncated_filter_name mb_strcut($filtertext0200);

        
// Create filter. All migrated filters will have AND rules
        
ps_query("INSERT INTO filter (name, filter_condition) VALUES (?, ?)", array("s"$truncated_filter_name"i"RS_FILTER_ALL));
        
$filterid sql_insert_id();
        
$logtext .= "FILTER MIGRATION: - Created new filter. ID = " $filterid "'\n";
        }

    
$filter_rules explode(";",$filtertext);

    
$errors = array();
    
$n 1;
    foreach(
$filter_rules as $filter_rule)
        {
        
$rulevalid=false;
        
$logtext .= "FILTER MIGRATION: -- Parsing filter rule #" $n " : '" $filter_rule "'\n";
        
$rule_parts explode("=",$filter_rule);
        
$rulefields $rule_parts[0];
        if (isset(
$rule_parts[1])){$rulevalues explode("|",trim($rule_parts[1]));}
        else{
$errors[]="Invalid filter, no values are set.";return $errors;}

        
// Create filter_rule
        
$logtext .=  "FILTER MIGRATION: -- Creating filter_rule for '" $filter_rule "'\n";
        
ps_query("INSERT INTO filter_rule (filter) VALUES (?)", array("i"$filterid));
        
$new_filter_rule sql_insert_id();
        
$logtext .=  "FILTER MIGRATION: -- Created filter_rule # " $new_filter_rule "\n";
        
        
$nodeinsert = array(); // This will contain the SQL value sets to be inserted for this rule
        
$nodeinsertparams = array();

        
$rulenot substr($rulefields,-1) == "!";
        
$node_condition RS_FILTER_NODE_IN;
        if(
$rulenot)
            {
            
$rulefields substr($rulefields,0,-1);
            
$node_condition RS_FILTER_NODE_NOT_IN;
            }
                
        
// If there is an OR between the fields we need to get all the possible options (nodes) into one array    
        
$rulefieldarr explode("|",$rulefields); 
        
$all_valid_nodes = array();
        foreach(
$rulefieldarr as $rulefield)
            {
            
$all_fields_index array_search(mb_strtolower($rulefield), array_map("mb_strtolower"array_column($all_fields'name')));
            
$field_ref $all_fields[$all_fields_index]["ref"];
            
$field_type $all_fields[$all_fields_index]["type"];
            
$logtext .= "FILTER MIGRATION: --- filter field name: '" $rulefield"' , field id #" $field_ref "\n";

            if(!
in_array($field_type,$FIXED_LIST_FIELD_TYPES))
                {
                
$errors[] = "Invalid field  '" $field_ref "' specified for rule: '" $filtertext "', skipping"
                
$logtext .=  "FILTER MIGRATION: --- Invalid field  '" $field_ref "', skipping\n";
                continue;
                }

            
$field_nodes get_nodes($field_refnull, (FIELD_TYPE_CATEGORY_TREE == $field_type true false));
            
$all_valid_nodes array_merge($all_valid_nodes,$field_nodes);
            }
            
        foreach(
$rulevalues as $rulevalue)
            {
            
// Check for value in field options
            
$logtext .=  "FILTER MIGRATION: --- Checking for filter rule value : '" $rulevalue "'\n";
            
$nodeidx array_search(mb_strtolower($rulevalue), array_map("mb_strtolower"array_column($all_valid_nodes'name')));
                    
            if(
$nodeidx !== false)
                {                       
                
$nodeid $all_valid_nodes[$nodeidx]["ref"];
                
$logtext .=  "FILTER MIGRATION: --- field option (node) exists, node id #: " $all_valid_nodes[$nodeidx]["ref"] . "\n";

                
$nodeinsert[] = "(?, ?, ?)";
                
$nodeinsertparams array_merge($nodeinsertparams, array("i"$new_filter_rule"i"$nodeid"i"$node_condition));
                if(
$allowpartialmigration){$rulevalid true;} // Atleast one rule is valid so the filter can be created
                
}
            else
                {
                
$errors[] = "Invalid field option '" $rulevalue "' specified for rule: '" $filtertext "', skipping"
                
$logtext .=  "FILTER MIGRATION: --- Invalid field option: '" $rulevalue "', skipping\n";
                }
            }

        
debug($logtext);       
        if(
count($errors) > && !$rulevalid)
            {
            
delete_filter($filterid);
            return 
$errors;
            }
            
        
// Insert associated filter_rules
        
$logtext .=  "FILTER MIGRATION: -- Adding nodes to filter_rule\n";
        
$sql "INSERT INTO filter_rule_node (filter_rule,node,node_condition) VALUES " implode(',',$nodeinsert);
        
ps_query($sql$nodeinsertparams);
        }
        
    
debug("FILTER MIGRATION: filter migration completed for '" $filtertext);
    
$logtext .= "FILTER MIGRATION: filter migration completed for '" $filtertext "\n";
    
    return 
$filterid;
    }

This article was last updated 4th December 2024 08:35 Europe/London time based on the source file dated 15th April 2024 16:05 Europe/London time.