<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
//v1.2 www.exoops.info 2006/03 
// $Id: remind_mail.php,v 1.1 2004/02/06 21:22:12 nobse 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, $bcoosConfig;

$query = $db->query("SELECT date FROM ".$db->prefix("ecal_remind")." WHERE uid= 0");
list($numrows) = $db->fetchRow($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->fetchArray($query)) {
		$sql = $db->query("SELECT email, uname FROM ".$db->prefix("users")." WHERE uid=".$myrow['uid']."");
		$userdata = $db->fetchArray($sql);
		
		$sql1 = $db->query("SELECT stamp, subject FROM ".$db->prefix("ecal")." WHERE id=".$myrow['tid']."");
		$termindata = $db->fetchArray($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", $bcoosConfig['title']);
		$xoopsMailer->assign("ADMINMAIL", $bcoosConfig['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($bcoosConfig['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 ");

}
}
?>