ILIAS  release_8 Revision v8.25-1-g13de6a5eca6
ilMailBoxQuery Class Reference

Mail query class. More...

+ Collaboration diagram for ilMailBoxQuery:

Static Public Member Functions

static _getMailBoxListData ()
 

Static Public Attributes

static int $folderId = -1
 
static int $userId = -1
 
static int $limit = 0
 
static int $offset = 0
 
static string $orderDirection = ''
 
static string $orderColumn = ''
 
static array $filter = []
 
static array $filtered_ids = []
 

Detailed Description

Mail query class.

Author
Michael Jansen mjans.nosp@m.en@d.nosp@m.ataba.nosp@m.y.de

Definition at line 28 of file class.ilMailBoxQuery.php.

Member Function Documentation

◆ _getMailBoxListData()

static ilMailBoxQuery::_getMailBoxListData ( )
static
Returns
array{set: array[], cnt: int, cnt_unread: int}
Exceptions
Exception

Definition at line 43 of file class.ilMailBoxQuery.php.

43 : array
44 {
45 global $DIC;
46
47 $mails = ['cnt' => 0, 'cnt_unread' => 0, 'set' => []];
48
49 $filter = [
50 'mail_filter_sender' => 'CONCAT(CONCAT(firstname, lastname), login)',
51 'mail_filter_recipients' => 'CONCAT(CONCAT(rcp_to, rcp_cc), rcp_bcc)',
52 'mail_filter_subject' => 'm_subject',
53 'mail_filter_body' => 'm_message',
54 'mail_filter_attach' => '',
55 ];
56
57 $filter_parts = [];
58 if (
59 isset(self::$filter['mail_filter']) &&
60 is_string(self::$filter['mail_filter']) &&
61 self::$filter['mail_filter'] !== ''
62 ) {
63 foreach ($filter as $key => $column) {
64 if ($column !== '' && isset(self::$filter[$key]) && (int) self::$filter[$key]) {
65 $filter_parts[] = $DIC->database()->like(
66 $column,
67 'text',
68 '%%' . self::$filter['mail_filter'] . '%%',
69 false
70 );
71 }
72 }
73 }
74
75 $filter_qry = '';
76 if ($filter_parts) {
77 $filter_qry = 'AND (' . implode(' OR ', $filter_parts) . ')';
78 }
79
80 if (isset(self::$filter['mail_filter_only_unread']) && self::$filter['mail_filter_only_unread']) {
81 $filter_qry .= ' AND m_status = ' . $DIC->database()->quote('unread', 'text') . ' ';
82 }
83
84 if (
85 isset(self::$filter['mail_filter_only_with_attachments']) &&
86 self::$filter['mail_filter_only_with_attachments']
87 ) {
88 $filter_qry .= ' AND attachments != ' . $DIC->database()->quote(serialize(null), 'text') . ' ';
89 $filter_qry .= ' AND attachments != ' . $DIC->database()->quote(serialize([]), 'text') . ' ';
90 }
91
92 if (isset(self::$filter['mail_filter_only_user_mails']) && self::$filter['mail_filter_only_user_mails']) {
93 $filter_qry .= ' AND sender_id != ' . $DIC->database()->quote(ANONYMOUS_USER_ID, ilDBConstants::T_INTEGER) . ' ';
94 }
95
96 if (isset(self::$filter['period']) && is_array(self::$filter['period'])) {
97 $dateFilterParts = [];
98
99 if (null !== self::$filter['period']['start']) {
100 $dateFilterParts[] = 'send_time >= ' . $DIC->database()->quote(
101 (new DateTimeImmutable(
102 '@' . self::$filter['period']['start']
103 ))->format('Y-m-d 00:00:00'),
104 'timestamp'
105 );
106 }
107
108 if (null !== self::$filter['period']['end']) {
109 $dateFilterParts[] = 'send_time <= ' . $DIC->database()->quote(
110 (new DateTimeImmutable(
111 '@' . self::$filter['period']['end']
112 ))->format('Y-m-d 23:59:59'),
113 'timestamp'
114 );
115 }
116
117 if (count($dateFilterParts) > 0) {
118 $filter_qry .= ' AND (' . implode(' AND ', $dateFilterParts) . ') ';
119 }
120 }
121
122 $queryCount = 'SELECT COUNT(mail_id) cnt FROM mail '
123 . 'LEFT JOIN usr_data ON usr_id = sender_id '
124 . 'WHERE user_id = %s '
125 . 'AND ((sender_id > 0 AND sender_id IS NOT NULL '
126 . 'AND usr_id IS NOT NULL) OR (sender_id = 0 OR sender_id IS NULL)) '
127 . 'AND folder_id = %s '
128 . $filter_qry;
129
130 if (self::$filtered_ids) {
131 $queryCount .= ' AND ' . $DIC->database()->in(
132 'mail_id',
133 self::$filtered_ids,
134 false,
135 'integer'
136 ) . ' ';
137 }
138
139 $queryCount .= ' UNION ALL '
140 . 'SELECT COUNT(mail_id) cnt FROM mail '
141 . 'LEFT JOIN usr_data ON usr_id = sender_id '
142 . 'WHERE user_id = %s '
143 . 'AND ((sender_id > 0 AND sender_id IS NOT NULL '
144 . 'AND usr_id IS NOT NULL) OR (sender_id = 0 OR sender_id IS NULL)) '
145 . 'AND folder_id = %s '
146 . $filter_qry . ' '
147 . 'AND m_status = %s';
148
149 if (self::$filtered_ids) {
150 $queryCount .= ' AND ' . $DIC->database()->in(
151 'mail_id',
152 self::$filtered_ids,
153 false,
154 'integer'
155 ) . ' ';
156 }
157
158 $res = $DIC->database()->queryF(
159 $queryCount,
160 ['integer', 'integer', 'integer', 'integer', 'text'],
161 [self::$userId, self::$folderId, self::$userId, self::$folderId, 'unread']
162 );
163
164 $counter = 0;
165 while ($cnt_row = $DIC->database()->fetchAssoc($res)) {
166 if ($counter === 0) {
167 $mails['cnt'] = (int) $cnt_row['cnt'];
168 } elseif ($counter === 1) {
169 $mails['cnt_unread'] = (int) $cnt_row['cnt'];
170 } else {
171 break;
172 }
173
174 ++$counter;
175 }
176
177 $sortColumn = '';
178 $firstnameSelection = '';
179 if (self::$orderColumn === 'from') {
180 // Because of the user id of automatically generated mails and ordering issues we have to do some magic
181 $firstnameSelection = '
182 ,(CASE
183 WHEN (usr_id = ' . ANONYMOUS_USER_ID . ') THEN firstname
184 ELSE ' . $DIC->database()->quote(ilMail::_getIliasMailerName(), 'text') . '
185 END) fname
186 ';
187 }
188
189 $query = 'SELECT mail.*' . $sortColumn . ' ' . $firstnameSelection . ' FROM mail '
190 . 'LEFT JOIN usr_data ON usr_id = sender_id '
191 . 'AND ((sender_id > 0 AND sender_id IS NOT NULL '
192 . 'AND usr_id IS NOT NULL) OR (sender_id = 0 OR sender_id IS NULL)) '
193 . 'WHERE user_id = %s '
194 . $filter_qry . ' '
195 . 'AND folder_id = %s';
196
197 if (self::$filtered_ids) {
198 $query .= ' AND ' . $DIC->database()->in(
199 'mail_id',
200 self::$filtered_ids,
201 false,
202 'integer'
203 ) . ' ';
204 }
205
206 $orderDirection = 'ASC';
207 if (in_array(strtolower(self::$orderDirection), ['desc', 'asc'], true)) {
209 }
210
211 if (self::$orderColumn === 'from') {
212 $query .= ' ORDER BY '
213 . ' fname ' . $orderDirection . ', '
214 . ' lastname ' . $orderDirection . ', '
215 . ' login ' . $orderDirection . ', '
216 . ' import_name ' . $orderDirection;
217 } elseif (self::$orderColumn !== '') {
218 if (
219 !in_array(strtolower(self::$orderColumn), ['m_subject', 'send_time', 'rcp_to'], true) &&
220 !$DIC->database()->tableColumnExists('mail', strtolower(self::$orderColumn))) {
221 // @todo: Performance problem...
222 self::$orderColumn = 'send_time';
223 }
224
225 $query .= ' ORDER BY ' . strtolower(self::$orderColumn) . ' ' . $orderDirection;
226 } else {
227 $query .= ' ORDER BY send_time DESC';
228 }
229
230 $DIC->database()->setLimit(self::$limit, self::$offset);
231 $res = $DIC->database()->queryF(
232 $query,
233 ['integer', 'integer'],
234 [self::$userId, self::$folderId]
235 );
236 while ($row = $DIC->database()->fetchAssoc($res)) {
237 if (isset($row['attachments'])) {
238 $row['attachments'] = (array) unserialize(
239 stripslashes($row['attachments']),
240 ['allowed_classes' => false]
241 );
242 } else {
243 $row['attachments'] = [];
244 }
245
246 if (isset($row['mail_id'])) {
247 $row['mail_id'] = (int) $row['mail_id'];
248 }
249
250 if (isset($row['user_id'])) {
251 $row['user_id'] = (int) $row['user_id'];
252 }
253
254 if (isset($row['folder_id'])) {
255 $row['folder_id'] = (int) $row['folder_id'];
256 }
257
258 if (isset($row['sender_id'])) {
259 $row['sender_id'] = (int) $row['sender_id'];
260 }
261
262 $mails['set'][] = $row;
263 }
264
265 return $mails;
266 }
static string $orderDirection
const ANONYMOUS_USER_ID
Definition: constants.php:27
global $DIC
Definition: feed.php:28
$res
Definition: ltiservices.php:69
string $key
Consumer key/client ID value.
Definition: System.php:193
$query

