19declare(strict_types=1);
42 'd_drafts' =>
'drafts',
43 'e_outbox' =>
'outbox',
55 throw new InvalidArgumentException(
'Cannot create mailbox without user id');
58 $this->
lng = $DIC->language();
59 $this->db =
$DIC->database();
60 $this->table_mail_obj_data =
'mail_obj_data';
61 $this->table_tree =
'mail_tree';
63 $this->mtree =
new ilTree($this->usr_id);
64 $this->mtree->setTableNames($this->table_tree, $this->table_mail_obj_data);
66 $this->
lng->loadLanguageModule(
'mail');
71 $res = $this->db->queryF(
72 'SELECT obj_id FROM ' . $this->table_mail_obj_data .
' WHERE user_id = %s AND m_type = %s',
74 [$this->usr_id,
'root']
77 $row = $this->db->fetchAssoc(
$res);
79 return isset($row[
'obj_id']) ? (
int) $row[
'obj_id'] :
null;
84 $res = $this->db->queryF(
85 'SELECT obj_id FROM ' . $this->table_mail_obj_data .
' WHERE user_id = %s AND m_type = %s',
87 [$this->usr_id,
'inbox']
90 $row = $this->db->fetchAssoc(
$res);
92 return (
int) $row[
'obj_id'];
97 $res = $this->db->queryF(
98 'SELECT obj_id FROM ' . $this->table_mail_obj_data .
' WHERE user_id = %s AND m_type = %s',
100 [$this->usr_id,
'drafts']
103 $row = $this->db->fetchAssoc(
$res);
105 return (
int) $row[
'obj_id'];
110 $res = $this->db->queryF(
111 'SELECT obj_id FROM ' . $this->table_mail_obj_data .
' WHERE user_id = %s AND m_type = %s',
113 [$this->usr_id,
'outbox']
116 $row = $this->db->fetchAssoc(
$res);
121 return (
int) $row[
'obj_id'];
127 if ($root_folder_id ===
null) {
130 $res = $this->db->queryF(
131 'SELECT obj_id FROM ' . $this->table_mail_obj_data .
' WHERE user_id = %s AND m_type = %s',
133 [$this->usr_id,
'outbox']
135 $row = $this->db->fetchAssoc(
$res);
137 return (
int) ($row[
'obj_id'] ?? 0);
140 $last_id = $this->db->nextId($this->table_mail_obj_data);
141 $this->db->manipulateF(
142 'INSERT INTO ' . $this->table_mail_obj_data .
143 ' (obj_id, user_id, title, m_type) VALUES(%s, %s, %s, %s)',
145 [$last_id, $this->usr_id,
'e_outbox',
'outbox']
148 $this->mtree->insertNode($last_id, $root_folder_id, $this->
getDraftsFolder());
155 $res = $this->db->queryF(
156 'SELECT obj_id FROM ' . $this->table_mail_obj_data .
' WHERE user_id = %s AND m_type = %s',
158 [$this->usr_id,
'trash']
161 $row = $this->db->fetchAssoc(
$res);
163 return (
int) $row[
'obj_id'];
168 $res = $this->db->queryF(
169 'SELECT obj_id FROM ' . $this->table_mail_obj_data .
' WHERE user_id = %s AND m_type = %s',
171 [$this->usr_id,
'sent']
174 $row = $this->db->fetchAssoc(
$res);
176 return (
int) $row[
'obj_id'];
184 $root_folder_id = $this->db->nextId($this->table_mail_obj_data);
185 $this->db->manipulateF(
186 'INSERT INTO ' . $this->table_mail_obj_data .
187 ' (obj_id, user_id, title, m_type) VALUES(%s, %s, %s, %s)',
188 [
'integer',
'integer',
'text',
'text'],
189 [$root_folder_id, $this->usr_id,
'a_root',
'root']
191 $this->mtree->addTree($this->usr_id, $root_folder_id);
193 foreach ($this->default_folders as $key => $folder) {
194 $last_id = $this->db->nextId($this->table_mail_obj_data);
195 $this->db->manipulateF(
196 'INSERT INTO ' . $this->table_mail_obj_data .
197 ' (obj_id, user_id, title, m_type) VALUES(%s, %s, %s, %s)',
198 [
'integer',
'integer',
'text',
'text'],
199 [$last_id, $this->usr_id, $key, $folder]
201 $this->mtree->insertNode($last_id, $root_folder_id);
211 $next_id = $this->db->nextId($this->table_mail_obj_data);
212 $this->db->manipulateF(
213 'INSERT INTO ' . $this->table_mail_obj_data .
214 ' (obj_id, user_id, title, m_type) VALUES(%s,%s,%s,%s)',
215 [
'integer',
'integer',
'text',
'text'],
216 [$next_id, $this->usr_id, $name,
'user_folder']
218 $this->mtree->insertNode($next_id, $parent_folder_id);
229 $this->db->manipulateF(
230 'UPDATE ' . $this->table_mail_obj_data .
' SET title = %s WHERE obj_id = %s AND user_id = %s',
231 [
'text',
'integer',
'integer'],
232 [$name, $folder_id, $this->usr_id]
240 $res = $this->db->queryF(
241 'SELECT obj_id FROM ' . $this->table_mail_obj_data .
' WHERE user_id = %s AND title = %s',
243 [$this->usr_id, $name]
245 $row = $this->db->fetchAssoc(
$res);
247 return is_array($row) && $row[
'obj_id'] > 0;
255 $query = $this->db->queryF(
256 'SELECT obj_id, title FROM ' . $this->table_mail_obj_data .
' WHERE obj_id = %s AND user_id = %s',
257 [
'integer',
'integer'],
258 [$folder_id, $this->usr_id]
260 $row = $this->db->fetchAssoc($query);
262 if (!is_array($row) || array_key_exists($row[
'title'], $this->default_folders)) {
266 $mailer =
new ilMail($this->usr_id);
268 $subtree = $this->mtree->getSubTree($this->mtree->getNodeData($folder_id));
269 $this->mtree->deleteTree($this->mtree->getNodeData($folder_id));
271 foreach ($subtree as $node) {
272 $node_id = (
int) $node[
'obj_id'];
274 $mails = $mailer->getMailsOfFolder($node_id);
277 foreach ($mails as $mail) {
278 $mail_ids[] = (
int) $mail[
'mail_id'];
281 $mailer->deleteMails($mail_ids);
283 $this->db->manipulateF(
284 'DELETE FROM ' . $this->table_mail_obj_data .
' WHERE obj_id = %s AND user_id = %s',
285 [
'integer',
'integer'],
286 [$node_id, $this->usr_id]
295 $res = $this->db->queryF(
296 'SELECT * FROM ' . $this->table_mail_obj_data .
' WHERE user_id = %s AND obj_id = %s',
297 [
'integer',
'integer'],
298 [$this->usr_id, $folder_id]
300 $row = $this->db->fetchAssoc(
$res);
302 if (is_array($row)) {
312 (
int) $row[
'obj_id'],
313 (
int) $row[
'user_id'],
317 : $this->
lng->txt(
'mail_' . $row[
'title']))
323 $res = $this->db->queryF(
324 'SELECT * FROM ' . $this->table_tree .
' WHERE child = %s AND tree = %s',
325 [
'integer',
'integer'],
326 [$folder_id, $this->usr_id]
328 $row = $this->db->fetchAssoc(
$res);
330 return is_array($row) ? (
int) $row[
'parent'] : 0;
340 foreach (array_keys($this->default_folders) as $key) {
341 $res = $this->db->queryF(
342 'SELECT * FROM ' . $this->table_mail_obj_data .
' WHERE user_id = %s AND title = %s',
344 [$this->usr_id, $key]
346 $row = $this->db->fetchAssoc(
$res);
347 if (is_array($row)) {
352 $query = implode(
' ', [
353 'SELECT * FROM ' . $this->table_tree .
', ' . $this->table_mail_obj_data,
354 'WHERE ' . $this->table_mail_obj_data .
'.obj_id = ' . $this->table_tree .
'.child',
355 'AND ' . $this->table_tree .
'.depth > %s',
356 'AND ' . $this->table_tree .
'.tree = %s',
357 'ORDER BY ' . $this->table_tree .
'.lft, ' . $this->table_mail_obj_data .
'.title',
359 $res = $this->db->queryF(
361 [
'integer',
'integer'],
364 while ($row = $this->db->fetchAssoc(
$res)) {
368 return $user_folders;
373 $this->usr_id = $usr_id;
378 return $this->usr_id;
381 public function delete():
void
383 $this->db->manipulateF(
384 'DELETE FROM mail_obj_data WHERE user_id = %s',
389 $this->db->manipulateF(
390 'DELETE FROM mail_options WHERE user_id = %s',
395 $this->db->manipulateF(
396 'DELETE FROM mail_saved WHERE user_id = %s',
401 $this->db->manipulateF(
402 'DELETE FROM mail_tree WHERE tree = %s',
407 $this->db->manipulateF(
408 'DELETE FROM mail_auto_responder WHERE sender_id = %s OR receiver_id = %s',
409 [
'integer',
'integer'],
410 [$this->usr_id, $this->usr_id]
416 $fdm->onUserDelete();
419 $this->db->manipulateF(
420 'DELETE FROM mail WHERE user_id = %s',
432 $this->db->manipulateF(
433 'UPDATE mail SET sender_id = %s, import_name = %s WHERE sender_id = %s',
434 [
'integer',
'text',
'integer'],
435 [0, $name_to_show, $this->usr_id]
443 return $folder_data?->getFolderId() === $folder_id;
renameFolder(int $folder_id, string $name)
updateMailsOfDeletedUser(string $name_to_show)
Update existing mails.
deleteFolder(int $folder_id)
isOwnedFolder(int $folder_id)
getFolderData(int $folder_id)
createDefaultFolder()
Creates all default folders for a user.
readonly ilDBInterface $db
microMigrateOutboxFolder()
getParentFolderId(int $folder_id)
getFolderDataFromRow(array $row)
readonly string $table_tree
readonly string $table_mail_obj_data
__construct(protected int $usr_id)
addFolder(int $parent_folder_id, string $name)
folderNameExists(string $name)
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...