<?php
// custom_archive_template.php
// Version 0.3
// Author: Rick Anderson - Tailoring The Web
// rick@tailoringtheweb.com
//
/* 

This is the custom template file called by the function in custom_functions.php.  This custom template replaces the fuctionality of the category archive page in Thesis.  It retains the default sidebar behaviour but replaces the content column with 3 sections.  The top section is a list of the 4 most popular posts in the category.  The bottom section is divded into left and right sides.
The left side has a list of posts in the category.  The right side has a list of all posts with a comment organized in order of descending comment count.

*/


/*******************************************************************************
* The controler for the custom category archive template
* The value of $post_cut_off is set here.
*******************************************************************************/

function ttwcat_custom_archive_template(){
    global $post, $ttwcat_category_post_ids, $ttwcat_post_count, $ttwcat_current_cat ;
    global $thesis_design;
    global $post_cut_off;
    $post_cut_off  = 8;

    $ttwcat_current_cat = get_query_var('cat');

    if ($thesis_design->layout['columns'] == 3 && $thesis_design->layout['order'] == 'invert'){
            echo "\t\t<div id=\"column_wrap\">\n";
            ttwcat_content_column();
            thesis_get_sidebar();
            echo "\t\t</div>\n";}
    else{
            ttwcat_content_column();
    }

    thesis_sidebars();
}

/*******************************************************************************
* The custom version of the Thesis Content Column
********************************************************************************/

function ttwcat_content_column(){
    echo "\t\t<div id=\"content\"" . thesis_content_classes() . ">\n\n";
    thesis_hook_before_content(); #hook
	
		echo "<h2 id=\"custom_category_title\">";
		single_cat_title('<span>Currently browsing:</span> ');
		echo "</h2>";

    ttwcat_most_popular_category_posts();
    ttwcat_latest_category_posts();
    ttwcat_most_commented_posts();

    thesis_hook_after_content(); #hook
    echo "\t\t</div>\n\n";

}


/*******************************************************************************
* Controler for the most popular category posts.  
* If there are fewer than X ($post_cut_off) posts in a given category
* All of the posts will be displayed.
* If there are more than X ($post_cut_off) the X most visited posts will be displayed
* in order of # of views
*******************************************************************************/
function ttwcat_most_popular_category_posts(){
     global $post, $ttwcat_category_post_ids, $ttwcat_post_count, $ttwcat_views_per_post;
     global $post_cut_off;

    ttwcat_get_post_ids();
    if ($ttwcat_post_count > $post_cut_off){
        ttwcat_sort_by_popular_posts();
    }else{
        ttwcat_show_all_posts();
    }
    echo '<div class="clear">&nbsp;</div>';
}

/*******************************************************************************
* Displays all of the Posts in the category
*******************************************************************************/
function ttwcat_latest_category_posts(){
    global $post, $ttwcat_current_cat;

    echo "\t\t<div id=\"ttwcat-post-lists\">\n\n";
    echo "\t\t<div id=\"ttwcat-post-lists-left\">\n\n";
    echo '<h2>Latest Posts</h2>';
    echo '<ul class="latest_posts">';
    $my_query = new WP_Query(array('cat' => $ttwcat_current_cat));
    while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID;

        echo '<li class="entry-title"><a href="' . get_permalink() . '" rel="bookmark" title="Permanent link to ' . get_the_title() . '">' . get_the_title() . "</a></li>\n";

    endwhile;
    echo "\t\t</ul>\n\n";
    echo "\t\t</div>\n\n";

}

/*******************************************************************************
* Displays all of the Posts in the category with comments sorted 
* by the number of comments
*******************************************************************************/
function ttwcat_most_commented_posts(){
    global $post, $ttwcat_current_cat;

    echo "\t\t<div id=\"ttwcat-post-lists-right\">\n\n";
    echo '<h2>Most Commented Posts</h2>';
    echo '<ul class="most_commented">';
    $my_query = new WP_Query(array( 'cat' => $ttwcat_current_cat,
                                    'orderby' => 'comment_count',
                                    'order' => 'DESC'));
    while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID;
        $post_id = $post->ID;
        $comments = get_comments(array(
                                        'status' => 'approve',
                                        'post_id' => $post_id));
        
        
        if($comments){
            $comment_count = count($comments);
        		if ($comment_count < 1) {
        		echo '<li class="entry-title">There are presently no commented posts in this category</li>';   
        		};
        echo '<li class="entry-title"><a href="' . get_permalink() . '" rel="bookmark" title="Permanent link to ' . get_the_title() . '">' . get_the_title() . "</a> <span>($comment_count)</span></li>\n";
        }
    endwhile;
    echo "\t\t</ul>\n\n";
    echo "\t\t</div>\n\n";
    echo "\t\t</div>\n\n";
}

