From 0ad1eca4c904e7cd8178a445c1cab233f9309321 Mon Sep 17 00:00:00 2001
From: Murtaza Sultani <sultani@data-quest.de>
Date: Wed, 2 Oct 2024 14:28:02 +0000
Subject: [PATCH] =?UTF-8?q?Resolve=20"Besseres=20Betreffpr=C3=A4fix=20f?=
 =?UTF-8?q?=C3=BCr=20E-Mails"?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Closes #4529

Merge request studip/studip!3404
---
 app/controllers/new_password.php  |  6 +--
 db/migrations/6.0.20_tic_4529.php | 63 +++++++++++++++++++++++++++++++
 lib/classes/UserManagement.php    | 24 ++++--------
 lib/messaging.inc.php             |  2 +-
 4 files changed, 74 insertions(+), 21 deletions(-)
 create mode 100644 db/migrations/6.0.20_tic_4529.php

diff --git a/app/controllers/new_password.php b/app/controllers/new_password.php
index 3a6e6419a9a..8b385683bb6 100644
--- a/app/controllers/new_password.php
+++ b/app/controllers/new_password.php
@@ -47,10 +47,8 @@ class NewPasswordController extends StudipController
             setTempLanguage($users[0]->id);
 
             // there are mutliple accounts with this mail addresses!
