ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
29  public static function _getMailBoxListData()
30  {
31  global $ilDB;
32 
33  // initialize array
34  $mails = array('cnt' => 0, 'cnt_unread' => 0, 'set' => array());
35 
36  // count query
37  $queryCount = 'SELECT COUNT(mail_id) cnt FROM mail '
38  . 'LEFT JOIN usr_data ON usr_id = sender_id '
39  . 'WHERE user_id = %s '
40  . '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)) '
41  . 'AND folder_id = %s '
42  . 'UNION ALL '
43  . 'SELECT COUNT(mail_id) cnt FROM mail '
44  . 'LEFT JOIN usr_data ON usr_id = sender_id '
45  . 'WHERE user_id = %s '
46  . '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)) '
47  . 'AND folder_id = %s '
48  . 'AND m_status = %s';
49 
50  $res = $ilDB->queryf(
51  $queryCount,
52  array('integer', 'integer', 'integer', 'integer', 'text'),
53  array(self::$userId, self::$folderId, self::$userId, self::$folderId, 'unread')
54  );
55  $counter = 0;
56  while($cnt_row = $ilDB->fetchAssoc($res))
57  {
58  if($counter == 0)
59  {
60  $mails['cnt'] = $cnt_row['cnt'];
61  }
62  else if($counter == 1)
63  {
64  $mails['cnt_unread'] = $cnt_row['cnt'];
65  }
66  else
67  {
68  break;
69  }
70 
71  ++$counter;
72  }
73 
74  $sortColumn = '';
75  if(self::$orderColumn == 'rcp_to' && $ilDB->getDBType() == 'oracle')
76  {
77  $sortColumn = ", CAST(rcp_to AS VARCHAR2(4000)) SORTCOL";
78  }
79 
80  if(self::$orderColumn == 'from')
81  {
82  // Because of the user id of automatically generated mails and ordering issues we have to do some magic
83  $firstname = '
84  ,(CASE
85  WHEN (usr_id = '.ANONYMOUS_USER_ID.') THEN firstname
86  ELSE '.$ilDB->quote(ilMail::_getIliasMailerName(), 'text').'
87  END) fname
88  ';
89  }
90 
91  // item query
92  $query = 'SELECT mail.*'.$sortColumn.' '.$firstname.' FROM mail '
93  . 'LEFT JOIN usr_data ON usr_id = sender_id '
94  . '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)) '
95  . 'WHERE user_id = %s '
96  . 'AND folder_id = %s';
97 
98  // order direction
99  $orderDirection = '';
100  if(in_array(strtolower(self::$orderDirection), array('desc', 'asc')))
101  {
103  }
104  else
105  {
106  $orderDirection = 'ASC';
107  }
108 
109  // order column
110  if(self::$orderColumn == 'rcp_to' && $ilDB->getDBType() == 'oracle')
111  {
112  $query .= ' ORDER BY SORTCOL '.$orderDirection;
113  }
114  else if(self::$orderColumn == 'from')
115  {
116  $query .= ' ORDER BY '
117  . ' fname '.$orderDirection.', '
118  . ' lastname '.$orderDirection.', '
119  . ' login '.$orderDirection.', '
120  . ' import_name '.$orderDirection;
121  }
122  else if(strlen(self::$orderColumn))
123  {
124  if(!in_array(strtolower(self::$orderColumn), array('m_subject', 'send_time', 'rcp_to')) &&
125  !$ilDB->tableColumnExists('mail', strtolower(self::$orderColumn)))
126  {
127  // @todo: Performance problem...
128  self::$orderColumn = 'send_time';
129  }
130 
131  $query .= ' ORDER BY '.strtolower(self::$orderColumn).' '.$orderDirection;
132  }
133  else
134  {
135  $query .= ' ORDER BY send_time DESC';
136  }
137 
138  $ilDB->setLimit(self::$limit, self::$offset);
139  $res = $ilDB->queryF(
140  $query,
141  array('integer', 'integer'),
142  array(self::$userId, self::$folderId)
143  );
144  while($row = $ilDB->fetchAssoc($res))
145  {
146  $row['attachments'] = unserialize(stripslashes($row['attachments']));
147  $row['m_type'] = unserialize(stripslashes($row['m_type']));
148  $mails['set'][] = $row;
149  }
150 
151  return $mails;
152  }
153 }