ILIAS  trunk Revision v11.0_alpha-1862-g4e205cb56d4
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ILIAS\Mail\Message\MailBoxQuery Class Reference

Database query for mails of a user. More...

+ Collaboration diagram for ILIAS\Mail\Message\MailBoxQuery:

Public Member Functions

 __construct (private readonly int $user_id,)
 
 withFolderId (?int $folder_id)
 
 withSender (?string $sender)
 
 withRecipients (?string $recipients)
 
 withSubject (?string $subject)
 
 withBody (?string $body)
 
 withIsUnread (?bool $is_unread)
 
 withIsSystem (?bool $is_system)
 
 withHasAttachment (?bool $has_attachment)
 
 withPeriodStart (?DateTimeImmutable $period_start)
 
 withPeriodEnd (?DateTimeImmutable $period_end)
 
 withFilteredIds (?array $filtered_ids)
 
 withLimit (int $limit)
 
 withOffset (int $offset)
 
 withOrderColumn (?MailBoxOrderColumn $order_column)
 
 withOrderDirection (?string $order_direction)
 
 countUnread ()
 Count the number of unread mails with applied filter. More...
 
 count ()
 Count the number of all mails with applied filter. More...
 
 queryMailIds ()
 Get a list of mail ids. More...
 
 query ($short)
 Query for mail data with applied filter. More...
 

Private Member Functions

 getFrom ()
 
 getWhere ()
 

Private Attributes

const DEFAULT_ORDER_COLUMN = MailBoxOrderColumn::SEND_TIME
 
const DEFAULT_ORDER_DIRECTION = Order::ASC
 
ilDBInterface $db
 
int $folder_id = null
 
string $sender = null
 
string $recipients = null
 
string $subject = null
 
string $body = null
 
bool $is_unread = null
 
bool $is_system = null
 
bool $has_attachment = null
 
DateTimeImmutable $period_start = null
 
DateTimeImmutable $period_end = null
 
array $filtered_ids = null
 
int $limit = 999999
 
int $offset = 0
 
MailBoxOrderColumn $order_column = self::DEFAULT_ORDER_COLUMN
 
string $order_direction = self::DEFAULT_ORDER_DIRECTION
 

Detailed Description

Database query for mails of a user.

Definition at line 33 of file MailBoxQuery.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\Mail\Message\MailBoxQuery::__construct ( private readonly int  $user_id)

Definition at line 56 of file MailBoxQuery.php.

References $DIC.

58  {
59  global $DIC;
60  $this->db = $DIC->database();
61  }
global $DIC
Definition: shib_login.php:22

Member Function Documentation

◆ count()

ILIAS\Mail\Message\MailBoxQuery::count ( )

Count the number of all mails with applied filter.

Definition at line 205 of file MailBoxQuery.php.

References $res, ILIAS\Mail\Message\MailBoxQuery\getFrom(), and ILIAS\Mail\Message\MailBoxQuery\getWhere().

205  : int
206  {
207  if ($this->filtered_ids === []) {
208  return 0;
209  }
210 
211  $query = 'SELECT COUNT(m.mail_id) cnt '
212  . $this->getFrom()
213  . $this->getWhere();
214 
215  $res = $this->db->query($query);
216  if ($row = $this->db->fetchAssoc($res)) {
217  return (int) $row['cnt'];
218  }
219 
220  return 0;
221  }
$res
Definition: ltiservices.php:66
+ Here is the call graph for this function:

◆ countUnread()

ILIAS\Mail\Message\MailBoxQuery::countUnread ( )

Count the number of unread mails with applied filter.

Definition at line 197 of file MailBoxQuery.php.

References ILIAS\Mail\Message\MailBoxQuery\withIsUnread().

197  : int
198  {
199  return $this->withIsUnread(true)->count();
200  }
+ Here is the call graph for this function:

◆ getFrom()

ILIAS\Mail\Message\MailBoxQuery::getFrom ( )
private

Definition at line 309 of file MailBoxQuery.php.

Referenced by ILIAS\Mail\Message\MailBoxQuery\count(), ILIAS\Mail\Message\MailBoxQuery\query(), and ILIAS\Mail\Message\MailBoxQuery\queryMailIds().

