<?php
///////////////////////////////////////////////////////////////////////////////////////
// PHPizabi 0.848b C1 [ALICIA]                               http://www.phpizabi.net //
///////////////////////////////////////////////////////////////////////////////////////
// Please read the license.txt file before using / modifying this software           //
// Original author:         Claude Desjardins, R - feedback@realitymedias.com        //
// Last modification date:  August 26th 2006                                         //
// Version:                 PHPizabi 0.848b C1                                       //
//                                                                                   //
// (C) 2005, 2006 Real!ty Medias / PHPizabi - All rights reserved                    //
///////////////////////////////////////////////////////////////////////////////////////

	/* Check Structure Availability */
	if (!defined("CORE_STRAP")) die("Out of structure call");
	
	// TEMPLATE HANDLING ////////////////////////////////////////////////////////////////// FINAL //
	/*
		Initialize the template engine and
		load the template file. Get objects,
		convert self user.
	*/
	$tpl = new template;
	$tpl -> Load("index");
	$tpl -> GetObjects();
	

	// TEMP //
	$s[0]["TITLE"] = "This is a subject";
	$s[0]["ID"] = "2147483647";
	$s[0]["LOCKED"] = false;
	$s[0]["TOPIC_COUNT"] = 1;
	$s[0]["POST_COUNT"] = 5;
	$s[0]["TYPES"] = "g,u";
	
	$s[1]["TITLE"] = "Yozemite ...";
	$s[1]["ID"] = "2147483646";
	$s[1]["LOCKED"] = false;
	$s[1]["TOPIC_COUNT"] = 10;
	$s[1]["POST_COUNT"] = 41;
	$s[1]["TYPES"] = "g,u";
	
	$s[2]["TITLE"] = "shouldnt allow guests";
	$s[2]["ID"] = "2147483611";
	$s[2]["LOCKED"] = false;
	$s[2]["TOPIC_COUNT"] = 0;
	$s[2]["POST_COUNT"] = 0;
	$s[2]["TYPES"] = "u";

	$s[3]["TITLE"] = "a locked topic";
	$s[3]["ID"] = "2147483610";
	$s[3]["LOCKED"] = true;
	$s[3]["TOPIC_COUNT"] = 0;
	$s[3]["POST_COUNT"] = 0;
	$s[3]["TYPES"] = "g,u";


