BuddyDev

Search

BuddyDev Friends Suggestions Pro “like/not like”

  • Participant
    Level: Initiated
    Posts: 11
    Filip Alexander on #54997

    Hi Brajesh,

    thank you so much, this works just perfectly!! You’re amazing! I will keep in mind it could get resource heavy and change the setup in case my user base would grow a lot, but for now I don’t rly have that many users so it should be okay for some time. I really really appreciate you taking your time to create a solution for me.
    I’m now trying to work on the nested conditions (as i mentioned before in my first post) and i came up with something like this:

     function bp_friends_suggestions_pro_get_xprofile_query($suggestion_rule_id) {
        if (!is_user_logged_in() || !$suggestion_rule_id || empty(get_post_meta($suggestion_rule_id, '_is_active', true))) {
            return array();
        }
    
        if (!bp_is_active('xprofile')) {
            return array();
        }
    
        $user_id = bp_loggedin_user_id();
        $matching_rules = (array) get_post_meta($suggestion_rule_id, '_matching_rules', true);
        
        // New meta field for rule grouping
        $rule_groups = (array) get_post_meta($suggestion_rule_id, '_rule_groups', true);
        
        if (empty($matching_rules)) {
            return array();
        }
    
        $xp_query = array();
        $mandatory_rules = array();
        $optional_rules = array();
    
        // Split rules into mandatory and optional based on group settings
        foreach ($matching_rules as $index => $rule) {
            if (empty($rule['field_id']) || empty($rule['match_field_id']) || empty($rule['operator'])) {
                continue;
            }
    
            $value = maybe_unserialize(BP_XProfile_ProfileData::get_value_byid($rule['field_id'], $user_id));
            
            // Determine comparison operator
            switch ($rule['operator']) {
                case '=':
                case '!=':
                case 'LIKE':
                case 'NOT LIKE':
                    $compare = $rule['operator'];
                    break;
                case '<':
                    $compare = '>'; // opposite
                    break;
                case '<=':
                    $compare = '>=';
                    break;
                case '>':
                    $compare = '<';
                    break;
                case '>=':
                    $compare = '<=';
                    break;
                default:
                    $compare = null;
                    break;
            }
    
            if (!$compare) {
                continue;
            }
    
            // Handle array values
            if (is_array($value) && !in_array($compare, array('IN', 'NOT IN'))) {
                if (count($value) === 1 && !bp_friends_suggestions_pro_is_multi_valued_field($rule['match_field_id'])) {
                    $value = array_pop($value);
                } else {
                    $value = maybe_serialize($value);
                }
            }
    
            $query_item = array(
                'field' => $rule['match_field_id'],
                'value' => $value,
                'compare' => $compare
            );
    
            // Special handling for field_id 43
            if ($rule['field_id'] == '43') {
                $queries = bpfscustom_get_multiselect_field_query(
                    $user_id, 
                    $rule['field_id'], 
                    $rule['match_field_id'], 
                    $compare
                );
                if (!empty($queries)) {
                    if (isset($rule_groups[$index]) && $rule_groups[$index] === 'mandatory') {
                        $mandatory_rules = array_merge($mandatory_rules, $queries);
                    } else {
                        $optional_rules = array_merge($optional_rules, $queries);
                    }
                }
                continue;
            }
    
            // Add to appropriate group based on rule_groups setting
            if (isset($rule_groups[$index]) && $rule_groups[$index] === 'mandatory') {
                $mandatory_rules[] = $query_item;
            } else {
                $optional_rules[] = $query_item;
            }
        }
    
        // Build nested query structure
        if (!empty($mandatory_rules)) {
            $xp_query[] = array(
                'relation' => 'AND',
                'rules' => $mandatory_rules
            );
        }
    
        if (!empty($optional_rules)) {
            $xp_query[] = array(
                'relation' => 'OR',
                'rules' => $optional_rules
            );
        }
    
        if (!empty($xp_query)) {
            $xp_query['relation'] = 'AND';
        }
    
        return $xp_query;
    } 

    and then this:

    // Example of how to store rule groups
    $rule_groups = array(
        0 => 'mandatory', // First rule is mandatory
        1 => 'mandatory', // Second rule is mandatory
        2 => 'optional',  // Third rule is optional
        3 => 'optional',  // Fourth rule is optional
        4 => 'optional'   // Fifth rule is optional
    );
    update_post_meta($suggestion_rule_id, '_rule_groups', $rule_groups); 

    Could you please just tell me what you think of this approach? I originally wanted to incorporate the ability to choose whether the condition is mandatory or optional into the form, but I just couldn’t rly manage. I would love any form of feedback from you, but it’s totally okay if you don’t have a time/space for it, as im already extremely grateful for your help regarding the multi select.

    Best regards
    Alex

You must be logged in to reply to this topic.

This topic is: not resolved