<?php require('wp-config.php'); $single 1$siteurl get_settings('siteurl'); ?>
<?php 
include ("include-start.php"?>

    <title><?php bloginfo('name'?> &raquo; Blog Archive</title>

    <script language="javascript" type="text/javascript">
    function Choose(whichSort) {
        if (whichSort.selectedIndex == 2) {
            document.getElementById('asc_desc').selectedIndex = 1;
        } else {
            document.getElementById('asc_desc').selectedIndex = 0;
        }
    }
    </script>

<?php include ("include-head.php"?>
<body id="masterarchives">
<?php include ("include-structurestart.php"?>

    <h2><a href="/archives/" title="Back to the Blog Archives">Blog Archive</a></h2>
    <ul>
<?php
$posts_per_page 
'-1';
$category_exclude '-28';
$defaultorderby 'post_date';
$defaultorder 'DESC';

define('NL'"\n");

function 
show_year_select() {
    global 
$wpdb$tableposts$m;
    
$m substr($m,0,4);
    
$years $wpdb->get_col("SELECT DISTINCT YEAR(post_date) as year FROM $tableposts ORDER BY year ASC");
    
$output .= '<option value="">All Years</option>'.NL;
    foreach (
$years as $year) {
        
$output .= '<option value="'.$year.'"';
        if (
$year == $m) {
            
$output .= ' selected="selected"';
        }
        
$output .= '>'.$year.'</option>';
    }
    
$output  '<select name="m">'.NL.$output.'</select>'.NL;
    echo 
$output;
}

function 
show_author_select() {
    global 
$wpdb$tableusers$author;
    
$users $wpdb->get_results("SELECT * FROM $tableusers WHERE user_level > 0"ARRAY_A);
    
$output .= '<option value="">All Authors</option>'.NL;
    foreach (
$users as $user) {
        
$output .= '<option value="'.$user['ID'].'"';
        if (
$user['ID'] == $author) {
            
$output .= 'selected="selected"';
        }
        
$output .= '>'.$user['user_nickname'].'</option>'.NL;
    }
    
$output '<select name="author">'.NL.$output.'</select>'.NL;
    echo 
$output;
}

function 
show_orderby_select() {
    global 
$orderby;
    
$orderby explode(' '$orderby);
    
$orderby $orderby[0];
    if (
$orderby == 'date') {
       
$output .= '<option value="date" selected="selected">Date</option>'.NL;
    } else {
       
$output .= '<option value="date">Date</option>'.NL;
    }
    
/*if ($orderby == 'title') {
       $output .= '<option value="title" selected="selected">Title</option>'.NL;
    } else {
       $output .= '<option value="title">Title</option>'.NL;
    }*/
    
if ($orderby == 'category') {
       
$output .= '<option value="category" selected="selected">Category</option>'.NL;
    } else {
       
$output .= '<option value="category">Category</option>'.NL;
    }
    
$output '<select name="orderby" onchange="Choose(this)">'.NL.$output.'</select>'.NL;
    echo 
$output;
}

function 
show_order_select() {
    global 
$order;
    if (
$order == 'ASC') {
       
$output .= '<option value="ASC" selected="selected">Ascending</option>'.NL;
    } else {
       
$output .= '<option value="ASC">Ascending</option>'.NL;
    }
    if (
$order == 'DESC') {
       
$output .= '<option value="DESC" selected="selected">Descending</option>'.NL;
    } else {
       
$output .= '<option value="DESC">Descending</option>'.NL;
   }
   
$output '<select name="order" id="asc_desc">'.NL.$output.'</select>'.NL;
   echo 
$output;
}

function 
archive_header($before=''$after='') {
    global 
$post$orderby$month$previous$siteurl$blogfilename$archiveheadstart$archiveheadend$category_name;
    
$siteurl get_settings('siteurl');
    
$blogfilename get_settings('blogfilename');
    
$orderby explode(' '$orderby);
    
$orderby $orderby[0];
    if (
'date' == $orderby || empty($orderby)) {
        
$thismonth mysql2date('m'$post->post_date);
        
$thisyear mysql2date('Y'$post->post_date);
        
$thisdate $thisyear.$thismonth;
        if (
$thisdate != $previous) {
            
$thismonth mysql2date('m'$post->post_date);
            
$output .= '<h3><a href="'.get_month_link($yeartemp,$monthtemp).'">'.$month[$thismonth].' '.$thisyear.'</a></h3>';
        }            
        
$previous $thisdate;
    } elseif (
'title' == $orderby) {
        
preg_match('/[a-z0-9]{1}/i'$post->post_title$match);
        
$thisletter ucfirst($match[0]);


        if (
$thisletter != $previous) {
            
$output .= "<br/><br/>".$thisletter;
        }
        
$previous $thisletter;
    } elseif (
'category' == $orderby) {
        
$thiscategory $category_name;
        if (
$thiscategory != $previous) {
            
/*$output .= '<h3><a href="'.get_category_link(true,$thiscategory,get_catname($thiscategory)).'">'.get_catname($thiscategory).'</a></h3>';*/
            
$output .= '<h3>'.get_catname($thiscategory).'</h3>';
        }
        
$previous $thiscategory;
    }
    if (!empty(
$output)) {
        
$output $before.$output.$after.NL;
        echo 
$output;
    }
}

function 
archive_date2($format='H:i:s') {
    global 
$post;
    
$yeartemp mysql2date('Y',$post->post_date);
    
$monthtemp mysql2date('m',$post->post_date);
    
$daytemp mysql2date('d',$post->post_date);
     echo 
"<a href=\"".get_month_link($yeartemp,$monthtemp)."\">".mysql2date('M'$post->post_date)."</a> ";
     echo 
"<a href=\"".get_day_link($yeartemp,$monthtemp,$daytemp)."\">".mysql2date('jS'$post->post_date)."</a> ";
     echo 
"<a href=\"".$yeartemp."\">".mysql2date('Y'$post->post_date)."</a>";
    
// echo mysql2date('h:iA', $post->post_date);


function 
archive_date($format='H:i:s') {
    global 
$post;
    
$yeartemp mysql2date('Y',$post->post_date);
    
$monthtemp mysql2date('m',$post->post_date);
    
$daytemp mysql2date('d',$post->post_date);
     echo 
"<a href=\"".get_day_link($yeartemp,$monthtemp,$daytemp)."\">".mysql2date('jS'$post->post_date)."</a>";
    
// echo mysql2date('h:iA', $post->post_date);


?>

<?php
//Make sure categories get parsed out, they are deprecated in wp-blog-header.php
if ($_POST["orderby"] == 'category') {
    global 
$author$m;
    
$orderby 'category';
    if (
$_POST["order"] == ''$order "DESC";
    else 
$order $_POST["order"];
    
$year '' intval($_POST["m"]);
    
$m $year;
    
$author ''.intval($_POST["author"]);
    if (empty(
$author)) {
        
$whichauthor='';
    } else {
        
$author ''.urldecode($author).'';
        
$author addslashes_gpc($author);
        if (
stristr($author'-')) {
            
$eq '!=';
            
$andor 'AND';
            
$author explode('-'$author);
            
$author ''.intval($author[1]);
        } else {
            
$eq '=';
            
$andor 'OR';
        }
        
$author_array explode(' '$author);
        
$whichauthor .= ' AND (post_author '.$eq.' '.intval($author_array[0]);
        for (
$i 1$i < (count($author_array)); $i $i 1) {
            
$whichauthor .= ' '.$andor.' post_author '.$eq.' '.intval($author_array[$i]);
        }
        
$whichauthor .= ')';
    }

    
// Author stuff for nice URIs

    
if ('' != $author_name) {
        if (
stristr($author_name,'/')) {
            
$author_name explode('/',$author_name);
            if (
$author_name[count($author_name)-1]) {
            
$author_name $author_name[count($author_name)-1];#no trailing slash
            
} else {
            
$author_name $author_name[count($author_name)-2];#there was a trailling slash
            
}
        }
        
$author_name preg_replace('|[^a-z0-9-]|'''strtolower($author_name));
        
$author $wpdb->get_var("SELECT ID FROM $tableusers WHERE user_nicename='".$author_name."'");
        
$whichauthor .= ' AND (post_author = '.intval($author).')';
    }
    if (!empty(
$year)) $where .= ' AND YEAR(post_date)=' $year;
    if (!empty(
$whichauthor)) $where .= $whichauthor;
    
?>

        <form id="archivesorter" action="<?php getenv('PHP_SELF'?>" method="post">
        <?php show_orderby_select() ?>
           <?php show_order_select() ?>
        <?php show_year_select() ?>
        <input type="submit" name="submit" value="sort" />
        </form>
    <?php
    
global $category_exclude;
    
$cat $category_exclude;
    if (
stristr($cat,'-')) {
        
// Note: if we have a negative, we ignore all the positives. It must
        // always mean 'everything /except/ this one'. We should be able to do
        // multiple negatives but we don't :-(
        
$eq '!=';
        
$andor 'AND';
        
$cat explode('-',$cat);
        
$cat intval($cat[1]);
    }
    
$cat_array explode(' ',$cat);

    
$dogs $wpdb->get_results("SELECT * FROM $tablecategories WHERE cat_ID != $cat_array[0] ORDER BY cat_name $order");
    foreach (
$dogs as $catt) {
        
$categories $wpdb->get_results("SELECT * FROM $tablepost2cat WHERE category_id = $catt->cat_ID");
        if (
$categories) {
            foreach (
$categories as $post2category) {
                
$posts $wpdb->get_results("SELECT * FROM $tableposts WHERE $post2category->post_id = ID".$where);
                
//$category_realname = $wpdb->get_row("SELECT cat_name FROM $tablecategories WHERE cat_ID = $post2category->category_id");
                //print_r($category_realname);
                
global $category_name;
                
$category_name $post2category->category_id;
                if (
$posts) {
                    foreach (
$posts as $post) {
                        
start_wp();
                        
archive_header('</ul>''<ul class="masterarchives">'); ?>
                        <li><div class="postcat"><?php archive_date2() ?></div> <div class="posttitle4cat"><a href="<?php echo get_permalink($post->ID?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a></div></li>
                        <?php

                    
}
                }
            }
        }
    }
}
else {
    if (
$_POST["orderby"] == '') {
        
$monthnum date("m");
        
$year date("Y");
        }
    
$cat $category_exclude;
    require_once (
'wp-blog-header.php');
    
// echo $request;
        
if ($monthnum) {
            
$orderby '';
        }
    
?>

        <form id="archivesorter" action="<?php getenv('PHP_SELF'?>" method="post">
        <?php show_orderby_select() ?>
           <?php show_order_select() ?>
        <?php show_year_select() ?>
        <input type="submit" name="submit" value="sort" />
        </form>

    <?php if ($posts) { foreach ($posts as $post) { start_wp(); ?>
    <?php //print_r($post); ?>
    <?php archive_header('</ul>''<ul class="masterarchives">'?>
    <li><div class="postdate"><?php archive_date('jS'?></div> <div class="posttitle"><a href="<?php echo get_permalink($post->ID?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a></div></li>
    <?php } }
    }
?>
</ul></div>

<div id="menu">
        <ul>
            <?php include ("include-sidebartop.php"?>

            <li id="calendar">
                <?php get_calendar() ?>
            </li>

            <li><h2>Blog Statistics</h2>
                <p><?php get_post_count(); ?> entries with <?php get_post_word_count(); ?> words.<br />
                <?php get_comment_count(); ?> comments with <?php get_comment_word_count(); ?> words.</p>
            </li>
                
            <li><h2>Last 10 Posts</h2>
                <ul>
                     <?php get_archives('postbypost''10''other''<li>''</li>'true); ?>
                </ul>
            </li>
                        
            <li><h2>Most Commented Entries</h2>
                <ol>
                <?php get_most_commented('15','<li>','</li>','false'); ?>
                </ol>
            </li>
            
            <li><h2>The Archives</h2>
                <ul>
                    <?php get_archives() ?>
                </ul>
            </li>

            <li><h2>Clippings</h2>
                <p>For a while I carried a list of daily updated links. It no longer exists, but the archives remain:</p>
                <p style="text-align: right"><a href="http://binarybonsai.com/archives/category/clippings">Clippings Archive &raquo;</a><br />
                <a href="/linksarchive" title="Arg, here be dragons!">Ye Olde Clippings Archive &raquo;</a></p>
            </li>
    
            <li><h2>Categories</h2>
                <ul>
                    <?php list_cats(1'All Categories''name''asc''/archivespage.php'1011110,'','','','','28'?>
                </ul>
            </li>
        </ul>    
</div>


<?php include ("include-footer.php"?>