ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilForumDraftsHistory.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 {
27  public const MEDIAOBJECT_TYPE = 'frm~h:html';
28 
29  private readonly ilDBInterface $db;
30  private int $history_id = 0;
31  private int $draft_id = 0;
32  private string $post_subject = '';
33  private string $post_message = '';
34  protected string $draft_date = '0000-00-00 00:00:00';
35 
36  public function getHistoryId(): int
37  {
38  return $this->history_id;
39  }
40 
41  public function setHistoryId(int $history_id): void
42  {
43  $this->history_id = $history_id;
44  }
45 
46  public function getDraftId(): int
47  {
48  return $this->draft_id;
49  }
50 
51  public function setDraftId(int $draft_id): void
52  {
53  $this->draft_id = $draft_id;
54  }
55 
56  public function getPostSubject(): string
57  {
58  return $this->post_subject;
59  }
60 
61  public function setPostSubject(string $post_subject): void
62  {
63  $this->post_subject = $post_subject;
64  }
65 
66  public function getPostMessage(): string
67  {
68  return $this->post_message;
69  }
70 
71  public function setPostMessage(string $post_message): void
72  {
73  $this->post_message = $post_message;
74  }
75 
76  public function getDraftDate(): string
77  {
78  return $this->draft_date;
79  }
80 
81  public function setDraftDate(string $draft_date): void
82  {
83  $this->draft_date = $draft_date;
84  }
85 
86  public function __construct(int $history_id = 0)
87  {
88  global $DIC;
89 
90  $this->db = $DIC->database();
91 
92  if ($history_id > 0) {
93  $this->readByHistoryId($history_id);
94  }
95  }
96 
97  private function readByHistoryId(int $history_id): void
98  {
99  $res = $this->db->queryF(
100  'SELECT * FROM frm_drafts_history WHERE history_id = %s',
101  ['integer'],
102  [$history_id]
103  );
104 
105  while ($row = $this->db->fetchAssoc($res)) {
106  $this->setHistoryId((int) $row['history_id']);
107  $this->setDraftId((int) $row['draft_id']);
108  $this->setPostMessage($row['post_message']);
109  $this->setPostSubject($row['post_subject']);
110  $this->setDraftDate($row['draft_date']);
111  }
112  }
113 
117  public static function getInstancesByDraftId(int $draft_id): array
118  {
119  global $DIC;
120 
121  $ilDB = $DIC->database();
122 
123  $res = $ilDB->queryF(
124  'SELECT * FROM frm_drafts_history WHERE draft_id = %s ORDER BY draft_date DESC',
125  ['integer'],
126  [$draft_id]
127  );
128  $instances = [];
129  while ($row = $ilDB->fetchAssoc($res)) {
130  $draftHistory = new self();
131  $draftHistory = self::populateWithDatabaseRecord($draftHistory, $row);
132 
133  $instances[] = $draftHistory;
134  }
135 
136  return $instances;
137  }
138 
139  protected static function populateWithDatabaseRecord(
140  ilForumDraftsHistory $history_draft,
141  array $row
143  $history_draft->setHistoryId((int) $row['history_id']);
144  $history_draft->setDraftId((int) $row['draft_id']);
145  $history_draft->setPostMessage($row['post_message']);
146  $history_draft->setPostSubject($row['post_subject']);
147  $history_draft->setDraftDate($row['draft_date']);
148 
149  return $history_draft;
150  }
151 
152  public function delete(): void
153  {
154  $this->db->manipulateF(
155  'DELETE FROM frm_drafts_history WHERE history_id = %s',
156  ['integer'],
157  [$this->getHistoryId()]
158  );
159  }
160 
161  public function populateWithFirstAutosaveByDraftId(int $draft_id): void
162  {
163  $this->db->setLimit(1);
164  $res = $this->db->queryF(
165  'SELECT * FROM frm_drafts_history WHERE draft_id = %s ORDER BY history_id ASC',
166  ['integer'],
167  [$draft_id]
168  );
169 
170  if ($row = $this->db->fetchAssoc($res)) {
171  $this->setHistoryId((int) $row['history_id']);
172  $this->setDraftId((int) $row['draft_id']);
173  $this->setPostSubject($row['post_subject']);
174  $this->setPostMessage($row['post_message']);
175  }
176  }
177 
178  public function getLastAutosaveByDraftId(int $draft_id): void
179  {
180  $this->db->setLimit(1);
181  $res = $this->db->queryF(
182  'SELECT * FROM frm_drafts_history WHERE draft_id = %s ORDER BY history_id DESC',
183  ['integer'],
184  [$draft_id]
185  );
186 
187  while ($row = $this->db->fetchAssoc($res)) {
188  $this->setHistoryId((int) $row['history_id']);
189  $this->setDraftId((int) $row['draft_id']);
190  $this->setPostSubject($row['post_subject']);
191  $this->setPostMessage($row['post_message']);
192  }
193  }
194 
195  public function addDraftToHistory(): void
196  {
197  $next_id = $this->db->nextId('frm_drafts_history');
198  $this->db->insert(
199  'frm_drafts_history',
200  [
201  'history_id' => ['integer', $next_id],
202  'draft_id' => ['integer', $this->getDraftId()],
203  'post_subject' => ['text', $this->getPostSubject()],
204  'post_message' => ['text', $this->getPostMessage()],
205  'draft_date' => ['timestamp', date("Y-m-d H:i:s")]
206  ]
207  );
208  $this->setHistoryId($next_id);
209  }
210 
211  public function deleteMobs(): void
212  {
213  $oldMediaObjects = ilObjMediaObject::_getMobsOfObject('frm~h:html', $this->getHistoryId());
214  foreach ($oldMediaObjects as $oldMob) {
215  if (ilObjMediaObject::_exists($oldMob)) {
216  ilObjMediaObject::_removeUsage($oldMob, 'frm~h:html', $this->getHistoryId());
217  $mob_obj = new ilObjMediaObject($oldMob);
218  $mob_obj->delete();
219  }
220  }
221  }
222 
224  {
226  $draft->setPostSubject($this->getPostSubject());
227  $draft->setPostMessage($this->getPostMessage());
228 
230  $this->getPostMessage(),
231  self::MEDIAOBJECT_TYPE,
232  $this->getHistoryId(),
234  $draft->getDraftId()
235  );
236 
237  $draft->updateDraft();
238  $this->deleteHistoryByDraftIds([$draft->getDraftId()]);
239 
240  return $draft;
241  }
242 
247  public function deleteHistoryByPostIds(array $post_ids = []): array
248  {
249  $draft_ids = [];
250  if ($post_ids !== []) {
251  $res = $this->db->query('
252  SELECT frm_drafts_history.history_id, frm_drafts_history.draft_id
253  FROM frm_posts_drafts
254  INNER JOIN frm_drafts_history ON frm_posts_drafts.draft_id
255  WHERE ' . $this->db->in('post_id', $post_ids, false, 'integer'));
256 
257  while ($row = $this->db->fetchAssoc($res)) {
258  $draft_ids[] = (int) $row['draft_id'];
259  }
260 
261  $this->deleteHistoryByDraftIds($draft_ids);
262  }
263 
264  return $draft_ids;
265  }
266 
270  public function deleteHistoryByDraftIds(array $draft_ids = []): void
271  {
272  if ($draft_ids !== []) {
273  $res = $this->db->query(
274  'SELECT history_id FROM frm_drafts_history WHERE ' . $this->db->in('draft_id', $draft_ids, false, 'integer')
275  );
276 
277  while ($row = $this->db->fetchAssoc($res)) {
278  $this->setHistoryId((int) $row['history_id']);
279  $this->deleteMobs();
280  }
281 
282  $this->db->manipulate(
283  'DELETE FROM frm_drafts_history WHERE ' . $this->db->in('draft_id', $draft_ids, false, 'integer')
284  );
285  }
286  }
287 }
$res
Definition: ltiservices.php:66
static getInstancesByDraftId(int $draft_id)
static moveMediaObjects(string $post_message, string $source_type, int $source_id, string $target_type, int $target_id, int $direction=0)
deleteHistoryByPostIds(array $post_ids=[])
setDraftDate(string $draft_date)
populateWithFirstAutosaveByDraftId(int $draft_id)
Class ilForumDraftHistory.
setPostSubject(string $post_subject)
global $DIC
Definition: shib_login.php:22
deleteHistoryByDraftIds(array $draft_ids=[])
setPostMessage(string $post_message)
static _exists(int $id, bool $reference=false, ?string $type=null)
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.
readonly ilDBInterface $db
static newInstanceByDraftId(int $draft_id)
static populateWithDatabaseRecord(ilForumDraftsHistory $history_draft, array $row)