/*******************************************************************************
* Creates an array of post ids in a given category & counts the number of posts
*******************************************************************************/
function ttwcat_get_post_ids(){
    global $post, $ttwcat_category_post_ids, $ttwcat_post_count, $ttwcat_current_cat, $ttwcat_views_per_post;

    $ttwcat_post_count = 0;
    array ($ttwcat_category_post_ids);

    //count the number of posts
    $args=array('category__in' => array($ttwcat_current_cat));
    query_posts($args);

    if ( have_posts() ) : while ( have_posts() ) : the_post();
        $post_id = $post->ID;
        $ttwcat_category_post_ids[] = $post_id;
        $ttwcat_post_count++;
        endwhile;
        
    endif;
}

/*******************************************************************************
* Displays the teasers when there are more than X ($post_cut_off) posts in
* the category, sorted by their place in the array
*******************************************************************************/
function ttwcat_sort_by_popular_posts(){
    global $post, $ttwcat_category_post_ids, $ttwcat_current_cat, $ttwcat_post_count, $ttwcat_views_per_post;
    $teaser_count = 1; // set count for teaser wrappers
    $post_count = 1; // set count for post wrappers
    
    ttwcat_find_most_popular_posts();

    foreach ($ttwcat_category_post_ids as $post_id) {
        $my_query = new WP_Query(array('p' => $post_id));
        while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID;

            if (($teaser_count % 2) == 1) {
                $top = ($post_count == 1) ? ' top' : '';
                $open_box = "\t\t\t<div class=\"teasers_box$top\">\n\n";
                $close_box = '';
                $right = false;
            }
            else {
                    $open_box = '';
                    $close_box = "\t\t\t</div>\n\n";
                    $right = true;
            }

            if ($open_box != '') {
                    echo $open_box;
                    thesis_hook_before_teasers_box($post_count);
            }

            thesis_teaser($classes, $post_count, $right);

            if ($close_box != '') {
                    echo $close_box;
                    thesis_hook_after_teasers_box($post_count);
            }

            $teaser_count++;

        endwhile;
        

        wp_reset_query();
    }
    if ((($teaser_count - 1) % 2) == 1){
        echo "\t\t\t</div>\n\n";
    }
}

/******************************************************************************
* Checks views for each post, sorts the post id by the # of views &
* chops the array to X ($post_cut_off)
* This relies on a function from the WordPress Popular Posts plugin.
* If the plugin isn't installed it returns the last 4 posts.
*******************************************************************************/
function ttwcat_find_most_popular_posts(){
    global $ttwcat_views_per_post, $ttwcat_category_post_ids;
    global $post_cut_off;

    array ($ttwcat_views_per_post);

    if (function_exists('wpp_get_views')){
        foreach ($ttwcat_category_post_ids as $post_id) {

            $ttwcat_views_per_post[] = wpp_get_views($post_id);
          }

        array_multisort($ttwcat_views_per_post, SORT_DESC, $ttwcat_category_post_ids);
        $ttwcat_category_post_ids = array_slice($ttwcat_category_post_ids, 0, $post_cut_off, true);
        return $ttwcat_category_post_ids;
    }

}

/*******************************************************************************
* Displays all of teasers when there are less than X ($post_cut_off) in the category
*******************************************************************************/
function ttwcat_show_all_posts(){
    global $post, $ttwcat_current_cat;

    $teaser_count = 1; // set count for teaser wrappers
    $post_count = 1; // set count for post wrappers
   
    $my_query = new WP_Query(array('cat' => $ttwcat_current_cat));
    while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID;

        if (($teaser_count % 2) == 1) {
            $top = ($post_count == 1) ? ' top' : '';
            $open_box = "\t\t\t<div class=\"teasers_box$top\">\n\n";
            $close_box = '';
            $right = false;
        }
        else {
                $open_box = '';
                $close_box = "\t\t\t</div>\n\n";
                $right = true;
        }

        if ($open_box != '') {
                echo $open_box;
                thesis_hook_before_teasers_box($post_count);
        }

        thesis_teaser($classes, $post_count, $right);

        if ($close_box != '') {
                echo $close_box;
                thesis_hook_after_teasers_box($post_count);
        }

        $teaser_count++;

    endwhile;
    if ((($teaser_count - 1) % 2) == 1){
            echo "\t\t\t</div>\n\n";
        }
    wp_reset_query();
}
?>