<?php
// custom_archive_template.php
// Version 0.9
// October 12, 2010
// Author: Rick Anderson - Tailoring The Web
// rick@tailoringtheweb.com
//
/* 

 * This is the custom template file included by reference in custom_functions.php.
 * This custom template replaces the loop fuctionality of the category archive page in
 * Thesis with a new custom loop using the Thesis 1.8 Custom Loop API.
 * It retains the default sidebar behaviour and adds 2 sections below the content column.
 *
 * The content column is a list of the n 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.

*/


/*******************************************************************************
* Implementation of the custom loop
*******************************************************************************/

$ttw_popular_post_loop = new ttw_custom_loops;

class ttw_custom_loops extends thesis_custom_loop {
    function category(){
        global $ttwcat_current_cat;
        
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;     // necessary for paging
        $ttwcat_current_cat = get_query_var('cat');                         // necessary for the other functions below
        $post_cut_off  = 8;                                                 // set the number of posts to show per page

        ttwcat_set_post_popularity();           // set the post meta value for views

        query_posts (array(                     // New query vars
                'posts_per_page' => $post_cut_off,
                'paged' => $paged,
                'cat' => $ttwcat_current_cat,
                'meta_key' => 'ttwcat_popularity',
                'orderby' => 'meta_value',
                'order' => DESC)
          );
       thesis_loop::category();// Use the default category loop
    }
}

/*******************************************************************************
* Displays the archive intro headline text as modified below
*******************************************************************************/

function ttwcat_archive_intro($output) {
    if (is_category()) {
        $prefix = 'You Are Viewing the ';
        $suffix = ' Category';
        $ttwcat_current_cat = get_query_var('cat');
        $ttwcat_current_cat_name = get_cat_name( $ttwcat_current_cat );
        $output = $prefix . $ttwcat_current_cat_name . $suffix;
    }
return $output;
}

add_filter('thesis_archive_intro_headline','ttwcat_archive_intro');

/*******************************************************************************
* Writes the number of views for each post to its post meta
*******************************************************************************/


function ttwcat_set_post_popularity(){
   
        $ttwcat_post_ids = array();

        $args=array('nopaging' => true);        //get all the posts

        $postlist = get_posts($args);
        foreach ($postlist as $post){
            setup_postdata($post);
            $post_id = $post->ID;
            $ttwcat_post_ids[] = $post_id;      //list of all post ids
        }

        if (function_exists('wpp_get_views')){
            foreach ($ttwcat_post_ids as $post_id) {            //write views to post meta
                $ttwcat_views = wpp_get_views($post_id);
                update_post_meta($post_id, 'ttwcat_popularity', $ttwcat_views);
              }
        }  
}


/*******************************************************************************
* 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";

}

add_action('thesis_hook_after_content', 'ttwcat_latest_category_posts');

/*******************************************************************************
* 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";
}

add_action('thesis_hook_after_content', 'ttwcat_most_commented_posts');
?>