310  {
311  return " FROM mail m
312  LEFT JOIN usr_data u ON u.usr_id = m.sender_id
313  LEFT JOIN usr_pref p ON p.usr_id = m.sender_id AND p.keyword = 'public_profile'";
314  }
+ Here is the caller graph for this function:

◆ getWhere()

ILIAS\Mail\Message\MailBoxQuery::getWhere ( )
private

Definition at line 316 of file MailBoxQuery.php.

References ILIAS\Mail\Message\MailBoxQuery\$body, $parts, ILIAS\Mail\Message\MailBoxQuery\$recipients, ILIAS\Mail\Message\MailBoxQuery\$subject, ilUserSearchOptions\_isEnabled(), ANONYMOUS_USER_ID, null, ilDBConstants\T_INTEGER, ilDBConstants\T_TEXT, and ilDBConstants\T_TIMESTAMP.

Referenced by ILIAS\Mail\Message\MailBoxQuery\count(), ILIAS\Mail\Message\MailBoxQuery\query(), and ILIAS\Mail\Message\MailBoxQuery\queryMailIds().

316  : string
317  {
318  $parts = [];
319 
320  // minimum condition: only mailbox of the given user
321  $parts[] = 'm.user_id = ' . $this->db->quote($this->user_id, ilDBConstants::T_INTEGER);
322 
323  // sender conditions have to respect searchability and visibility of profile fields
324  $sender_conditions = [];
325  if (($this->sender ?? '') !== '') {
326  $sender_conditions[] = $this->db->like('u.login', ilDBConstants::T_TEXT, '%%' . $this->sender . '%%');
327 
328  if (ilUserSearchOptions::_isEnabled('firstname')) {
329  $sender_conditions[] = '(' .
330  $this->db->like('u.firstname', ilDBConstants::T_TEXT, '%%' . $this->sender . '%%')
331  . " AND p.value = 'y')";
332  }
333 
334  if (ilUserSearchOptions::_isEnabled('lastname')) {
335  $sender_conditions[] = '(' .
336  $this->db->like('u.lastname', ilDBConstants::T_TEXT, '%%' . $this->sender . '%%')
337  . " AND p.value = 'y')";
338  }
339  }
340  if (!empty($sender_conditions)) {
341  $parts[] = '(' . implode(' OR ', $sender_conditions) . ')';
342  }
343 
344  // other text conditions
345  $text_conditions = [
346  [$this->recipients, 'CONCAT(CONCAT(m.rcp_to, m.rcp_cc), m.rcp_bcc)'],
347  [$this->subject, 'm.m_subject'],
348  [$this->body, 'm.m_message'],
349  ];
350 
351  foreach ($text_conditions as $cond) {
352  if (($cond[0] ?? '') !== '') {
353  $parts[] = $this->db->like(
354  $cond[1],
356  '%%' . $cond[0] . '%%',
357  false
358  );
359  }
360  }
361 
362  if ($this->folder_id !== null) {
363  $parts[] = 'm.folder_id = ' . $this->db->quote($this->folder_id, ilDBConstants::T_INTEGER);
364  }
365 
366  if ($this->is_unread === true) {
367  $parts[] = 'm.m_status = ' . $this->db->quote('unread', 'text');
368  } elseif ($this->is_unread === false) {
369  $parts[] = 'm.m_status != ' . $this->db->quote('unread', 'text');
370  }
371 
372  if ($this->is_system === true) {
373  $parts[] = 'm.sender_id = ' . $this->db->quote(ANONYMOUS_USER_ID, ilDBConstants::T_INTEGER);
374  } elseif ($this->is_system === false) {
375  $parts[] = 'm.sender_id != ' . $this->db->quote(ANONYMOUS_USER_ID, ilDBConstants::T_INTEGER);
376  }
377 
378  if ($this->has_attachment === true) {
379  $parts[] = '(m.attachments != ' . $this->db->quote(serialize(null), ilDBConstants::T_TEXT)
380  . ' AND m.attachments != ' . $this->db->quote(serialize([]), ilDBConstants::T_TEXT) . ')';
381  } elseif ($this->has_attachment === false) {
382  $parts[] = '(m.attachments = ' . $this->db->quote(serialize(null), ilDBConstants::T_TEXT)
383  . ' OR m.attachments = ' . $this->db->quote(serialize([]), ilDBConstants::T_TEXT) . ')';
384  }
385 
386  if ($this->period_start !== null) {
387  $parts[] = 'm.send_time >= ' . $this->db->quote(
388  // convert to server time zone (set by ilias initialisation)
389  $this->period_start
390  ->setTimezone(new DateTimeZone(date_default_timezone_get()))
391  ->format('Y-m-d H:i:s'),
393  );
394  }
395 
396  if ($this->period_end !== null) {
397  $parts[] = 'm.send_time <= ' . $this->db->quote(
398  // convert to server time zone (set by ilias initialisation)
399  $this->period_end
400  ->setTimezone(new DateTimeZone(date_default_timezone_get()))
401  ->format('Y-m-d H:i:s'),
403  );
404  }
405 
406  if (!empty($this->filtered_ids)) {
407  $parts[] = $this->db->in(
408  'm.mail_id',
409  $this->filtered_ids,
410  false,
412  ) . ' ';
413  }
414 
415  if ($parts !== []) {
416  return ' WHERE ' . implode(' AND ', $parts);
417  }
418 
419  return '';
420  }
const ANONYMOUS_USER_ID
Definition: constants.php:27
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ query()

