<?php
// minden nap 07:30-kor		- kék / sárga könyvből üzeneteket elküldeni e-mailben
include("config.php");
include("version.php");
include("email.php");
require_once("../configuration.php");

$managerek = array("25", "10");	// egyelore csak Illesnek es nekem
$kiirni = "";
$conf = new JConfig();
foreach (array(CRMADATBAZIS) as $db) {
	foreach ($managerek as $i) {	// foreach managers
		$ma = date("Y-m-d");

		$email = new CI_Email();
		$email->initialize();
		$email->setFrom($conf->mailfrom, 'CadLine');

		$mailsql = "SELECT strMail FROM `{$db}`.managers WHERE nManagerID={$i}";
		$msh->query($mailsql);
		$mailrow = $msh->fetchNext();
		$m = (object)$mailrow;
		$email = $m->strMail;

		if (!$email)
			continue;

		if ($email == "barthaattila@hotmail.com")
			$email = "eszter@cadline.hu";

		$query = "SELECT nMsgID, strTopic, strMsg, nUserID FROM `{$db}`.`u_messages` WHERE nManagerID={$i} AND `dateSend` = '{$ma}' AND bFlag=0 ";

		$msh->query($query);
		$result = $msh->fetchAssoc();

		foreach ($result as $row) {		// foreach messages
			$r = (object)$row;
			$message = $r->strMsg . "<br><br>";
			$message .= "<a href='https://crm.cadline.hu/hu/users/show/" . $r->nUserID . "'>";
			$message .= "https://crm.cadline.hu/hu/users/show/" . $r->nUserID;
			$message .= "</a>";

			$email->subject($r->strTopic);
			$email->message($message);
			$elkuldve = $email->send($email, 'smtp');

			print "kuldom: " . $email . "-nek: " . $message;

			if ($elkuldve) {
				$valtoztatas['bFlag'] = 1;
				$msh->update("{$db}.u_messages", $valtoztatas, "nMsgID=" . $r->nMsgID);
				$kiirni .= " elkuldve";
			}

			$email->clear();
		}

		$kiirni .= "<br /><br />";
	}
}

print $kiirni;
