<?php
/**
 * AgileBill - Open Billing Software
 *
 * This body of work is free software; you can redistribute it and/or
 * modify it under the terms of the Open AgileBill License
 * License as published at http://www.agileco.com/agilebill/license1-4.txt
 *
 * Originally authored by Tony Landis, AgileBill LLC
 *
 * Recent modifications by Deon George
 *
 * @author Deon George <deonATleenooksDOTnet>
 * @copyright 2009 Deon George
 * @link http://osb.leenooks.net
 *
 * @link http://www.agileco.com/
 * @copyright 2004-2008 Agileco, LLC.
 * @license http://www.agileco.com/agilebill/license1-4.txt
 * @author Tony Landis <tony@agileco.com>
 * @package AgileBill
 * @subpackage Module:EmailQueue
 */

/**
 * The main AgileBill Email Queue Class
 *
 * @package AgileBill
 * @subpackage Module:EmailQueue
 */
class email_queue extends OSB_module {
	/**
	 * Send mails in the queue
	 */
	public function send($VAR) {
		$db = &DB();
		$rs = $db->Execute(sqlSelect($db,'email_queue','*',array('status'=>0)));

		if ($rs && $rs->RecordCount()) {
			require_once(PATH_MODULES.'email_template/email_template.inc.php');

			while (! $rs->EOF) {
				# Get values
				$sql = array();
				$sql[1] = $rs->fields['sql1'];
				$sql[2] = $rs->fields['sql2'];
				$sql[3] = $rs->fields['sql3'];

				# Unserialize the SQL data if required.
				foreach ($sql as $i)
					if (preg_match('/^a:/',$sql[$i]) && is_array($a = unserialize($sql[$a])))
						$sql[$i] = $a;

				# Send email
				$mail = new email_template;
				$result = $mail->send($rs->fields['email_template'],$rs->fields['account_id'],$sql[1],$sql[2],$sql[3],false);

				# Update to sent status
				if ($result)
					$db->Execute(sqlUpdate($db,'email_queue',array('status'=>1),array('id'=>$rs->fields['id'])));

				$rs->MoveNext();
			}
		}
	}
}
?>