ILIAS\Mail\Message\MailBoxQuery::query (   $short)

Query for mail data with applied filter.

Parameters
bool$shortget only data that is needed for a listing
Returns
MailRecordData[]

Definition at line 251 of file MailBoxQuery.php.

References ILIAS\Mail\Message\MailBoxQuery\$order_direction, $res, ILIAS\Mail\Message\MailBoxQuery\getFrom(), ILIAS\Mail\Message\MailBoxQuery\getWhere(), and null.

251  : array
252  {
253  if ($this->filtered_ids === []) {
254  return [];
255  }
256 
257  if ($short) {
258  $query = 'SELECT m.mail_id, m.user_id, m.folder_id, m.sender_id, m.send_time, '
259  . 'm.m_status, m.m_subject, m.import_name, m.rcp_to, m.attachments'
260  . $this->getFrom()
261  . $this->getWhere();
262  } else {
263  $query = 'SELECT m.*'
264  . $this->getFrom()
265  . $this->getWhere();
266  }
267 
268  if ($this->order_column === MailBoxOrderColumn::FROM) {
269  $query .= ' ORDER BY '
270  . ' u.firstname ' . $this->order_direction . ', '
271  . ' u.lastname ' . $this->order_direction . ', '
272  . ' u.login ' . $this->order_direction . ', '
273  . ' m.import_name ' . $this->order_direction;
274  } else {
275  $query .= ' ORDER BY ' . $this->order_column->value . ' ' . $this->order_direction;
276  }
277 
278  $this->db->setLimit($this->limit, $this->offset);
279  $res = $this->db->query($query);
280 
281  $set = [];
282  while ($row = $this->db->fetchAssoc($res)) {
283  $set[] = new MailRecordData(
284  isset($row['mail_id']) ? (int) $row['mail_id'] : 0,
285  isset($row['user_id']) ? (int) $row['user_id'] : 0,
286  isset($row['folder_id']) ? (int) $row['folder_id'] : 0,
287  isset($row['sender_id']) ? (int) $row['sender_id'] : null,
288  isset($row['send_time']) ? new DateTimeImmutable($row['send_time']) : null,
289  isset($row['m_status']) ? (string) $row['m_status'] : null,
290  isset($row['m_subject']) ? (string) $row['m_subject'] : null,
291  isset($row['import_name']) ? (string) $row['import_name'] : null,
292  isset($row['use_placeholders']) ? (bool) $row['use_placeholders'] : false,
293  isset($row['m_message']) ? (string) $row['m_message'] : null,
294  isset($row['rcp_to']) ? (string) $row['rcp_to'] : null,
295  isset($row['rcp_cc']) ? (string) $row['rcp_cc'] : null,
296  isset($row['rcp_bcc']) ? (string) $row['rcp_bcc'] : null,
297  isset($row['attachments']) ? (array) unserialize(
298  stripslashes($row['attachments']),
299  ['allowed_classes' => false]
300  ) : [],
301  isset($row['tpl_ctx_id']) ? (string) $row['tpl_ctx_id'] : null,
302  isset($row['tpl_ctx_params']) ? (string) $row['tpl_ctx_params'] : null
303  );
304  }
305 
306  return $set;
307  }
$res
Definition: ltiservices.php:66
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
+ Here is the call graph for this function:

