ILIAS  release_8 Revision v8.24
class.ilMailbox.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
30{
31 protected ilLanguage $lng;
32 protected ilDBInterface $db;
33 protected ilTree $mtree;
34 protected int $usrId;
36 protected array $actions = [
37 'moveMails' => '',
38 'markMailsRead' => '',
39 'markMailsUnread' => '',
40 'deleteMails' => '',
41 ];
43 protected array $defaultFolders = [
44 'b_inbox' => 'inbox',
45 'c_trash' => 'trash',
46 'd_drafts' => 'drafts',
47 'e_sent' => 'sent',
48 'z_local' => 'local',
49 ];
50 protected string $table_mail_obj_data;
51 protected string $table_tree;
52
53 public function __construct(int $a_user_id)
54 {
55 global $DIC;
56
57 if ($a_user_id < 1) {
58 throw new InvalidArgumentException("Cannot create mailbox without user id");
59 }
60
61 $this->lng = $DIC->language();
62 $this->db = $DIC->database();
63
64 $this->usrId = $a_user_id;
65 $this->table_mail_obj_data = 'mail_obj_data';
66 $this->table_tree = 'mail_tree';
67
68 $this->mtree = new ilTree($this->usrId);
69 $this->mtree->setTableNames($this->table_tree, $this->table_mail_obj_data);
70
71 // i added this, becaus if i create a new user automatically during
72 // CAS authentication, we have no $lng variable (alex, 16.6.2006)
73 // (alternative: make createDefaultFolder call static in ilObjUser->saveAsNew())
74 if (is_object($this->lng)) {
75 $this->lng->loadLanguageModule("mail");
76
77 $this->actions = [
78 'moveMails' => $this->lng->txt('mail_move_to'),
79 'markMailsRead' => $this->lng->txt('mail_mark_read'),
80 'markMailsUnread' => $this->lng->txt('mail_mark_unread'),
81 'deleteMails' => $this->lng->txt('delete'),
82 ];
83 }
84 }
85
86 public function getRooFolder(): int
87 {
88 $res = $this->db->queryF(
89 'SELECT obj_id FROM ' . $this->table_mail_obj_data . ' WHERE user_id = %s AND m_type = %s',
90 ['integer', 'text'],
91 [$this->usrId, 'root']
92 );
93
94 $row = $this->db->fetchAssoc($res);
95
96 return (int) $row['obj_id'];
97 }
98
99 public function getInboxFolder(): int
100 {
101 $res = $this->db->queryF(
102 'SELECT obj_id FROM ' . $this->table_mail_obj_data . ' WHERE user_id = %s AND m_type = %s',
103 ['integer', 'text'],
104 [$this->usrId, 'inbox']
105 );
106
107 $row = $this->db->fetchAssoc($res);
108
109 return (int) $row['obj_id'];
110 }
111
112 public function getDraftsFolder(): int
113 {
114 $res = $this->db->queryF(
115 'SELECT obj_id FROM ' . $this->table_mail_obj_data . ' WHERE user_id = %s AND m_type = %s',
116 ['integer', 'text'],
117 [$this->usrId, 'drafts']
118 );
119
120 $row = $this->db->fetchAssoc($res);
121
122 return (int) $row['obj_id'];
123 }
124
125 public function getTrashFolder(): int
126 {
127 $res = $this->db->queryF(
128 'SELECT obj_id FROM ' . $this->table_mail_obj_data . ' WHERE user_id = %s AND m_type = %s',
129 ['integer', 'text'],
130 [$this->usrId, 'trash']
131 );
132
133 $row = $this->db->fetchAssoc($res);
134
135 return (int) $row['obj_id'];
136 }
137
138 public function getSentFolder(): int
139 {
140 $res = $this->db->queryF(
141 'SELECT obj_id FROM ' . $this->table_mail_obj_data . ' WHERE user_id = %s AND m_type = %s',
142 ['integer', 'text'],
143 [$this->usrId, 'sent']
144 );
145
146 $row = $this->db->fetchAssoc($res);
147
148 return (int) $row['obj_id'];
149 }
150
155 public function getActions(int $folderId): array
156 {
157 return $this->actions;
158 }
159
163 public function createDefaultFolder(): void
164 {
165 $rootFolderId = $this->db->nextId($this->table_mail_obj_data);
166 $this->db->manipulateF(
167 'INSERT INTO ' . $this->table_mail_obj_data .
168 ' (obj_id, user_id, title, m_type) VALUES(%s, %s, %s, %s)',
169 ['integer', 'integer', 'text', 'text'],
170 [$rootFolderId, $this->usrId, 'a_root', 'root']
171 );
172 $this->mtree->addTree($this->usrId, $rootFolderId);
173
174 foreach ($this->defaultFolders as $key => $folder) {
175 $last_id = $this->db->nextId($this->table_mail_obj_data);
176 $this->db->manipulateF(
177 'INSERT INTO ' . $this->table_mail_obj_data .
178 ' (obj_id, user_id, title, m_type) VALUES(%s, %s, %s, %s)',
179 ['integer', 'integer', 'text', 'text'],
180 [$last_id, $this->usrId, $key, $folder]
181 );
182 $this->mtree->insertNode($last_id, $rootFolderId);
183 }
184 }
185
186 public function addFolder(int $parentFolderId, string $name): int
187 {
188 if ($this->folderNameExists($name)) {
189 return 0;
190 }
191
192 $nextId = $this->db->nextId($this->table_mail_obj_data);
193 $this->db->manipulateF(
194 'INSERT INTO ' . $this->table_mail_obj_data .
195 ' (obj_id, user_id, title, m_type) VALUES(%s,%s,%s,%s)',
196 ['integer', 'integer', 'text', 'text'],
197 [$nextId, $this->usrId, $name, 'user_folder']
198 );
199 $this->mtree->insertNode($nextId, $parentFolderId);
200
201 return $nextId;
202 }
203
204 public function renameFolder(int $folderId, string $name): bool
205 {
206 if ($this->folderNameExists($name)) {
207 return false;
208 }
209
210 $this->db->manipulateF(
211 'UPDATE ' . $this->table_mail_obj_data . ' SET title = %s WHERE obj_id = %s AND user_id = %s',
212 ['text', 'integer', 'integer'],
213 [$name, $folderId, $this->usrId]
214 );
215
216 return true;
217 }
218
219 protected function folderNameExists(string $name): bool
220 {
221 $res = $this->db->queryF(
222 'SELECT obj_id FROM ' . $this->table_mail_obj_data . ' WHERE user_id = %s AND title = %s',
223 ['integer', 'text'],
224 [$this->usrId, $name]
225 );
226 $row = $this->db->fetchAssoc($res);
227
228 return is_array($row) && $row['obj_id'] > 0;
229 }
230
234 public function deleteFolder(int $folderId): bool
235 {
236 $query = $this->db->queryF(
237 'SELECT obj_id, title FROM ' . $this->table_mail_obj_data . ' WHERE obj_id = %s AND user_id = %s',
238 ['integer', 'integer'],
239 [$folderId, $this->usrId]
240 );
241 $row = $this->db->fetchAssoc($query);
242
243 if (!is_array($row) || array_key_exists($row['title'], $this->defaultFolders)) {
244 return false;
245 }
246
247 $mailer = new ilMail($this->usrId);
248
249 $subtree = $this->mtree->getSubTree($this->mtree->getNodeData($folderId));
250 $this->mtree->deleteTree($this->mtree->getNodeData($folderId));
251
252 foreach ($subtree as $node) {
253 $nodeId = (int) $node['obj_id'];
254
255 $mails = $mailer->getMailsOfFolder($nodeId);
256
257 $mailIds = [];
258 foreach ($mails as $mail) {
259 $mailIds[] = (int) $mail['mail_id'];
260 }
261
262 $mailer->deleteMails($mailIds);
263
264 $this->db->manipulateF(
265 'DELETE FROM ' . $this->table_mail_obj_data . ' WHERE obj_id = %s AND user_id = %s',
266 ['integer', 'integer'],
267 [$nodeId, $this->usrId]
268 );
269 }
270
271 return true;
272 }
273
277 public function getFolderData(int $folderId): ?array
278 {
279 $res = $this->db->queryF(
280 'SELECT * FROM ' . $this->table_mail_obj_data . ' WHERE user_id = %s AND obj_id = %s',
281 ['integer', 'integer'],
282 [$this->usrId, $folderId]
283 );
284 $row = $this->db->fetchAssoc($res);
285
286 if (is_array($row)) {
287 return [
288 'obj_id' => (int) $row['obj_id'],
289 'title' => (string) $row['title'],
290 'type' => (string) $row['m_type'],
291 ];
292 }
293
294 return null;
295 }
296
297 public function getParentFolderId(int $folderId): int
298 {
299 $res = $this->db->queryF(
300 'SELECT * FROM ' . $this->table_tree . ' WHERE child = %s AND tree = %s',
301 ['integer', 'integer'],
302 [$folderId, $this->usrId]
303 );
304 $row = $this->db->fetchAssoc($res);
305
306 return is_array($row) ? (int) $row['parent'] : 0;
307 }
308
309 public function getSubFolders(): array
310 {
311 $userFolders = [];
312
313 foreach ($this->defaultFolders as $key => $value) {
314 $res = $this->db->queryF(
315 'SELECT obj_id, m_type FROM ' . $this->table_mail_obj_data . ' WHERE user_id = %s AND title = %s',
316 ['integer', 'text'],
317 [$this->usrId, $key]
318 );
319 $row = $this->db->fetchAssoc($res);
320
321 $userFolders[] = [
322 'title' => $key,
323 'type' => (string) $row['m_type'],
324 'obj_id' => (int) $row['obj_id'],
325 ];
326 }
327
328 $query = implode(' ', [
329 'SELECT * FROM ' . $this->table_tree . ', ' . $this->table_mail_obj_data,
330 'WHERE ' . $this->table_mail_obj_data . '.obj_id = ' . $this->table_tree . '.child',
331 'AND ' . $this->table_tree . '.depth > %s',
332 'AND ' . $this->table_tree . '.tree = %s',
333 'ORDER BY ' . $this->table_tree . '.lft, ' . $this->table_mail_obj_data . '.title',
334 ]);
335 $res = $this->db->queryF(
336 $query,
337 ['integer', 'integer'],
338 [2, $this->usrId]
339 );
340 while ($row = $this->db->fetchAssoc($res)) {
341 $userFolders[] = [
342 'title' => (string) $row['title'],
343 'type' => (string) $row['m_type'],
344 'obj_id' => (int) $row['child'],
345 ];
346 }
347
348 return $userFolders;
349 }
350
351 public function setUsrId(int $usrId): void
352 {
353 $this->usrId = $usrId;
354 }
355
356 public function getUsrId(): int
357 {
358 return $this->usrId;
359 }
360
361 public function delete(): void
362 {
363 $this->db->manipulateF(
364 'DELETE FROM mail_obj_data WHERE user_id = %s',
365 ['integer'],
366 [$this->usrId]
367 );
368
369 $this->db->manipulateF(
370 'DELETE FROM mail_options WHERE user_id = %s',
371 ['integer'],
372 [$this->usrId]
373 );
374
375 $this->db->manipulateF(
376 'DELETE FROM mail_saved WHERE user_id = %s',
377 ['integer'],
378 [$this->usrId]
379 );
380
381 $this->db->manipulateF(
382 'DELETE FROM mail_tree WHERE tree = %s',
383 ['integer'],
384 [$this->usrId]
385 );
386
387 // Delete the user's files from filesystem:
388 // This has to be done before deleting the database entries in table 'mail'
389 $fdm = new ilFileDataMail($this->usrId);
390 $fdm->onUserDelete();
391
392 // Delete mails of deleted user
393 $this->db->manipulateF(
394 'DELETE FROM mail WHERE user_id = %s',
395 ['integer'],
396 [$this->usrId]
397 );
398 }
399
404 public function updateMailsOfDeletedUser(string $nameToShow): void
405 {
406 $this->db->manipulateF(
407 'UPDATE mail SET sender_id = %s, import_name = %s WHERE sender_id = %s',
408 ['integer', 'text', 'integer'],
409 [0, $nameToShow, $this->usrId]
410 );
411 }
412
413 public function isOwnedFolder(int $folderId): bool
414 {
415 $folderData = $this->getFolderData($folderId);
416
417 return $folderData !== null && (int) $folderData['obj_id'] === $folderId;
418 }
419}
This class handles all operations on files (attachments) in directory ilias_data/mail.
language handling
Mail Box class Base class for creating and handling mail boxes.
ilDBInterface $db
getActions(int $folderId)
isOwnedFolder(int $folderId)
deleteFolder(int $folderId)
__construct(int $a_user_id)
addFolder(int $parentFolderId, string $name)
createDefaultFolder()
Creates all default folders for a user.
array $defaultFolders
string $table_tree
ilLanguage $lng
updateMailsOfDeletedUser(string $nameToShow)
Update existing mails.
getFolderData(int $folderId)
renameFolder(int $folderId, string $name)
setUsrId(int $usrId)
string $table_mail_obj_data
folderNameExists(string $name)
getParentFolderId(int $folderId)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
if($format !==null) $name
Definition: metadata.php:247
string $key
Consumer key/client ID value.
Definition: System.php:193
$query