Merge remote-tracking branch 'refs/remotes/origin/3.0'
[GitHub/WoltLab/com.woltlab.wcf.conversation.git] / files / lib / system / conversation / ConversationHandler.class.php
1 <?php
2 namespace wcf\system\conversation;
3 use wcf\system\database\util\PreparedStatementConditionBuilder;
4 use wcf\system\user\storage\UserStorageHandler;
5 use wcf\system\SingletonFactory;
6 use wcf\system\WCF;
7
8 /**
9 * Handles the number of conversations and unread conversations of the active user.
10 *
11 * @author Marcel Werk
12 * @copyright 2001-2018 WoltLab GmbH
13 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
14 * @package WoltLabSuite\Core\System\Conversation
15 */
16 class ConversationHandler extends SingletonFactory {
17 /**
18 * number of unread conversations
19 * @var integer[]
20 */
21 protected $unreadConversationCount = [];
22
23 /**
24 * number of conversations
25 * @var integer[]
26 */
27 protected $conversationCount = [];
28
29 /**
30 * Returns the number of unread conversations for given user.
31 *
32 * @param integer $userID
33 * @param boolean $skipCache
34 * @return integer
35 */
36 public function getUnreadConversationCount($userID = null, $skipCache = false) {
37 if ($userID === null) $userID = WCF::getUser()->userID;
38
39 if (!isset($this->unreadConversationCount[$userID]) || $skipCache) {
40 $this->unreadConversationCount[$userID] = 0;
41
42 // load storage data
43 UserStorageHandler::getInstance()->loadStorage([$userID]);
44
45 // get ids
46 $data = UserStorageHandler::getInstance()->getStorage([$userID], 'unreadConversationCount');
47
48 // cache does not exist or is outdated
49 if ($data[$userID] === null || $skipCache) {
50 $conditionBuilder = new PreparedStatementConditionBuilder();
51 $conditionBuilder->add('conversation.conversationID = conversation_to_user.conversationID');
52 $conditionBuilder->add('conversation_to_user.participantID = ?', [$userID]);
53 $conditionBuilder->add('conversation_to_user.hideConversation = 0');
54 $conditionBuilder->add('conversation_to_user.lastVisitTime < conversation.lastPostTime');
55 $conditionBuilder->add('conversation_to_user.leftAt = 0');
56
57 $sql = "SELECT COUNT(*) AS count
58 FROM wcf".WCF_N."_conversation_to_user conversation_to_user,
59 wcf".WCF_N."_conversation conversation
60 ".$conditionBuilder;
61 $statement = WCF::getDB()->prepareStatement($sql);
62 $statement->execute($conditionBuilder->getParameters());
63 $row = $statement->fetchArray();
64 $this->unreadConversationCount[$userID] = $row['count'];
65
66 // update storage data
67 UserStorageHandler::getInstance()->update($userID, 'unreadConversationCount', serialize($this->unreadConversationCount[$userID]));
68 }
69 else {
70 $this->unreadConversationCount[$userID] = unserialize($data[$userID]);
71 }
72 }
73
74 return $this->unreadConversationCount[$userID];
75 }
76
77 /**
78 * Returns the number of conversations for given user.
79 *
80 * @param integer $userID
81 * @return integer
82 */
83 public function getConversationCount($userID = null) {
84 if ($userID === null) $userID = WCF::getUser()->userID;
85
86 if (!isset($this->conversationCount[$userID])) {
87 $this->conversationCount[$userID] = 0;
88
89 // load storage data
90 UserStorageHandler::getInstance()->loadStorage([$userID]);
91
92 // get ids
93 $data = UserStorageHandler::getInstance()->getStorage([$userID], 'conversationCount');
94
95 // cache does not exist or is outdated
96 if ($data[$userID] === null) {
97 $conditionBuilder1 = new PreparedStatementConditionBuilder();
98 $conditionBuilder1->add('conversation_to_user.participantID = ?', [$userID]);
99 $conditionBuilder1->add('conversation_to_user.hideConversation IN (0,1)');
100 $conditionBuilder2 = new PreparedStatementConditionBuilder();
101 $conditionBuilder2->add('conversation.userID = ?', [$userID]);
102 $conditionBuilder2->add('conversation.isDraft = 1');
103
104 $sql = "SELECT (SELECT COUNT(*)
105 FROM wcf".WCF_N."_conversation_to_user conversation_to_user
106 ".$conditionBuilder1->__toString().")
107 +
108 (SELECT COUNT(*)
109 FROM wcf".WCF_N."_conversation conversation
110 ".$conditionBuilder2->__toString().") AS count";
111 $statement = WCF::getDB()->prepareStatement($sql);
112 $statement->execute(array_merge($conditionBuilder1->getParameters(), $conditionBuilder2->getParameters()));
113 $row = $statement->fetchArray();
114 $this->conversationCount[$userID] = $row['count'];
115
116 // update storage data
117 UserStorageHandler::getInstance()->update($userID, 'conversationCount', serialize($this->conversationCount[$userID]));
118 }
119 else {
120 $this->conversationCount[$userID] = unserialize($data[$userID]);
121 }
122 }
123
124 return $this->conversationCount[$userID];
125 }
126 }