<?php

// this file contains all display functions

// will show the add/remove links
function mar_read_unread_links($read_text = 'Mark as read', $unread_text = 'Mark as unread', $wrapper = true) {

	global $mar_options;

	if(is_user_logged_in()) {
		$post_id = get_the_ID();
		
		// if this post has been bookmarked, show the remove link, otherwise show the add link
		$link = '';
		if($wrapper == true) {
			$link .= '<div class="mar_links">';
		}
		if($read_text == '') { 
			if($mar_options['read_text'] != '') {
				$read_text = $mar_options['read_text']; 
			} else {
				$read_text = 'Mark as Read';
			}
		}
		if($unread_text == '') { 
			if($mar_options['unread_text'] != '') {
				$unread_text = $mar_options['unread_text']; 
			} else {
				$unread_text = 'Mark as Unread';
			}
		}
		
		if(mar_check_post_is_read($post_id, mar_get_user_id())) {
			$link .= '<a href="#" rel="' . $post_id . '" class="mar_mark_as_unread mar_read_control mar_read_control_' . $post_id . '">' . $unread_text . '</a>';
			$link .= '<a href="#" rel="' . $post_id . '" class="mar_mark_as_read mar_read_control mar_read_control_' . $post_id . '" style="display:none;">' . $read_text . '</a>';
		} else {
			$link .= '<a href="#" rel="' . $post_id . '" class="mar_mark_as_unread mar_read_control mar_read_control_' . $post_id . '" style="display:none;">' . $unread_text . '</a>';
			$link .= '<a href="#" rel="' . $post_id . '" class="mar_mark_as_read mar_read_control mar_read_control_' . $post_id . '">' . $read_text . '</a>';
		}
		if($wrapper == true) {	
			$link .= '</div>';
		}
		return $link;
	}
}

// show mark as read / unread links automatically if enabled. Do not use this as a template tag
function mar_show_links($content) {
	global $post;
	global $mar_options;
	
	if(is_single() && get_post_type() == 'post' && $mar_options['show_links'] == true) {
		
		if($mar_options['post_position'] == 'Top') {
			$content = mar_read_unread_links($mar_options['read_text'], $mar_options['unread_text']) . $content;
		} else {
			$content = $content . mar_read_unread_links($mar_options['read_text'], $mar_options['unread_text']);
		}
	}
	return $content;
}
add_filter('the_content', 'mar_show_links');


function mar_list_unread_posts($number = 5) {
	if(is_user_logged_in()) {
		$read_ids = mar_get_user_read_posts( mar_get_user_id() );
		$unread_posts = get_posts(array('exclude' => $read_ids, 'numberposts' => $number));
		ob_start(); ?>
			<ul class="mar_unread_posts_list">
				<?php foreach($unread_posts as $unread) : ?>
					<li class="mar_unread_post"><a href="<?php echo get_permalink($unread->ID); ?>"><?php echo get_the_title($unread->ID); ?></a></li>
				<?php endforeach; ?>
			</ul>
		<?php
		return ob_get_clean();
	}
}

function mar_list_user_report( $args ) {
	if(is_user_logged_in()) {
		$defaults = array(
			'read' => true,
			'unread' => true,
			'total' => true,
			'percentage' => true,
			'labels' => array(
				'read' => 'Read Posts: ',
				'unread' => 'Unread Posts: ',
				'total' => 'Total Posts: ',
				'percentage' => 'Percentage Complete: '
			)
		);
		$args = wp_parse_args( $args, $defaults );
		extract( $args, EXTR_SKIP );
		
		$report = mar_get_user_report();
		
		ob_start(); ?>
			<ul class="mar_user_report">
				<?php if($read) { ?>
				<li><?php echo $labels['read'] . $report['read_posts_count']; ?></li>
				<?php } if($unread) { ?>
				<li><?php echo $labels['unread'] . $report['unread_posts_count']; ?></li>
				<?php } if($total) { ?>
				<li><?php echo $labels['total'] . $report['total_posts']; ?></li>
				<?php } if($percentage) { ?>
				<li><?php echo $labels['percentage'] . $report['read_percentage']; ?></li>
				<?php } ?>
			</ul>
		<?php
		return ob_get_clean();
	}
}

function mar_most_read($number = 5, $count = true) {
	global $wpdb;
	global $mar_db_table;

	$read_posts = $wpdb->get_results("SELECT * FROM " . $mar_db_table . " WHERE read_count != 0 ORDER BY read_count DESC LIMIT $number;");
	if($read_posts) {
		$popular_list = '<ul class="mar_most_read_posts">';	
		foreach($read_posts as $popular) {
			$popular_list .= '<li class="popular_post">';
				$popular_list .= '<a href="' . get_permalink($popular->id) . '" class="popular_post_link" title="' . get_the_title($popular->id) . ' - ' . __('Read ') . $popular->read_count . __(' times') . '">';
				$popular_list .= get_the_title($popular->id) . '</a>';
				if($count == true) {
					$popular_list .= ' (' . $popular->read_count . ')';
				}
			$popular_list .= '</li>';
		}	
		$popular_list .= '</ul>';
	}
	return $popular_list;
}