32 {
34
35 $mails = ['cnt' => 0, 'cnt_unread' => 0, 'set' => []];
36
38 'mail_filter_sender' => 'CONCAT(CONCAT(firstname, lastname), login)',
39 'mail_filter_recipients' => 'CONCAT(CONCAT(rcp_to, rcp_cc), rcp_bcc)',
40 'mail_filter_subject' => 'm_subject',
41 'mail_filter_body' => 'm_message',
42 'mail_filter_attach' => ''
43 ];
44
45 $filter_parts = [];
46 if (isset(self::$filter['mail_filter']) && strlen(self::$filter['mail_filter'])) {
48 if (strlen($column) && isset(self::$filter[
$key]) && (
int) self::$filter[
$key]) {
49 $filter_parts[] =
$DIC->database()->like(
50 $column,
51 'text',
52 '%%' . self::$filter['mail_filter'] . '%%',
53 false
54 );
55 }
56 }
57 }
58
59 $filter_qry = '';
60 if ($filter_parts) {
61 $filter_qry = 'AND (' . implode(' OR ', $filter_parts) . ')';
62 }
63
64 if (isset(self::$filter['mail_filter_only_unread']) && self::$filter['mail_filter_only_unread']) {
65 $filter_qry .=
' AND m_status = ' .
$DIC->database()->quote(
'unread',
'text') .
' ';
66 }
67
68 if (isset(self::$filter['mail_filter_only_with_attachments']) && self::$filter['mail_filter_only_with_attachments']) {
69 $filter_qry .=
' AND attachments != ' .
$DIC->database()->quote(serialize(
null),
'text') .
' ';
70 }
71
72 if (isset(self::$filter['period']) && is_array(self::$filter['period'])) {
73 $dateFilterParts = [];
74
75 if (null !== self::$filter['period']['start']) {
76 $dateFilterParts[] =
'send_time >= ' .
$DIC->database()->quote(
77 (new \DateTimeImmutable('@' . self::$filter['period']['start']))->format('Y-m-d 00:00:00'),
78 'timestamp'
79 );
80 }
81
82 if (null !== self::$filter['period']['end']) {
83 $dateFilterParts[] =
'send_time <= ' .
$DIC->database()->quote(
84 (new \DateTimeImmutable('@' . self::$filter['period']['end']))->format('Y-m-d 23:59:59'),
85 'timestamp'
86 );
87 }
88
89 if (count($dateFilterParts) > 0) {
90 $filter_qry .= ' AND (' . implode(' AND ', $dateFilterParts) . ') ';
91 }
92 }
93
94
95 $queryCount = 'SELECT COUNT(mail_id) cnt FROM mail '
96 . 'LEFT JOIN usr_data ON usr_id = sender_id '
97 . 'WHERE user_id = %s '
98 . 'AND ((sender_id > 0 AND sender_id IS NOT NULL AND usr_id IS NOT NULL) OR (sender_id = 0 OR sender_id IS NULL)) '
99 . 'AND folder_id = %s '
100 . $filter_qry;
101
102 if (self::$filtered_ids) {
103 $queryCount .=
' AND ' .
$DIC->database()->in(
'mail_id', self::$filtered_ids,
false,
'integer') .
' ';
104 }
105
106 $queryCount .= ' UNION ALL '
107 . 'SELECT COUNT(mail_id) cnt FROM mail '
108 . 'LEFT JOIN usr_data ON usr_id = sender_id '
109 . 'WHERE user_id = %s '
110 . 'AND ((sender_id > 0 AND sender_id IS NOT NULL AND usr_id IS NOT NULL) OR (sender_id = 0 OR sender_id IS NULL)) '
111 . 'AND folder_id = %s '
112 . $filter_qry . ' '
113 . 'AND m_status = %s';
114
115 if (self::$filtered_ids) {
116 $queryCount .=
' AND ' .
$DIC->database()->in(
'mail_id', self::$filtered_ids,
false,
'integer') .
' ';
117 }
118
120 $queryCount,
121 ['integer', 'integer', 'integer', 'integer', 'text'],
122 [self::$userId, self::$folderId, self::$userId, self::$folderId, 'unread']
123 );
124
125 $counter = 0;
126 while ($cnt_row =
$DIC->database()->fetchAssoc(
$res)) {
127 if ($counter === 0) {
128 $mails['cnt'] = $cnt_row['cnt'];
129 } else {
130 if ($counter === 1) {
131 $mails['cnt_unread'] = $cnt_row['cnt'];
132 } else {
133 break;
134 }
135 }
136
137 ++$counter;
138 }
139
140 $sortColumn = '';
141 $firstnameSelection = '';
142 if (self::$orderColumn === 'from') {
143
144 $firstnameSelection = '
145 ,(CASE
146 WHEN (usr_id = ' . ANONYMOUS_USER_ID . ') THEN firstname
147 ELSE ' .
$DIC->database()->quote(ilMail::_getIliasMailerName(),
'text') .
'
148 END) fname
149 ';
150 }
151
152
153 $query =
'SELECT mail.*' . $sortColumn .
' ' . $firstnameSelection .
' FROM mail '
154 . 'LEFT JOIN usr_data ON usr_id = sender_id '
155 . 'AND ((sender_id > 0 AND sender_id IS NOT NULL AND usr_id IS NOT NULL) OR (sender_id = 0 OR sender_id IS NULL)) '
156 . 'WHERE user_id = %s '
157 . $filter_qry . ' '
158 . 'AND folder_id = %s';
159
160 if (self::$filtered_ids) {
161 $query .=
' AND ' .
$DIC->database()->in(
'mail_id', self::$filtered_ids,
false,
'integer') .
' ';
162 }
163
165 if (in_array(strtolower(self::$orderDirection), array('desc', 'asc'))) {
167 }
168
169 if (self::$orderColumn === 'from') {
175 } elseif (strlen(self::$orderColumn) > 0) {
176 if (
177 !in_array(strtolower(self::$orderColumn), ['m_subject', 'send_time', 'rcp_to']) &&
178 !
$DIC->database()->tableColumnExists(
'mail', strtolower(self::$orderColumn))) {
179
180 self::$orderColumn = 'send_time';
181 }
182
184 } else {
185 $query .=
' ORDER BY send_time DESC';
186 }
187
188 $DIC->database()->setLimit(self::$limit, self::$offset);
191 ['integer', 'integer'],
192 [self::$userId, self::$folderId]
193 );
195 $row[
'attachments'] = unserialize(stripslashes(
$row[
'attachments']));
196 $row[
'm_type'] = unserialize(stripslashes(
$row[
'm_type']));
197 $mails[
'set'][] =
$row;
198 }
199
200 return $mails;
201 }
foreach($_POST as $key=> $value) $res