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