diff --git a/application/controllers/api/v1/system/Message.php b/application/controllers/api/v1/system/Message.php index 33ccbb202..1d400b41e 100644 --- a/application/controllers/api/v1/system/Message.php +++ b/application/controllers/api/v1/system/Message.php @@ -32,11 +32,12 @@ class Message extends APIv1_Controller public function getMessagesByPersonID() { $person_id = $this->get('person_id'); + $oe_kurzbz = $this->get('oe_kurzbz'); // prestudent organisation unit $all = $this->get('all'); if (isset($person_id)) { - $result = $this->messagelib->getMessagesByPerson($person_id, $all); + $result = $this->messagelib->getMessagesByPerson($person_id, $oe_kurzbz, $all); $this->response($result, REST_Controller::HTTP_OK); } @@ -52,11 +53,12 @@ class Message extends APIv1_Controller public function getMessagesByUID() { $uid = $this->get('uid'); + $oe_kurzbz = $this->get('oe_kurzbz'); // prestudent organisation unit $all = $this->get('all'); if (isset($uid)) { - $result = $this->messagelib->getMessagesByUID($uid, $all); + $result = $this->messagelib->getMessagesByUID($uid, $oe_kurzbz, $all); $this->response($result, REST_Controller::HTTP_OK); } @@ -91,11 +93,12 @@ class Message extends APIv1_Controller public function getSentMessagesByPerson() { $person_id = $this->get('person_id'); + $oe_kurzbz = $this->get('oe_kurzbz'); // prestudent organisation unit $all = $this->get('all'); if (isset($person_id)) { - $result = $this->messagelib->getSentMessagesByPerson($person_id, $all); + $result = $this->messagelib->getSentMessagesByPerson($person_id, $oe_kurzbz, $all); $this->response($result, REST_Controller::HTTP_OK); } @@ -111,10 +114,11 @@ class Message extends APIv1_Controller public function getCountUnreadMessages() { $person_id = $this->get('person_id'); + $oe_kurzbz = $this->get('oe_kurzbz'); // prestudent organisation unit if (isset($person_id)) { - $result = $this->messagelib->getCountUnreadMessages($person_id); + $result = $this->messagelib->getCountUnreadMessages($person_id, $oe_kurzbz); $this->response($result, REST_Controller::HTTP_OK); } diff --git a/application/libraries/MessageLib.php b/application/libraries/MessageLib.php index 3b18874eb..d5daca634 100755 --- a/application/libraries/MessageLib.php +++ b/application/libraries/MessageLib.php @@ -62,12 +62,12 @@ class MessageLib /** * getMessagesByUID() - will return all messages, including the latest status for specified user. It don´t returns Attachments. */ - public function getMessagesByUID($uid, $all = false) + public function getMessagesByUID($uid, $oe_kurzbz = null, $all = false) { if (empty($uid)) return $this->_error('', MSG_ERR_INVALID_MSG_ID); - $msg = $this->ci->RecipientModel->getMessagesByUID($uid, $all); + $msg = $this->ci->RecipientModel->getMessagesByUID($uid, $oe_kurzbz, $all); return $msg; } @@ -75,12 +75,12 @@ class MessageLib /** * getMessagesByPerson() - will return all messages, including the latest status for specified user. It don´t returns Attachments. */ - public function getMessagesByPerson($person_id, $all = false) + public function getMessagesByPerson($person_id, $oe_kurzbz = null, $all = false) { if (empty($person_id)) return $this->_error('', MSG_ERR_INVALID_MSG_ID); - $msg = $this->ci->RecipientModel->getMessagesByPerson($person_id, $all); + $msg = $this->ci->RecipientModel->getMessagesByPerson($person_id, $oe_kurzbz, $all); return $msg; } @@ -88,12 +88,12 @@ class MessageLib /** * getSentMessagesByPerson() - Get all sent messages from a person identified by person_id */ - public function getSentMessagesByPerson($person_id, $all = false) + public function getSentMessagesByPerson($person_id, $oe_kurzbz = null, $all = false) { if (empty($person_id)) return $this->_error('', MSG_ERR_INVALID_MSG_ID); - $msg = $this->ci->MessageModel->getMessagesByPerson($person_id, $all); + $msg = $this->ci->MessageModel->getMessagesByPerson($person_id, $oe_kurzbz, $all); return $msg; } @@ -144,12 +144,12 @@ class MessageLib /** * getCountUnreadMessages */ - public function getCountUnreadMessages($person_id) + public function getCountUnreadMessages($person_id, $oe_kurzbz = null) { if (!is_numeric($person_id)) return $this->_error('', MSG_ERR_INVALID_RECIPIENTS); - $msg = $this->ci->RecipientModel->getCountUnreadMessages($person_id); + $msg = $this->ci->RecipientModel->getCountUnreadMessages($person_id, $oe_kurzbz); return $msg; } diff --git a/application/models/system/Message_model.php b/application/models/system/Message_model.php index 76e86b356..6b8b0fbdc 100644 --- a/application/models/system/Message_model.php +++ b/application/models/system/Message_model.php @@ -1,4 +1,4 @@ -dbTable = 'public.tbl_msg_message'; $this->pk = 'message_id'; } - + /** * Get all sent messages from a person identified by person_id */ - public function getMessagesByPerson($person_id, $all) + public function getMessagesByPerson($person_id, $oe_kurzbz, $all) { // Checks if the operation is permitted by the API caller if (isError($ent = $this->isEntitled('public.tbl_person', PermissionLib::SELECT_RIGHT, FHC_NORIGHT, FHC_MODEL_ERROR))) @@ -26,7 +26,7 @@ class Message_model extends DB_Model return $ent; if (isError($ent = $this->isEntitled('public.tbl_msg_message', PermissionLib::SELECT_RIGHT, FHC_NORIGHT, FHC_MODEL_ERROR))) return $ent; - + $sql = 'SELECT m.message_id, m.person_id, m.subject, @@ -52,9 +52,9 @@ class Message_model extends DB_Model ORDER BY insertamum DESC ) s ON (m.message_id = s.message_id AND m.person_id = s.person_id) WHERE m.person_id = ?'; - + $parametersArray = array($person_id); - + if ($all == 'true') { $sql = sprintf($sql, ''); @@ -63,17 +63,23 @@ class Message_model extends DB_Model { $sql = sprintf($sql, 'WHERE status >= 3'); } - + + if ($oe_kurzbz != null) + { + array_push($parametersArray, $oe_kurzbz); + $sql .= ' AND m.oe_kurzbz = ?'; + } + return $this->execQuery($sql, $parametersArray); } - + /** * getMessageVars */ public function getMessageVars() { $result = $this->db->query('SELECT * FROM public.vw_msg_vars WHERE 0 = 1'); - + if ($result) { return success($result->list_fields()); @@ -83,14 +89,14 @@ class Message_model extends DB_Model return error($this->db->error(), FHC_DB_ERROR); } } - + /** * getMsgVarsDataByPrestudentId */ public function getMsgVarsDataByPrestudentId($prestudent_id) { $query = 'SELECT * FROM public.vw_msg_vars WHERE prestudent_id %s ?'; - + return $this->execQuery(sprintf($query, is_array($prestudent_id) ? 'IN' : '='), array($prestudent_id)); } } diff --git a/application/models/system/Recipient_model.php b/application/models/system/Recipient_model.php index b8ffecab3..53170a4cc 100644 --- a/application/models/system/Recipient_model.php +++ b/application/models/system/Recipient_model.php @@ -87,7 +87,7 @@ class Recipient_model extends DB_Model /** * Get all received messages for a person identified by person_id */ - public function getMessagesByPerson($person_id, $all) + public function getMessagesByPerson($person_id, $oe_kurzbz, $all) { // Checks if the operation is permitted by the API caller if (isError($ent = $this->isEntitled('public.tbl_msg_recipient', PermissionLib::SELECT_RIGHT, FHC_NORIGHT, FHC_MODEL_ERROR))) @@ -124,8 +124,7 @@ class Recipient_model extends DB_Model %s ORDER BY insertamum DESC ) s ON (m.message_id = s.message_id AND r.person_id = s.person_id) - WHERE r.person_id = ? - ORDER BY r.message_id DESC, s.status DESC'; + WHERE r.person_id = ?'; $parametersArray = array($person_id); @@ -139,16 +138,24 @@ class Recipient_model extends DB_Model $sql = sprintf($sql, 'WHERE person_id = ? AND message_id NOT IN (SELECT message_id FROM public.tbl_msg_status WHERE status >= 3 AND person_id = ?)'); } + if ($oe_kurzbz != null) + { + array_push($parametersArray, $oe_kurzbz); + $sql .= ' AND m.oe_kurzbz = ?'; + } + + $sql .= ' ORDER BY r.message_id DESC, s.status DESC'; + return $this->execQuery($sql, $parametersArray); } /** * Get all received messages for a person identified by uid */ - public function getMessagesByUID($uid, $all) + public function getMessagesByUID($uid, $oe_kurzbz, $all) { // Checks if the operation is permitted by the API caller - // @ToDo: Define the special right for reading own messages 'basis/message:own' + // TODO: Define the special right for reading own messages 'basis/message:own' // if same user if ($uid === getAuthUID()) { @@ -163,15 +170,14 @@ class Recipient_model extends DB_Model } // get Data - $sql = 'SELECT b.uid, + $sql = 'SELECT DISTINCT ON (r.message_id) r.message_id, m.person_id, - m.message_id, m.subject, m.body, m.priority, + m.insertamum, m.relationmessage_id, m.oe_kurzbz, - m.insertamum, p.anrede, p.titelpost, p.titelpre, @@ -185,14 +191,26 @@ class Recipient_model extends DB_Model JOIN public.tbl_person p ON (r.person_id = p.person_id) JOIN public.tbl_benutzer b ON (r.person_id = b.person_id) JOIN ( - SELECT * FROM public.tbl_msg_status ORDER BY insertamum DESC LIMIT 1 - ) s ON (r.message_id = s.message_id AND r.person_id = s.person_id) + SELECT message_id, person_id, status, statusinfo, insertamum + FROM public.tbl_msg_status + ORDER BY insertamum DESC + ) s ON (m.message_id = s.message_id AND r.person_id = s.person_id) WHERE b.uid = ?'; - if (! $all) - $sql .= ' AND (status < 3 OR status IS NULL)'; + $parametersArray = array($uid); - return $this->execQuery($sql, array($uid)); + if ($all == 'true') + { + $sql .= ' AND (status < 3 OR status IS NULL)'; + } + + if ($oe_kurzbz != null) + { + array_push($parametersArray, $oe_kurzbz); + $sql .= ' AND m.oe_kurzbz = ?'; + } + + return $this->execQuery($sql, $parametersArray); } /** @@ -270,7 +288,7 @@ class Recipient_model extends DB_Model /** * Get all unread messages for a person identified by person_id */ - public function getCountUnreadMessages($person_id) + public function getCountUnreadMessages($person_id, $oe_kurzbz) { // Checks if the operation is permitted by the API caller if (isError($ent = $this->isEntitled('public.tbl_msg_recipient', PermissionLib::SELECT_RIGHT, FHC_NORIGHT, FHC_MODEL_ERROR))) @@ -279,8 +297,9 @@ class Recipient_model extends DB_Model return $ent; $sql = 'SELECT COUNT(r.message_id) AS unreadMessages - FROM public.tbl_msg_recipient r JOIN public.tbl_msg_status s - ON (r.message_id = s.message_id AND r.person_id = s.person_id) + FROM public.tbl_msg_recipient r + JOIN public.tbl_msg_status s ON (r.message_id = s.message_id AND r.person_id = s.person_id) + JOIN public.tbl_msg_message m ON (r.message_id = m.message_id) WHERE r.person_id = ? AND s.status = ? AND r.message_id NOT IN ( @@ -293,6 +312,12 @@ class Recipient_model extends DB_Model $parametersArray = array($person_id, MSG_STATUS_UNREAD, $person_id, MSG_STATUS_UNREAD); + if ($oe_kurzbz != null) + { + array_push($parametersArray, $oe_kurzbz); + $sql .= ' AND m.oe_kurzbz = ?'; + } + return $this->execQuery($sql, $parametersArray); } }