<?php

class BP_Activity_Template {
	var $current_activity = -1;
	var $activity_count;
	var $activities;
	var $activity;
	
	var $in_the_loop;
	
	var $pag_page;
	var $pag_num;
	var $pag_links;
	var $total_activity_count;
	
	var $full_name;
	
	var $table_name;
	var $filter_content;
	var $is_home;
	
	function bp_activity_template( $user_id = false, $limit = false, $filter_content = true ) {
		global $bp;
		
		if ( !$user_id )
			$user_id = $bp['current_userid'];
			
		if ( $bp['current_component'] != $bp['activity']['slug'] || ( $bp['current_component'] == $bp['activity']['slug'] && $bp['current_action'] == 'just-me' ) ) {
			$this->activities = BP_Activity_Activity::get_activity_for_user( $user_id, $limit );
		} else {
			$this->activities = BP_Activity_Activity::get_activity_for_friends( $user_id, $limit );
		}

		$this->activity_count = count($this->activities);
	
		$this->full_name = $bp['current_fullname'];

		$this->is_home = bp_is_home();
		$this->filter_content = $filter_content;
	}
	
	function has_activities() {
		if ( $this->activity_count )
			return true;
		
		return false;
	}
	
	function next_activity() {
		$this->current_activity++;
		$this->activity = $this->activities[$this->current_activity];
		
		return $this->activity;
	}
	
	function rewind_activities() {
		$this->current_activity = -1;
		if ( $this->activity_count > 0 ) {
			$this->activity = $this->activities[0];
		}
	}
	
	function user_activities() { 
		if ( $this->current_activity + 1 < $this->activity_count ) {
			return true;
		} elseif ( $this->current_activity + 1 == $this->activity_count ) {
			do_action('loop_end');
			// Do some cleaning up after the loop
			$this->rewind_activities();
		}

		$this->in_the_loop = false;
		return false;
	}
	
	function the_activity() {
		global $activity;

		$this->in_the_loop = true;
		$this->activity = $this->next_activity();

		if ( $this->current_activity == 0 ) // loop has just started
			do_action('loop_start');
	}
}

function bp_activity_get_list( $user_id, $title, $no_activity, $limit = false ) {
	global $bp_activity_user_id, $bp_activity_limit, $bp_activity_title, $bp_activity_no_activity;
	
	$bp_activity_user_id = $user_id;
	$bp_activity_limit = $limit;
	$bp_activity_title = $title;
	$bp_activity_no_activity = $no_activity;
	
	load_template( TEMPLATEPATH . '/activity/activity-list.php' );
}

function bp_has_activities() {
	global $bp, $activities_template, $bp_activity_user_id, $bp_activity_limit;
	
	if ( $bp['current_action'] == 'my-friends' )
		$filter_content = false;
	else
		$filter_content = true;
	
	$activities_template = new BP_Activity_Template( $bp_activity_user_id, $bp_activity_limit, $filter_content );		
	return $activities_template->has_activities();
}

function bp_activities() {
	global $activities_template;
	return $activities_template->user_activities();
}

function bp_the_activity() {
	global $activities_template;
	return $activities_template->the_activity();
}

function bp_activities_title() {
	global $bp_activity_title;
	echo $bp_activity_title;
}

function bp_activities_no_activity() {
	global $bp_activity_no_activity;
	echo $bp_activity_no_activity;
}

function bp_activity_content() {
	global $activities_template;
	
	if ( $activities_template->filter_content ) {
		if ( $activities_template->is_home ) {
			echo bp_activity_content_filter( $activities_template->activity['content'], $activities_template->activity['date_recorded'], $activities_template->full_name );						
		} else {
			echo bp_activity_content_filter( $activities_template->activity['content'], $activities_template->activity['date_recorded'], $activities_template->full_name, true, false, false );									
		}
	} else {
		$activities_template->activity['content'] = bp_activity_insert_time_since( $activities_template->activity['content'], $activities_template->activity['date_recorded'] );
		echo $activities_template->activity['content'];
	}
}

function bp_activity_content_filter( $content, $date_recorded, $full_name, $insert_time = true, $filter_words = true, $filter_you = true ) {
	if ( !$content )
		return false;
		
	/* Split the content so we don't evaluate and replace text on content we don't want to */
	$content = explode( '%s', $content );

	/** Commented to work for local langugaes made changes to "content" column in wp_bp_activity_sitewide table 
	*** Re-add the exploded %s *
	** $content[0] .= '%s';
	**/
	/* Insert the time since */
	if ( $insert_time )
		$content[0] = bp_activity_insert_time_since( $content[0], $date_recorded );

	/* Switch 'their/your' depending on whether the user is logged in or not and viewing their profile */
	if ( $filter_words ) {
		$content[0] = str_replace( __('their', 'buddypress'), __('your', 'buddypress'), $content[0] );
	}
	
	/* Remove the 'You' and replace if with the persons name */
	if ( $filter_you ) {
		$content[0] = str_replace( $full_name, __('You', 'buddypress'), $content[0] );				
	}
	
	for ( $i = 0; $i < count($content); $i++ )
		$content_new .= $content[$i];
	
	return $content_new;
}

function bp_activity_insert_time_since( $content, $date ) {
	/** Commented & replaced to work for local langugaes. Reason of commenting - long url getting interrupted 
	** return sprintf( $content, '&nbsp; ' . bp_core_time_since( strtotime( $date ) ) . ' ' . __('ago', 'buddypress') );
	**/
	return $content . sprintf( "%s", '&nbsp; ' . bp_core_time_since( strtotime( $date ) ) . ' ' . __('ago', 'buddypress') );
}

function bp_activity_css_class() {
	global $activities_template;
	echo $activities_template->activity['component_name'];
}



?>