◆ queryMailIds()

ILIAS\Mail\Message\MailBoxQuery::queryMailIds ( )

Get a list of mail ids.

Returns
int[]

Definition at line 227 of file MailBoxQuery.php.

References $res, ILIAS\Mail\Message\MailBoxQuery\getFrom(), ILIAS\Mail\Message\MailBoxQuery\getWhere(), and ILIAS\Repository\int().

227  : array
228  {
229  if ($this->filtered_ids === []) {
230  return [];
231  }
232 
233  $query = 'SELECT m.mail_id '
234  . $this->getFrom()
235  . $this->getWhere();
236 
237  $ids = [];
238  $res = $this->db->query($query);
239  while ($row = $this->db->fetchAssoc($res)) {
240  $ids[] = (int) $row['mail_id'];
241  }
242 
243  return $ids;
244  }
$res
Definition: ltiservices.php:66
+ Here is the call graph for this function:

◆ withBody()

ILIAS\Mail\Message\MailBoxQuery::withBody ( ?string  $body)

Definition at line 95 of file MailBoxQuery.php.

References ILIAS\Mail\Message\MailBoxQuery\$body.

95  : MailBoxQuery
96  {
97  $clone = clone $this;
98  $clone->body = $body;
99 
100  return $clone;
101  }

◆ withFilteredIds()

ILIAS\Mail\Message\MailBoxQuery::withFilteredIds ( ?array  $filtered_ids)
Parameters
int[]|null$filtered_ids

Definition at line 146 of file MailBoxQuery.php.

References ILIAS\Mail\Message\MailBoxQuery\$filtered_ids.

146  : MailBoxQuery
147  {
148  $clone = clone $this;
149  $clone->filtered_ids = $filtered_ids;
150 
151  return $clone;
152  }

◆ withFolderId()

ILIAS\Mail\Message\MailBoxQuery::withFolderId ( ?int  $folder_id)

Definition at line 63 of file MailBoxQuery.php.

References ILIAS\Mail\Message\MailBoxQuery\$folder_id.

63  : MailBoxQuery
64  {
65  $clone = clone $this;
66  $clone->folder_id = $folder_id;
67 
68  return $clone;
69  }

◆ withHasAttachment()

ILIAS\Mail\Message\MailBoxQuery::withHasAttachment ( ?bool  $has_attachment)

Definition at line 119 of file MailBoxQuery.php.

References ILIAS\Mail\Message\MailBoxQuery\$has_attachment.

119  : MailBoxQuery
120  {
121  $clone = clone $this;
122  $clone->has_attachment = $has_attachment;
123 
124  return $clone;
125  }

◆ withIsSystem()

ILIAS\Mail\Message\MailBoxQuery::withIsSystem ( ?bool  $is_system)

Definition at line 111 of file MailBoxQuery.php.

References ILIAS\Mail\Message\MailBoxQuery\$is_system.

111  : MailBoxQuery
112  {
113  $clone = clone $this;
114  $clone->is_system = $is_system;
115 
116  return $clone;
117  }

◆ withIsUnread()

ILIAS\Mail\Message\MailBoxQuery::withIsUnread ( ?bool  $is_unread)

Definition at line 103 of file MailBoxQuery.php.

References ILIAS\Mail\Message\MailBoxQuery\$is_unread.

Referenced by ILIAS\Mail\Message\MailBoxQuery\countUnread().

103  : MailBoxQuery
104  {
105  $clone = clone $this;
106  $clone->is_unread = $is_unread;
107 
108  return $clone;
109  }
+ Here is the caller graph for this function:

