ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMailForm.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
8 class ilMailForm
9 {
10  private $allow_smtp = null;
11  private $user_id = null;
12  private $setMap = array();
13  private $result;
14  private $max_entries = 20;
15 
23  public function __construct()
24  {
25  global $ilUser, $rbacsystem;
26 
27  $this->allow_smtp = $rbacsystem->checkAccess('smtp_mail', MAIL_SETTINGS_ID);
28  $this->user_id = $ilUser->getId();
29 
30  $this->result = array();
31  }
32 
41  private function addResult($login, $firstname, $lastname, $type)
42  {
43  if(count($this->result) > $this->max_entries)
44  {
45  throw new ilException('exceeded_max_entries');
46  }
47 
48  if (isset($this->setMap[$login]))
49  return;
50 
51  $tmp = new stdClass();
52  $tmp->value = $login;
53 
54  $label = $login;
55  if($firstname && $lastname)
56  {
57  $label .= " [" . $firstname . ", " . $lastname . "]";
58  }
59  $tmp->label = $label;
60 
61  $this->result[] = $tmp;
62 
63  $this->setMap[$login] = 1;
64  }
65 
76  public function getRecipientAsync($a_search, $a_native_search)
77  {
78  global $ilDB;
79 
80  $query =
81  "SELECT DISTINCT
82  abook.login login,
83  abook.firstname firstname,
84  abook.lastname lastname,
85  'addressbook' type
86  FROM addressbook abook
87  WHERE abook.user_id = ".$ilDB->quote($this->user_id,'integer')."
88  AND abook.login IS NOT NULL
89  AND (". $ilDB->like('abook.login', 'text', $a_search)."
90  OR ".$ilDB->like('abook.firstname', 'text', $a_search)."
91  OR ".$ilDB->like('abook.lastname', 'text', $a_search)."
92  )";
93 
94  $union_query_1 = "SELECT DISTINCT
95  abook.email login,
96  abook.firstname firstname,
97  abook.lastname lastname,
98  'addressbook' type
99  FROM addressbook abook
100  WHERE abook.user_id = ".$ilDB->quote($this->user_id,'integer')."
101  AND abook.login IS NULL
102  AND (".$ilDB->like('abook.email', 'text', $a_search)."
103  OR ".$ilDB->like('abook.firstname', 'text', $a_search)."
104  OR ".$ilDB->like('abook.lastname', 'text', $a_search)."
105  )";
106 
107  $union_query_2 = "SELECT DISTINCT
108  mail.rcp_to login,
109  '' firstname,
110  '' lastname,
111  'mail' type
112  FROM mail
113  WHERE ".$ilDB->like('mail.rcp_to', 'text', $a_search)."
114  AND sender_id = ".$ilDB->quote($this->user_id,'integer')."
115  AND mail.sender_id = mail.user_id";
116 
117  $queries = array(
118  'addressbook_1' => $query,
119  'mail' => $union_query_2
120  );
121 
122  if($this->allow_smtp == 1)
123  $queries['addressbook_2'] = $union_query_1;
124 
125  include_once 'Services/Utilities/classes/class.ilStr.php';
126 
127  try
128  {
129  // MySql: Join the array values for mysql to one select statement
130  if($ilDB->getDbType() != 'oracle')
131  $queries['all'] = implode(' UNION ', $queries);
132 
133  foreach($queries as $type => $query)
134  {
135  // Oracle: Distincts do no work with clobs
136  if('mail' == $type && $ilDB->getDbType() == 'oracle')
137  {
138  $query = str_replace('DISTINCT', '', $query);
139  }
140 
141  $ilDB->setLimit(0,20);
142  $query_res = $ilDB->query( $query );
143 
144  while($row = $ilDB->fetchObject($query_res))
145  {
146  if($row->type == 'mail')
147  {
148  if(strpos($row->login, ',') || strpos($row->login, ';'))
149  {
150  $parts = preg_split("/[ ]*[;,][ ]*/", trim($row->login));
151  foreach($parts as $part)
152  {
153  if(ilStr::strPos(ilStr::strToLower($part), ilStr::strToLower($a_native_search)) !== false)
154  {
155  $this->addResult($part, '', '', 'mail');
156  }
157  }
158  }
159  else
160  {
161  $this->addResult($row->login, '', '', 'mail');
162  }
163  }
164  else
165  {
166  $this->addResult($row->login, $row->firstname, $row->lastname, 'addressbook');
167  }
168  }
169  }
170  } catch(ilException $e) {}
171 
172  return $this->result;
173  }
174 }
175 ?>