<?php
// $Id: remind_mail.php,v 1.1 2004/07/11 10:27:56 Farsus Exp $
// ------------------------------------------------------------------------- //
//               E-Xoops: Content Management for the Masses                  //
//                       < http://www.e-xoops.com >                          //
// ------------------------------------------------------------------------- //
// Original Author: Nobse
// Author Website : http://www.e-xoops.de
// License Type   : GPL: See /manual/LICENSES/GPL.txt
// ------------------------------------------------------------------------- //
function remindmail(){
global $db, $meta, $xoopsConfig;

$query = $db->query("SELECT date FROM ".$db->prefix("ecal_remind")." WHERE uid= 0");
list($numrows) = $db->fetch_row($query);
if (!$numrows) {
	$remindstart = strtotime ("+1 Day",strtotime(date("Y/m/d 0:0:0",time())));
	$newid = $db->genId($db->prefix('ecal_remind')."_id_seq");
	$db->query("INSERT INTO ".$db->prefix("ecal_remind")." SET id = $newid, tid = 0, uid = 0, date = $remindstart ");
              
}elseif ($numrows == strtotime(date("Y/m/d 0:0:0",time()))){
	include_once(XOOPS_ROOT_PATH.'/class/xoopsmailer.php');
	$start = strtotime(date("Y/m/d 0:0:0",time()));
	$end = strtotime("+1 Day",$start);
	$query = $db->query("SELECT * FROM ".$db->prefix("ecal_remind")." WHERE date >= $start and date < $end and uid > 0");	

	while($myrow = $db->fetch_array($query)) {
		$sql = $db->query("SELECT email, uname FROM ".$db->prefix("users")." WHERE uid=".$myrow['uid']."");
		$userdata = $db->fetch_array($sql);
		
		$sql1 = $db->query("SELECT stamp, subject FROM ".$db->prefix("ecal")." WHERE id=".$myrow['tid']."");
		$termindata = $db->fetch_array($sql1);
		$year = substr($termindata['stamp'],0,4);
		$month = substr($termindata['stamp'],5,2);
		$day = substr($termindata['stamp'],8,2); 
		$termindatum = strftime(_EVEDATESHORT,strtotime($termindata['stamp']));
		$terminbetreff = XOOPS_URL."/modules/ecal/display.php?day=$day&month=$month&year=$year";
		
		$xoopsMailer =& getMailer();
		$xoopsMailer->useMail();
		$xoopsMailer->setTemplate("remind.tpl");
		$xoopsMailer->setTemplateDir(XOOPS_ROOT_PATH."/modules/ecal");
		
		$xoopsMailer->assign("SITENAME", $meta['title']);
		$xoopsMailer->assign("ADMINMAIL", $xoopsConfig['adminmail']);
		$xoopsMailer->assign("SITEURL", XOOPS_URL."/");
		$xoopsMailer->assign("USERNAME", $userdata['uname']);
		$xoopsMailer->assign("TERMINDATUM", $termindatum);
		$xoopsMailer->assign("TERMINBETREFF", $terminbetreff);
		
		$xoopsMailer->setToEmails($userdata['email']);
		$xoopsMailer->setFromEmail($mail);
		$xoopsMailer->setFromName($meta['title']);
		$xoopsMailer->setSubject(_CAL_MAILSUBJECT);
		$xoopsMailer->send();
		$db->query("DELETE FROM ".$db->prefix("ecal_remind")." WHERE id=".$myrow['id']."");
	}
	$db->query("UPDATE ".$db->prefix("ecal_remind")." SET date = $end WHERE uid = 0 ");

}
}
?>