References $DIC, $filter, ILIAS\LTI\ToolProvider\$key, $orderDirection, $query, $res, ANONYMOUS_USER_ID, ILIAS\Repository\int(), and ilDBConstants\T_INTEGER.

Referenced by ilMailFolderTableGUI\fetchTableData().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $filter

array ilMailBoxQuery::$filter = []
static

◆ $filtered_ids

array ilMailBoxQuery::$filtered_ids = []
static

Definition at line 37 of file class.ilMailBoxQuery.php.

Referenced by ilMailFolderTableGUI\fetchTableData().

◆ $folderId

int ilMailBoxQuery::$folderId = -1
static

Definition at line 30 of file class.ilMailBoxQuery.php.

Referenced by ilMailFolderTableGUI\fetchTableData().

◆ $limit

int ilMailBoxQuery::$limit = 0
static

Definition at line 32 of file class.ilMailBoxQuery.php.

Referenced by ilMailFolderTableGUI\fetchTableData().

◆ $offset

int ilMailBoxQuery::$offset = 0
static

Definition at line 33 of file class.ilMailBoxQuery.php.

Referenced by ilMailFolderTableGUI\fetchTableData().

◆ $orderColumn

string ilMailBoxQuery::$orderColumn = ''
static

Definition at line 35 of file class.ilMailBoxQuery.php.

Referenced by ilMailFolderTableGUI\fetchTableData().

◆ $orderDirection

string ilMailBoxQuery::$orderDirection = ''
static

◆ $userId

int ilMailBoxQuery::$userId = -1
static

Definition at line 31 of file class.ilMailBoxQuery.php.

Referenced by ilMailFolderTableGUI\fetchTableData().


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