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