Commit | Line | Data |
---|---|---|
df8f8628 MW |
1 | <?php |
2 | namespace wcf\system\conversation; | |
3 | use wcf\system\database\util\PreparedStatementConditionBuilder; | |
4 | use wcf\system\package\PackageDependencyHandler; | |
5 | use wcf\system\user\storage\UserStorageHandler; | |
6 | use wcf\system\SingletonFactory; | |
7 | use wcf\system\WCF; | |
8 | ||
9 | class ConversationHandler extends SingletonFactory { | |
10 | /** | |
11 | * number of unread conversations | |
61f754e0 | 12 | * @var array<integer> |
df8f8628 | 13 | */ |
56b80269 | 14 | protected $unreadConversationCount = array(); |
df8f8628 | 15 | |
3bbcb27d MW |
16 | /** |
17 | * number of conversations | |
61f754e0 | 18 | * @var array<integer> |
3bbcb27d | 19 | */ |
56b80269 | 20 | protected $conversationCount = array(); |
3bbcb27d | 21 | |
df8f8628 | 22 | /** |
56b80269 | 23 | * Returns the number of unread conversations for given user. |
df8f8628 | 24 | * |
56b80269 | 25 | * @param integer $userID |
df8f8628 MW |
26 | * @return integer |
27 | */ | |
56b80269 MW |
28 | public function getUnreadConversationCount($userID = null) { |
29 | if ($userID === null) $userID = WCF::getUser()->userID; | |
df8f8628 | 30 | |
56b80269 MW |
31 | if (!isset($this->unreadConversationCount[$userID])) { |
32 | $this->unreadConversationCount[$userID] = 0; | |
33 | ||
34 | // load storage data | |
35 | UserStorageHandler::getInstance()->loadStorage(array($userID)); | |
36 | ||
37 | // get ids | |
38 | $data = UserStorageHandler::getInstance()->getStorage(array($userID), 'unreadConversationCount'); | |
39 | ||
40 | // cache does not exist or is outdated | |
41 | if ($data[$userID] === null) { | |
42 | $conditionBuilder = new PreparedStatementConditionBuilder(); | |
43 | $conditionBuilder->add('conversation.conversationID = conversation_to_user.conversationID'); | |
44 | $conditionBuilder->add('conversation_to_user.participantID = ?', array($userID)); | |
45 | $conditionBuilder->add('conversation_to_user.hideConversation = 0'); | |
46 | $conditionBuilder->add('conversation_to_user.lastVisitTime < conversation.lastPostTime'); | |
df8f8628 | 47 | |
56b80269 MW |
48 | $sql = "SELECT COUNT(*) AS count |
49 | FROM wcf".WCF_N."_conversation_to_user conversation_to_user, | |
50 | wcf".WCF_N."_conversation conversation | |
51 | ".$conditionBuilder->__toString(); | |
52 | $statement = WCF::getDB()->prepareStatement($sql); | |
53 | $statement->execute($conditionBuilder->getParameters()); | |
54 | $row = $statement->fetchArray(); | |
55 | $this->unreadConversationCount[$userID] = $row['count']; | |
56 | ||
57 | // update storage data | |
58 | UserStorageHandler::getInstance()->update($userID, 'unreadConversationCount', serialize($this->unreadConversationCount[$userID]), PackageDependencyHandler::getInstance()->getPackageID('com.woltlab.wcf.conversation')); | |
59 | } | |
60 | else { | |
61 | $this->unreadConversationCount[$userID] = unserialize($data[$userID]); | |
df8f8628 MW |
62 | } |
63 | } | |
64 | ||
56b80269 | 65 | return $this->unreadConversationCount[$userID]; |
df8f8628 | 66 | } |
3bbcb27d MW |
67 | |
68 | /** | |
56b80269 | 69 | * Returns the number of conversations for given user. |
3bbcb27d | 70 | * |
56b80269 | 71 | * @param integer $userID |
3bbcb27d MW |
72 | * @return integer |
73 | */ | |
56b80269 MW |
74 | public function getConversationCount($userID = null) { |
75 | if ($userID === null) $userID = WCF::getUser()->userID; | |
3bbcb27d | 76 | |
56b80269 MW |
77 | if (!isset($this->conversationCount[$userID])) { |
78 | $this->conversationCount[$userID] = 0; | |
61f754e0 | 79 | |
56b80269 MW |
80 | // load storage data |
81 | UserStorageHandler::getInstance()->loadStorage(array($userID)); | |
82 | ||
83 | // get ids | |
84 | $data = UserStorageHandler::getInstance()->getStorage(array($userID), 'conversationCount'); | |
85 | ||
86 | // cache does not exist or is outdated | |
87 | if ($data[$userID] === null) { | |
88 | $conditionBuilder1 = new PreparedStatementConditionBuilder(); | |
89 | $conditionBuilder1->add('conversation_to_user.participantID = ?', array($userID)); | |
90 | $conditionBuilder1->add('conversation_to_user.hideConversation IN (0,1)'); | |
91 | $conditionBuilder2 = new PreparedStatementConditionBuilder(); | |
92 | $conditionBuilder2->add('conversation.userID = ?', array($userID)); | |
93 | $conditionBuilder2->add('conversation.isDraft = 1'); | |
3bbcb27d | 94 | |
56b80269 MW |
95 | $sql = "SELECT (SELECT COUNT(*) |
96 | FROM wcf".WCF_N."_conversation_to_user conversation_to_user | |
97 | ".$conditionBuilder1->__toString().") | |
98 | + | |
99 | (SELECT COUNT(*) | |
100 | FROM wcf".WCF_N."_conversation conversation | |
101 | ".$conditionBuilder2->__toString().") AS count"; | |
102 | $statement = WCF::getDB()->prepareStatement($sql); | |
103 | $statement->execute(array_merge($conditionBuilder1->getParameters(), $conditionBuilder2->getParameters())); | |
104 | $row = $statement->fetchArray(); | |
105 | $this->conversationCount[$userID] = $row['count']; | |
106 | ||
107 | // update storage data | |
108 | UserStorageHandler::getInstance()->update($userID, 'conversationCount', serialize($this->conversationCount[$userID]), PackageDependencyHandler::getInstance()->getPackageID('com.woltlab.wcf.conversation')); | |
109 | } | |
110 | else { | |
111 | $this->conversationCount[$userID] = unserialize($data[$userID]); | |
3bbcb27d MW |
112 | } |
113 | } | |
114 | ||
56b80269 | 115 | return $this->conversationCount[$userID]; |
3bbcb27d | 116 | } |
df8f8628 | 117 | } |