Commit fd68e46f authored by Carsten  Rose's avatar Carsten Rose
Browse files

Feature 4250 / autocron: sending mails

parent b6c2dcee
......@@ -10,11 +10,12 @@ namespace qfq;
use qfq;
//require_once(__DIR__ . '/../qfq/QuickFormQuery.php');
require_once(__DIR__ . '/../qfq/store/Store.php');
require_once(__DIR__ . '/../qfq/Constants.php');
require_once(__DIR__ . '/../qfq/database/Database.php');
require_once(__DIR__ . '/../qfq/exceptions/ShellException.php');
require_once(__DIR__ . '/../qfq/Evaluate.php');
require_once(__DIR__ . '/../qfq/report/Sendmail.php');
class AutoCron {
......@@ -28,6 +29,11 @@ class AutoCron {
*/
protected $store = null;
/**
* @var Evaluate
*/
protected $evaluate = null;
/**
* @var bool
*/
......@@ -43,7 +49,7 @@ class AutoCron {
$this->store = Store::getInstance();
$this->db = new Database();
// $this->eval = new Evaluate($this->store, $this->db);
$this->evaluate = new Evaluate($this->store, $this->db);
}
/**
......@@ -89,7 +95,7 @@ class AutoCron {
// Do the calculation with MySQL to stay in the MySQL date/time calculation world.
$sql = "SELECT DATE_ADD('" . $job[AUTOCRON_NEXT_RUN] . "', INTERVAL CEILING( ( 1 + TIMESTAMPDIFF($unit, '" .
$job[AUTOCRON_NEXT_RUN] . "', NOW() ) ) / $count ) * " . $job[AUTOCRON_FREQUENCY] . ") AS " . AUTOCRON_NEXT_RUN;
$row = $this->db->sql($sql); // frequency, nextRun, unit, count
$row = $this->db->sql($sql, ROW_EXPECT_1); // frequency, nextRun, unit, count
$job[AUTOCRON_NEXT_RUN] = $row[AUTOCRON_NEXT_RUN];
......@@ -97,8 +103,10 @@ class AutoCron {
}
/**
* Calls the Webpage given in $job[AUTOCRON_CONTENT].
*
* @param array $job
* @return array
* @return array $job, updated with AUTOCRON_LAST_STATUS
*/
private function doJobWebsite(array $job) {
......@@ -124,11 +132,71 @@ class AutoCron {
}
/**
* @param array $mailEntry
* @return array
*/
private function mailEntryFill(array $mailEntry) {
foreach ([FE_SENDMAIL_TO, FE_SENDMAIL_CC, FE_SENDMAIL_BCC, FE_SENDMAIL_FROM, FE_SENDMAIL_SUBJECT,
FE_SENDMAIL_REPLY_TO, FE_SENDMAIL_FLAG_AUTO_SUBMIT, FE_SENDMAIL_GR_ID, FE_SENDMAIL_X_ID] as $key) {
if (!isset($mailEntry[$key])) {
$mailEntry[$key] = '';
}
}
return $mailEntry;
}
/**
* Send as many emails as $job[AUTOCRON_SQL1] has records.
* Do not send mails if: no record OR no receiver OR empty body
*
* @param array $job
* @return array $job, updated with AUTOCRON_LAST_STATUS
*/
private function doJobMail(array $job) {
$mailArr = $this->evaluate->parse($job[AUTOCRON_SQL1]);
$mailsSent = 0;
foreach ($mailArr as $mailEntry) {
$mailEntry = $this->mailEntryFill($mailEntry);
$content = $this->evaluate->parse($job[AUTOCRON_CONTENT]);
if ($content == '' OR ($mailEntry[FE_SENDMAIL_TO] == '' AND $mailEntry[FE_SENDMAIL_CC] == '' AND $mailEntry[FE_SENDMAIL_BCC] == '')) {
continue; // no receiver: skip
}
$mail[SENDMAIL_IDX_RECEIVER] = $this->evaluate->parse($mailEntry[FE_SENDMAIL_TO]);
$mail[SENDMAIL_IDX_SENDER] = $this->evaluate->parse($mailEntry[FE_SENDMAIL_FROM]);
$mail[SENDMAIL_IDX_SUBJECT] = $this->evaluate->parse($mailEntry[FE_SENDMAIL_SUBJECT]);
$mail[SENDMAIL_IDX_BODY] = $content;
$mail[SENDMAIL_IDX_REPLY_TO] = $this->evaluate->parse($mailEntry[FE_SENDMAIL_REPLY_TO]);
$mail[SENDMAIL_IDX_FLAG_AUTO_SUBMIT] = $this->evaluate->parse($mailEntry[FE_SENDMAIL_FLAG_AUTO_SUBMIT]) === 'off' ? 'off' : 'on';
$mail[SENDMAIL_IDX_GR_ID] = $this->evaluate->parse($mailEntry[FE_SENDMAIL_GR_ID]);
$mail[SENDMAIL_IDX_X_ID] = $this->evaluate->parse($mailEntry[FE_SENDMAIL_X_ID]);
$mail[SENDMAIL_IDX_RECEIVER_CC] = $this->evaluate->parse($mailEntry[FE_SENDMAIL_CC]);
$mail[SENDMAIL_IDX_RECEIVER_BCC] = $this->evaluate->parse($mailEntry[FE_SENDMAIL_BCC]);
$mail[SENDMAIL_IDX_SRC] = "AutoCron: Cron.id=" . $job[COLUMN_ID];
// Mail: send
new Sendmail($mail);
$mailsSent++;
}
$job[AUTOCRON_LAST_STATUS] = "OK: $mailsSent mails sent";
return $job;
}
/**
* Iterates over all AutoCron jobs and fire pending.
*
* @throws CodeException
* @throws DbException
*/
public function process() {
$sql = "SELECT c.id, c.type, c.content, c.frequency, c.nextRun FROM Cron AS c WHERE c.status='enable' AND c.nextRun < NOW() AND c.nextRun!=0 AND c.inProgress=0";
// Select pending jobs
$sql = "SELECT c.id, c.type, c.content, c.frequency, c.nextRun, c.sql1 FROM Cron AS c WHERE c.status='enable' AND c.nextRun < NOW() AND c.nextRun!=0 AND c.inProgress=0";
$jobs = $this->db->sql($sql);
// Iterate over all AutoCron Jobs
......
......@@ -1146,6 +1146,7 @@ const AUTOCRON_FREQUENCY = 'frequency';
const AUTOCRON_IN_PROGRESS = 'inProgress';
const AUTOCRON_STATUS = 'status';
const AUTOCRON_CONTENT = 'content';
const AUTOCRON_SQL1 = 'sql1';
const AUTOCRON_UNIT = 'unit';
const AUTOCRON_COUNT = 'count';
......
......@@ -559,6 +559,11 @@ class Support {
* @return mixed
*/
public static function decryptDoubleCurlyBraces($text) {
if (!is_string($text)) {
return $text;
}
$text = str_replace(LONG_CURLY_OPEN, '{{', $text);
$text = str_replace(LONG_CURLY_CLOSE, '}}', $text);
......
......@@ -15,7 +15,7 @@ class Sendmail {
* Sends a mail as specified in $mailarr.
* If there is no receiver specified as 'TO': no mail is sent. This is ok and no error.
* Logs every send mail as a record in table `mailLog`. Additionally a `grId` and a `xId` can be specified
* to assing the logentry to a specific action.
* to assign the log entry to a specific action.
* The log record also contains some information which generates the mail (form/formelement or QFQ query).
*
* Structure mailarr:
......@@ -101,7 +101,7 @@ class Sendmail {
* Creates a new MailLog Record based on $mailArr / $header.
*
* @param array $mailarr
* @param $headers
* @param $header
* @throws CodeException
* @throws DbException
*/
......@@ -115,9 +115,9 @@ class Sendmail {
$log[SENDMAIL_IDX_SUBJECT] = $mailarr[SENDMAIL_IDX_SUBJECT];
$log[SENDMAIL_IDX_BODY] = $mailarr[SENDMAIL_IDX_BODY];
$log[4] = $header;
$log[5] = $mailarr[SENDMAIL_IDX_GR_ID];
$log[6] = $mailarr[SENDMAIL_IDX_X_ID];
$log[7] = $mailarr[SENDMAIL_IDX_SRC];
$log[5] = empty($mailarr[SENDMAIL_IDX_GR_ID]) ? 0 : $mailarr[SENDMAIL_IDX_GR_ID];
$log[6] = empty($mailarr[SENDMAIL_IDX_X_ID]) ? 0 : $mailarr[SENDMAIL_IDX_X_ID];
$log[7] = empty($mailarr[SENDMAIL_IDX_SRC]) ? 0 : $mailarr[SENDMAIL_IDX_SRC];
$db = new Database();
$db->sql('INSERT INTO MailLog (`receiver`, `sender`, `subject`, `body`, `header`, `grId`, `xId`, `src`) VALUES ( ?, ? ,?, ?, ? ,?, ?, ? )', ROW_REGULAR, $log);
......
......@@ -417,6 +417,7 @@ CREATE TABLE IF NOT EXISTS `Cron` (
`frequency` VARCHAR(32) NOT NULL,
`inProgress` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
`status` ENUM('enable', 'disable') NOT NULL DEFAULT 'enable',
`sql1` TEXT NOT NULL,
`content` TEXT NOT NULL,
`comment` TEXT NOT NULL,
`modified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment