This topic has 6 replies, 3 voices, and was last updated 10 years by bhozar.

  • Author
  • #20618
     Adam
    Participant

    This asks a user if they’d like to auto-filter the members page based on their (and other members) profile answers, so far, only around gender.

    I’m looking for a way to avoid refreshing with window.location to get the desired results. Also, with what I have here the page 2, 3, 4 etc end up loading the next set of members but then reverting to the first page (redirect is happening but shouldn’t be.)

    Any suggestions?

    COPY CODE
    /************** AUTO FILTER MEMBERS PAGE ******/
    
    add_action('bp_before_members_loop', 'mm_autofilter_members');
    
    function mm_autofilter_members() {
    
      if(!isset($_GET["field_4"])) {
      // This
        if(is_user_logged_in()) {
          
          global $bp;
          $loggedin_user_id = $bp->loggedin_user->id;
        
          // User has most basic filtering data saved in profile
          if (xprofile_get_field_data('Looking to meet', $loggedin_user_id )) {
        
            $user_looking_for = xprofile_get_field_data('Looking to meet', $loggedin_user_id );
            
            $user_looking_urlparam = (xprofile_get_field_data('Looking to meet', $loggedin_user_id ) == 'Men' ? 'Male' : 'Female' );
            
            $user_sex = xprofile_get_field_data('Sex', $loggedin_user_id );
            
            $translated_sex = (xprofile_get_field_data('Sex', $loggedin_user_id ) == 'Male' ? 'Men' : 'Women' );
            
            $reloadurl = '/members?field_4='; // this approach sucks
            
            // TODO what is this?
            $bsplus = '&bs=+';  
            
            if(!isset($_COOKIE['autofilter'])) {
            echo "
              <script type='text/javascript'>
                if(window.confirm('Should we only show you " . $user_looking_for . " looking for " . $translated_sex . "?')) {
                  
                  document.cookie='autofilter=true; expires=Thu, 18 Dec 2015 12:00:00 GMT; path=/';
                  window.location = '" . $reloadurl . $user_looking_urlparam . $bsplus . "';
                };
              </script>
            ";
            } else {
              
                echo "
                <script type='text/javascript'>
                  window.location = '" . $reloadurl . $user_looking_urlparam . $bsplus . "';
                </script>
                ";
                
            }
            
          }
          
        } else {
          return false;
        }
      } else {
        return false; 
      }
    } 
    #20623
     Adam
    Participant

    I changed that up to this. Does anyone know why this only works when I EXCLUDE based on the query? I expected this to select IDs to INCLUDE but it works perfectly inverse..

    COPY CODE
    
    /************** AUTO FILTER MEMBERS PAGE ******/
    
    add_action('bp_before_members_loop', 'mm_autofilter_members'); 
    
    function mm_autofilter_members() {
      
      // Only offer feature to logged in users who havent yet answered prompt and are not executing a search already..
      if(is_user_logged_in() && !isset($_GET["field_4"]) && $_COOKIE['mm_autofilter'] != 'true' ): 
        
        // Prepare info to be saved to cookies
        global $bp;
        $loggedin_user_id = $bp->loggedin_user->id;
        
        $user_sex = xprofile_get_field_data('Sex', $loggedin_user_id );    
        
        $user_looking_for = xprofile_get_field_data('Looking to meet', $loggedin_user_id );
        
        $translated_user_sex = ($user_sex == 'Male' ? 'Men' : 'Women' );
        
        $translated_user_looking_for = ($user_looking_for == 'Men' ? 'Male' : 'Female' );
        
        $expirestring = 'Thu, 18 Dec 2015 12:00:00 GMT; path=/';
    
        ?>
        <script>
          if(confirm('Only show <?php echo $user_looking_for ?>?')) {
            document.cookie='mm_autofilter=true; expires=<? echo $expirestring; ?>';
            document.cookie='mm_results_looking_for=<? echo $translated_user_sex; ?>; expires=<? echo $expirestring; ?>';
            document.cookie='mm_results_gender_is=<? echo $translated_user_looking_for; ?>; expires=<? echo $expirestring; ?>';
    
            window.location = '/members';
          } else {
            document.cookie='mm_autofilter=false; expires=<? echo $expirestring; ?>';
          }
        </script>
      
      <?php
      endif;
      
    }
    
    
    
    
    function mm_saved_search_params() {
      
      if($_COOKIE['mm_autofilter'] == 'true') {
        
        return mm_custom_ids('Sex', $_COOKIE['mm_results_gender_is']);
            
      } else {
        return '';
      }
    }
    
    
    
    function mm_custom_ids( $field_name, $field_value = '' ) {
      
      if ( empty( $field_name ) )
        return '';
      
      global $wpdb;
      
      $field_id = xprofile_get_field_id_from_name( $field_name ); 
     
      if ( !empty( $field_id ) ) 
        $query = "SELECT user_id FROM " . $wpdb->prefix . "bp_xprofile_data WHERE field_id = " . $field_id;
      else
       return '';
      
      if ( $field_value != '' ) 
        $query .= " AND value = '" . $field_value . "'";
      
      $custom_ids = $wpdb->get_col( $query );
      
      if ( !empty( $custom_ids ) ) {
        // convert the array to a csv string
        // had to switch to exclude here??
        $custom_ids_str = 'exclude=' . implode(",", $custom_ids);
        return $custom_ids_str;
      }
      else
       return '';
       
    }
    
    #20624
     Adam
    Participant
    COPY CODE
    /************** AUTO FILTER MEMBERS PAGE ******/
    
    add_action('bp_before_members_loop', 'mm_autofilter_members'); 
    
    function mm_autofilter_members() {
      
      // Only offer feature to logged in users who havent yet answered prompt and are not executing a search already..
      if(is_user_logged_in() && !isset($_GET["field_4"]) && $_COOKIE['mm_autofilter'] != 'true' ): 
        
        // Prepare info to be saved to cookies
        global $bp;
        $loggedin_user_id = $bp->loggedin_user->id;
        
        $user_sex = xprofile_get_field_data('Sex', $loggedin_user_id );    
        
        $user_looking_for = xprofile_get_field_data('Looking to meet', $loggedin_user_id );
        
        $translated_user_sex = ($user_sex == 'Male' ? 'Men' : 'Women' );
        
        $translated_user_looking_for = ($user_looking_for == 'Men' ? 'Male' : 'Female' );
        
        $expirestring = 'Thu, 18 Dec 2015 12:00:00 GMT; path=/';
    
        ?>
        <script>
          if(confirm('Only show <?php echo $user_looking_for ?>?')) {
            document.cookie='mm_autofilter=true; expires=<? echo $expirestring; ?>';
            document.cookie='mm_results_looking_for=<? echo $translated_user_sex; ?>; expires=<? echo $expirestring; ?>';
            document.cookie='mm_results_gender_is=<? echo $translated_user_looking_for; ?>; expires=<? echo $expirestring; ?>';
    
            window.location = '/members';
          } else {
            document.cookie='mm_autofilter=false; expires=<? echo $expirestring; ?>';
          }
        </script>
      
      <?php
      endif;
      
    }
    
    function mm_saved_search_params() {
      
      if($_COOKIE['mm_autofilter'] == 'true') {
        
        return mm_custom_ids('Sex', $_COOKIE['mm_results_gender_is']);
            
      } else {
        return '';
      }
    }
    
    function mm_custom_ids( $field_name, $field_value = '' ) {
      
      if ( empty( $field_name ) )
        return '';
      
      global $wpdb;
      
      $field_id = xprofile_get_field_id_from_name( $field_name ); 
     
      if ( !empty( $field_id ) ) 
        $query = "SELECT user_id FROM " . $wpdb->prefix . "bp_xprofile_data WHERE field_id = " . $field_id;
      else
       return '';
      
      if ( $field_value != '' ) 
        $query .= " AND value = '" . $field_value . "'";
      
      $custom_ids = $wpdb->get_col( $query );
      
      if ( !empty( $custom_ids ) ) {
        // convert the array to a csv string
        // had to switch to exclude here??
        $custom_ids_str = 'exclude=' . implode(",", $custom_ids);
        return $custom_ids_str;
      }
      else
       return '';
       
    }
    
    #20625
     Adam
    Participant

    and my modified members loop code

    COPY CODE
    
    <?php if ( bp_has_members( bp_ajax_querystring( 'members' ).mm_saved_search_params().'&per_page='.sq_option('buddypress_perpage') ) ) : ?>
    
    
    #20692
     Catalin
    Moderator

    Thank you for your code.

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution
    #21575
     Adam
    Participant

    Any chance this kind of thing is being rolled into sweetdate at some point?

    #21624
     bhozar
    Participant

    This is very interesting Adam would be good to filter by gender and looking for. good work

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

Log in with your credentials

Forgot your details?