1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
<?php
/**
***********************************************************************************************
* Class manages access to database table adm_messages
*
* @copyright 2004-2016 The Admidio Team
* @see http://www.admidio.org/
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2.0 only
***********************************************************************************************
*/
/**
* @class TableMessage
* This class manages the set, update and delete in the table adm_messages
*/
class TableMessage extends TableAccess
{
protected $msg_id;
/**
* Constructor that will create an object of a recordset of the table adm_messages.
* If the id is set than the specific message will be loaded.
* @param \Database $database Object of the class Database. This should be the default global object @b $gDb.
* @param int $msg_id The recordset of the message with this conversation id will be loaded. If id isn't set than an empty object of the table is created.
*/
public function __construct(&$database, $msg_id = 0)
{
$this->msg_id = $msg_id;
parent::__construct($database, TBL_MESSAGES, 'msg', $this->msg_id);
}
/**
* Reads the number of all unread messages of this table
* @param int $usrId
* @return int Number of unread messages of this table
*/
public function countUnreadMessageRecords($usrId)
{
$sql = 'SELECT COUNT(*) AS count
FROM '.$this->tableName.'
WHERE msg_usr_id_receiver LIKE \''. $usrId .'\'
AND msg_read = 1';
$countStatement = $this->db->query($sql);
return (int) $countStatement->fetchColumn();
}
/**
* Reads the number of all conversations in this table
* @return int Number of conversations in this table
*/
public function countMessageConversations()
{
$sql = 'SELECT COUNT(*) AS count FROM '. TBL_MESSAGES;
$countStatement = $this->db->query($sql);
return (int) $countStatement->fetchColumn();
}
/**
* Reads the number of all messages in actual conversation
* @return int Number of all messages in actual conversation
*/
public function countMessageParts()
{
$sql = 'SELECT COUNT(*) AS count
FROM '.TBL_MESSAGES_CONTENT.'
WHERE msc_msg_id = '.$this->getValue('msg_id');
$countStatement = $this->db->query($sql);
return (int) $countStatement->fetchColumn();
}
/**
* Set a new value for a column of the database table.
* @param int $usrId of the receiver - just for security reasons.
* @return \PDOStatement Returns @b answer of the SQL execution
*/
public function setReadValue($usrId)
{
$sql = 'UPDATE '.TBL_MESSAGES.' SET msg_read = \'0\'
WHERE msg_id = '.$this->msg_id.'
AND msg_usr_id_receiver LIKE \''.$usrId.'\'';
return $this->db->query($sql);
}
/**
* get a list with all messages of an conversation.
* @param int $msgId of the conversation - just for security reasons.
* @return \PDOStatement Returns @b answer of the SQL execution
*/
public function getConversation($msgId)
{
$sql = 'SELECT msc_usr_id, msc_message, msc_timestamp
FROM '. TBL_MESSAGES_CONTENT. '
WHERE msc_msg_id = '. $msgId .'
ORDER BY msc_part_id DESC';
return $this->db->query($sql);
}
/**
* Set a new value for a column of the database table.
* The value is only saved in the object. You must call the method @b save to store the new value to the database
* @param int $usrId
* @return int Returns @b ID of the user that is partner in the actual conversation
*/
public function getConversationPartner($usrId)
{
$sql = 'SELECT msg_id,
CASE WHEN msg_usr_id_sender = '. $usrId .' THEN msg_usr_id_receiver
ELSE msg_usr_id_sender
END AS user
FROM '.TBL_MESSAGES.'
WHERE msg_type = \'PM\'
AND msg_id = '. $this->msg_id;
$partnerStatement = $this->db->query($sql);
$row = $partnerStatement->fetch();
return $row['user'];
}
/**
* Deletes the selected message with all associated fields.
* After that the class will be initialize.
* @return bool @b true if message is deleted or message with additional information if it is marked
* for other user to delete. On error it is false
*/
public function delete()
{
global $gCurrentUser;
$this->db->startTransaction();
if($this->getValue('msg_read') == 2 || $this->getValue('msg_type') === 'EMAIL')
{
$sql = 'DELETE FROM '.TBL_MESSAGES_CONTENT.'
WHERE msc_msg_id = '. $this->getValue('msg_id');
$this->db->query($sql);
parent::delete();
}
else
{
$other = $this->getValue('msg_usr_id_sender');
if($other == $gCurrentUser->getValue('usr_id'))
{
$other = $this->getValue('msg_usr_id_receiver');
}
$sql = 'UPDATE '.TBL_MESSAGES.
' SET msg_read = 2, msg_timestamp = CURRENT_TIMESTAMP,
msg_usr_id_sender = '.$gCurrentUser->getValue('usr_id').', msg_usr_id_receiver = \''.$other.'\'
WHERE msg_id = '.$this->getValue('msg_id');
$this->db->query($sql);
}
$this->db->endTransaction();
return true;
}
}