ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilForumAutoSaveAsyncDraftAction.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22{
23 public function __construct(
24 private readonly ilObjUser $actor,
25 private readonly ilPropertyFormGUI $form,
26 private readonly ilForumProperties $forumProperties,
27 private readonly ilForumTopic $thread,
28 private readonly ?ilForumPost $relatedPost,
29 private readonly Closure $subjectFormatterCallable,
30 private readonly int $relatedDraftId,
31 private readonly int $relatedForumId,
32 private readonly string $action
33 ) {
34 }
35
36 public function executeAndGetResponseObject(): stdClass
37 {
38 $response = new stdClass();
39 $response->draft_id = 0;
40
41 if ($this->actor->isAnonymous() || $this->actor->getId() <= 0) {
42 return $response;
43 }
44
45 if ($this->thread->getId() > 0 && $this->thread->isClosed()) {
46 return $response;
47 }
48
50 return $response;
51 }
52
53 if (
54 $this->relatedPost instanceof ilForumPost && (
55 !$this->relatedPost->isActivated() || $this->relatedPost->isCensored()
56 )
57 ) {
58 return $response;
59 }
60
61 $relatedPostId = 0;
62 if ($this->relatedPost instanceof ilForumPost) {
63 $relatedPostId = $this->relatedPost->getId();
64 }
65
66 $this->form->checkInput();
67 $inputValues = $this->getInputValuesFromForm();
68
69 if ($this->relatedDraftId > 0) {
70 $draftId = $this->relatedDraftId;
71 } else {
72 $draftId = (int) $this->form->getInput('draft_id');
73 }
74
75 $subjectFormatterCallback = $this->subjectFormatterCallable;
76
77 if ($draftId > 0) {
78 if ('showreply' === $this->action) {
80
81 if ($draft->getPostAuthorId() !== $this->actor->getId()) {
82 return $response;
83 }
84
85 $draft->setPostSubject($subjectFormatterCallback($inputValues['subject']));
86 $draft->setPostMessage(ilRTE::_replaceMediaObjectImageSrc($inputValues['message'], 0));
87 $draft->setPostUserAlias($inputValues['alias']);
88 $draft->setNotificationStatus((bool) $inputValues['notify']);
89 $draft->setUpdateUserId($this->actor->getId());
90 $draft->setPostAuthorId($this->actor->getId());
91 $draft->setPostDisplayUserId(($this->forumProperties->isAnonymized() ? 0 : $this->actor->getId()));
92 $draft->updateDraft();
93
94 $uploadedObjects = ilObjMediaObject::_getMobsOfObject('frm~:html', $this->actor->getId());
95 $oldMediaObjects = ilObjMediaObject::_getMobsOfObject('frm~d:html', $draft->getDraftId());
96 $curMediaObjects = ilRTE::_getMediaObjects($inputValues['message'], 0);
97
98 $this->handleMedia(
100 $draft->getDraftId(),
101 $uploadedObjects,
102 $oldMediaObjects,
103 $curMediaObjects
104 );
105 } else {
107 if ($draft->getPostAuthorId() !== $this->actor->getId()) {
108 return $response;
109 }
110
111 $history_entry = new ilForumDraftsHistory();
112 $history_entry->setDraftId($draft->getDraftId());
113 $history_entry->setPostSubject($subjectFormatterCallback($inputValues['subject']));
114 $history_entry->setPostMessage(ilRTE::_replaceMediaObjectImageSrc($inputValues['message'], 0));
115 $history_entry->addDraftToHistory();
116
117 $uploadedObjects = ilObjMediaObject::_getMobsOfObject('frm~:html', $this->actor->getId());
118 $oldMediaObjects = ilObjMediaObject::_getMobsOfObject('frm~d:html', $history_entry->getDraftId());
119 $curMediaObjects = ilRTE::_getMediaObjects($inputValues['message'], 0);
120
121 $this->handleMedia(
123 $history_entry->getHistoryId(),
124 $uploadedObjects,
125 $oldMediaObjects,
126 $curMediaObjects
127 );
128 }
129 } else {
130 $draft = new ilForumPostDraft();
131 $draft->setForumId($this->relatedForumId);
132 $draft->setThreadId($this->thread->getId());
133 $draft->setPostId($relatedPostId);
134 $draft->setPostSubject($subjectFormatterCallback($inputValues['subject']));
135 $draft->setPostMessage(ilRTE::_replaceMediaObjectImageSrc($inputValues['message'], 0));
136 $draft->setPostUserAlias($inputValues['alias']);
137 $draft->setNotificationStatus((bool) $inputValues['notify']);
138 $draft->setPostAuthorId($this->actor->getId());
139 $draft->setPostDisplayUserId(($this->forumProperties->isAnonymized() ? 0 : $this->actor->getId()));
140 $draft->saveDraft();
141
142 $uploadedObjects = ilObjMediaObject::_getMobsOfObject('frm~:html', $this->actor->getId());
143 $oldMediaObjects = ilObjMediaObject::_getMobsOfObject('frm~d:html', $draft->getDraftId());
144 $curMediaObjects = ilRTE::_getMediaObjects($inputValues['message'], 0);
145
146 $this->handleMedia(
148 $draft->getDraftId(),
149 $uploadedObjects,
150 $oldMediaObjects,
151 $curMediaObjects
152 );
153 }
154
155 $response->draft_id = $draft->getDraftId();
156
157 return $response;
158 }
159
165 protected function handleMedia(
166 string $type,
167 int $draftId,
168 array $uploadedObjects,
169 array $oldMediaObjects,
170 array $curMediaObjects
171 ): void {
172 foreach ($uploadedObjects as $mob) {
173 ilObjMediaObject::_removeUsage($mob, 'frm~:html', $this->actor->getId());
174 ilObjMediaObject::_saveUsage($mob, $type, $draftId);
175 }
176
177 foreach ($oldMediaObjects as $mob) {
178 ilObjMediaObject::_saveUsage($mob, $type, $draftId);
179 }
180
181 foreach ($curMediaObjects as $mob) {
182 ilObjMediaObject::_saveUsage($mob, $type, $draftId);
183 }
184 }
185
189 protected function getInputValuesFromForm(): array
190 {
191 return [
192 'subject' => (string) $this->form->getInput('subject'),
193 'message' => (string) $this->form->getInput('message'),
194 'notify' => (int) $this->form->getInput('notify'),
196 $this->form->getInput('alias'),
197 $this->forumProperties->isAnonymized()
198 )
199 ];
200 }
201}
__construct(private readonly ilObjUser $actor, private readonly ilPropertyFormGUI $form, private readonly ilForumProperties $forumProperties, private readonly ilForumTopic $thread, private readonly ?ilForumPost $relatedPost, private readonly Closure $subjectFormatterCallable, private readonly int $relatedDraftId, private readonly int $relatedForumId, private readonly string $action)
handleMedia(string $type, int $draftId, array $uploadedObjects, array $oldMediaObjects, array $curMediaObjects)
Class ilForumDraftHistory.
Class ilForumPostDraft.
static newInstanceByDraftId(int $draft_id)
static getPublicUserAlias(string $user_alias, bool $is_anonymized)
static _getMobsOfObject(string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
static _removeUsage(int $a_mob_id, string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
Remove usage of mob in another container.
static _saveUsage(int $a_mob_id, string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
Save usage of mob within another container (e.g.
User class.
This class represents a property form user interface.
static _replaceMediaObjectImageSrc(string $a_text, int $a_direction=0, string $nic='')
Replaces image source from mob image urls with the mob id or replaces mob id with the correct image s...
static _getMediaObjects(string $a_text, int $a_direction=0)
Returns all media objects found in the passed string.
form( $class_path, string $cmd, string $submit_caption="")
$response
Definition: xapitoken.php:93