-            $subject = sprintf(
-                _("[Stud.IP - %s] Passwortänderung angefordert"),
-                Config::get()->UNI_NAME_CLEAN
-            );
+            $prefix = ltrim(Config::get()->MAIL_SUBJECT_PREFIX . ' ');
+            $subject = $prefix . _('Passwortänderung angefordert');
 
             $mailbody = sprintf(
                 _("Dies ist eine Informationsmail des Stud.IP-Systems\n"
diff --git a/db/migrations/6.0.20_tic_4529.php b/db/migrations/6.0.20_tic_4529.php
new file mode 100644
index 00000000000..74c74f04c46
--- /dev/null
+++ b/db/migrations/6.0.20_tic_4529.php
@@ -0,0 +1,63 @@
+<?php
+
+final class Tic4529 extends Migration
+{
+    public function description()
+    {
+        return 'Delete MAIL_USE_SUBJECT_PREFIX and add MAIL_SUBJECT_PREFIX';
+    }
+
+    public function up()
+    {
+        $db = DBManager::get();
+
+        $MAIL_USE_SUBJECT_PREFIX = $db->fetchColumn("SELECT `value` FROM `config_values` WHERE `field` = ?", ["MAIL_USE_SUBJECT_PREFIX"]);
+        if (!$MAIL_USE_SUBJECT_PREFIX) {
+            $MAIL_USE_SUBJECT_PREFIX = $db->fetchColumn("SELECT `value` FROM `config` WHERE `field` = ?", ["MAIL_USE_SUBJECT_PREFIX"]);
+        }
+
+        $value = '';
+        if ($MAIL_USE_SUBJECT_PREFIX) {
+            $UNI_NAME_CLEAN = $db->fetchColumn("SELECT `value` FROM `config_values` WHERE `field` = ?", ["UNI_NAME_CLEAN"]);
+
+            $value = $UNI_NAME_CLEAN ? sprintf('[Stud.IP - %s]', $UNI_NAME_CLEAN) : '[Stud.IP]';
+        }
+
+        $db->execute(
+            "INSERT IGNORE INTO `config` VALUES (:field, :value, :type, :range, :section, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), :desc)",
+            [
+                'field' => 'MAIL_SUBJECT_PREFIX',
+                'value' => $value,
+                'type' => 'string',
+                'range' => 'global',
+                'section' => 'global',
+                'desc' => 'Stellt dem Titel von per Mail versandten Nachrichten'
+            ]
+        );
+
+        $db->execute(
+            "DELETE `config`, `config_values` FROM `config` LEFT JOIN `config_values` USING(`field`) WHERE `field` = ?",
+            ["MAIL_USE_SUBJECT_PREFIX"]
+        );
+    }
+
+    public function down()
+    {
+        DBManager::get()->execute(
+            "DELETE `config`, `config_values` FROM `config` LEFT JOIN `config_values` USING(`field`) WHERE `field` = ?",
+            ["MAIL_SUBJECT_PREFIX"]
+        );
+
+        DBManager::get()->execute(
+            "INSERT IGNORE INTO `config` VALUES (:field, :value, :type, :range, :section, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), :desc)",
+            [
+                'field' => 'MAIL_USE_SUBJECT_PREFIX',
+                'value' => '0',
+                'type' => 'boolean',
+                'range' => 'global',
+                'section' => 'global',
+                'desc' => 'Stellt dem Titel von per Mail versandten Nachrichten den Wert von UNI_NAME_CLEAN voran.'
+            ]
+        );
+    }
+}
diff --git a/lib/classes/UserManagement.php b/lib/classes/UserManagement.php
index 809d0c3e15a..cda3ada6ef7 100644
--- a/lib/classes/UserManagement.php
+++ b/lib/classes/UserManagement.php
@@ -688,10 +688,8 @@ class UserManagement
 
         // new users alawys receive a link to generate a password
         if ($new) {
-            $subject = sprintf(
-                _("[Stud.IP - %s] Es wurde ein Zugang für sie erstellt - Setzen sie ein Passwort"),
-                Config::get()->UNI_NAME_CLEAN
-            );
+            $prefix = ltrim(Config::get()->MAIL_SUBJECT_PREFIX . ' ');
+            $subject = $prefix . _('Es wurde ein Zugang für sie erstellt - Setzen sie ein Passwort');
 
             $mailbody = sprintf(
                 _("Dies ist eine Bestätigungsmail des Stud.IP-Systems\n"
@@ -719,10 +717,8 @@ class UserManagement
         if ($user->auth_plugin !== 'standard') {
 
             // inform user, that their password cannot be reset via mail
-            $subject = sprintf(
-                _("[Stud.IP - %s] Passwortänderung angefordert"),
-                Config::get()->UNI_NAME_CLEAN
-            );
+            $prefix = ltrim(Config::get()->MAIL_SUBJECT_PREFIX . ' ');
+            $subject = $prefix . _('Passwortänderung angefordert');
 
             $mailbody = sprintf(
                 _("Dies ist eine Informationsmail des Stud.IP-Systems\n"
@@ -739,10 +735,8 @@ class UserManagement
 
         } else {
 
-            $subject = sprintf(
-                _("[Stud.IP - %s] Neues Passwort setzen"),
-                Config::get()->UNI_NAME_CLEAN
-            );
+            $prefix = ltrim(Config::get()->MAIL_SUBJECT_PREFIX . ' ');
+            $subject = $prefix . _('Neues Passwort setzen');
 
             $mailbody = sprintf(
                 _("Dies ist eine Bestätigungsmail des Stud.IP-Systems\n"
@@ -1307,10 +1301,8 @@ class UserManagement
         // include language-specific subject and mailbody
         setTempLanguage($this->user_data['auth_user_md5.user_id']);
 
-        $subject = sprintf(
-            _("[Stud.IP - %s] Passwortänderung"),
-            Config::get()->UNI_NAME_CLEAN
-        );
+        $prefix = ltrim(Config::get()->MAIL_SUBJECT_PREFIX . ' ');
+        $subject = $prefix . _('Passwortänderung');
 
         $mailbody = sprintf(
             _("Dies ist eine Informationsmail des Stud.IP-Systems\n"
diff --git a/lib/messaging.inc.php b/lib/messaging.inc.php
index bb1990ba5c0..2a1761ed600 100644
--- a/lib/messaging.inc.php
+++ b/lib/messaging.inc.php
@@ -177,7 +177,7 @@ class messaging
 
         setTempLanguage($rec_user_id);
 
-        $title_prefix = Config::get()->MAIL_USE_SUBJECT_PREFIX ? '[Stud.IP - ' . Config::get()->UNI_NAME_CLEAN . '] ' : '';
+        $title_prefix = ltrim(Config::get()->MAIL_SUBJECT_PREFIX . ' ');;
         $title = $title_prefix . kill_format(str_replace(["\r", "\n"], '', $subject));
 
         if ($snd_user_id != "____%system%____") {
-- 
GitLab