<?php
/*
	plan.php

	SQL-backed blog-like thing akin to a .plan, with archives

	Copyright (C) 2000-2007 Jeff Teunissen <deek@quakeforge.net>
	Copyright (C) 2000 Daniel David Olson <theoddone33@users.sourceforge.net>

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

	See the GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to:

	Free Software Foundation, Inc.
	59 Temple Place - Suite 330
	Boston, MA  02111-1307, USA.
*/

have ('plan');
									// SQL definition for news table
$createQuery="CREATE TABLE plans ("
			."	p_id	INT NOT NULL PRIMARY KEY AUTO_INCREMENT,"
			."	p_date	TIMESTAMP NOT NULL DEFAULT NOW(),"
			."	p_user	TINYTEXT NOT NULL,"
			."	p_title	TINYTEXT NOT NULL,"
			."	p_plan	TEXT NOT NULL"
			.")";

function formatPlan ($date, $user, $subj, $text)
{
	return "<DL>\n"
		.'	<DT>'
		. 		"<EM>Posted on </EM>$date<EM> by </EM><STRONG>$user</STRONG>"
		.	"</DT>\n"
		."	<DD><H4>$subj</H4>\n"
		."	$text</DD>\n"
		."</DL>\n";
}


function formatPlanArray ($a)
{
	need ('date');

	return formatPlan (dateFromSQLDateTime ($a['p_date']), $a['p_user'], stripSlashes ($a['p_title']), stripSlashes ($a['p_plan']));
}


function printPlan ($date, $user, $subj, $text)
{
	echo formatPlan ($date, $user, $subj, $text);
}


function printPlanArray ($a)
{
	echo formatPlanArray ($a);
}


function latestPlans ($user = null, $max = 5)
{
	need ('sql');

	if ($user) {
		$query = 'SELECT p_date, p_user, p_title, p_plan FROM plans'
				." WHERE p_user = "
				."  (SELECT u_displayname FROM members WHERE u_username='$user')"
				." ORDER BY p_date DESC LIMIT $max";
	} else {
		$query = 'SELECT p_date, p_user, p_title, p_plan FROM plans, members'
				." WHERE p_user = u_displayname"
				." ORDER BY p_date DESC LIMIT $max";
	}

	return sqlReadQuery ($query);
}


function printLatestPlans ($user = null, $max = 5)
{
	$filler = date ('d M Y');
	$entries = latestPlans ($user, $max);

	if ($entries === true) {	// success, but no data
		printPlan ($filler, 'Web Server', 'Sorry', 'Nobody has posted news yet, so I got nothing to say right now.');
	} elseif ($entries === false) {	// bad query
		printPlan ($filler, 'Web Server', 'Sorry', 'Got an error while finding news. Bug a project administrator, eh?');
	} elseif ($entries === null) {	// boom
		printPlan ($filler, 'Web Server', 'Sorry', 'Couldn\'t connect to the project news server...Sorry.');
	} else foreach ($entries as $entry) {
		printPlanArray ($entry);
	}
}


function searchPlans ($string, $user = null)
{
	need ('sql');

	if ($user)
		$where = " AND p_user='$user' ";

	$filler = date ('d M Y');
	$search = addSlashes ("%$string%");
	$query = 'SELECT p_date, p_user, p_title, p_plan FROM plans'
			." WHERE p_plan LIKE '$search'"
			. $where
			.' ORDER BY n_date DESC';

	$entries = sqlReadQuery ($query);
	if ($entries === true) {	// success, but no data
		printPlan ($filler, 'Web Server', 'Sorry', "No plan entries found matching '$string'");
	} elseif ($entries === false) {	// bad query
		printPlan ($filler, 'Web Server', 'Sorry', 'Got an error while finding news. Bug a project administrator, eh?');
	} elseif ($entries === null) {	// boom
		printPlan ($filler, 'Web Server', 'Sorry', 'Couldn\'t connect to the project plan server.');
	} else foreach ($entries as $entry) {
		printPlanArray ($entry);
	}
}


function usersWithPlans ()
{
	need ('sql');

	$query = 'SELECT u_username, u_displayname FROM members,plans'
			.' WHERE u_displayname=p_user'
			.' ORDER BY u_displayname';

	return sqlReadQuery ($query);
}
?>