◆ withLimit()

ILIAS\Mail\Message\MailBoxQuery::withLimit ( int  $limit)

Definition at line 154 of file MailBoxQuery.php.

References ILIAS\Mail\Message\MailBoxQuery\$limit.

154  : MailBoxQuery
155  {
156  $clone = clone $this;
157  $clone->limit = $limit;
158 
159  return $clone;
160  }

◆ withOffset()

ILIAS\Mail\Message\MailBoxQuery::withOffset ( int  $offset)

Definition at line 162 of file MailBoxQuery.php.

References ILIAS\Mail\Message\MailBoxQuery\$offset.

162  : MailBoxQuery
163  {
164  $clone = clone $this;
165  $clone->offset = $offset;
166 
167  return $clone;
168  }

◆ withOrderColumn()

ILIAS\Mail\Message\MailBoxQuery::withOrderColumn ( ?MailBoxOrderColumn  $order_column)

Definition at line 170 of file MailBoxQuery.php.

References ILIAS\Mail\Message\MailBoxQuery\$order_column, and null.

170  : MailBoxQuery
171  {
172  $clone = clone $this;
173  if ($order_column !== null) {
174  $clone->order_column = $order_column;
175  } else {
176  $clone->order_column = self::DEFAULT_ORDER_COLUMN;
177  }
178 
179  return $clone;
180  }
MailBoxOrderColumn $order_column
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null

◆ withOrderDirection()

ILIAS\Mail\Message\MailBoxQuery::withOrderDirection ( ?string  $order_direction)

Definition at line 182 of file MailBoxQuery.php.

References ILIAS\Mail\Message\MailBoxQuery\$order_direction, ILIAS\Data\Order\ASC, and ILIAS\Data\Order\DESC.

182  : MailBoxQuery
183  {
184  $clone = clone $this;
185  if (in_array($order_direction, [Order::ASC, Order::DESC])) {
186  $clone->order_direction = $order_direction;
187  } else {
188  $clone->order_direction = self::DEFAULT_ORDER_DIRECTION;
189  }
190 
191  return $clone;
192  }
const DESC
Definition: Order.php:31

◆ withPeriodEnd()

ILIAS\Mail\Message\MailBoxQuery::withPeriodEnd ( ?DateTimeImmutable  $period_end)

Definition at line 135 of file MailBoxQuery.php.

References ILIAS\Mail\Message\MailBoxQuery\$period_end.

135  : MailBoxQuery
136  {
137  $clone = clone $this;
138  $clone->period_end = $period_end;
139 
140  return $clone;
141  }

◆ withPeriodStart()

ILIAS\Mail\Message\MailBoxQuery::withPeriodStart ( ?DateTimeImmutable  $period_start)

Definition at line 127 of file MailBoxQuery.php.

References ILIAS\Mail\Message\MailBoxQuery\$period_start.

127  : MailBoxQuery
128  {
129  $clone = clone $this;
130  $clone->period_start = $period_start;
131 
132  return $clone;
133  }
DateTimeImmutable $period_start

◆ withRecipients()

ILIAS\Mail\Message\MailBoxQuery::withRecipients ( ?string  $recipients)

Definition at line 79 of file MailBoxQuery.php.

References ILIAS\Mail\Message\MailBoxQuery\$recipients.

79  : MailBoxQuery
80  {
81  $clone = clone $this;
82  $clone->recipients = $recipients;
83 
84  return $clone;
85  }

◆ withSender()

ILIAS\Mail\Message\MailBoxQuery::withSender ( ?string  $sender)

Definition at line 71 of file MailBoxQuery.php.

References ILIAS\Mail\Message\MailBoxQuery\$sender.

71  : MailBoxQuery
72  {
73  $clone = clone $this;
74  $clone->sender = $sender;
75 
76  return $clone;
77  }

◆ withSubject()

ILIAS\Mail\Message\MailBoxQuery::withSubject ( ?string  $subject)

Definition at line 87 of file MailBoxQuery.php.

References ILIAS\Mail\Message\MailBoxQuery\$subject.

