<?php

/*
 * AltSci Concepts Tag Display for Del.icio.us Mashup
 * by Javantea
 * Dec 13, 2008
 *
 * Description:

db tag -> db page

 */

// Require login.
//require('inc/auth.inc.php');
// The database needs to be loaded.
require('inc/common.inc.php');

?>
<!DOCTYPE HTML PUBLIC public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>AltSci Concepts Tag List Tag Cloud</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="/images/asc5.css" />
<link rel="alternate" type="application/rss+xml" title="RSS" href="/rss2.xml" />
<style>
.t0 { font-size:0.5em;}
.t1 { font-size:0.8em;}
.t2 { font-size:0.8em;}
.t3 { font-size:0.8em;}
.t4 { font-size:0.9em;}
.t5 { font-size:0.9em;}
.t6 { font-size:0.9em;}
.t7 { font-size:1.0em;}
.t8 { font-size:1.0em;}
.t9 { font-size:1.0em;}
.t10 { font-size:1.1em;}
.t11, .t12, .t13, .t14 { font-size:1.1em;}
.t15, .t16, .t17, .t18 { font-size:1.2em;}
.t19, .t20 { font-size:1.3em;}
.t21, .t22, .t23, .t24 { font-size:1.4em;}
.t25, .t26, .t27, .t28 { font-size:1.5em;}
.t29, .t30 { font-size:1.6em;}
.tagcloud a { text-decoration:none; }
.tagcloud { border:1px solid black; padding:4px; }
.blink { text-decoration:none; }
dl dt a { font-weight:bold; text-decoration:none; }
dl dd { padding:0; margin-left:5px; }
/*dl dd ul li { display:inline; padding-left:5px; }
dl dd ul { margin:0; padding:5px; }*/
</style>
</head>
<body style="background-color:#eeeeee;">
<div class="main">
<h1>AltSci Network</h1>
<h2>Tag Cloud, Article List, articles by date</h2>
<a href="https://www.altsci.com/rss2.xml" class="blink"><img src="/images/feed-icon-28x28.png" alt="RSS Feed" border="0"> RSS feed</a><br /><br />
<?php

$showTag = $_GET['q'];
$disp_tag = htmlentities($showTag, ENT_QUOTES);
$url_tag = urlencode($showTag);
$page = (int)$_GET['page'];
$perpage = 50;

/*if ($showSite == '') {
  $siteQuery = '';
} else {
  $siteQuery = 'AND s.site = "' . sql_validate($site) . '"';
}*/

if ($page != '') {
  $limit_start = ($page * $perpage);
  $page_str = "Page " . ($page+1);
} else {
  $limit = 0;
}

if ($page_str != '') {
  print "<p><a href=\"/tag/$url_tag\" class=\"blink\">&laquo; First</a><br />\n";
  print "$page_str";
} else {
  print "<p><a href=\"/\" class=\"blink\">&laquo; Home</a> ";
  if (!empty($showTag)) {
    print "<a href=\"/tag/\" class=\"blink\">&laquo; All</a>";
  }
  print "</p>\n";
}

print "<p><form action=\"/search/search\"><input type='text' name='q' value='$disp_tag' /> <input type='submit' value='Search' /></form>";

