Skip to:
Content

BuddyPress.org

Changeset 1839


Ignore:
Timestamp:
09/10/2009 01:24:01 AM (12 years ago)
Author:
apeatling
Message:

Added support for significantly more advanced activity stream filtering.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-activity.php

    r1819 r1839  
    352352}
    353353
    354 function bp_activity_get_user_activity( $user_id, $max_items = 30, $since = '-4 weeks', $pag_num = false, $pag_page = false, $filter = false ) {
    355     return BP_Activity_Activity::get_activity_for_user( $user_id, $max_items, $since, $pag_num, $pag_page, $filter );
    356 }
    357 
    358 function bp_activity_get_friends_activity( $user_id, $max_items = 30, $since = '-4 weeks', $max_items_per_friend = false, $pag_num = false, $pag_page = false, $filter = false ) {
    359     return BP_Activity_Activity::get_activity_for_friends( $user_id, $max_items, $since, $max_items_per_friend, $pag_num, $pag_page, $filter );
     354function bp_activity_get_user_activity( $user_id, $max_items = 30, $pag_num = false, $pag_page = false, $filter = false ) {
     355    return BP_Activity_Activity::get_activity_for_user( $user_id, $max_items, $pag_num, $pag_page, $filter );
     356}
     357
     358function bp_activity_get_friends_activity( $user_id, $max_items = 30, $max_items_per_friend = false, $pag_num = false, $pag_page = false, $filter = false ) {
     359    return BP_Activity_Activity::get_activity_for_friends( $user_id, $max_items, $max_items_per_friend, $pag_num, $pag_page, $filter );
    360360}
    361361
  • trunk/bp-activity/bp-activity-classes.php

    r1819 r1839  
    144144    }
    145145   
    146     function get_activity_for_user( $user_id, $max_items, $since, $limit, $page, $filter ) {
     146    function get_activity_for_user( $user_id, $max_items, $limit, $page, $filter ) {
    147147        global $wpdb, $bp;
    148148
     
    155155            $max_sql = $wpdb->prepare( "LIMIT %d", $max_items );
    156156       
     157        /* Sort out filtering */
    157158        if ( $filter )
    158             $filter_sql = $wpdb->prepare( "AND component_name = %s", $filter );
     159            $filter_sql = BP_Activity_Activity::get_filter_sql( $filter );
    159160       
    160161        if ( $limit && $page && $max_items )
    161             $activities = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name} WHERE user_id = %d AND date_recorded >= FROM_UNIXTIME(%d) $privacy_sql $filter_sql ORDER BY date_recorded DESC $pag_sql", $user_id, $since ) );
     162            $activities = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name} WHERE user_id = %d $privacy_sql $filter_sql ORDER BY date_recorded DESC $pag_sql", $user_id ) );
    162163        else
    163             $activities = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name} WHERE user_id = %d AND date_recorded >= FROM_UNIXTIME(%d) $privacy_sql $filter_sql ORDER BY date_recorded DESC $pag_sql $max_sql", $user_id, $since ) );
    164        
    165         $total_activities = $wpdb->get_var( $wpdb->prepare( "SELECT count(id) FROM {$bp->activity->table_name} WHERE user_id = %d AND date_recorded >= FROM_UNIXTIME(%d) $privacy_sql $filter_sql ORDER BY date_recorded DESC $max_sql", $user_id, $since ) );
     164            $activities = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name} WHERE user_id = %d $privacy_sql $filter_sql ORDER BY date_recorded DESC $pag_sql $max_sql", $user_id ) );
     165       
     166        $total_activities = $wpdb->get_var( $wpdb->prepare( "SELECT count(id) FROM {$bp->activity->table_name} WHERE user_id = %d $privacy_sql $filter_sql ORDER BY date_recorded DESC $max_sql", $user_id ) );
    166167       
    167168        return array( 'activities' => $activities, 'total' => (int)$total_activities );
    168169    }
    169170   
    170     function get_activity_for_friends( $user_id, $max_items, $since, $max_items_per_friend, $limit, $page, $filter ) {
     171    function get_activity_for_friends( $user_id, $max_items, $max_items_per_friend, $limit, $page, $filter ) {
    171172        global $wpdb, $bp;
    172173       
     
    182183            $max_sql = $wpdb->prepare( "LIMIT %d", $max_items );
    183184
     185        /* Sort out filtering */
    184186        if ( $filter )
    185             $filter_sql = $wpdb->prepare( "AND component_name = %s", $filter );
    186 
    187         $since = strtotime($since);
     187            $filter_sql = BP_Activity_Activity::get_filter_sql( $filter );
    188188
    189189        $friend_ids = friends_get_friend_user_ids( $user_id );
     
    195195       
    196196        if ( $limit && $page && $max_items )
    197             $activities = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT user_id, content, primary_link, date_recorded, component_name, component_action FROM {$bp->activity->table_name} WHERE user_id IN ({$friend_ids}) AND date_recorded >= FROM_UNIXTIME(%d) $filter_sql ORDER BY date_recorded DESC $pag_sql", $since ) );
     197            $activities = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT user_id, content, primary_link, date_recorded, component_name, component_action FROM {$bp->activity->table_name} WHERE user_id IN ({$friend_ids}) $filter_sql ORDER BY date_recorded DESC $pag_sql" ) );
    198198        else
    199             $activities = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT user_id, content, primary_link, date_recorded, component_name, component_action FROM {$bp->activity->table_name} WHERE user_id IN ({$friend_ids}) AND date_recorded >= FROM_UNIXTIME(%d) $filter_sql ORDER BY date_recorded DESC $pag_sql $max_sql", $since ) );             
    200 
    201         $total_activities = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT count(user_id) FROM {$bp->activity->table_name} WHERE user_id IN ({$friend_ids}) AND date_recorded >= FROM_UNIXTIME(%d) $filter_sql ORDER BY date_recorded DESC $max_sql", $since ) );
     199            $activities = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT user_id, content, primary_link, date_recorded, component_name, component_action FROM {$bp->activity->table_name} WHERE user_id IN ({$friend_ids}) $filter_sql ORDER BY date_recorded DESC $pag_sql $max_sql" ) );           
     200
     201        $total_activities = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT count(user_id) FROM {$bp->activity->table_name} WHERE user_id IN ({$friend_ids}) $filter_sql ORDER BY date_recorded DESC $max_sql" ) );
    202202       
    203203        return array( 'activities' => $activities, 'total' => (int)$total_activities );
     
    212212        if ( $max )
    213213            $max_sql = $wpdb->prepare( "LIMIT %d", $max );
    214 
     214           
     215        /* Sort out filtering */
    215216        if ( $filter )
    216             $filter_sql = $wpdb->prepare( "AND component_name = %s", $filter );
    217 
     217            $filter_sql = BP_Activity_Activity::get_filter_sql( $filter );
     218       
    218219        if ( $limit && $page && $max )
    219220            $activities = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name} WHERE hide_sitewide = 0 $filter_sql ORDER BY date_recorded DESC $pag_sql" ) );
     
    248249    }
    249250   
     251    function get_filter_sql( $filter_array ) {
     252        global $wpdb;
     253       
     254        if ( !empty( $filter_array['object'] ) ) {
     255            $object_filter = explode( ',', $filter_array['object'] );
     256            $object_sql = ' AND ( ';
     257           
     258            $counter = 1;
     259            foreach( (array) $object_filter as $object ) {
     260                $object_sql .= $wpdb->prepare( "component_name = %s", trim( $object ) );
     261               
     262                if ( $counter != count( $object_filter ) )
     263                    $object_sql .= ' || ';
     264               
     265                $counter++;
     266            }
     267           
     268            $object_sql .= ' )';
     269        }
     270
     271        if ( !empty( $filter_array['action'] ) ) {
     272            $action_filter = explode( ',', $filter_array['action'] );
     273            $action_sql = ' AND ( ';
     274           
     275            $counter = 1;
     276            foreach( (array) $action_filter as $action ) {
     277                $action_sql .= $wpdb->prepare( "component_action = %s", trim( $action ) );
     278               
     279                if ( $counter != count( $action_filter ) )
     280                    $action_sql .= ' || ';
     281               
     282                $counter++;
     283            }
     284           
     285            $action_sql .= ' )';
     286        }
     287
     288        if ( !empty( $filter_array['primary_id'] ) ) {
     289            $pid_filter = explode( ',', $filter_array['action'] );
     290            $pid_sql = ' AND ( ';
     291           
     292            $counter = 1;
     293            foreach( (array) $pid_filter as $pid ) {
     294                $pid_sql .= $wpdb->prepare( "item_id = %s", trim( $pid ) );
     295               
     296                if ( $counter != count( $pid_filter ) )
     297                    $pid_sql .= ' || ';
     298               
     299                $counter++;
     300            }
     301           
     302            $pid_sql .= ' )';
     303        }
     304
     305        if ( !empty( $filter_array['secondary_id'] ) ) {
     306            $sid_filter = explode( ',', $filter_array['action'] );
     307            $sid_sql = ' AND ( ';
     308           
     309            $counter = 1;
     310            foreach( (array) $sid_filter as $sid ) {
     311                $sid_sql .= $wpdb->prepare( "secondary_item_id = %s", trim( $sid ) );
     312               
     313                if ( $counter != count( $sid_filter ) )
     314                    $sid_sql .= ' || ';
     315               
     316                $counter++;
     317            }
     318           
     319            $sid_sql .= ' )';
     320        }
     321       
     322        return $object_sql . $action_sql . $pid_sql . $sid_sql;
     323    }
     324   
    250325    function get_last_updated() {
    251326        global $bp, $wpdb;
  • trunk/bp-activity/bp-activity-templatetags.php

    r1834 r1839  
    1717    var $full_name;
    1818
    19     function bp_activity_template( $type, $user_id, $per_page, $max, $timeframe, $filter ) {
     19    function bp_activity_template( $type, $user_id, $per_page, $max, $filter ) {
    2020        global $bp;
    2121
     
    2828       
    2929        if ( $type == 'personal' )
    30             $this->activities = bp_activity_get_user_activity( $user_id, $max, $timeframe, $this->pag_num, $this->pag_page, $filter );
     30            $this->activities = bp_activity_get_user_activity( $user_id, $max, $this->pag_num, $this->pag_page, $filter );
    3131
    3232        if ( $type == 'friends' && ( bp_is_home() || is_site_admin() || $bp->loggedin_user->id == $user_id ) )
    33             $this->activities = bp_activity_get_friends_activity( $user_id, $max, $timeframe, false, $this->pag_num, $this->pag_page, $filter );
     33            $this->activities = bp_activity_get_friends_activity( $user_id, $max, false, $this->pag_num, $this->pag_page, $filter );
    3434       
    3535        if ( !$max || $max >= (int)$this->activities['total'] )
     
    8989            return true;
    9090        } elseif ( $this->current_activity + 1 == $this->activity_count ) {
    91             do_action('loop_end');
     91            do_action('activity_loop_end');
    9292            // Do some cleaning up after the loop
    9393            $this->rewind_activities();
     
    108108
    109109        if ( $this->current_activity == 0 ) // loop has just started
    110             do_action('loop_start');
     110            do_action('activity_loop_start');
    111111    }
    112112}
     
    125125function bp_has_activities( $args = '' ) {
    126126    global $bp, $activities_template, $bp_activity_user_id, $bp_activity_limit;
    127 
     127   
     128    /* Note: any params used for filtering can be a single value, or multiple values comma seperated. */
     129   
    128130    $defaults = array(
    129131        'type' => 'sitewide',
    130         'user_id' => false,
    131132        'per_page' => 25,
    132133        'max' => false,
    133         'timeframe' => '-4 weeks',
    134         'filter' => false
     134        'user_id' => false, // user_id to filter on
     135        'object' => false, // object to filter on e.g. groups, profile, status, friends
     136        'action' => false, // action to filter on e.g. new_wire_post, new_forum_post, profile_updated
     137        'primary_id' => false, // object ID to filter on e.g. a group_id or forum_id or blog_id etc.
     138        'secondary_id' => false, // secondary object ID to filter on e.g. a post_id
    135139    );
    136140
     
    162166   
    163167    if ( isset( $_GET['afilter'] ) )
    164         $filter = $_GET['afilter'];
    165 
    166     $activities_template = new BP_Activity_Template( $type, $user_id, $per_page, $max, $timeframe, $filter );       
     168        $filter = array( 'object' => $_GET['afilter'] );
     169    else
     170        $filter = array( 'object' => $object, 'action' => $action, 'primary_id' => $primary_id, 'secondary_id' => $secondary_id );
     171   
     172    $activities_template = new BP_Activity_Template( $type, $user_id, $per_page, $max, $filter );       
    167173    return $activities_template->has_activities();
    168174}
Note: See TracChangeset for help on using the changeset viewer.