Remove unnecessary parentheses around join conditions
[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\form\IForm;
9 use wcf\form\SearchForm;
10 use wcf\system\database\util\PreparedStatementConditionBuilder;
11 use wcf\system\WCF;
12
13 /**
14 * An implementation of ISearchableObjectType for searching in conversations.
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
20 */
21 class ConversationMessageSearch extends AbstractSearchableObjectType
22 {
23 /**
24 * id of the searched conversation
25 * @var int
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
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 {
69 if (isset($this->messageCache[$objectID])) {
70 return $this->messageCache[$objectID];
71 }
72 }
73
74 /**
75 * @inheritDoc
76 */
77 public function getJoins()
78 {
79 return " JOIN wcf" . WCF_N . "_conversation_to_user conversation_to_user
80 ON conversation_to_user.participantID = " . WCF::getUser()->userID . "
81 AND conversation_to_user.conversationID = " . $this->getTableName() . ".conversationID
82 LEFT JOIN wcf" . WCF_N . "_conversation conversation
83 ON conversation.conversationID = " . $this->getTableName() . ".conversationID";
84 }
85
86 /**
87 * @inheritDoc
88 */
89 public function getTableName()
90 {
91 return 'wcf' . WCF_N . '_conversation_message';
92 }
93
94 /**
95 * @inheritDoc
96 */
97 public function getIDFieldName()
98 {
99 return $this->getTableName() . '.messageID';
100 }
101
102 /**
103 * @inheritDoc
104 */
105 public function getSubjectFieldName()
106 {
107 return 'conversation.subject';
108 }
109
110 /**
111 * @inheritDoc
112 */
113 public function getConditions(?IForm $form = null)
114 {
115 $conditionBuilder = new PreparedStatementConditionBuilder();
116 $conditionBuilder->add('conversation_to_user.hideConversation IN (0,1)');
117
118 if (isset($_POST['conversationID'])) {
119 $this->conversationID = \intval($_POST['conversationID']);
120
121 $conditionBuilder->add('conversation.conversationID = ?', [$this->conversationID]);
122 }
123
124 return $conditionBuilder;
125 }
126
127 /**
128 * @inheritDoc
129 */
130 public function isAccessible()
131 {
132 if (!WCF::getUser()->userID) {
133 return false;
134 }
135 if (!MODULE_CONVERSATION) {
136 return false;
137 }
138
139 return WCF::getSession()->getPermission('user.conversation.canUseConversation');
140 }
141
142 /**
143 * @inheritDoc
144 */
145 public function getFormTemplateName()
146 {
147 if ($this->conversation) {
148 return 'searchConversationMessage';
149 }
150 }
151
152 /**
153 * @inheritDoc
154 */
155 public function show(?IForm $form = null)
156 {
157 /** @var SearchForm $form */
158
159 // get existing values
160 if (
161 $form !== null
162 && isset($form->searchData['additionalData']['com.woltlab.wcf.conversation.message']['conversationID'])
163 ) {
164 $this->conversationID = $form->searchData['additionalData']['com.woltlab.wcf.conversation.message']['conversationID'];
165
166 if ($this->conversationID) {
167 $this->conversation = Conversation::getUserConversation($this->conversationID, WCF::getUser()->userID);
168
169 if ($this->conversation === null || !$this->conversation->canRead()) {
170 $this->conversationID = 0;
171 $this->conversation = null;
172 }
173 }
174 }
175
176 WCF::getTPL()->assign('searchedConversation', $this->conversation);
177 }
178 }