//	$handle = fopen("system/cache/inkspot.dat", "w");
//	fwrite($handle, pk($s));
//	fclose($handle);
	
	// End
	
	// LOAD SUBJECTS /////////////////////////////////////////////////////////////////////
	/*
		Load the inkspot subjects array
	*/
	$inkSpotSubjects = unpk(file_get_contents("system/cache/inkspot.dat"));
	if (!is_array($inkSpotSubjects)) $inkSpotSubjects = array();
	
	/* 
		Set a couple cyclic counter variables
	*/
	$totalSubjects = 0;
	$totalTopics = 0;
	$totalPosts = 0;
	
	/* 
		Generate the inkspot subjects list
		array to be injected into the spot subjects
		block
	*/
	$i=0;
	foreach ($inkSpotSubjects as $key => $spotArray) {

	$topic = myF(myQ("select count(*) as count from `[x]inkspot` where `subject` = '{$spotArray["ID"]}'"));

	$posts = myF(myQ("select count(*) as count from `[x]inkspot` where `subject` = '{$spotArray["ID"]}' and `origin` != '1'"));

		$subjectsReplacementArray[$spotArray["ID"]] = array(

			"subject.title" => $spotArray["TITLE"],

			"subject.topicCount" => $topic["count"],

			"subject.postCount" => $posts["count"],

			"subject.id" => $spotArray["ID"]

		);

	$totalSubjects++;

	$totalTopics +=$topic["count"];

	$totalPosts += $posts["count"];

	$i++;
	}

	if (isset($subjectsReplacementArray)){

	$tpl->Zone("subjects","enabled");

	$tpl -> Loop("inkSpotSubjects", $subjectsReplacementArray);

	} else $tpl->Zone("subjects","disabled");



	$tpl -> AssignArray(array(

		"total.subjects" => $totalSubjects,

		"total.topics" => $totalTopics,

		"total.posts" => $totalPosts

	));

	

	// LOAD POPULAR TOPICS ///////////////////////////////////////////////////////////////

	$select = myQ("

		SELECT *, COUNT(topic) AS count FROM `[x]inkspot`

		GROUP BY `topic`

		ORDER BY `count` DESC

		LIMIT 10

	");

	if(myNum($select) > 0 ){

	while ($row = myF($select)) {

		

		$popularReplacementArray[] = array(

			"pop.date" => date($CONF["LOCALE_HEADER_DATE"], $row["date"]),

			"pop.username" => _fnc("user", $row["user"], "username"),

			"pop.topic" => $row["topic"],

			"pop.subject"=>$subjectsReplacementArray[$row["subject"]]["subject.title"],

			"pop.subid" => $row["subject"],

			"pop.count" => $row["count"]-1,

			"pop.id" => $row["id"],

			"pop.userid" => $row["user"],

			"pop.mainpicture" => _fnc("user", $row["user"], "mainpicture")

		);

	}

	

	if (isset($popularReplacementArray)) {	

		$tpl->Zone("popularTopicsList","enabled");

		$tpl -> Loop("popularTopicsList", $popularReplacementArray);

	}

	}

	// LOAD LAST TOPICS /////////////////////////////////////////////////////////////////

	$select = myQ("

		SELECT *, COUNT(topic) AS count FROM `[x]inkspot`

		GROUP BY `topic`

		ORDER BY `date` DESC

		LIMIT 10

	");

	

	if(myNum($select) > 0 ){

	while ($row = myF($select)) {

		

		$lastReplacementArray[] = array(

			"last.date" => date($CONF["LOCALE_HEADER_DATE"], $row["date"]),

			"last.username" => _fnc("user", $row["user"], "username"),

			"last.subject"=>$subjectsReplacementArray[$row["subject"]]["subject.title"],

			"last.subid" => $row["subject"],

			"last.topic" => $row["topic"],

			"last.count" => $row["count"]-1,

			"last.id" => $row["id"],

			"last.userid" => $row["user"],

			"last.mainpicture" => _fnc("user", $row["user"], "mainpicture")

		);

	}

	

	if (isset($lastReplacementArray)) {	

		$tpl->Zone("lastTopicsList","enabled");

		$tpl -> Loop("lastTopicsList", $lastReplacementArray);

	}

}	

	// LOAD LAST TOPICS /////////////////////////////////////////////////////////////////

	$select = myQ("

		SELECT *, COUNT(topic) AS count FROM `[x]inkspot`

		WHERE `user`='".me("id")."'

		GROUP BY `topic`

		ORDER BY `date` DESC

		LIMIT 10

	");

	if(myNum($select) > 0 ){	

	while ($row = myF($select)) {

		

		$participateReplacementArray[] = array(

			"par.date" => date($CONF["LOCALE_HEADER_DATE"], $row["date"]),

			"par.username" => _fnc("user", $row["user"], "username"),

			"par.subject"=>$subjectsReplacementArray[$row["subject"]]["subject.title"],

			"par.subid" => $row["subject"],

			"par.topic" => $row["topic"],

			"par.count" => $row["count"]-1,

			"par.id" => $row["id"],

			"par.userid" => $row["user"],

			"par.mainpicture" => _fnc("user", $row["user"], "mainpicture")

		);

	}

	

	if (isset($participateReplacementArray)) {	

		$tpl->Zone("participateTopicsList","enabled");

		$tpl -> Loop("participateTopicsList", $participateReplacementArray);

	}

	
}
	
	$tpl -> CleanZones();
	$tpl -> Flush();

	

?>