mirror of
https://github.com/OpenXE-org/OpenXE.git
synced 2024-12-25 14:10:28 +01:00
Migrated chat cronjob to new systemmailer
This commit is contained in:
parent
7afba4a3fa
commit
77b2d7e2d3
@ -7,6 +7,7 @@
|
|||||||
* - Cronjob prüft ob es Chat-Nachrichten gibt die seit 30 Minuten ungelesen sind.
|
* - Cronjob prüft ob es Chat-Nachrichten gibt die seit 30 Minuten ungelesen sind.
|
||||||
* Benutzer wird dann per Mail über ungelesene Chat-Nachrichten informiert.
|
* Benutzer wird dann per Mail über ungelesene Chat-Nachrichten informiert.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if(!class_exists('app_t2')) {
|
if(!class_exists('app_t2')) {
|
||||||
class app_t2 extends ApplicationCore
|
class app_t2 extends ApplicationCore
|
||||||
{
|
{
|
||||||
@ -38,6 +39,7 @@ if(empty($app->erp)) {
|
|||||||
|
|
||||||
// Alle Benutzer ermitteln die seit 30 Minuten ungelesene private Nachrichten haben
|
// Alle Benutzer ermitteln die seit 30 Minuten ungelesene private Nachrichten haben
|
||||||
// u.kalender_ausblenden = "Im Kalender/Chat ausblenden"; außerdem keine Chat-Benachrichtigung empfangen
|
// u.kalender_ausblenden = "Im Kalender/Chat ausblenden"; außerdem keine Chat-Benachrichtigung empfangen
|
||||||
|
|
||||||
$usersWithUnreadPrivateMessages = $app->DB->SelectArr(
|
$usersWithUnreadPrivateMessages = $app->DB->SelectArr(
|
||||||
'SELECT c.user_to AS id, MAX(c.zeitstempel) AS message_date, COUNT(c.id) AS message_count
|
'SELECT c.user_to AS id, MAX(c.zeitstempel) AS message_date, COUNT(c.id) AS message_count
|
||||||
FROM `user` AS u
|
FROM `user` AS u
|
||||||
@ -45,7 +47,7 @@ $usersWithUnreadPrivateMessages = $app->DB->SelectArr(
|
|||||||
LEFT JOIN `chat_gelesen` AS g ON c.id = g.message
|
LEFT JOIN `chat_gelesen` AS g ON c.id = g.message
|
||||||
WHERE g.id IS NULL AND u.activ = 1 AND u.kalender_ausblenden != 1 AND DATE_ADD(c.zeitstempel, INTERVAL 30 MINUTE) < NOW()
|
WHERE g.id IS NULL AND u.activ = 1 AND u.kalender_ausblenden != 1 AND DATE_ADD(c.zeitstempel, INTERVAL 30 MINUTE) < NOW()
|
||||||
GROUP BY c.user_to'
|
GROUP BY c.user_to'
|
||||||
);
|
);
|
||||||
|
|
||||||
// Alle Benutzer ermitteln die seit 30 Minuten ungelesene öffentliche Nachrichten haben
|
// Alle Benutzer ermitteln die seit 30 Minuten ungelesene öffentliche Nachrichten haben
|
||||||
// u.kalender_ausblenden = "Im Kalender/Chat ausblenden"; außerdem keine Chat-Benachrichtigung empfangen
|
// u.kalender_ausblenden = "Im Kalender/Chat ausblenden"; außerdem keine Chat-Benachrichtigung empfangen
|
||||||
@ -65,12 +67,9 @@ $usersWithUnreadMessages = combineUserResult(
|
|||||||
);
|
);
|
||||||
|
|
||||||
if(empty($usersWithUnreadMessages)){
|
if(empty($usersWithUnreadMessages)){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// PHPMailer konfigurieren
|
|
||||||
ConfigurePhpMailer();
|
|
||||||
|
|
||||||
foreach ($usersWithUnreadMessages as $user) {
|
foreach ($usersWithUnreadMessages as $user) {
|
||||||
|
|
||||||
$previousDate = (int)getUserCacheValue($user['id']);
|
$previousDate = (int)getUserCacheValue($user['id']);
|
||||||
@ -79,10 +78,10 @@ foreach ($usersWithUnreadMessages as $user) {
|
|||||||
// Datum gleich > User hat bereits Benachrichtigung bekommen + keine neuen Nachrichten vorhanden > Benachrichtigung NICHT senden
|
// Datum gleich > User hat bereits Benachrichtigung bekommen + keine neuen Nachrichten vorhanden > Benachrichtigung NICHT senden
|
||||||
// Datum ungleich > User hat bereits Benachrichtigung bekommen + Neue Nachrichten vorhanden > Benachrichtigung senden
|
// Datum ungleich > User hat bereits Benachrichtigung bekommen + Neue Nachrichten vorhanden > Benachrichtigung senden
|
||||||
// Kein Datum hinterlegt > User hat noch nie Benachrichtigung bekommen > Benachrichtigung senden
|
// Kein Datum hinterlegt > User hat noch nie Benachrichtigung bekommen > Benachrichtigung senden
|
||||||
if($previousDate === false || $previousDate !== $currentDate){
|
// if(empty($previousDate) || $previousDate !== $currentDate){
|
||||||
sendNotificationMail($user['id'], $user['private_count'], $user['public_count']);
|
sendNotificationMail($user['id'], $user['private_count'], $user['public_count']);
|
||||||
setUserCacheValue($user['id'], $currentDate);
|
setUserCacheValue($user['id'], $currentDate);
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -91,55 +90,6 @@ foreach ($usersWithUnreadMessages as $user) {
|
|||||||
******************************************/
|
******************************************/
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* PHPMailer konfigurieren
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
function ConfigurePhpMailer()
|
|
||||||
{
|
|
||||||
global $app;
|
|
||||||
|
|
||||||
// Mail-Konfiguration laden
|
|
||||||
$mailConfig = [
|
|
||||||
'benutzername' => $app->erp->Firmendaten("benutzername"),
|
|
||||||
'passwort' => $app->erp->Firmendaten("passwort"),
|
|
||||||
'host' => $app->erp->Firmendaten("host"),
|
|
||||||
'port' => $app->erp->Firmendaten("port"),
|
|
||||||
'mailssl' => (int)$app->erp->Firmendaten("mailssl"),
|
|
||||||
'noauth' => (int)$app->erp->Firmendaten("noauth"),
|
|
||||||
'mailanstellesmtp' => (int)$app->erp->Firmendaten("mailanstellesmtp"),
|
|
||||||
];
|
|
||||||
|
|
||||||
// PHPMailer initialisieren
|
|
||||||
$app->mail = new PHPMailer($app);
|
|
||||||
$app->mail->CharSet = 'UTF-8';
|
|
||||||
$app->mail->PluginDir = 'plugins/phpmailer/';
|
|
||||||
|
|
||||||
if($mailConfig['mailanstellesmtp'] === 1){
|
|
||||||
$app->mail->IsMail();
|
|
||||||
}else{
|
|
||||||
$app->mail->IsSMTP();
|
|
||||||
|
|
||||||
if($mailConfig['noauth'] === 1){
|
|
||||||
$app->mail->SMTPAuth = false;
|
|
||||||
}else{
|
|
||||||
$app->mail->SMTPAuth = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if($mailConfig['mailssl'] === 1){
|
|
||||||
$app->mail->SMTPSecure = 'tls';
|
|
||||||
}else if($mailConfig['mailssl'] === 2){
|
|
||||||
$app->mail->SMTPSecure = 'ssl';
|
|
||||||
}
|
|
||||||
|
|
||||||
$app->mail->Host = $mailConfig['host'];
|
|
||||||
$app->mail->Port = $mailConfig['port'];
|
|
||||||
$app->mail->Username = $mailConfig['benutzername'];
|
|
||||||
$app->mail->Password = $mailConfig['passwort'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Timestamp der letzten ungelesenen Chat-Nachricht abrufen, zu der eine Mail-Benachrichtigung versendet wurde
|
* Timestamp der letzten ungelesenen Chat-Nachricht abrufen, zu der eine Mail-Benachrichtigung versendet wurde
|
||||||
*
|
*
|
||||||
@ -205,25 +155,25 @@ function setUserCacheValue($userId, $cacheValue)
|
|||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
function combineUserResult($private = [], $public = [])
|
function combineUserResult($privates = [], $publics = [])
|
||||||
{
|
{
|
||||||
if (empty($private)) {
|
if (empty($privates)) {
|
||||||
$private = [];
|
$privates = [];
|
||||||
}
|
}
|
||||||
if (empty($public)) {
|
if (empty($publics)) {
|
||||||
$public = [];
|
$publics = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = [];
|
$result = [];
|
||||||
|
|
||||||
foreach ($private as $user) {
|
foreach ($privates as $user) {
|
||||||
$userId = $user['id'];
|
$userId = $user['id'];
|
||||||
$result[$userId]['id'] = $userId;
|
$result[$userId]['id'] = $userId;
|
||||||
$result[$userId]['private_date'] = strtotime($user['message_date']);
|
$result[$userId]['private_date'] = strtotime($user['message_date']);
|
||||||
$result[$userId]['private_count'] = $user['message_count'];
|
$result[$userId]['private_count'] = $user['message_count'];
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($public as $user) {
|
foreach ($publics as $user) {
|
||||||
$userId = $user['id'];
|
$userId = $user['id'];
|
||||||
$result[$userId]['id'] = $userId;
|
$result[$userId]['id'] = $userId;
|
||||||
$result[$userId]['public_date'] = strtotime($user['message_date']);
|
$result[$userId]['public_date'] = strtotime($user['message_date']);
|
||||||
@ -249,6 +199,7 @@ function sendNotificationMail($userId, $privateMessages, $publicMessages)
|
|||||||
if((int)$userId === 0){
|
if((int)$userId === 0){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if((int)$publicMessages === 0 && (int)$privateMessages === 0){
|
if((int)$publicMessages === 0 && (int)$privateMessages === 0){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -267,46 +218,48 @@ function sendNotificationMail($userId, $privateMessages, $publicMessages)
|
|||||||
);
|
);
|
||||||
|
|
||||||
if(empty($toMail)){
|
if(empty($toMail)){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$app->mail->ClearData();
|
|
||||||
$app->mail->AddAddress($toMail, $toName);
|
|
||||||
|
|
||||||
$fromMail = $app->erp->GetFirmaMail();
|
$fromMail = $app->erp->GetFirmaMail();
|
||||||
$fromName = $app->erp->GetFirmaName();
|
$fromName = $app->erp->GetFirmaName();
|
||||||
$subject = '[Xentral] Ungelesene Chat-Nachrichten';
|
$subject = 'Ungelesene Chat-Nachrichten';
|
||||||
$totalMessageCount = (int)$privateMessages + (int)$publicMessages;
|
$totalMessageCount = (int)$privateMessages + (int)$publicMessages;
|
||||||
|
|
||||||
$messages = GetNewestMessagesForUser($userId);
|
$messages = GetNewestMessagesForUser($userId);
|
||||||
if (empty($messages)) {
|
if (empty($messages)) {
|
||||||
return false;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Xentral-Logo einbinden
|
/* // Profilbilder einbinden
|
||||||
$logoFilePath = dirname(__DIR__) . '/www/themes/new/images/xentral_logo.png';
|
|
||||||
$app->mail->AddEmbeddedImage($logoFilePath, 'logo', 'logo.png');
|
|
||||||
|
|
||||||
// Profilbilder einbinden
|
|
||||||
$userFromIds = array_unique(array_column($messages, 'user_from'));
|
$userFromIds = array_unique(array_column($messages, 'user_from'));
|
||||||
foreach ($userFromIds as $userFromId) {
|
foreach ($userFromIds as $userFromId) {
|
||||||
$profileImage = GetProfilImage($userFromId);
|
$profileImage = GetProfilImage($userFromId);
|
||||||
$app->mail->AddEmbeddedImage($profileImage['path'], $profileImage['cid'], $profileImage['name']);
|
$app->mail->AddEmbeddedImage($profileImage['path'], $profileImage['cid'], $profileImage['name']);
|
||||||
|
}*/
|
||||||
|
|
||||||
|
// $text = GetHtmlMessage($toName, $totalMessageCount, $messages); ?!? -> IF we want to use formatted mails, we will have to make use of templates
|
||||||
|
|
||||||
|
// Very lean implementation:
|
||||||
|
$text = "Neue Chat-Nachrichten vorhanden.</br></br>";
|
||||||
|
foreach ($messages as $message) {
|
||||||
|
$zeitstempel = strtotime($message['zeitstempel']);
|
||||||
|
$message['zeitstempel'] = 'am ' . date('d.m.Y', $zeitstempel) . ' um ' . date('H:i', $zeitstempel) .' Uhr';
|
||||||
|
|
||||||
|
$text .= "\"".$message['message']."\"</br>";
|
||||||
|
if (empty($message['user_to'])) {
|
||||||
|
$text .= "<i>(öffentlich)</i></br>";
|
||||||
|
}
|
||||||
|
$text .= "<i>".$message['user_from_name']." ".$message['zeitstempel']."</i></br></br>";
|
||||||
|
$text = htmlspecialchars($text);
|
||||||
}
|
}
|
||||||
|
|
||||||
$app->mail->From = $fromMail;
|
// function MailSend($from,$from_name,$to,$to_name,$betreff,$text,$files="",$projekt="",$signature=true,$cc="",$bcc="", $system = false)
|
||||||
$app->mail->FromName = $fromName;
|
$app->erp->MailSend($fromMail,$fromName,$toMail,$toName,$subject,$text,"","",false,"","", true);
|
||||||
$app->mail->Subject = $subject;
|
|
||||||
$app->mail->Body = GetHtmlMessage($toName, $totalMessageCount, $messages);
|
$app->erp->LogFile("Mailed ".$subject." to ".$toMail);
|
||||||
$app->mail->IsHTML(true);
|
|
||||||
|
|
||||||
if(!$app->mail->Send()){
|
|
||||||
echo $app->mail->ErrorInfo;
|
|
||||||
$app->erp->LogFile("Mailer Error: " . $app->mail->ErrorInfo);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -468,7 +421,7 @@ function GetHtmlMessage($receipientName, $messageTotalCount, $messages = [])
|
|||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>[Xentral] Ungelesene Chat-Nachrichten</title>
|
<title>[Xenomporio] Ungelesene Chat-Nachrichten</title>
|
||||||
<style type="text/css">
|
<style type="text/css">
|
||||||
body { font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; width: 100%; max-width: 100%; font-size: 17px; line-height: 24px; color: #48494B; background: #F5F5F5; }
|
body { font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; width: 100%; max-width: 100%; font-size: 17px; line-height: 24px; color: #48494B; background: #F5F5F5; }
|
||||||
h1, h2, h3, h4 { color: #42B8C4; margin-bottom: 12px; line-height: 26px; }
|
h1, h2, h3, h4 { color: #42B8C4; margin-bottom: 12px; line-height: 26px; }
|
||||||
|
Loading…
Reference in New Issue
Block a user