ILIAS  Release_4_0_x_branch Revision 61816
 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  // item query
81  $query = 'SELECT mail.*'.$sortColumn.' FROM mail '
82  . 'LEFT JOIN usr_data ON usr_id = sender_id '
83  . '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)) '
84  . 'WHERE user_id = %s '
85  . 'AND folder_id = %s';
86 
87  // order direction
88  $orderDirection = '';
89  if(in_array(strtolower(self::$orderDirection), array('desc', 'asc')))
90  {
92  }
93  else
94  {
95  $orderDirection = 'ASC';
96  }
97 
98  // order column
99  if(self::$orderColumn == 'rcp_to' && $ilDB->getDBType() == 'oracle')
100  {
101  $query .= ' ORDER BY SORTCOL '.$orderDirection;
102  }
103  else if(strlen(self::$orderColumn) &&
104  $ilDB->tableColumnExists('mail', strtolower(self::$orderColumn)))
105  {
106  $query .= ' ORDER BY '.strtolower(self::$orderColumn).' '.$orderDirection;
107  }
108  else if(self::$orderColumn == 'from')
109  {
110  $query .= ' ORDER BY '
111  . ' firstname '.$orderDirection.', '
112  . ' lastname '.$orderDirection.', '
113  . ' login '.$orderDirection.', '
114  . ' import_name '.$orderDirection;
115  }
116  else
117  {
118  $query .= ' ORDER BY send_time DESC';
119  }
120 
121  $ilDB->setLimit(self::$limit, self::$offset);
122  $res = $ilDB->queryf(
123  $query,
124  array('integer', 'integer'),
125  array(self::$userId, self::$folderId)
126  );
127  while($row = $ilDB->fetchAssoc($res))
128  {
129  $row['attachments'] = unserialize(stripslashes($row['attachments']));
130  $row['m_type'] = unserialize(stripslashes($row['m_type']));
131  $mails['set'][] = $row;
132  }
133 
134  return $mails;
135  }
136 }
137 ?>