43 : array
44 {
46
47 $mails = ['cnt' => 0, 'cnt_unread' => 0, 'set' => []];
48
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 ) {
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']) {
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
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
181 $firstnameSelection = '
182 ,(CASE
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) {
199 'mail_id',
200 self::$filtered_ids,
201 false,
202 'integer'
203 ) . ' ';
204 }
205
207 if (in_array(strtolower(self::$orderDirection), ['desc', 'asc'], true)) {
209 }
210
211 if (self::$orderColumn === 'from') {
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
222 self::$orderColumn = 'send_time';
223 }
224
226 } else {
227 $query .=
' ORDER BY send_time DESC';
228 }
229
230 $DIC->database()->setLimit(self::$limit, self::$offset);
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