87  : MailBoxQuery
88  {
89  $clone = clone $this;
90  $clone->subject = $subject;
91 
92  return $clone;
93  }

Field Documentation

◆ $body

string ILIAS\Mail\Message\MailBoxQuery::$body = null
private

◆ $db

ilDBInterface ILIAS\Mail\Message\MailBoxQuery::$db
private

Definition at line 38 of file MailBoxQuery.php.

◆ $filtered_ids

array ILIAS\Mail\Message\MailBoxQuery::$filtered_ids = null
private

Definition at line 50 of file MailBoxQuery.php.

Referenced by ILIAS\Mail\Message\MailBoxQuery\withFilteredIds().

◆ $folder_id

int ILIAS\Mail\Message\MailBoxQuery::$folder_id = null
private

Definition at line 39 of file MailBoxQuery.php.

Referenced by ILIAS\Mail\Message\MailBoxQuery\withFolderId().

◆ $has_attachment

bool ILIAS\Mail\Message\MailBoxQuery::$has_attachment = null
private

Definition at line 46 of file MailBoxQuery.php.

Referenced by ILIAS\Mail\Message\MailBoxQuery\withHasAttachment().

◆ $is_system

bool ILIAS\Mail\Message\MailBoxQuery::$is_system = null
private

Definition at line 45 of file MailBoxQuery.php.

Referenced by ILIAS\Mail\Message\MailBoxQuery\withIsSystem().

◆ $is_unread

bool ILIAS\Mail\Message\MailBoxQuery::$is_unread = null
private

Definition at line 44 of file MailBoxQuery.php.

Referenced by ILIAS\Mail\Message\MailBoxQuery\withIsUnread().

◆ $limit

int ILIAS\Mail\Message\MailBoxQuery::$limit = 999999
private

Definition at line 51 of file MailBoxQuery.php.

Referenced by ILIAS\Mail\Message\MailBoxQuery\withLimit().

◆ $offset

int ILIAS\Mail\Message\MailBoxQuery::$offset = 0
private

Definition at line 52 of file MailBoxQuery.php.

Referenced by ILIAS\Mail\Message\MailBoxQuery\withOffset().

◆ $order_column

MailBoxOrderColumn ILIAS\Mail\Message\MailBoxQuery::$order_column = self::DEFAULT_ORDER_COLUMN
private

Definition at line 53 of file MailBoxQuery.php.

Referenced by ILIAS\Mail\Message\MailBoxQuery\withOrderColumn().

◆ $order_direction

string ILIAS\Mail\Message\MailBoxQuery::$order_direction = self::DEFAULT_ORDER_DIRECTION
private

◆ $period_end

DateTimeImmutable ILIAS\Mail\Message\MailBoxQuery::$period_end = null
private

Definition at line 48 of file MailBoxQuery.php.

Referenced by ILIAS\Mail\Message\MailBoxQuery\withPeriodEnd().

◆ $period_start

DateTimeImmutable ILIAS\Mail\Message\MailBoxQuery::$period_start = null
private

Definition at line 47 of file MailBoxQuery.php.

Referenced by ILIAS\Mail\Message\MailBoxQuery\withPeriodStart().

◆ $recipients

string ILIAS\Mail\Message\MailBoxQuery::$recipients = null
private

◆ $sender

string ILIAS\Mail\Message\MailBoxQuery::$sender = null
private

Definition at line 40 of file MailBoxQuery.php.

Referenced by ILIAS\Mail\Message\MailBoxQuery\withSender().

◆ $subject

string ILIAS\Mail\Message\MailBoxQuery::$subject = null
private

◆ DEFAULT_ORDER_COLUMN

const ILIAS\Mail\Message\MailBoxQuery::DEFAULT_ORDER_COLUMN = MailBoxOrderColumn::SEND_TIME
private

Definition at line 35 of file MailBoxQuery.php.

◆ DEFAULT_ORDER_DIRECTION

const ILIAS\Mail\Message\MailBoxQuery::DEFAULT_ORDER_DIRECTION = Order::ASC
private

Definition at line 36 of file MailBoxQuery.php.


The documentation for this class was generated from the following file: