diff --git a/application/libraries/MessageLib.php b/application/libraries/MessageLib.php
index fd2051f48..be496fe4a 100644
--- a/application/libraries/MessageLib.php
+++ b/application/libraries/MessageLib.php
@@ -215,6 +215,31 @@ class MessageLib
return $messageVars; // otherwise returns the error
}
+
+ /**
+ * Retrieves message vars of the logged in user from view vw_msg_vars_user
+ */
+ public function getMessageVarsLoggedInUser()
+ {
+ // Retrieves message vars from view vw_msg_vars
+ $messageVars = $this->_ci->MessageModel->getMsgVarsLoggedInUser();
+ if (isSuccess($messageVars)) // if everything is ok
+ {
+ $variablesArray = array();
+ $tmpVariablesArray = getData($messageVars);
+
+ // Starts from 1 to skip the first element which is uid
+ for ($i = 1; $i < count($tmpVariablesArray); $i++)
+ {
+ $variablesArray['{'.str_replace(' ', '_', strtolower($tmpVariablesArray[$i])).'}']
+ = strtoupper($tmpVariablesArray[$i]);
+ }
+
+ return success($variablesArray);
+ }
+
+ return $messageVars; // otherwise returns the error
+ }
/**
* Retrieves organisation units for each role that a user plays inside that organisation unit
@@ -595,11 +620,24 @@ class MessageLib
$this->_ci->load->model('person/Benutzer_model', 'BenutzerModel');
// And the receiver has an active account for the given organisation unit
- $benutzerResult = $this->_ci->BenutzerModel->getActiveUserByPersonIdAndOrganisationUnit($message->receiver_id, $message->sender_ou);
+ $benutzerResult = $this->_ci->BenutzerModel->getActiveUserByPersonIdAndOrganisationUnit(
+ $message->receiver_id,
+ $message->sender_ou
+ );
+
if (isError($benutzerResult)) return $benutzerResult; // if an error occured then return it
- // Use the uid + domain email
- if (hasData($benutzerResult)) $message->receiverContact = getData($benutzerResult)[0]->uid .'@'.DOMAIN;
+ // If an active user for the given organization unit was found
+ if (hasData($benutzerResult))
+ {
+ // Checks if the user was NOT created in the last 24 hours
+ if (getData($benutzerResult)[0]->insertamum < date('Y-m-d H:i:s', strtotime('-1 day')))
+ {
+ // Use the uid + domain email
+ $message->receiverContact = getData($benutzerResult)[0]->uid .'@'.DOMAIN;
+ }
+ // otherwise do NOT use the internal email account
+ }
}
// Otherwise try with the private email
@@ -644,7 +682,7 @@ class MessageLib
// If there are presetudent
if (hasData($prestudentResults))
{
- $inArray = true;
+ $privateOnly = false;
$organisationUnits = getData($prestudentResults);
// Look if any of the organization units of this prestudent are in the list of the
@@ -652,16 +690,21 @@ class MessageLib
foreach ($organisationUnits as $organisationUnit)
{
// If the recipient organisation unit is NOT in the list of organisation units that sent only to private emails
+ // NOTE: done in this way because it is easyer to check the result of array_search
if (array_search($organisationUnit, $this->_ci->config->item(self::CFG_OU_RECEIVERS_PRIVATE)) === false)
{
- $inArray = false;
+ // NOP
+ }
+ else // otherwise If the recipient organisation unit is the list of organisation units that sent only to private emails
+ {
+ $privateOnly = true;
break;
}
}
// If the recipient prestudent organization unit is not in in the list of the
// organization units that will not send the notice email to the internal account
- if (!$inArray)
+ if ($privateOnly)
{
// Then use the private email
$privateEmailResult = $this->_getPrivateEmail($message->receiver_id);
@@ -676,10 +719,37 @@ class MessageLib
$this->_ci->BenutzerModel->addOrder('updateamum', 'DESC');
$this->_ci->BenutzerModel->addOrder('insertamum', 'DESC');
- $benutzerResult = $this->_ci->BenutzerModel->loadWhere(array('person_id' => $message->receiver_id));
+ $benutzerResult = $this->_ci->BenutzerModel->loadWhere(
+ array(
+ 'person_id' => $message->receiver_id
+ )
+ );
if (isError($benutzerResult)) return $benutzerResult; // if an error occured then return it
- $message->receiverContact = getData($benutzerResult)[0]->uid .'@'.DOMAIN; // Use the uid + domain email
+ // If an active user for the given organization unit was found
+ if (hasData($benutzerResult))
+ {
+ // For each benutzer found for this person
+ foreach (getData($benutzerResult) as $benutzer)
+ {
+ // Checks if the user was NOT created in the last 24 hours
+ if (getData($benutzerResult)[0]->insertamum < date('Y-m-d H:i:s', strtotime('-1 day')))
+ {
+ // Use the uid + domain as email address
+ $message->receiverContact = getData($benutzerResult)[0]->uid .'@'.DOMAIN;
+ }
+ }
+ }
+
+ // Otherwise try with the private email
+ if (isEmptyString($message->receiverContact))
+ {
+ // Then use the private email
+ $privateEmailResult = $this->_getPrivateEmail($message->receiver_id);
+ if (isError($privateEmailResult)) return $privateEmailResult; // if an error occured then return it
+
+ if (hasData($privateEmailResult)) $message->receiverContact = getData($privateEmailResult);
+ }
}
}
}
diff --git a/application/models/CL/Messages_model.php b/application/models/CL/Messages_model.php
index 17824bbb8..9ffd12cf6 100644
--- a/application/models/CL/Messages_model.php
+++ b/application/models/CL/Messages_model.php
@@ -48,6 +48,9 @@ class Messages_model extends CI_Model
$this->load->model('system/Benutzerrolle_model', 'BenutzerrolleModel');
// Loads model Prestudent_model
$this->load->model('crm/Prestudent_model', 'PrestudentModel');
+ // Loads model Benutzer_model
+ $this->load->model('person/Benutzer_model', 'BenutzerModel');
+
}
//------------------------------------------------------------------------------------------------------------------
@@ -402,7 +405,10 @@ class Messages_model extends CI_Model
// Looping on receivers data
foreach (getData($msgVarsData) as $receiver)
{
- $msgVarsDataArray = $this->_lowerReplaceSpaceArrayKeys((array)$receiver); // replaces array keys
+ // Merge receivers data with logged in user data
+ $msgVarsDataArray = $this->_addMsgVarsDataOfLoggedInUser($receiver);
+
+ $msgVarsDataArray = $this->_lowerReplaceSpaceArrayKeys((array)getData($msgVarsDataArray)[0]); // replaces array keys
$parsedSubject = parseText($subject, $msgVarsDataArray);
$parsedBody = parseText($body, $msgVarsDataArray);
@@ -466,6 +472,15 @@ class Messages_model extends CI_Model
if (!hasData($msgVarsData)) show_error('No recipients were given');
$prestudentsData = $this->PrestudentModel->getOrganisationunits($prestudents);
+
+ // Get the senders uid (if user is an active employee)
+ $this->BenutzerModel->addSelect('uid');
+ $this->BenutzerModel->addJoin('public.tbl_mitarbeiter ma', 'ma.mitarbeiter_uid = uid');
+ if (!$result = getData($this->BenutzerModel->getFromPersonId($sender_id)))
+ {
+ show_error('No sender_uid found');
+ }
+ $sender_uid = $result[0]->uid;
// Adds the organisation unit to each prestudent
if (isEmptyString($oe_kurzbz) && hasData($msgVarsData) && hasData($prestudentsData))
@@ -475,7 +490,15 @@ class Messages_model extends CI_Model
foreach (getData($msgVarsData) as $receiver)
{
- $msgVarsDataArray = $this->_lowerReplaceSpaceArrayKeys((array)$receiver); // replaces array keys
+ /**
+ * Merge receivers data with senders data
+ * NOTE: _addMsgVarsDataOfLoggedInUser usually retrieves data of the logged in user that is set in the
+ * templates user fields. As sendExplicitTemplateSenderId is run by a job, a sender uid is passed to be used
+ * instead the logged in user.
+ */
+ $msgVarsDataArray = $this->_addMsgVarsDataOfLoggedInUser($receiver, $sender_uid);
+
+ $msgVarsDataArray = $this->_lowerReplaceSpaceArrayKeys((array)getData($msgVarsDataArray)[0]); // replaces array keys
// Additional message variables
if (is_array($msgVars)) $msgVarsDataArray = array_merge($msgVarsDataArray, $msgVars);
@@ -606,6 +629,9 @@ class Messages_model extends CI_Model
$parseMessageText = error('The given person_id is not a valid number');
if (is_numeric($person_id)) $parseMessageText = $this->MessageModel->getMsgVarsDataByPersonId($person_id);
+
+ // Add message vars data of the logged in user
+ $parseMessageText = $this->_addMsgVarsDataOfLoggedInUser($parseMessageText);
if (hasData($parseMessageText))
{
@@ -629,7 +655,10 @@ class Messages_model extends CI_Model
$parseMessageText = error('The given prestudent_id is not a valid number');
if (is_numeric($prestudent_id)) $parseMessageText = $this->MessageModel->getMsgVarsDataByPrestudentId($prestudent_id);
-
+
+ // Add message vars data of the logged in user
+ $parseMessageText = $this->_addMsgVarsDataOfLoggedInUser($parseMessageText);
+
if (hasData($parseMessageText))
{
$parseMessageText = success(
@@ -839,6 +868,26 @@ class Messages_model extends CI_Model
$variables[] = $tmpVar;
}
+
+ // ---------------------------------------------------------------------------------------
+ // Retrieves message vars of logged in user from database view vw_msg_vars_person
+ $result = null;
+
+ // If data contains a prestudent id
+ $result = $this->messagelib->getMessageVarsLoggedInUser();
+
+ if (isError($result)) show_error(getError($result));
+
+ // Then builds an array that contains objects with field name and field description of logged in user data
+ $user_fields = array();
+ foreach (getData($result) as $id => $description)
+ {
+ $obj = new stdClass();
+ $obj->id = $id;
+ $obj->description = $description;
+
+ $user_fields[] = $obj;
+ }
// ---------------------------------------------------------------------------------------
// Retrieves the sender id
@@ -859,6 +908,7 @@ class Messages_model extends CI_Model
'subject' => $replySubject,
'body' => $replyBody,
'variables' => $variables,
+ 'user_fields' => $user_fields,
'organisationUnits' => getData($organisationUnits),
'senderIsAdmin' => getData($senderIsAdmin),
'recipientsArray' => $recipientsArray,
@@ -867,4 +917,30 @@ class Messages_model extends CI_Model
'type' => $type
);
}
+
+ /**
+ * Adds message vars data of the logged in user to the given object (that should also have message vars data)
+ * @param object $otherMsgVarsDataObj Can be success object or simple object.
+ * @return object Returns success object.
+ */
+ public function _addMsgVarsDataOfLoggedInUser($otherMsgVarsDataObj, $uid = null)
+ {
+ // First check if param type is object
+ if (!is_object($otherMsgVarsDataObj)) show_error('Must pass an object to merge with data of logged in user');
+
+ // If it is a return object, extract the simple data object
+ if (isSuccess($otherMsgVarsDataObj))
+ {
+ $otherMsgVarsDataObj = getData($otherMsgVarsDataObj)[0];
+ }
+
+ // Retrieve message vars data of the logged in user
+ if (!$msgVarsDataLoggedInUser = getData($this->MessageModel->getMsgVarsDataByLoggedInUser($uid))[0])
+ {
+ return success($otherMsgVarsDataObj); // If failed, return at least given object as expected success object
+ }
+
+ return success(array((object)(array_merge((array) $otherMsgVarsDataObj, (array) $msgVarsDataLoggedInUser))));
+
+ }
}
diff --git a/application/models/organisation/Studiensemester_model.php b/application/models/organisation/Studiensemester_model.php
index 41baf2489..07783cf0d 100644
--- a/application/models/organisation/Studiensemester_model.php
+++ b/application/models/organisation/Studiensemester_model.php
@@ -132,7 +132,7 @@ class Studiensemester_model extends DB_Model
$query .= ' WHERE SUBSTRING(studiensemester_kurzbz FROM 1 FOR 2) = \'' . $ss . '\'';
}
- $query .= ' ORDER BY delta LIMIT 1';
+ $query .= ' ORDER BY delta, start LIMIT 1';
return $this->execQuery($query);
}
@@ -188,7 +188,7 @@ class Studiensemester_model extends DB_Model
{
$query = "SELECT studiensemester_kurzbz, start, ende FROM public.vw_studiensemester
WHERE studiensemester_kurzbz <> ?
- ORDER BY delta LIMIT 1";
+ ORDER BY delta, start LIMIT 1";
return $this->execQuery($query, array($studiensemester_kurzbz));
}
diff --git a/application/models/person/Benutzer_model.php b/application/models/person/Benutzer_model.php
index 45edf5122..c1e76ce38 100644
--- a/application/models/person/Benutzer_model.php
+++ b/application/models/person/Benutzer_model.php
@@ -23,13 +23,17 @@ class Benutzer_model extends DB_Model
*/
public function getActiveUserByPersonIdAndOrganisationUnit($person_id, $oe_kurzbz)
{
- $sql = 'SELECT b.uid
- FROM public.tbl_benutzer b
- JOIN public.tbl_prestudent ps USING (person_id)
- JOIN public.tbl_studiengang sg USING (studiengang_kz)
- WHERE ps.person_id = ?
- AND sg.oe_kurzbz = ?
- AND b.aktiv = TRUE';
+ $sql = 'SELECT
+ b.uid,
+ b.insertamum
+ FROM
+ public.tbl_prestudent ps
+ JOIN public.tbl_studiengang sg USING (studiengang_kz)
+ JOIN public.tbl_student USING(prestudent_id)
+ JOIN public.tbl_benutzer b ON(uid = student_uid)
+ WHERE ps.person_id = ?
+ AND sg.oe_kurzbz = ?
+ AND b.aktiv = TRUE';
return $this->execQuery($sql, array($person_id, $oe_kurzbz));
}
diff --git a/application/models/system/Message_model.php b/application/models/system/Message_model.php
index 764c3ae14..d9f8585ed 100644
--- a/application/models/system/Message_model.php
+++ b/application/models/system/Message_model.php
@@ -171,6 +171,23 @@ class Message_model extends DB_Model
return error($this->db->error(), FHC_DB_ERROR);
}
}
+
+ /**
+ * Get message variables for logged in user
+ */
+ public function getMsgVarsLoggedInUser()
+ {
+ $result = $this->db->query('SELECT * FROM public.vw_msg_vars_user WHERE 0 = 1');
+
+ if ($result)
+ {
+ return success($result->list_fields());
+ }
+ else
+ {
+ return error($this->db->error(), FHC_DB_ERROR);
+ }
+ }
/**
* getMsgVarsDataByPrestudentId
@@ -191,4 +208,26 @@ class Message_model extends DB_Model
return $this->execQuery(sprintf($query, is_array($person_id) ? 'IN' : '='), array($person_id));
}
+
+ /**
+ * Get message vars data for logged in user
+ * @param string uid The UID should ONLY be passed if this method is called by a cronjob.
+ * This is to enable jobs to use templates which use logged-in-user fields ('Eigene Felder').
+ * @return array|null
+ */
+ public function getMsgVarsDataByLoggedInUser($uid = null)
+ {
+ if (is_string($uid))
+ {
+ $params = array($uid);
+ }
+ else
+ {
+ $params = array(getAuthUID());
+ }
+
+ $query = 'SELECT * FROM public.vw_msg_vars_user WHERE my_uid = ?';
+
+ return $this->execQuery($query, $params);
+ }
}
diff --git a/application/views/system/messages/htmlWriteTemplate.php b/application/views/system/messages/htmlWriteTemplate.php
index 199a88cfa..95ab12630 100644
--- a/application/views/system/messages/htmlWriteTemplate.php
+++ b/application/views/system/messages/htmlWriteTemplate.php
@@ -83,19 +83,41 @@
19 ? 19 : count($variables);
echo $this->widgetlib->widget(
'MultipleDropdown_widget',
array('elements' => success($variables)),
array(
'name' => 'variables[]',
'id' => 'variables',
- 'size' => count($variables),
+ 'size' => $size,
'multiple' => true
)
);
?>
-
+
+