ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilMailBoxQuery.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
13 {
14  public static $folderId = -1;
15  public static $userId = -1;
16  public static $limit = 0;
17  public static $offset = 0;
18  public static $orderDirection = '';
19  public static $orderColumn = '';
20  public static $filter = array();
21  public static $filtered_ids = array();
22 
31  public static function _getMailBoxListData()
32  {
33  global $DIC;
34 
35  $mails = ['cnt' => 0, 'cnt_unread' => 0, 'set' => []];
36 
37  $filter = [
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'])) {
47  foreach ($filter as $key => $column) {
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  // count query
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 
119  $res = $DIC->database()->queryF(
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  // Because of the user id of automatically generated mails and ordering issues we have to do some magic
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  // item query
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 
164  $orderDirection = 'ASC';
165  if (in_array(strtolower(self::$orderDirection), array('desc', 'asc'))) {
166  $orderDirection = self::$orderDirection;
167  }
168 
169  if (self::$orderColumn === 'from') {
170  $query .= ' ORDER BY '
171  . ' fname ' . $orderDirection . ', '
172  . ' lastname ' . $orderDirection . ', '
173  . ' login ' . $orderDirection . ', '
174  . ' import_name ' . $orderDirection;
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  // @todo: Performance problem...
180  self::$orderColumn = 'send_time';
181  }
182 
183  $query .= ' ORDER BY ' . strtolower(self::$orderColumn) . ' ' . $orderDirection;
184  } else {
185  $query .= ' ORDER BY send_time DESC';
186  }
187 
188  $DIC->database()->setLimit(self::$limit, self::$offset);
189  $res = $DIC->database()->queryF(
190  $query,
191  ['integer', 'integer'],
192  [self::$userId, self::$folderId]
193  );
194  while ($row = $DIC->database()->fetchAssoc($res)) {
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  }
202 }
Mail query class.
global $DIC
Definition: saml.php:7
static _getMailBoxListData()
_getMailBoxListData
foreach($_POST as $key=> $value) $res
$query
$row
$key
Definition: croninfo.php:18