ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilForumAutoSaveAsyncDraftAction.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3
9{
11 private $actor;
12
14 private $form;
15
18
20 private $thread;
21
23 private $relatedPost;
24
27
29 private $relatedDraftId = 0;
30
33
35 private $action = '';
36
49 public function __construct(
58 string $action
59 ) {
60 $this->actor = $actor;
61 $this->form = $form;
62 $this->forumProperties = $forumProperties;
63 $this->thread = $thread;
64 $this->relatedPost = $relatedPost;
65 $this->subjectFormatterCallable = $subjectFormatterCallable;
66
67 $this->relatedDraftId = $relatedDraftId;
68 $this->relatedForumId = $relatedForumId;
69 $this->action = $action;
70 }
71
76 {
77 $response = new \stdClass();
78 $response->draft_id = 0;
79
80 if ($this->actor->isAnonymous() || !($this->actor->getId() > 0)) {
81 return $response;
82 }
83
84 if ($this->thread->getId() > 0 && $this->thread->isClosed()) {
85 return $response;
86 }
87
89 return $response;
90 }
91
92 if (
93 $this->relatedPost instanceof \ilForumPost && (
94 !$this->relatedPost->isActivated() || $this->relatedPost->isCensored()
95 )
96 ) {
97 return $response;
98 }
99
100 $relatedPostId = 0;
101 if ($this->relatedPost instanceof \ilForumPost) {
102 $relatedPostId = $this->relatedPost->getId();
103 }
104
105 $this->form->checkInput();
106 $inputValues = $this->getInputValuesFromForm();
107
108 if ($this->relatedDraftId > 0) {
109 $draftId = $this->relatedDraftId;
110 } else {
111 $draftId = (int) $this->form->getInput('draft_id');
112 }
113
114 $subjectFormatterCallback = $this->subjectFormatterCallable;
115
116 if ($draftId > 0) {
117 if ('showreply' === $this->action) {
118 $draftObj = \ilForumPostDraft::newInstanceByDraftId($draftId);
119 $draftObj->setPostSubject($subjectFormatterCallback($inputValues['subject']));
120 $draftObj->setPostMessage(\ilRTE::_replaceMediaObjectImageSrc($inputValues['message'], 0));
121 $draftObj->setPostUserAlias($inputValues['alias']);
122 $draftObj->setNotify($inputValues['notify']);
123 $draftObj->setUpdateUserId($this->actor->getId());
124 $draftObj->setPostAuthorId($this->actor->getId());
125 $draftObj->setPostDisplayUserId(($this->forumProperties->isAnonymized() ? 0 : $this->actor->getId()));
126 $draftObj->updateDraft();
127
128 $uploadedObjects = \ilObjMediaObject::_getMobsOfObject('frm~:html', $this->actor->getId());
129 $oldMediaObjects = \ilObjMediaObject::_getMobsOfObject('frm~d:html', $draftObj->getDraftId());
130 $curMediaObjects = \ilRTE::_getMediaObjects($inputValues['message'], 0);
131
132 $this->handleMedia(
134 $draftObj->getDraftId(),
135 $uploadedObjects,
136 $oldMediaObjects,
137 $curMediaObjects
138 );
139 } else {
140 $draftObj = new \ilForumDraftsHistory();
141 $draftObj->setDraftId($draftId);
142 $draftObj->setPostSubject($subjectFormatterCallback($inputValues['subject']));
143 $draftObj->setPostMessage(\ilRTE::_replaceMediaObjectImageSrc($inputValues['message'], 0));
144 $draftObj->addDraftToHistory();
145
146 $uploadedObjects = \ilObjMediaObject::_getMobsOfObject('frm~:html', $this->actor->getId());
147 $oldMediaObjects = \ilObjMediaObject::_getMobsOfObject('frm~d:html', $draftObj->getDraftId());
148 $curMediaObjects = \ilRTE::_getMediaObjects($inputValues['message'], 0);
149
150 $this->handleMedia(
152 $draftObj->getHistoryId(),
153 $uploadedObjects,
154 $oldMediaObjects,
155 $curMediaObjects
156 );
157 }
158 } else {
159 $draftObj = new \ilForumPostDraft();
160 $draftObj->setForumId($this->relatedForumId);
161 $draftObj->setThreadId($this->thread->getId());
162 $draftObj->setPostId($relatedPostId);
163 $draftObj->setPostSubject($subjectFormatterCallback($inputValues['subject']));
164 $draftObj->setPostMessage(\ilRTE::_replaceMediaObjectImageSrc($inputValues['message'], 0));
165 $draftObj->setPostUserAlias($inputValues['alias']);
166 $draftObj->setNotify($inputValues['notify']);
167 $draftObj->setPostAuthorId($this->actor->getId());
168 $draftObj->setPostDisplayUserId(($this->forumProperties->isAnonymized() ? 0 : $this->actor->getId()));
169 $draftObj->saveDraft();
170
171 $uploadedObjects = \ilObjMediaObject::_getMobsOfObject('frm~:html', $this->actor->getId());
172 $oldMediaObjects = \ilObjMediaObject::_getMobsOfObject('frm~d:html', $draftObj->getDraftId());
173 $curMediaObjects = \ilRTE::_getMediaObjects($inputValues['message'], 0);
174
175 $this->handleMedia(
177 $draftObj->getDraftId(),
178 $uploadedObjects,
179 $oldMediaObjects,
180 $curMediaObjects
181 );
182 }
183
184 $response->draft_id = $draftObj->getDraftId();
185
186 return $response;
187 }
188
196 protected function handleMedia(
197 string $type,
198 int $draftId,
199 array $uploadedObjects,
200 array $oldMediaObjects,
201 array $curMediaObjects
202 ) {
203 foreach ($uploadedObjects as $mob) {
204 \ilObjMediaObject::_removeUsage($mob, 'frm~:html', $this->actor->getId());
205 \ilObjMediaObject::_saveUsage($mob, $type, $draftId);
206 }
207
208 foreach ($oldMediaObjects as $mob) {
209 \ilObjMediaObject::_saveUsage($mob, $type, $draftId);
210 }
211
212 foreach ($curMediaObjects as $mob) {
213 \ilObjMediaObject::_saveUsage($mob, $type, $draftId);
214 }
215 }
216
220 protected function getInputValuesFromForm() : array
221 {
222 $inputValues = [];
223
224 $inputValues['subject'] = (string) $this->form->getInput('subject');
225 $inputValues['message'] = (string) $this->form->getInput('message');
226 $inputValues['notify'] = (int) $this->form->getInput('notify');
227 $inputValues['alias'] = \ilForumUtil::getPublicUserAlias(
228 (string) $this->form->getInput('alias'),
229 $this->forumProperties->isAnonymized()
230 );
231
232 return $inputValues;
233 }
234}
An exception for terminatinating execution or to throw for unit testing.
handleMedia(string $type, int $draftId, array $uploadedObjects, array $oldMediaObjects, array $curMediaObjects)
__construct(\ilObjUser $actor, \ilPropertyFormGUI $form, \ilForumProperties $forumProperties, \ilForumTopic $thread, $relatedPost, callable $subjectFormatterCallable, int $relatedDraftId, int $relatedForumId, string $action)
ilForumAutoSaveAsyncDraftAction constructor.
static newInstanceByDraftId($draft_id)
static getPublicUserAlias($user_alias, $is_anonymized=false)
static _saveUsage($a_mob_id, $a_type, $a_id, $a_usage_hist_nr=0, $a_lang="-")
Save usage of mob within another container (e.g.
static _getMobsOfObject($a_type, $a_id, $a_usage_hist_nr=0, $a_lang="-")
get mobs of object
static _removeUsage($a_mob_id, $a_type, $a_id, $a_usage_hist_nr=0, $a_lang="-")
Remove usage of mob in another container.
This class represents a property form user interface.
static _replaceMediaObjectImageSrc($a_text, $a_direction=0, $nic=IL_INST_ID)
Replaces image source from mob image urls with the mob id or replaces mob id with the correct image s...
static _getMediaObjects($a_text, $a_direction=0)
Returns all media objects found in the passed string.
$type
$response