ILIAS  release_8 Revision v8.23
class.ilForumAutoSaveAsyncDraftAction.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 {
27  private ilObjUser $actor;
33  private int $relatedDraftId;
34  private int $relatedForumId;
35  private string $action;
36 
37  public function __construct(
38  ilObjUser $actor,
39  ilPropertyFormGUI $form,
40  ilForumProperties $forumProperties,
41  ilForumTopic $thread,
42  ?ilForumPost $relatedPost,
43  Closure $subjectFormatterCallable,
44  int $relatedDraftId,
45  int $relatedForumId,
46  string $action
47  ) {
48  $this->actor = $actor;
49  $this->form = $form;
50  $this->forumProperties = $forumProperties;
51  $this->thread = $thread;
52  $this->relatedPost = $relatedPost;
53  $this->subjectFormatterCallable = $subjectFormatterCallable;
54 
55  $this->relatedDraftId = $relatedDraftId;
56  $this->relatedForumId = $relatedForumId;
57  $this->action = $action;
58  }
59 
61  {
62  $response = new stdClass();
63  $response->draft_id = 0;
64 
65  if ($this->actor->isAnonymous() || !($this->actor->getId() > 0)) {
66  return $response;
67  }
68 
69  if ($this->thread->getId() > 0 && $this->thread->isClosed()) {
70  return $response;
71  }
72 
74  return $response;
75  }
76 
77  if (
78  $this->relatedPost instanceof ilForumPost && (
79  !$this->relatedPost->isActivated() || $this->relatedPost->isCensored()
80  )
81  ) {
82  return $response;
83  }
84 
85  $relatedPostId = 0;
86  if ($this->relatedPost instanceof ilForumPost) {
87  $relatedPostId = $this->relatedPost->getId();
88  }
89 
90  $this->form->checkInput();
91  $inputValues = $this->getInputValuesFromForm();
92 
93  if ($this->relatedDraftId > 0) {
94  $draftId = $this->relatedDraftId;
95  } else {
96  $draftId = (int) $this->form->getInput('draft_id');
97  }
98 
99  $subjectFormatterCallback = $this->subjectFormatterCallable;
100 
101  if ($draftId > 0) {
102  if ('showreply' === $this->action) {
103  $draftObj = ilForumPostDraft::newInstanceByDraftId($draftId);
104  $draftObj->setPostSubject($subjectFormatterCallback($inputValues['subject']));
105  $draftObj->setPostMessage(ilRTE::_replaceMediaObjectImageSrc($inputValues['message'], 0));
106  $draftObj->setPostUserAlias($inputValues['alias']);
107  $draftObj->setNotificationStatus((bool) $inputValues['notify']);
108  $draftObj->setUpdateUserId($this->actor->getId());
109  $draftObj->setPostAuthorId($this->actor->getId());
110  $draftObj->setPostDisplayUserId(($this->forumProperties->isAnonymized() ? 0 : $this->actor->getId()));
111  $draftObj->updateDraft();
112 
113  $uploadedObjects = ilObjMediaObject::_getMobsOfObject('frm~:html', $this->actor->getId());
114  $oldMediaObjects = ilObjMediaObject::_getMobsOfObject('frm~d:html', $draftObj->getDraftId());
115  $curMediaObjects = ilRTE::_getMediaObjects($inputValues['message'], 0);
116 
117  $this->handleMedia(
119  $draftObj->getDraftId(),
120  $uploadedObjects,
121  $oldMediaObjects,
122  $curMediaObjects
123  );
124  } else {
125  $draftObj = new ilForumDraftsHistory();
126  $draftObj->setDraftId($draftId);
127  $draftObj->setPostSubject($subjectFormatterCallback($inputValues['subject']));
128  $draftObj->setPostMessage(ilRTE::_replaceMediaObjectImageSrc($inputValues['message'], 0));
129  $draftObj->addDraftToHistory();
130 
131  $uploadedObjects = ilObjMediaObject::_getMobsOfObject('frm~:html', $this->actor->getId());
132  $oldMediaObjects = ilObjMediaObject::_getMobsOfObject('frm~d:html', $draftObj->getDraftId());
133  $curMediaObjects = ilRTE::_getMediaObjects($inputValues['message'], 0);
134 
135  $this->handleMedia(
137  $draftObj->getHistoryId(),
138  $uploadedObjects,
139  $oldMediaObjects,
140  $curMediaObjects
141  );
142  }
143  } else {
144  $draftObj = new ilForumPostDraft();
145  $draftObj->setForumId($this->relatedForumId);
146  $draftObj->setThreadId($this->thread->getId());
147  $draftObj->setPostId($relatedPostId);
148  $draftObj->setPostSubject($subjectFormatterCallback($inputValues['subject']));
149  $draftObj->setPostMessage(ilRTE::_replaceMediaObjectImageSrc($inputValues['message'], 0));
150  $draftObj->setPostUserAlias($inputValues['alias']);
151  $draftObj->setNotificationStatus((bool) $inputValues['notify']);
152  $draftObj->setPostAuthorId($this->actor->getId());
153  $draftObj->setPostDisplayUserId(($this->forumProperties->isAnonymized() ? 0 : $this->actor->getId()));
154  $draftObj->saveDraft();
155 
156  $uploadedObjects = ilObjMediaObject::_getMobsOfObject('frm~:html', $this->actor->getId());
157  $oldMediaObjects = ilObjMediaObject::_getMobsOfObject('frm~d:html', $draftObj->getDraftId());
158  $curMediaObjects = ilRTE::_getMediaObjects($inputValues['message'], 0);
159 
160  $this->handleMedia(
162  $draftObj->getDraftId(),
163  $uploadedObjects,
164  $oldMediaObjects,
165  $curMediaObjects
166  );
167  }
168 
169  $response->draft_id = $draftObj->getDraftId();
170 
171  return $response;
172  }
173 
181  protected function handleMedia(
182  string $type,
183  int $draftId,
184  array $uploadedObjects,
185  array $oldMediaObjects,
186  array $curMediaObjects
187  ): void {
188  foreach ($uploadedObjects as $mob) {
189  ilObjMediaObject::_removeUsage($mob, 'frm~:html', $this->actor->getId());
190  ilObjMediaObject::_saveUsage($mob, $type, $draftId);
191  }
192 
193  foreach ($oldMediaObjects as $mob) {
194  ilObjMediaObject::_saveUsage($mob, $type, $draftId);
195  }
196 
197  foreach ($curMediaObjects as $mob) {
198  ilObjMediaObject::_saveUsage($mob, $type, $draftId);
199  }
200  }
201 
202  protected function getInputValuesFromForm(): array
203  {
204  $inputValues = [];
205 
206  $inputValues['subject'] = $this->form->getInput('subject');
207  $inputValues['message'] = $this->form->getInput('message');
208  $inputValues['notify'] = (int) $this->form->getInput('notify');
209  $inputValues['alias'] = ilForumUtil::getPublicUserAlias(
210  $this->form->getInput('alias'),
211  $this->forumProperties->isAnonymized()
212  );
213 
214  return $inputValues;
215  }
216 }
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.
Class ilForumPostDraft.
handleMedia(string $type, int $draftId, array $uploadedObjects, array $oldMediaObjects, array $curMediaObjects)
$type
__construct(ilObjUser $actor, ilPropertyFormGUI $form, ilForumProperties $forumProperties, ilForumTopic $thread, ?ilForumPost $relatedPost, Closure $subjectFormatterCallable, int $relatedDraftId, int $relatedForumId, string $action)
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.
Class ilForumDraftHistory.
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="-")
form( $class_path, string $cmd)
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 newInstanceByDraftId(int $draft_id)
$response