ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMailSearchGUI.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 require_once './Services/User/classes/class.ilObjUser.php';
25 require_once 'Services/Mail/classes/class.ilMailbox.php';
26 require_once 'Services/Mail/classes/class.ilFormatMail.php';
27 require_once 'Services/Contact/classes/class.ilAddressbook.php';
28 include_once 'Services/Table/classes/class.ilTable2GUI.php';
29 
37 {
38  private $tpl = null;
39  private $ctrl = null;
40  private $lng = null;
41 
42  private $umail = null;
43 
44  private $errorDelete = false;
45 
46  public function __construct()
47  {
48  global $tpl, $ilCtrl, $lng, $ilUser;
49 
50  $this->tpl = $tpl;
51  $this->ctrl = $ilCtrl;
52  $this->lng = $lng;
53 
54  $this->ctrl->saveParameter($this, "mobj_id");
55 
56  $this->umail = new ilFormatMail($ilUser->getId());
57  }
58 
59  public function executeCommand()
60  {
61  $forward_class = $this->ctrl->getNextClass($this);
62  switch($forward_class)
63  {
64  default:
65  if (!($cmd = $this->ctrl->getCmd()))
66  {
67  $cmd = "showResults";
68  }
69 
70  $this->$cmd();
71  break;
72  }
73  return true;
74  }
75 
76  public function adopt()
77  {
78  // necessary because of select all feature of ilTable2GUI
79  $recipients = array();
80  $recipients = array_merge($recipients, (array)$_POST['search_name_to_addr']);
81  $recipients = array_merge($recipients, (array)$_POST['search_name_to_usr']);
82  $recipients = array_merge($recipients, (array)$_POST['search_name_to_grp']);
83 
84  $recipients = array_unique($recipients);
85 
86  $_SESSION["mail_search_results_to"] = $recipients;
87  $_SESSION["mail_search_results_cc"] = $_POST["search_name_cc"];
88  $_SESSION["mail_search_results_bcc"] = $_POST["search_name_bcc"];
89 
90  $this->saveMailData();
91 
92  $this->ctrl->returnToParent($this);
93  }
94 
95  private function saveMailData()
96  {
97  $mail_data = $this->umail->getSavedData();
98 
99  $this->umail->savePostData(
100  $mail_data["user_id"],
101  $mail_data["attachments"],
102  $mail_data["rcp_to"],
103  $mail_data["rcp_cc"],
104  $mail_data["rcp_bcc"],
105  $mail_data["m_type"],
106  $mail_data["m_email"],
107  $mail_data["m_subject"],
108  $mail_data["m_message"],
109  $mail_data["use_placeholders"]
110  );
111  }
112 
113  public function cancel()
114  {
115  $this->ctrl->returnToParent($this);
116  }
117 
118  function search()
119  {
120  $_SESSION["mail_search_search"] = $_POST["search"];
121  $_SESSION["mail_search_type_system"] = $_POST["type_system"];
122  $_SESSION["mail_search_type_addressbook"] = $_POST["type_addressbook"];
123 
124  // IF NO TYPE IS GIVEN SEARCH IN BOTH 'system' and 'addressbook'
125  if(!$_SESSION["mail_search_type_system"] &&
126  !$_SESSION["mail_search_type_addressbook"])
127  {
128  $_SESSION["mail_search_type_system"] = 1;
129  $_SESSION["mail_search_type_addressbook"] = 1;
130  }
131  if (strlen(trim($_SESSION["mail_search_search"])) == 0)
132  {
133  ilUtil::sendInfo($this->lng->txt("mail_insert_query"));
134  }
135  else if(strlen(trim($_SESSION["mail_search_search"])) < 3)
136  {
137  $this->lng->loadLanguageModule('search');
138  ilUtil::sendInfo($this->lng->txt('search_minimum_three'));
139  }
140 
141  $this->showResults();
142 
143  return true;
144  }
145 
146  public function showResults()
147  {
148  global $rbacsystem, $lng, $ilUser, $ilCtrl, $rbacreview;
149 
150  $this->saveMailData();
151 
152  $this->tpl->addBlockFile("ADM_CONTENT", "adm_content", "tpl.mail_search.html", "Services/Contact");
153  $this->tpl->setVariable("HEADER", $this->lng->txt("mail"));
154 
155  $this->tpl->setVariable("ACTION", $this->ctrl->getFormAction($this));
156  // searchform
157  include_once 'Services/Form/classes/class.ilPropertyFormGUI.php';
158  $form = new ilPropertyFormGUI();
159  $form->setTitle($this->lng->txt('search_recipients'));
160  $form->setId('search_rcp');
161  $form->setFormAction($ilCtrl->getFormAction($this, 'search'));
162 
163  $inp = new ilTextInputGUI($this->lng->txt("search_for"), 'search');
164  $inp->setSize(30);
165  if (strlen(trim($_SESSION["mail_search_search"])) > 0)
166  {
167  $inp->setValue(ilUtil::prepareFormOutput(trim($_SESSION["mail_search_search"]), true));
168  }
169  $form->addItem($inp);
170 
171  $chb = new ilCheckboxInputGUI($this->lng->txt("mail_search_addressbook"), 'type_addressbook');
172  if ($_SESSION['mail_search_type_addressbook'])
173  $chb->setChecked(true);
174  $inp->addSubItem($chb);
175 
176  $chb = new ilCheckboxInputGUI($this->lng->txt("mail_search_system"), 'type_system');
177  if ($_SESSION['mail_search_type_system'])
178  $chb->setChecked(true);
179  $inp->addSubItem($chb);
180 
181  $form->addCommandButton('search', $this->lng->txt("search"));
182  $form->addCommandButton('cancel', $this->lng->txt("cancel"));
183 
184  $this->tpl->setVariable('SEARCHFORM', $form->getHtml());
185  // searchform end
186 
187  if (strlen(trim($_SESSION["mail_search_search"])) > 0)
188  {
189  $this->tpl->setVariable("VALUE_SEARCH_FOR", ilUtil::prepareFormOutput(trim($_SESSION["mail_search_search"]), true));
190  }
191 
192  if (!$_SESSION['mail_search_type_system'] && !$_SESSION['mail_search_type_addressbook'])
193  {
194  $this->tpl->setVariable('CHECKED_TYPE_SYSTEM', "checked=\"checked\"");
195  }
196  else
197  {
198  if ($_SESSION['mail_search_type_addressbook']) $this->tpl->setVariable('CHECKED_TYPE_ADDRESSBOOK', "checked=\"checked\"");
199  if ($_SESSION['mail_search_type_system'])$this->tpl->setVariable('CHECKED_TYPE_SYSTEM', "checked=\"checked\"");
200  }
201 
202  if ($_SESSION['mail_search_type_addressbook'] && strlen(trim($_SESSION["mail_search_search"])) >= 3)
203  {
204  $abook = new ilAddressbook($ilUser->getId());
205  $entries = $abook->searchUsers(addslashes(urldecode($_SESSION['mail_search_search'])));
206 
207  if (count($entries))
208  {
209  $tbl_addr = new ilTable2GUI($this);
210  $tbl_addr->setTitle($lng->txt('mail_addressbook'));
211  $tbl_addr->setRowTemplate('tpl.mail_search_addr_row.html', 'Services/Contact');
212 
213  $result = array();
214  $counter = 0;
215  foreach ($entries as $entry)
216  {
217  $result[$counter]['check'] = ilUtil::formCheckbox(0, 'search_name_to_addr[]', ($entry['login'] ? $entry['login'] : $entry['email'])) .
218  ilUtil::formCheckbox(0, 'search_name_cc[]', ($entry['login'] ? $entry['login'] : $entry['email'])) .
219  ilUtil::formCheckbox(0, 'search_name_bcc[]', ($entry['login'] ? $entry['login'] : $entry['email']));
220  $result[$counter]['login'] = $entry['login'];
221  $result[$counter]['firstname'] = $entry['firstname'];
222  $result[$counter]['lastname'] = $entry['lastname'];
223 
224  $id = ilObjUser::_lookupId($entry['login']);
225  if (ilObjUser::_lookupPref($id, 'public_email') == 'y' || !$entry['login'])
226  {
227  $has_mail_addr = true;
228  $result[$counter]['email'] = $entry['email'];
229  }
230 
231  ++$counter;
232  }
233 
234  $tbl_addr->addColumn($this->lng->txt('mail_to') . '/' . $this->lng->txt('cc') . '/' . $this->lng->txt('bc'), 'check', '10%');
235  $tbl_addr->addColumn($this->lng->txt('login'), 'login', "15%");
236  $tbl_addr->addColumn($this->lng->txt('firstname'), 'firstname', "15%");
237  $tbl_addr->addColumn($this->lng->txt('lastname'), 'lastname', "15%");
238  if ($has_mail_addr)
239  {
240  foreach ($result as $key => $val)
241  {
242  if ($val['email'] == '') $result[$key]['email'] = '&nbsp;';
243  }
244 
245  $tbl_addr->addColumn($this->lng->txt('email'), 'email', "15%");
246  }
247  $tbl_addr->setData($result);
248 
249  $tbl_addr->setDefaultOrderField('login');
250  $tbl_addr->setPrefix('addr_');
251  $tbl_addr->enable('select_all');
252  $tbl_addr->setSelectAllCheckbox('search_name_to_addr');
253  $tbl_addr->setFormName('recipients');
254 
255  $this->tpl->setVariable('TABLE_ADDR', $tbl_addr->getHTML());
256  }
257  }
258  if ($_SESSION['mail_search_type_system'] && strlen(trim($_SESSION["mail_search_search"])) >= 3)
259  {
260  include_once 'Services/Search/classes/class.ilQueryParser.php';
261  include_once 'Services/Search/classes/class.ilObjectSearchFactory.php';
262  include_once 'Services/Search/classes/class.ilSearchResult.php';
263 
264  $all_results = new ilSearchResult();
265 
266  $query_parser = new ilQueryParser(addcslashes($_SESSION['mail_search_search'],'%_'));
267  $query_parser->setCombination(QP_COMBINATION_OR);
268  $query_parser->setMinWordLength(3);
269  $query_parser->parse();
270 
271  $user_search =& ilObjectSearchFactory::_getUserSearchInstance($query_parser);
272  $user_search->enableActiveCheck(true);
273  $user_search->setFields(array('login'));
274  $result_obj = $user_search->performSearch();
275  $all_results->mergeEntries($result_obj);
276 
277  $user_search->setFields(array('firstname'));
278  $result_obj = $user_search->performSearch();
279  $all_results->mergeEntries($result_obj);
280 
281  $user_search->setFields(array('lastname'));
282  $result_obj = $user_search->performSearch();
283  $all_results->mergeEntries($result_obj);
284 
285  $all_results->setMaxHits(100000);
286  $all_results->preventOverwritingMaxhits(true);
287  $all_results->filter(ROOT_FOLDER_ID,QP_COMBINATION_OR);
288 
289 
290  //$users = $all_results->getResults();
291 
292  // Filter users (depends on setting in user accounts)
293  include_once './Services/User/classes/class.ilUserFilter.php';
294  $users = ilUserFilter::getInstance()->filter($all_results->getResultIds());
295 
296 
297  if (count($users))
298  {
299  $tbl_users = new ilTable2GUI($this);
300  $tbl_users->setTitle($lng->txt('system').': '.$lng->txt('persons'));
301  $tbl_users->setRowTemplate('tpl.mail_search_users_row.html','Services/Contact');
302 
303  $result = array();
304  $counter = 0;
305  foreach ($users as $user)
306  {
307  $login = ilObjUser::_lookupLogin($user);
308 
309  $result[$counter]['check'] = ilUtil::formCheckbox(0, 'search_name_to_usr[]', $login) .
310  ilUtil::formCheckbox(0, 'search_name_cc[]', $login) .
311  ilUtil::formCheckbox(0, 'search_name_bcc[]', $login);
312  $result[$counter]['login'] = $login;
313 
314  if (in_array(ilObjUser::_lookupPref($user, 'public_profile'), array('y',"g")))
315  {
316  $name = ilObjUser::_lookupName($user);
317  $result[$counter]['firstname'] = $name['firstname'];
318  $result[$counter]['lastname'] = $name['lastname'];
319  }
320  else
321  {
322  $result[$counter]['firstname'] = '';
323  $result[$counter]['lastname'] = '';
324  }
325 
326  if (ilObjUser::_lookupPref($user, 'public_email') == 'y')
327  {
328  $has_mail_usr = true;
329  $result[$counter]['email'] = ilObjUser::_lookupEmail($user);
330  }
331 
332  ++$counter;
333  }
334 
335  $tbl_users->addColumn($this->lng->txt('mail_to') . '/' . $this->lng->txt('cc') . '/' . $this->lng->txt('bc'), 'check', '10%');
336  $tbl_users->addColumn($this->lng->txt('login'), 'login', '15%');
337  $tbl_users->addColumn($this->lng->txt('firstname'), 'firstname', '15%');
338  $tbl_users->addColumn($this->lng->txt('lastname'), 'lastname', '15%');
339  if ($has_mail_usr == true)
340  {
341  foreach ($result as $key => $val)
342  {
343  if ($val['email'] == '') $result[$key]['email'] = '&nbsp;';
344  }
345 
346  $tbl_users->addColumn($this->lng->txt('email'), 'email', '15%');
347  }
348  $tbl_users->setData($result);
349 
350  $tbl_users->setDefaultOrderField('login');
351  $tbl_users->setPrefix('usr_');
352  $tbl_users->enable('select_all');
353  $tbl_users->setSelectAllCheckbox('search_name_to_usr');
354  $tbl_users->setFormName('recipients');
355 
356  $this->tpl->setVariable('TABLE_USERS', $tbl_users->getHTML());
357  }
358 
359  $groups = ilUtil::searchGroups(addslashes(urldecode(addcslashes($_SESSION['mail_search_search'],'%_'))));
360 
361  if (count($groups))
362  {
363  $tbl_grp = new ilTable2GUI($this);
364  $tbl_grp->setTitle($lng->txt('system').': '.$lng->txt('groups'));
365  $tbl_grp->setRowTemplate('tpl.mail_search_groups_row.html','Services/Contact');
366 
367  $result = array();
368  $counter = 0;
369 
370 
371  foreach ($groups as $grp)
372  {
373  $members = array();
374  $roles = $rbacreview->getAssignableChildRoles($grp['ref_id']);
375  foreach ($roles as $role)
376  {
377  if (substr($role['title'], 0, 14) == 'il_grp_member_' ||
378  substr($role['title'], 0, 13) == 'il_grp_admin_')
379  {
380  // does not work if Pear is enabled and Mailbox Address contain special chars!!
381  //array_push($members, $rbacreview->getRoleMailboxAddress($role['obj_id']));
382  // FIX for Mantis: 7523
383  array_push($members, '#'.$role['title']);
384  }
385  }
386  $str_members = implode(',',$members);
387 
388  $result[$counter]['check'] = ilUtil::formCheckbox(0, 'search_name_to_grp[]', $str_members) .
389  ilUtil::formCheckbox(0, 'search_name_cc[]', $str_members) .
390  ilUtil::formCheckbox(0, 'search_name_bcc[]',$str_members);
391  $result[$counter]['title'] = $grp['title'];
392  $result[$counter]['description'] = $grp['description'];
393 
394  ++$counter;
395  }
396  $tbl_grp->setData($result);
397 
398  $tbl_grp->addColumn($this->lng->txt('mail_to') . '/' . $this->lng->txt('cc') . '/' . $this->lng->txt('bc'), 'check', '10%');
399  $tbl_grp->addColumn($this->lng->txt('title'), 'title', '15%');
400  $tbl_grp->addColumn($this->lng->txt('description'), 'description', '15%');
401 
402  $tbl_grp->setDefaultOrderField('title');
403  $tbl_grp->setPrefix('grp_');
404  $tbl_grp->enable('select_all');
405  $tbl_grp->setSelectAllCheckbox('search_name_to_grp');
406  $tbl_grp->setFormName('recipients');
407 
408  $this->tpl->setVariable('TABLE_GRP', $tbl_grp->getHTML());
409  }
410  }
411 
412  if (count($users) || count($groups) || count($entries))
413  {
414  $this->tpl->setVariable("IMG_ARROW", ilUtil::getImagePath("arrow_downright.gif"));
415  $this->tpl->setVariable("ALT_ARROW", '');
416  $this->tpl->setVariable('BUTTON_ADOPT', $this->lng->txt('adopt'));
417  }
418  else if (strlen(trim($_SESSION["mail_search_search"])) >= 3)
419  {
420  $this->lng->loadLanguageModule('search');
421  ilUtil::sendInfo($this->lng->txt('search_no_match'));
422  }
423 
424  $this->tpl->show();
425  }
426 }
427 ?>