<?php
header('Content-type: text/xml'); 

{
$todays_date=date('D, d M Y H:i:s'); 
}

{
$today=date('Y-m-d H:i:s'); 
}


### Include necessary files
# Include the site's config file
include "store_settings.php"; 

### Include file to connect to the database
include("$dbconnect_path/$dbconnect_directory/$dbconnect_filename");


$page_columns = 3;
$page_rows = 20;
$title_location = 2;

##Calculate the maximum number of items allowed on this page (columns times rows)
$max_items_per_page = ($page_columns * $page_rows);


// If current page number, use it 
// if not, set one! 

if(!isset($_GET['page'])){ 
    $page = 1; 
} else { 
    $page = $_GET['page']; 
} 


// Figure out the limit for the query based 
// on the current page number. 
$from = (($page * $max_items_per_page) - $max_items_per_page); 



### Query the blog information

$blog_query = "SELECT b.id,b.title,b.date,b.text,b.image,b.caption,b.alt_tag,a.id,a.author,a.email FROM lantern_blog b, lantern_blog_authors a WHERE b.author = a.id AND b.active = 'yes' AND a.active = '1'  AND b.date <= '$today'  ORDER BY date DESC LIMIT 20";

$blog_list = '';

$blog_result = mysql_query("$blog_query",$db);
while ($blog_array = mysql_fetch_row($blog_result)) {
	$blog_id = $blog_array[0];
	$blog_title = $blog_array[1];
	$blog_date = $blog_array[2];
	$blog_text = $blog_array[3];
	$blog_image = $blog_array[4];
	$blog_caption = $blog_array[5];
	$blog_alt_tag = $blog_array[6];
	$blog_author_id = $blog_array[7];
	$blog_author_name = $blog_array[8];
	$blog_author_email = $blog_array[9];
	
	$blog_title = strip_tags($blog_title);
	
	




	
### Encode email
$blog_author_email = str_replace("@", "&#64;",$blog_author_email);

### Make sure text fields insert line breaks at returns
$blog_text = nl2br($blog_text);

### Remove line breaks from lists
$blog_text = str_replace("<UL><br />", "<UL>",$blog_text);

$blog_text = str_replace("<br />\r\n<LI>", "<LI>",$blog_text);

$blog_text = str_replace("<br />\r\n</UL>", "</UL>",$blog_text);

### Add headline tags
$blog_text = str_replace('<subh>', '<span class="subh">',$blog_text);

### Add close headline tags
$blog_text = str_replace('</subh>', '</span>',$blog_text);

//format date for $blog_text = $blog_text.ing
list($blog_split_date,$blog_split_time) = split(' ',$blog_date); 
list($blog_year,$blog_month,$blog_day) = split('-',$blog_split_date); 
list($blog_hour,$blog_minute,$blog_second) = split(':',$blog_split_time); 
//Remove the '-' from the standar MySQL sate format
$stampeddate = mktime($blog_hour,$blog_minute,$blog_second,$blog_month,$blog_day,$blog_year); 
//Create a UNIX style timestamp from the result
$blog_date = date("D, d M Y H:i:s T",$stampeddate); 

if ($blog_image != '') {

/*
list($blog_image_width, $blog_image_height, $image_type, $image_w) = getimagesize('http://booklightinc.net/redwheelweiser/images/blog/'.$blog_image);
height="'.$blog_image_height.'" width="'.$blog_image_width.'"
*/
$image_code = '
<div class="illowrapper">
<div class="illoliner">
<img src="http://booklightinc.net/redwheelweiser/images/blog/'.$blog_image.'" alt="'.$blog_alt_tag.'"  />
<p>'.$blog_caption.'</p>
</div><!-- closes "illoliner" -->
</div><!-- closes "illowrapper" -->
';
}
else {
$image_code = '';
}
	







// Query the database for all books on the page

$sqlcode = "SELECT products.product_id, products.item_code, products.title, products.sub_title, products.publisher, products.publisher_imprint, products.product_type, products.product_price, products.image, products.short_description, products.author_1_job, products.author_2_job, products.author_3_job, products.author_4_job, products.author_5_job, products.author_1_number, products.author_2_number, products.author_3_number, products.author_4_number, products.author_5_number, products.author_6_number, products.author_7_number, products.author_8_number, products.author_6_job, products.author_7_job, products.author_8_job FROM products INNER JOIN products_lantern_blog ON (products.product_id = products_lantern_blog.product_id AND products_lantern_blog.lantern_blog_id = '$blog_id') WHERE products.active = 'yes' ORDER BY products_lantern_blog.sort_order LIMIT $from, $max_items_per_page";


$result = mysql_query("$sqlcode",$db);

// Get the total number of items

$number_of_total_items = mysql_result(mysql_query("SELECT COUNT(products.product_id) as Num FROM products INNER JOIN products_lantern_blog ON (products.product_id = products_lantern_blog.product_id AND products_lantern_blog.lantern_blog_id = '$blog_id') WHERE products.active = 'yes' ",$db),0); 


// Figure out how many items are actually on this page 
if (($max_items_per_page * $page) <= $number_of_total_items) {
	$number_of_items_on_this_page = $max_items_per_page;
}
else {
$number_of_items_on_this_page = (($max_items_per_page - (($max_items_per_page * $page) - $number_of_total_items)));
}

if ($number_of_items_on_this_page < 0) {
$number_of_items_on_this_page = 0;
}

if ($number_of_items_on_this_page > 0) {
$blog_text = $blog_text. '<h4>Related Titles</h4>';


// Set i as zero - later we'll tell it to increase with each loop, so it will represent each current record number

$i = 0;

// Figure out the total number of pages. Always round up using ceil() 
$total_pages = ceil($number_of_total_items / $max_items_per_page); 

if ($total_pages > 1) {
// Build Page Number Hyperlinks 
$blog_text = $blog_text.("<p class=\"pageselect\">Select a Page:</p><p class=\"pageoptions\">"); 

// Build Previous Link 
if($page > 1){ 
    $prev = ($page - 1); 
    $blog_text = $blog_text.("<span class=\"previouspage\"><a href=\"".$_SERVER['PHP_SELF']."?id=$id&page=$prev\">&lt;&lt;Previous</a></span>&nbsp; "); 
} 

for($i_page = 1; $i_page <= $total_pages; $i_page++){ 
    if(($page) == $i_page){ 
        $blog_text = $blog_text.("<span class=\"currentpage\">$i_page</span> "); 
        } else { 
            $blog_text = $blog_text.("<span class=\"pagechoices\"><a href=\"".$_SERVER['PHP_SELF']."?id=$id&page=$i_page\">$i_page</a></span> "); 
    } 
} 

// Build Next Link 
if($page < $total_pages){ 
    $next = ($page + 1); 
    $blog_text = $blog_text.("&nbsp; <span class=\"nextpage\"><a href=\"".$_SERVER['PHP_SELF']."?id=$id&page=$next\">Next&gt;&gt;</a></span>"); 
} 
$blog_text = $blog_text.("</p><br clear=\"all\">"); 

}

// Open the table that will contain the books

$blog_text = $blog_text.("<TABLE cellspacing=\"0\" cellpadding=\"0\" border=\"0\" class=\"box\" width=\"100%\">
");

// Create the array and set variables

while ($myrow = mysql_fetch_row($result)) 

{
	$product_id = $myrow[0];
	$product_item_code = $myrow[1];
	$product_title = $myrow[2];
	$sub_title = $myrow[3];
	$publisher = $myrow[4];
	$publisher_imprint = $myrow[5];
	$product_type = $myrow[6];
	$product_price = $myrow[7];
	$image  = $myrow[8];
	$short_description = $myrow[9];
	$author_1_job = $myrow[10];
	$author_2_job = $myrow[11];
	$author_3_job = $myrow[12];
	$author_4_job = $myrow[13];
	$author_5_job = $myrow[14];
	$author_1_number = $myrow[15];
	$author_2_number = $myrow[16];
	$author_3_number = $myrow[17];
	$author_4_number = $myrow[18];
	$author_5_number = $myrow[19];
	$author_6_number = $myrow[20];
	$author_7_number = $myrow[21];
	$author_8_number = $myrow[22];
	$author_6_job = $myrow[23];
	$author_7_job = $myrow[24];
	$author_8_job = $myrow[25];
	
	###Format book image	
	if ($image  != "") {
		$small_graphic_code = "images/small/$image";
	}
	else {
		$small_graphic_code = "images/small/notfound.jpg";
	}
	
	### Format subtitle
	if ($sub_title != "") {
		$sub_title_code = "<br /><span class=\"product_subtitle\">$sub_title</span>";
	}
	else {
		$sub_title_code = "";
	}
	
	### Format description
	
	if ($description != "") {		
		$description = nl2br($description);
		$description_code = "<br /><br /><span class=\"product_description\">$description</span>";
	}
	else {
		$description_code = "";
	}
	
			###Get the author names from the author database

if ($author_1_number != "0") {

	$author_1_query = "SELECT author_id, author_1_lastname, author_1_firstname, author_1_middlename, author_1_prefix, author_1_suffix FROM authors WHERE author_id = $author_1_number";
	$author_1_result = mysql_query("$author_1_query",$db);

	while ($author_1_array = mysql_fetch_row($author_1_result)) {
	
		$author_1_id = $author_1_array[0];
		$author_1_lastname = $author_1_array[1];
		$author_1_firstname = $author_1_array[2];
		$author_1_middlename = $author_1_array[3];
		$author_1_prefix = $author_1_array[4];
		$author_1_suffix = $author_1_array[5];
		
	if ($author_1_suffix != "") {
		$author_1_suffix = ", $author_1_suffix";
	}
	else {
		$author_1_suffix = "";
	}
	
	}	

}

if ($author_1_number == "0") {
		$author_1_id = "";
		$author_1_lastname = "";
		$author_1_firstname = "";
		$author_1_middlename = "";
		$author_1_prefix = "";
		$author_1_suffix = "";
}

if ($author_2_number != "0") {

	$author_2_query = "SELECT author_id, author_1_lastname, author_1_firstname, author_1_middlename, author_1_prefix, author_1_suffix FROM authors WHERE author_id = $author_2_number";
	$author_2_result = mysql_query("$author_2_query",$db);

	while ($author_2_array = mysql_fetch_row($author_2_result)) {
	
		$author_2_id = $author_2_array[0];
		$author_2_lastname = $author_2_array[1];
		$author_2_firstname = $author_2_array[2];
		$author_2_middlename = $author_2_array[3];
		$author_2_prefix = $author_2_array[4];
		$author_2_suffix = $author_2_array[5];
		$author_2_comma = ", ";
		
	if ($author_2_suffix != "") {
		$author_2_suffix = ", $author_2_suffix";
	}
	else {
		$author_2_suffix = "";
	}
	}
}

if ($author_2_number == "0") {
		$author_2_id = "";
		$author_2_lastname = "";
		$author_2_firstname = "";
		$author_2_middlename = "";
		$author_2_prefix = "";
		$author_2_suffix = "";
		$author_2_comma = "";
}

if ($author_3_number != "0") {

	$author_3_query = "SELECT author_id, author_1_lastname, author_1_firstname, author_1_middlename, author_1_prefix, author_1_suffix FROM authors WHERE author_id = $author_3_number";
	$author_3_result = mysql_query("$author_3_query",$db);

	while ($author_3_array = mysql_fetch_row($author_3_result)) {
	
		$author_3_id = $author_3_array[0];
		$author_3_lastname = $author_3_array[1];
		$author_3_firstname = $author_3_array[2];
		$author_3_middlename = $author_3_array[3];
		$author_3_prefix = $author_3_array[4];
		$author_3_suffix = $author_3_array[5];
		$author_3_comma = ", ";
		
	if ($author_3_suffix != "") {
		$author_3_suffix = ", $author_3_suffix";
	}
	else {
		$author_3_suffix = "";
	}
	}
}

if ($author_3_number == "0") {
		$author_3_id = "";
		$author_3_lastname = "";
		$author_3_firstname = "";
		$author_3_middlename = "";
		$author_3_prefix = "";
		$author_3_suffix = "";
		$author_3_comma = "";
}

if ($author_4_number != "0") {

	$author_4_query = "SELECT author_id, author_1_lastname, author_1_firstname, author_1_middlename, author_1_prefix, author_1_suffix FROM authors WHERE author_id = $author_4_number";
	$author_4_result = mysql_query("$author_4_query",$db);

	while ($author_4_array = mysql_fetch_row($author_4_result)) {
	
		$author_4_id = $author_4_array[0];
		$author_4_lastname = $author_4_array[1];
		$author_4_firstname = $author_4_array[2];
		$author_4_middlename = $author_4_array[3];
		$author_4_prefix = $author_4_array[4];
		$author_4_suffix = $author_4_array[5];
		$author_4_comma = ", ";
		
	if ($author_4_suffix != "") {
		$author_4_suffix = ", $author_4_suffix";
	}
	else {
		$author_4_suffix = "";
	}
	}
}

if ($author_4_number == "0") {
		$author_4_id = "";
		$author_4_lastname = "";
		$author_4_firstname = "";
		$author_4_middlename = "";
		$author_4_prefix = "";
		$author_4_suffix = "";
		$author_4_comma = "";
}

if ($author_5_number != "0") {

	$author_5_query = "SELECT author_id, author_1_lastname, author_1_firstname, author_1_middlename, author_1_prefix, author_1_suffix FROM authors WHERE author_id = $author_5_number";
	$author_5_result = mysql_query("$author_5_query",$db);

	while ($author_5_array = mysql_fetch_row($author_5_result)) {
	
		$author_5_id = $author_5_array[0];
		$author_5_lastname = $author_5_array[1];
		$author_5_firstname = $author_5_array[2];
		$author_5_middlename = $author_5_array[3];
		$author_5_prefix = $author_5_array[4];
		$author_5_suffix = $author_5_array[5];
		$author_5_comma = ", ";
		
	if ($author_5_suffix != "") {
		$author_5_suffix = ", $author_5_suffix";
	}
	else {
		$author_5_suffix = "";
	}
	}
}

if ($author_5_number == "0") {
		$author_5_id = "";
		$author_5_lastname = "";
		$author_5_firstname = "";
		$author_5_middlename = "";
		$author_5_prefix = "";
		$author_5_suffix = "";
		$author_5_comma = "";
}

if ($author_6_number != "0") {

	$author_6_query = "SELECT author_id, author_1_lastname, author_1_firstname, author_1_middlename, author_1_prefix, author_1_suffix FROM authors WHERE author_id = $author_6_number";
	$author_6_result = mysql_query("$author_6_query",$db);

	while ($author_6_array = mysql_fetch_row($author_6_result)) {
	
		$author_6_id = $author_6_array[0];
		$author_6_lastname = $author_6_array[1];
		$author_6_firstname = $author_6_array[2];
		$author_6_middlename = $author_6_array[3];
		$author_6_prefix = $author_6_array[4];
		$author_6_suffix = $author_6_array[5];
		$author_6_comma = ", ";
		
	if ($author_6_suffix != "") {
		$author_6_suffix = ", $author_6_suffix";
	}
	else {
		$author_6_suffix = "";
	}
	}
}

if ($author_6_number == "0") {
		$author_6_id = "";
		$author_6_lastname = "";
		$author_6_firstname = "";
		$author_6_middlename = "";
		$author_6_prefix = "";
		$author_6_suffix = "";
		$author_6_comma = "";
}

if ($author_7_number != "0") {

	$author_7_query = "SELECT author_id, author_1_lastname, author_1_firstname, author_1_middlename, author_1_prefix, author_1_suffix FROM authors WHERE author_id = $author_7_number";
	$author_7_result = mysql_query("$author_7_query",$db);

	while ($author_7_array = mysql_fetch_row($author_7_result)) {
	
		$author_7_id = $author_7_array[0];
		$author_7_lastname = $author_7_array[1];
		$author_7_firstname = $author_7_array[2];
		$author_7_middlename = $author_7_array[3];
		$author_7_prefix = $author_7_array[4];
		$author_7_suffix = $author_7_array[5];
		$author_7_comma = ", ";
		
	if ($author_7_suffix != "") {
		$author_7_suffix = ", $author_7_suffix";
	}
	else {
		$author_7_suffix = "";
	}
	}
}

if ($author_7_number == "0") {
		$author_7_id = "";
		$author_7_lastname = "";
		$author_7_firstname = "";
		$author_7_middlename = "";
		$author_7_prefix = "";
		$author_7_suffix = "";
		$author_7_comma = "";
		
}

if ($author_8_number != "0") {

	$author_8_query = "SELECT author_id, author_1_lastname, author_1_firstname, author_1_middlename, author_1_prefix, author_1_suffix FROM authors WHERE author_id = $author_8_number";
	$author_8_result = mysql_query("$author_8_query",$db);

	while ($author_8_array = mysql_fetch_row($author_8_result)) {
	
		$author_8_id = $author_8_array[0];
		$author_8_lastname = $author_8_array[1];
		$author_8_firstname = $author_8_array[2];
		$author_8_middlename = $author_8_array[3];
		$author_8_prefix = $author_8_array[4];
		$author_8_suffix = $author_8_array[5];
		$author_8_comma = ", ";
		
	if ($author_8_suffix != "") {
		$author_8_suffix = ", $author_8_suffix";
	}
	else {
		$author_8_suffix = "";
	}
	}
}

if ($author_8_number == "0") {
		$author_8_id = "";
		$author_8_lastname = "";
		$author_8_firstname = "";
		$author_8_middlename = "";
		$author_8_prefix = "";
		$author_8_suffix = "";
		$author_8_comma = "";
}

//This code specifies whether the text should appear below or next to the cover, and includes the appropriate formatting for each
if ($title_location == 1) {
$tdalign = "";
$title_below_code = "<div class=\"below_nexto_desc\">";
$title_below_close_code = "</div>";
$imagealign = " align=\"left\"";
}
if ($title_location == 2) {
$tdalign = " align=\"center\"";
$title_below_code = "<BR clear=\"all\"><div class=\"below_book_desc\">";
$title_below_close_code = "</div>";
$imagealign = " align=\"center\"";
}

// This tells i to increase plus one with each loop, so that it will represent the current record number

	$i++;
	
// These variables determine the layout of the table

##Set variable for number of columns
$number_of_table_columns = $page_columns;

### If there are fewer items than there are columns, the number of columns should equal the number of items -- this way we avoid having extra empty columns
if ($number_of_items_on_this_page < $number_of_table_columns) {
	$number_of_table_columns = $number_of_items_on_this_page;
	}
else {
	$number_of_table_columns = $number_of_table_columns;
	}

##Set the width of each column -- whatever percent that column is of the whole
$column_width = floor(100 / $number_of_table_columns);

###Determine the number of rows there should be (divide number of items by number of columns and round up)
$number_of_table_rows = ceil($number_of_items_on_this_page / $number_of_table_columns);

###Total number of grid spaces
$total_number_of_grid_spaces = ($number_of_table_rows * $number_of_table_columns);

### Number of extra grid spaces
$extra_grid_spaces = ($total_number_of_grid_spaces - $number_of_items_on_this_page);

###This is for figuring out whether the current cell starts or ends a row
$current_record_divided_by_number_of_columns = ($i / $number_of_table_columns);
$current_record_minus_one_divided_by_number_of_columns = (($i -1) / $number_of_table_columns);

// This is the code for cells in the center columns 
	$open_table_cell_tag = "
	<TD valign=\"top\" class=\"boxmid\" width=\"$column_width%\"$tdalign>
	";
	$close_table_cell_tag = "
	</TD>
	";

// This is the code for cells in the far-right column 
if ($current_record_divided_by_number_of_columns == round($current_record_divided_by_number_of_columns)) {
	$open_table_cell_tag = "
	<TD valign=\"top\" class=\"boxright\" width=\"$column_width%\"$tdalign>
	";
	$close_table_cell_tag = "
	</TD>
	</TR>
	";
}

// This is the code for cells in the far-left column 
if (round($current_record_minus_one_divided_by_number_of_columns) == ($current_record_minus_one_divided_by_number_of_columns)) {
	$open_table_cell_tag = "
	<TR>
	<TD valign=\"top\" class=\"boxleft\"$tdalign width=\"$column_width%\">
	";
	$close_table_cell_tag = "
	</TD>
	";
}


// This determines what the content of each cell looks like - format the data here!

if ($product_item_code != "0") {
	$arow = "
	$open_table_cell_tag<a href=\"detail.html?id=$product_item_code\"><img src=\"$small_graphic_code\" border=\"0\" alt=\"$product_title\"$imagealign class=\"page_image\">$title_below_code<span class=\"product_title\">$product_title</span></a>$sub_title_code<BR /><span class=\"product_authors\">$author_1_prefix $author_1_firstname $author_1_middlename $author_1_lastname$author_1_suffix$author_2_comma$author_2_prefix $author_2_firstname $author_2_middlename $author_2_lastname$author_2_suffix$author_3_comma$author_3_prefix $author_3_firstname $author_3_middlename $author_3_lastname$author_3_suffix$author_4_comma$author_4_prefix $author_4_firstname $author_4_middlename $author_4_lastname$author_4_suffix$author_5_comma$author_5_prefix $author_5_firstname $author_5_middlename $author_5_lastname$author_5_suffix$author_6_comma$author_6_prefix $author_6_firstname $author_6_middlename $author_6_lastname$author_6_suffix$author_7_comma$author_7_prefix $author_7_firstname $author_7_middlename $author_7_lastname$author_7_suffix$author_8_comma$author_8_prefix $author_8_firstname $author_8_middlename $author_8_lastname$author_8_suffix$description_code</span>$title_below_close_code$close_table_cell_tag
";	}

// This formats an 'empty' cell--ie, the placeholder where the product_item_code is 0

else {
	$arow = "$open_table_cell_tag &nbsp; $close_table_cell_tag";
	}
// $blog_text = $blog_text. the cell

$blog_text = $blog_text.("$arow");

}

// If there need to be any extra TDs at the end, format and $blog_text = $blog_text. them here

if ( $extra_grid_spaces != 0 ) {
	
		$blog_text = $blog_text.("
		<TD valign=\"top\" class=\"boxright\" colspan=$extra_grid_spaces>&nbsp;</TD>
		</TR>
		");
	
	}


// Close the table

$blog_text = $blog_text.("</TABLE>");

if ($total_pages > 1) {
// Build Page Number Hyperlinks 
$blog_text = $blog_text.("<p class=\"pageselect\">Select a Page:</p><p class=\"pageoptions\">"); 

// Build Previous Link 
if($page > 1){ 
    $prev = ($page - 1); 
    $blog_text = $blog_text.("<span class=\"previouspage\"><a href=\"".$_SERVER['PHP_SELF']."?id=$id&page=$prev\">&lt;&lt;Previous</a></span>&nbsp; "); 
} 

for($i_page = 1; $i_page <= $total_pages; $i_page++){ 
    if(($page) == $i_page){ 
        $blog_text = $blog_text.("<span class=\"currentpage\">$i_page</span> "); 
        } else { 
            $blog_text = $blog_text.("<span class=\"pagechoices\"><a href=\"".$_SERVER['PHP_SELF']."?id=$id&page=$i_page\">$i_page</a></span> "); 
    } 
} 

// Build Next Link 
if($page < $total_pages){ 
    $next = ($page + 1); 
    $blog_text = $blog_text.("&nbsp; <span class=\"nextpage\"><a href=\"".$_SERVER['PHP_SELF']."?id=$id&page=$next\">Next&gt;&gt;</a></span>"); 
} 
$blog_text = $blog_text.("</p><br clear=\"all\">"); 

}

}



	$blog_list = $blog_list.'<item>
<title><![CDATA[
'.$blog_title.'
]]></title>

<description><![CDATA[
'.$image_code.$blog_text.'
]]></description>
<pubDate>'.$blog_date.'</pubDate>
<author>'.$blog_author_email.' ('.$blog_author_name.')</author>
<guid isPermaLink="true"><![CDATA[
http://redwheelweiser.com/entry.php?id='.$blog_id.'
]]></guid>
<link><![CDATA[
http://redwheelweiser.com/entry.php?id='.$blog_id.'
]]></link>
</item>';
	
	
	}
	$blog_list = $blog_list;
?>

<rss version="2.0">
<channel>
 
<title>The Red Wheel/Weiser/Conari Blog</title>
<link>http://www.redwheelweiser.com/blog.php</link>
<description>This blog presents news and thoughts from the staff of Conari, Red Wheel, and Weiser.</description>
<language>en-us</language>
<lastBuildDate><?php echo $todays_date; ?> -0500</lastBuildDate>
<copyright>Copyright 2009 Conari</copyright>

<image>
<title>The Red Wheel/Weiser/Conari Blog</title>
<url>http://www.redwheelweiser.com</url>
<link>http://www.redwheelweiser.com</link>
<width>16</width>
<height>16</height>
</image>
<?php
echo $blog_list;
?>
</channel>
</rss>