Add `@method` comment for `DatabaseObjectList::getSingleObject()` in subclasses
[GitHub/WoltLab/com.woltlab.wcf.conversation.git] / files / lib / data / conversation / message / ViewableConversationMessageList.class.php
1 <?php
2
3 namespace wcf\data\conversation\message;
4
5 use wcf\data\attachment\GroupedAttachmentList;
6 use wcf\data\conversation\Conversation;
7 use wcf\data\object\type\ObjectTypeCache;
8 use wcf\system\cache\runtime\UserProfileRuntimeCache;
9 use wcf\system\message\embedded\object\MessageEmbeddedObjectManager;
10
11 /**
12 * Represents a list of viewable conversation messages.
13 *
14 * @author Marcel Werk
15 * @copyright 2001-2019 WoltLab GmbH
16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
17 * @package WoltLabSuite\Core\Data\Conversation\Message
18 *
19 * @method ViewableConversationMessage current()
20 * @method ViewableConversationMessage[] getObjects()
21 * @method ViewableConversationMessage|null getSingleObject()
22 * @method ViewableConversationMessage|null seach($objectID)
23 * @property ViewableConversationMessage[] $objects
24 */
25 class ViewableConversationMessageList extends ConversationMessageList
26 {
27 /**
28 * @inheritDoc
29 */
30 public $sqlOrderBy = 'conversation_message.time';
31
32 /**
33 * @inheritDoc
34 */
35 public $decoratorClassName = ViewableConversationMessage::class;
36
37 /**
38 * attachment object ids
39 * @var int[]
40 */
41 public $attachmentObjectIDs = [];
42
43 /**
44 * ids of the messages with embedded objects
45 * @var int[]
46 */
47 public $embeddedObjectMessageIDs = [];
48
49 /**
50 * attachment list
51 * @var GroupedAttachmentList
52 */
53 protected $attachmentList;
54
55 /**
56 * max post time
57 * @var int
58 */
59 protected $maxPostTime = 0;
60
61 /**
62 * enables/disables the loading of attachments
63 * @var bool
64 */
65 protected $attachmentLoading = true;
66
67 /**
68 * enables/disables the loading of embedded objects
69 * @var bool
70 */
71 protected $embeddedObjectLoading = true;
72
73 /**
74 * conversation object
75 * @var Conversation
76 */
77 protected $conversation;
78
79 /**
80 * @inheritDoc
81 */
82 public function readObjects()
83 {
84 if ($this->objectIDs === null) {
85 $this->readObjectIDs();
86 }
87
88 parent::readObjects();
89
90 $userIDs = [];
91 foreach ($this->objects as $message) {
92 if ($message->time > $this->maxPostTime) {
93 $this->maxPostTime = $message->time;
94 }
95 if ($this->conversation !== null) {
96 $message->setConversation($this->conversation);
97 }
98
99 if ($message->attachments) {
100 $this->attachmentObjectIDs[] = $message->messageID;
101 }
102
103 if ($message->hasEmbeddedObjects) {
104 $this->embeddedObjectMessageIDs[] = $message->messageID;
105 }
106 if ($message->userID) {
107 $userIDs[] = $message->userID;
108 }
109 }
110
111 if (!empty($userIDs)) {
112 UserProfileRuntimeCache::getInstance()->cacheObjectIDs($userIDs);
113 }
114
115 if ($this->embeddedObjectLoading) {
116 $this->readEmbeddedObjects();
117 }
118 if ($this->attachmentLoading) {
119 $this->readAttachments();
120 }
121 }
122
123 /**
124 * Reads the embedded objects of the messages in the list.
125 */
126 public function readEmbeddedObjects()
127 {
128 if (!empty($this->embeddedObjectMessageIDs)) {
129 // add message objects to attachment object cache to save SQL queries
130 ObjectTypeCache::getInstance()
131 ->getObjectTypeByName('com.woltlab.wcf.attachment.objectType', 'com.woltlab.wcf.conversation.message')
132 ->getProcessor()
133 ->setCachedObjects($this->objects);
134
135 // load embedded objects
136 MessageEmbeddedObjectManager::getInstance()
137 ->loadObjects('com.woltlab.wcf.conversation.message', $this->embeddedObjectMessageIDs);
138 }
139 }
140
141 /**
142 * Reads the list of attachments.
143 */
144 public function readAttachments()
145 {
146 if (!empty($this->attachmentObjectIDs)) {
147 $this->attachmentList = new GroupedAttachmentList('com.woltlab.wcf.conversation.message');
148 $this->attachmentList->getConditionBuilder()
149 ->add('attachment.objectID IN (?)', [$this->attachmentObjectIDs]);
150 $this->attachmentList->readObjects();
151 }
152 }
153
154 /**
155 * Returns the max post time.
156 *
157 * @return int
158 */
159 public function getMaxPostTime()
160 {
161 return $this->maxPostTime;
162 }
163
164 /**
165 * Returns the list of attachments.
166 *
167 * @return GroupedAttachmentList
168 */
169 public function getAttachmentList()
170 {
171 return $this->attachmentList;
172 }
173
174 /**
175 * Enables/disables the loading of attachments.
176 *
177 * @param bool $enable
178 */
179 public function enableAttachmentLoading($enable = true)
180 {
181 $this->attachmentLoading = $enable;
182 }
183
184 /**
185 * Enables/disables the loading of embedded objects.
186 *
187 * @param bool $enable
188 */
189 public function enableEmbeddedObjectLoading($enable = true)
190 {
191 $this->embeddedObjectLoading = $enable;
192 }
193
194 /**
195 * Sets active conversation.
196 *
197 * @param Conversation $conversation
198 */
199 public function setConversation(Conversation $conversation)
200 {
201 $this->conversation = $conversation;
202 }
203 }