// Get a list of popular tags:
$limit_top = 'limit 50';
$limit_tags = 50;
//$tag_list = query("SELECT t.tag, count(p2t.id) as pages from tag t left outer join page2tag p2t on (t.id = p2t.tag_id or p2t.id is null) group by t.id order by pages desc $limit_top;");
if(!file_exists('/tmp/awesomecache1.txt'))
{
  $sql = $db3->prepare("SELECT t.tag, count(p2t.id) as pages from tag t left outer join page2tag p2t on (t.id = p2t.tag_id or p2t.id is null) group by t.id order by pages desc limit %d;", $limit_tags);
  $tags = $db3->get_results($sql);

  #$stmt->bind_result($tag, $pages);
  $top_tags = array();
  $top_tags_str = '';
  foreach ($tags as $tag) {
  //while ($tag = mysql_fetch_assoc($tag_list)) {
  	$top_tags[] = $tag->tag;
	#$top_tags_str .= "<a href=\"http://delicious.com/Javantea/$tag[tag]\" class=\"t$tag[pages]\" title=\"$tag[pages]\">$tag[tag]</a> ";
	$top_tags_str .= "<a href=\"/tag/$tag->tag\" class=\"t$tag->pages\" title=\"$tag->pages\">$tag->tag</a> ";
  }
  file_put_contents('/tmp/awesomecache1.txt', $top_tags_str);
}
else
{
  $top_tags_str = file_get_contents('/tmp/awesomecache1.txt');
}  
if (!empty($showTag)) {
        //$tag_id = query('SELECT t.id from tag t where t.tag="' . sql_validate($showTag) . '" limit 1');
        $limitx = 1;
	$sql = $db3->prepare("SELECT t.id from tag t where t.tag = %s limit %d;", $showTag, $limitx);
	#print '[' . htmlentities($sql) . ',' . htmlentities(print_r($showTag, true)) . ',' . $disp_tag . ',' . $url_tag . ']';
	$tag_id = $db3->get_var($sql);
	if (empty($tag_id)) {
                die("Fail.");
        }
	if ($showSite == '') {
		$sql = $db3->prepare("SELECT p.pagenum, s.shortname, p.id as pid, p.rant, s.name as section, p.title, m.name as author, p.PDate, t.name as site_name, t.url as site_url, SUBSTRING(p.rant, 1, 256) as content
		FROM page p, scene s, account m, site t, page2tag p2t
		WHERE p.member = m.id AND p.scene=s.id AND s.site = t.id AND p2t.page_id = p.id AND p2t.tag_id = %d AND p.OmitEnd = 'N' 
		GROUP BY p.id ORDER BY p.PDate DESC LIMIT %d, %d;", $tag_id, $limit_start, $perpage);
	} else {
		$sql = $db3->prepare("SELECT p.pagenum, s.shortname, p.id as pid, p.rant, s.name as section, p.title, m.name as author, p.PDate, t.name as site_name, t.url as site_url, SUBSTRING(p.rant, 1, 256) as content
		FROM page p, scene s, account m, site t, page2tag p2t
		WHERE p.member = m.id AND p.scene=s.id AND s.site = t.id AND p2t.page_id = p.id AND s.site = %d AND p2t.tag_id = %d AND p.OmitEnd = 'N' 
		GROUP BY p.id ORDER BY p.PDate DESC LIMIT %d, %d;", $site, $tag_id, $limit, $perpage);
	}
        /*$sql = "SELECT p.pagenum, s.shortname, p.id as pid, p.rant, s.name as section, p.title, m.name as author, p.PDate, t.name as site_name, t.url as site_url, SUBSTRING(p.rant, 1, 256) as content 
        FROM page p, scene s, account m, site t, page2tag p2t
        WHERE p.member = m.id AND p.scene=s.id AND s.site = t.id AND p2t.page_id = p.id $siteQuery AND p2t.tag_id = '$tag_id' AND p.OmitEnd = 'N' 
        GROUP BY p.id ORDER BY p.PDate DESC LIMIT $limit;";*/
} else {
	if ($showSite == '') {
		$sql = $db3->prepare("SELECT p.pagenum, s.shortname, p.id as pid, p.rant, s.name as section, p.title, m.name as author, p.PDate, t.name as site_name, t.url as site_url, SUBSTRING(p.rant, 1, 256) as content
		FROM page p, scene s, account m, site t
		WHERE p.member = m.id AND p.scene=s.id AND s.site = t.id AND p.OmitEnd = 'N' 
		GROUP BY p.id ORDER BY p.PDate DESC LIMIT %d, %d;", $limit_start, $perpage);
	} else {
		$sql = $db3->prepare("SELECT p.pagenum, s.shortname, p.id as pid, p.rant, s.name as section, p.title, m.name as author, p.PDate, t.name as site_name, t.url as site_url, SUBSTRING(p.rant, 1, 256) as content
		FROM page p, scene s, account m, site t
		WHERE p.member = m.id AND p.scene=s.id AND s.site = t.id AND s.site = %d AND p.OmitEnd = 'N' 
		GROUP BY p.id ORDER BY p.PDate DESC LIMIT %d, %d;", $site, $limit_start, $perpage);
	}
        /*$sql = "SELECT p.pagenum, s.shortname, p.id as pid, p.rant, s.name as section, p.title, m.name as author, p.PDate, t.name as site_name, t.url as site_url, SUBSTRING(p.rant, 1, 256) as content 
        FROM page p, scene s, account m, site t 
        WHERE p.member = m.id AND p.scene=s.id AND s.site = t.id $siteQuery AND p.OmitEnd = 'N' 
        GROUP BY p.id ORDER BY p.PDate DESC LIMIT $limit;";*/
}

#$stmt->bind_result($pagenum, $shortname, $pid, $rant, $section, $title, $author, $date, $site_name, $site_url, $content);
$pages = $db3->get_results($sql);

print "<div class=\"tagcloud\">$top_tags_str</div><br />\n";

$delurl = '';
if (!empty($showTag)) {
        $delurl = '/' . $disp_tag;
        print '<div class="dellink1"><a href="http://delicious.com/tag' . $delurl . '" class="blink"><img src="/images/delicious.20.png" alt="Delicious" border="0"> ' . $disp_tag . ' on Delicious</a></div>' . "\n";
}
print '<div class="dellink1"><a href="http://delicious.com/Javantea' . $delurl . '" class="blink"><img src="/images/delicious.20.png" alt="Delicious" border="0"> ' . $disp_tag . ' Delicious Bookmarks</a> from the author</div>' . "\n";

print '<dl class="alist">';

$shown = 0;
//$articles = query($sql);
//while ($article = mysql_fetch_assoc($articles)) {
foreach ($pages as $page_row) {
	//$date = $article['PDate']; #date('Y-m-d', $article['PDate']);
	//$title = $article['title'];
	if ($page_row->title == '') {
		$page_row->title = "Untitled";
	}
	$sql = $db3->prepare("SELECT t.tag from page2tag p2t join tag t on t.id = p2t.tag_id where p2t.page_id = %d group by t.id", $pid);
	//$tag_list = query("SELECT t.tag from page2tag p2t join tag t on t.id = p2t.tag_id where p2t.page_id = '" . sql_validate($article['pid']) . "' group by t.id;");
	$tags = array();
	$tag_list = $db3->get_results($sql);
	foreach ($tag_list as $tag) {
		$tags[] = $tag->tag;
	}
	$tags = '<span>' . join('</span> <span>', $tags) . '</span>';
	print <<<_DOC
	<dt><a href="{$page_row->site_url}page.php?s=$page_row->shortname&p=$page_row->pagenum">$page_row->title</a></dt>
	<dd>In $page_row->site_name:$page_row->section, by $page_row->author on <span class="date">$page_row->PDate</span>.<br />
	$tags
	</dd>
_DOC;
	$shown++;
}
print <<<_DOC
	</dl>
_DOC;

print "<div class=\"nav2\">\n";
if ($shown == $perpage) {
  $end = $page + 5;
} else {
  $end = $page;
}
#if ($end < 9) $end = 9;
if (empty($page)) {
  print "<span>1</span>";
} else {
  print "<a href='/tag/$url_tag'>1</a> \n";
}
for ($i = 1; $i < $end; $i++) {
  $j = $i+1;
  if ($page == $i) {
    print "<span'>$j</span> \n";
  } else {
    print "<a href='/tag/$url_tag?page=$i'>$j</a> \n";
  }
}

?>
</div>

</div>
<br clear="all" />
<div>
&copy; 2000-2008 AltSci Concepts
</div>
</body>
</html>