Migrate to new event organization
[GitHub/WoltLab/com.woltlab.wcf.conversation.git] / files / lib / system / search / ConversationMessageSearch.class.php
1 <?php
2
3 namespace wcf\system\search;
4
5 use wcf\data\conversation\Conversation;
6 use wcf\data\conversation\message\SearchResultConversationMessage;
7 use wcf\data\conversation\message\SearchResultConversationMessageList;
8 use wcf\data\search\ISearchResultObject;
9 use wcf\system\database\util\PreparedStatementConditionBuilder;
10 use wcf\system\WCF;
11
12 /**
13 * An implementation of ISearchProvider for searching in conversations.
14 *
15 * @author Marcel Werk
16 * @copyright 2001-2019 WoltLab GmbH
17 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
18 */
19 final class ConversationMessageSearch extends AbstractSearchProvider
20 {
21 /**
22 * @var int
23 */
24 private $conversationID = 0;
25
26 /**
27 * searched conversation
28 * @var Conversation
29 */
30 private $conversation;
31
32 /**
33 * @var SearchResultConversationMessage[]
34 */
35 private $messageCache = [];
36
37 /**
38 * @inheritDoc
39 */
40 public function cacheObjects(array $objectIDs, ?array $additionalData = null): void
41 {
42 $messageList = new SearchResultConversationMessageList();
43 $messageList->setObjectIDs($objectIDs);
44 $messageList->readObjects();
45 foreach ($messageList->getObjects() as $message) {
46 $this->messageCache[$message->messageID] = $message;
47 }
48 }
49
50 /**
51 * @inheritDoc
52 */
53 public function getAdditionalData(): ?array
54 {
55 return [
56 'conversationID' => $this->conversationID,
57 ];
58 }
59
60 /**
61 * @inheritDoc
62 */
63 public function getObject(int $objectID): ?ISearchResultObject
64 {
65 return $this->messageCache[$objectID] ?? null;
66 }
67
68 /**
69 * @inheritDoc
70 */
71 public function getJoins(): string
72 {
73 return " JOIN wcf" . WCF_N . "_conversation_to_user conversation_to_user
74 ON conversation_to_user.participantID = " . WCF::getUser()->userID . "
75 AND conversation_to_user.conversationID = " . $this->getTableName() . ".conversationID
76 LEFT JOIN wcf" . WCF_N . "_conversation conversation
77 ON conversation.conversationID = " . $this->getTableName() . ".conversationID";
78 }
79
80 /**
81 * @inheritDoc
82 */
83 public function getTableName(): string
84 {
85 return 'wcf' . WCF_N . '_conversation_message';
86 }
87
88 /**
89 * @inheritDoc
90 */
91 public function getIDFieldName(): string
92 {
93 return $this->getTableName() . '.messageID';
94 }
95
96 /**
97 * @inheritDoc
98 */
99 public function getSubjectFieldName(): string
100 {
101 return 'conversation.subject';
102 }
103
104 /**
105 * @inheritDoc
106 */
107 public function getConditionBuilder(array $parameters): ?PreparedStatementConditionBuilder
108 {
109 $this->readParameters($parameters);
110
111 $conditionBuilder = new PreparedStatementConditionBuilder();
112 $conditionBuilder->add('conversation_to_user.hideConversation IN (0,1)');
113 if ($this->conversationID) {
114 $conditionBuilder->add('conversation.conversationID = ?', [$this->conversationID]);
115 }
116
117 return $conditionBuilder;
118 }
119
120 /**
121 * @inheritDoc
122 */
123 public function isAccessible(): bool
124 {
125 if (!WCF::getUser()->userID) {
126 return false;
127 }
128 if (!MODULE_CONVERSATION) {
129 return false;
130 }
131
132 return WCF::getSession()->getPermission('user.conversation.canUseConversation');
133 }
134
135 /**
136 * @inheritDoc
137 */
138 public function getFormTemplateName(): string
139 {
140 if ($this->conversation) {
141 return 'searchConversationMessage';
142 }
143
144 return '';
145 }
146
147 /**
148 * @inheritDoc
149 */
150 public function assignVariables(): void
151 {
152 if (!empty($_REQUEST['conversationID'])) {
153 $conversation = Conversation::getUserConversation(\intval($_REQUEST['conversationID']), WCF::getUser()->userID);
154 if ($conversation !== null && $conversation->canRead()) {
155 $this->conversation = $conversation;
156 WCF::getTPL()->assign('searchedConversation', $conversation);
157 }
158 }
159 }
160
161 /**
162 * @inheritDoc
163 */
164 private function readParameters(array $parameters): void
165 {
166 if (!empty($parameters['conversationID'])) {
167 $this->conversationID = \intval($parameters['conversationID']);
168 }
169 }
170 }