ILIAS  release_4-3 Revision
 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($wsp_access_handler = null, $wsp_node_id = null)
47  {
48  global $tpl, $ilCtrl, $lng, $ilUser;
49 
50  $this->tpl = $tpl;
51  $this->ctrl = $ilCtrl;
52  $this->lng = $lng;
53 
54  // personal workspace
55  $this->wsp_access_handler = $wsp_access_handler;
56  $this->wsp_node_id = $wsp_node_id;
57 
58  $this->ctrl->saveParameter($this, "mobj_id");
59  $this->ctrl->saveParameter($this, "ref");
60 
61  $this->umail = new ilFormatMail($ilUser->getId());
62  }
63 
64  public function executeCommand()
65  {
66  $forward_class = $this->ctrl->getNextClass($this);
67  switch($forward_class)
68  {
69  default:
70  if (!($cmd = $this->ctrl->getCmd()))
71  {
72  $cmd = "showResults";
73  }
74 
75  $this->$cmd();
76  break;
77  }
78  return true;
79  }
80 
81  public function adopt()
82  {
83  // necessary because of select all feature of ilTable2GUI
84  $recipients = array();
85  $recipients = array_merge($recipients, (array)$_POST['search_name_to_addr']);
86  $recipients = array_merge($recipients, (array)$_POST['search_name_to_usr']);
87  $recipients = array_merge($recipients, (array)$_POST['search_name_to_grp']);
88 
89  $recipients = array_unique($recipients);
90 
91  $_SESSION["mail_search_results_to"] = $recipients;
92  $_SESSION["mail_search_results_cc"] = $_POST["search_name_cc"];
93  $_SESSION["mail_search_results_bcc"] = $_POST["search_name_bcc"];
94 
95  if($_GET["ref"] != "wsp")
96  {
97  $this->saveMailData();
98  }
99  else
100  {
101  $this->addPermission($recipients);
102  }
103 
104  $this->ctrl->returnToParent($this);
105  }
106 
107  private function saveMailData()
108  {
109  $mail_data = $this->umail->getSavedData();
110 
111  $this->umail->savePostData(
112  $mail_data["user_id"],
113  $mail_data["attachments"],
114  $mail_data["rcp_to"],
115  $mail_data["rcp_cc"],
116  $mail_data["rcp_bcc"],
117  $mail_data["m_type"],
118  $mail_data["m_email"],
119  $mail_data["m_subject"],
120  $mail_data["m_message"],
121  $mail_data["use_placeholders"]
122  );
123  }
124 
125  public function cancel()
126  {
127  $this->ctrl->returnToParent($this);
128  }
129 
130  function search()
131  {
132  $_SESSION["mail_search_search"] = $_POST["search"];
133  $_SESSION["mail_search_type_system"] = $_POST["type_system"];
134  $_SESSION["mail_search_type_addressbook"] = $_POST["type_addressbook"];
135 
136  if (strlen(trim($_SESSION["mail_search_search"])) == 0)
137  {
138  ilUtil::sendInfo($this->lng->txt("mail_insert_query"));
139  }
140  else if(strlen(trim($_SESSION["mail_search_search"])) < 3)
141  {
142  $this->lng->loadLanguageModule('search');
143  ilUtil::sendInfo($this->lng->txt('search_minimum_three'));
144  }
145 
146  $this->showResults();
147 
148  return true;
149  }
150 
151  protected function initSearchForm()
152  {
153  global $ilCtrl;
154 
155  if($_GET["ref"] != "wsp")
156  {
157  $this->saveMailData();
158  $title = $this->lng->txt('search_recipients');
159  }
160  else
161  {
162  $this->lng->loadLanguageModule("wsp");
163  $title = $this->lng->txt("wsp_share_search_users");
164  }
165 
166  // searchform
167  include_once 'Services/Form/classes/class.ilPropertyFormGUI.php';
168  $form = new ilPropertyFormGUI();
169  $form->setTitle($title);
170  $form->setId('search_rcp');
171  $form->setFormAction($ilCtrl->getFormAction($this, 'search'));
172 
173  $inp = new ilTextInputGUI($this->lng->txt("search_for"), 'search');
174  $inp->setSize(30);
175  if (strlen(trim($_SESSION["mail_search_search"])) > 0)
176  {
177  $inp->setValue(ilUtil::prepareFormOutput(trim($_SESSION["mail_search_search"]), true));
178  }
179  $form->addItem($inp);
180 
181  $chb = new ilCheckboxInputGUI($this->lng->txt("mail_search_addressbook"), 'type_addressbook');
182  if ($_SESSION['mail_search_type_addressbook'])
183  $chb->setChecked(true);
184  $inp->addSubItem($chb);
185 
186  $chb = new ilCheckboxInputGUI($this->lng->txt("mail_search_system"), 'type_system');
187  if ($_SESSION['mail_search_type_system'])
188  $chb->setChecked(true);
189  $inp->addSubItem($chb);
190 
191  $form->addCommandButton('search', $this->lng->txt("search"));
192  $form->addCommandButton('cancel', $this->lng->txt("cancel"));
193 
194  return $form;
195  }
196 
197  public function showResults()
198  {
199  global $rbacsystem, $lng, $ilUser, $ilCtrl, $rbacreview, $ilObjDataCache;
200 
201  $form = $this->initSearchForm();
202 
203  $this->tpl->addBlockFile("ADM_CONTENT", "adm_content", "tpl.mail_search.html", "Services/Contact");
204  $this->tpl->setTitle($this->lng->txt("mail"));
205 
206  $this->tpl->setVariable("ACTION", $this->ctrl->getFormAction($this));
207 
208  $this->tpl->setVariable('SEARCHFORM', $form->getHtml());
209 
210  // #14109
211  if(strlen($_SESSION['mail_search_search']) < 3)
212  {
213  if($_GET["ref"] != "wsp")
214  {
215  $this->tpl->show();
216  }
217  return;
218  }
219 
220  if (strlen(trim($_SESSION["mail_search_search"])) > 0)
221  {
222  $this->tpl->setVariable("VALUE_SEARCH_FOR", ilUtil::prepareFormOutput(trim($_SESSION["mail_search_search"]), true));
223  }
224 
225  if ($_SESSION['mail_search_type_addressbook']) $this->tpl->setVariable('CHECKED_TYPE_ADDRESSBOOK', "checked=\"checked\"");
226  if ($_SESSION['mail_search_type_system'])$this->tpl->setVariable('CHECKED_TYPE_SYSTEM', "checked=\"checked\"");
227 
228  if ($_SESSION['mail_search_type_addressbook'] == NULL && $_SESSION['mail_search_type_system'] == NULL)
229  {
230  $this->lng->loadLanguageModule('search');
231  ilUtil::sendFailure($this->lng->txt('search_one_action'));
232  }
233 
234  if ($_SESSION['mail_search_type_addressbook'] && strlen(trim($_SESSION["mail_search_search"])) >= 3)
235  {
236  $abook = new ilAddressbook($ilUser->getId());
237  $entries = $abook->searchUsers(addslashes(urldecode($_SESSION['mail_search_search'])));
238 
239  // remove all contacts who are not registered users for personal workspace
240  if($_GET["ref"] == "wsp")
241  {
242  foreach($entries as $idx => $entry)
243  {
244  if(!$entry["login"])
245  {
246  unset($entries[$idx]);
247  }
248  }
249  }
250 
251  if (count($entries))
252  {
253  $tbl_addr = new ilTable2GUI($this);
254  $tbl_addr->setTitle($lng->txt('mail_addressbook'));
255  $tbl_addr->setRowTemplate('tpl.mail_search_addr_row.html', 'Services/Contact');
256 
257  $result = array();
258  $counter = 0;
259  foreach ($entries as $entry)
260  {
261  if($_GET["ref"] != "wsp")
262  {
263  $result[$counter]['check'] = ilUtil::formCheckbox(0, 'search_name_to_addr[]', ($entry['login'] ? $entry['login'] : $entry['email'])) .
264  ilUtil::formCheckbox(0, 'search_name_cc[]', ($entry['login'] ? $entry['login'] : $entry['email'])) .
265  ilUtil::formCheckbox(0, 'search_name_bcc[]', ($entry['login'] ? $entry['login'] : $entry['email']));
266  }
267  else
268  {
269  $user_id = ilObjUser::_loginExists($entry["login"]);
270  $result[$counter]['check'] = ilUtil::formCheckbox(0, 'search_name_to_addr[]', $user_id);
271  }
272 
273  $result[$counter]['login'] = $entry['login'];
274  $result[$counter]['firstname'] = $entry['firstname'];
275  $result[$counter]['lastname'] = $entry['lastname'];
276 
277  $id = ilObjUser::_lookupId($entry['login']);
278  if (ilObjUser::_lookupPref($id, 'public_email') == 'y' || !$entry['login'])
279  {
280  $has_mail_addr = true;
281  $result[$counter]['email'] = $entry['email'];
282  }
283 
284  ++$counter;
285  }
286 
287  if($_GET["ref"] != "wsp")
288  {
289  $tbl_addr->addColumn($this->lng->txt('mail_to') . '/' . $this->lng->txt('cc') . '/' . $this->lng->txt('bc'), 'check', '10%');
290  }
291  else
292  {
293  $tbl_addr->addColumn("", "", "1%");
294  }
295  $tbl_addr->addColumn($this->lng->txt('login'), 'login', "15%");
296  $tbl_addr->addColumn($this->lng->txt('firstname'), 'firstname', "15%");
297  $tbl_addr->addColumn($this->lng->txt('lastname'), 'lastname', "15%");
298  if ($has_mail_addr)
299  {
300  foreach ($result as $key => $val)
301  {
302  if ($val['email'] == '') $result[$key]['email'] = '&nbsp;';
303  }
304 
305  $tbl_addr->addColumn($this->lng->txt('email'), 'email', "15%");
306  }
307  $tbl_addr->setData($result);
308 
309  $tbl_addr->setDefaultOrderField('login');
310  $tbl_addr->setPrefix('addr_');
311  $tbl_addr->enable('select_all');
312  $tbl_addr->setSelectAllCheckbox('search_name_to_addr');
313  $tbl_addr->setFormName('recipients');
314 
315  $this->tpl->setVariable('TABLE_ADDR', $tbl_addr->getHTML());
316  }
317  }
318  if ($_SESSION['mail_search_type_system'] && strlen(trim($_SESSION["mail_search_search"])) >= 3)
319  {
320  include_once 'Services/Search/classes/class.ilQueryParser.php';
321  include_once 'Services/Search/classes/class.ilObjectSearchFactory.php';
322  include_once 'Services/Search/classes/class.ilSearchResult.php';
323 
324  $all_results = new ilSearchResult();
325 
326  $query_parser = new ilQueryParser(addcslashes($_SESSION['mail_search_search'],'%_'));
327  $query_parser->setCombination(QP_COMBINATION_AND);
328  $query_parser->setMinWordLength(3);
329  $query_parser->parse();
330 
331  $user_search =& ilObjectSearchFactory::_getUserSearchInstance($query_parser);
332  $user_search->enableActiveCheck(true);
333  $user_search->setFields(array('login'));
334  $result_obj = $user_search->performSearch();
335  $all_results->mergeEntries($result_obj);
336 
337  $user_search->setFields(array('firstname'));
338  $result_obj = $user_search->performSearch();
339  $all_results->mergeEntries($result_obj);
340 
341  $user_search->setFields(array('lastname'));
342  $result_obj = $user_search->performSearch();
343  $all_results->mergeEntries($result_obj);
344 
345  $all_results->setMaxHits(100000);
346  $all_results->preventOverwritingMaxhits(true);
347  $all_results->filter(ROOT_FOLDER_ID, true);
348 
349 
350  //$users = $all_results->getResults();
351 
352  // Filter users (depends on setting in user accounts)
353  include_once './Services/User/classes/class.ilUserFilter.php';
354  $users = ilUserFilter::getInstance()->filter($all_results->getResultIds());
355 
356 
357  if (count($users))
358  {
359  $tbl_users = new ilTable2GUI($this);
360  $tbl_users->setTitle($lng->txt('system').': '.$lng->txt('persons'));
361  $tbl_users->setRowTemplate('tpl.mail_search_users_row.html','Services/Contact');
362 
363  $result = array();
364  $counter = 0;
365  foreach ($users as $user)
366  {
367  $login = ilObjUser::_lookupLogin($user);
368 
369  if($_GET["ref"] != "wsp")
370  {
371  $result[$counter]['check'] = ilUtil::formCheckbox(0, 'search_name_to_usr[]', $login) .
372  ilUtil::formCheckbox(0, 'search_name_cc[]', $login) .
373  ilUtil::formCheckbox(0, 'search_name_bcc[]', $login);
374  }
375  else
376  {
377  $result[$counter]['check'] = ilUtil::formCheckbox(0, 'search_name_to_usr[]', $user);
378  }
379  $result[$counter]['login'] = $login;
380 
381  if (in_array(ilObjUser::_lookupPref($user, 'public_profile'), array('y',"g")))
382  {
383  $name = ilObjUser::_lookupName($user);
384  $result[$counter]['firstname'] = $name['firstname'];
385  $result[$counter]['lastname'] = $name['lastname'];
386  }
387  else
388  {
389  $result[$counter]['firstname'] = '';
390  $result[$counter]['lastname'] = '';
391  }
392 
393  if (ilObjUser::_lookupPref($user, 'public_email') == 'y')
394  {
395  $has_mail_usr = true;
396  $result[$counter]['email'] = ilObjUser::_lookupEmail($user);
397  }
398 
399  ++$counter;
400  }
401 
402  if($_GET["ref"] != "wsp")
403  {
404  $tbl_users->addColumn($this->lng->txt('mail_to') . '/' . $this->lng->txt('cc') . '/' . $this->lng->txt('bc'), 'check', '10%');
405  }
406  else
407  {
408  $tbl_users->addColumn("", "", "1%");
409  }
410  $tbl_users->addColumn($this->lng->txt('login'), 'login', '15%');
411  $tbl_users->addColumn($this->lng->txt('firstname'), 'firstname', '15%');
412  $tbl_users->addColumn($this->lng->txt('lastname'), 'lastname', '15%');
413  if ($has_mail_usr == true)
414  {
415  foreach ($result as $key => $val)
416  {
417  if ($val['email'] == '') $result[$key]['email'] = '&nbsp;';
418  }
419 
420  $tbl_users->addColumn($this->lng->txt('email'), 'email', '15%');
421  }
422  $tbl_users->setData($result);
423 
424  $tbl_users->setDefaultOrderField('login');
425  $tbl_users->setPrefix('usr_');
426  $tbl_users->enable('select_all');
427  $tbl_users->setSelectAllCheckbox('search_name_to_usr');
428  $tbl_users->setFormName('recipients');
429 
430  $this->tpl->setVariable('TABLE_USERS', $tbl_users->getHTML());
431  }
432 
433  include_once 'Services/Search/classes/class.ilQueryParser.php';
434  include_once 'Services/Search/classes/class.ilObjectSearchFactory.php';
435  include_once 'Services/Search/classes/class.ilSearchResult.php';
436  include_once 'Services/Membership/classes/class.ilParticipants.php';
437 
438  $group_results = new ilSearchResult();
439 
440  $query_parser = new ilQueryParser(addcslashes($_SESSION['mail_search_search'], '%_'));
441  $query_parser->setCombination(QP_COMBINATION_AND);
442  $query_parser->setMinWordLength(3);
443  $query_parser->parse();
444 
445  $search = ilObjectSearchFactory::_getObjectSearchInstance($query_parser);
446  $search->setFilter(array('grp'));
447  $result = $search->performSearch();
448  $group_results->mergeEntries($result);
449  $group_results->setMaxHits(PHP_INT_MAX);
450  $group_results->preventOverwritingMaxhits(true);
451  $group_results->setRequiredPermission('read');
452  $group_results->filter(ROOT_FOLDER_ID, true);
453 
454  $visible_groups = array();
455  if($group_results->getResults())
456  {
457  $tbl_grp = new ilTable2GUI($this);
458  $tbl_grp->setTitle($lng->txt('system').': '.$lng->txt('groups'));
459  $tbl_grp->setRowTemplate('tpl.mail_search_groups_row.html','Services/Contact');
460 
461  $result = array();
462  $counter = 0;
463 
464  $ilObjDataCache->preloadReferenceCache(array_keys($group_results->getResults()));
465 
466  $groups = $group_results->getResults();
467  foreach ($groups as $grp)
468  {
469  if(!ilParticipants::hasParticipantListAccess($grp['obj_id']))
470  {
471  continue;
472  }
473 
474  if($_GET["ref"] != "wsp")
475  {
476  $members = array();
477  $roles = $rbacreview->getAssignableChildRoles($grp['ref_id']);
478  foreach ($roles as $role)
479  {
480  if (substr($role['title'], 0, 14) == 'il_grp_member_' ||
481  substr($role['title'], 0, 13) == 'il_grp_admin_')
482  {
483  // does not work if Pear is enabled and Mailbox Address contain special chars!!
484  //array_push($members, $rbacreview->getRoleMailboxAddress($role['obj_id']));
485  // FIX for Mantis: 7523
486  array_push($members, '#'.$role['title']);
487  }
488  }
489  $str_members = implode(',',$members);
490 
491  $result[$counter]['check'] = ilUtil::formCheckbox(0, 'search_name_to_grp[]', $str_members) .
492  ilUtil::formCheckbox(0, 'search_name_cc[]', $str_members) .
493  ilUtil::formCheckbox(0, 'search_name_bcc[]',$str_members);
494  }
495  else
496  {
497  $result[$counter]['check'] = ilUtil::formCheckbox(0, 'search_name_to_grp[]', $grp['obj_id']);
498  }
499  $result[$counter]['title'] = $ilObjDataCache->lookupTitle($grp['obj_id']);
500  $result[$counter]['description'] = $ilObjDataCache->lookupDescription($grp['obj_id']);
501  $visible_groups[] = $grp;
502  ++$counter;
503  }
504 
505  if($visible_groups)
506  {
507  $tbl_grp->setData($result);
508 
509  if($_GET["ref"] != "wsp")
510  {
511  $tbl_grp->addColumn($this->lng->txt('mail_to') . '/' . $this->lng->txt('cc') . '/' . $this->lng->txt('bc'), 'check', '10%');
512  }
513  else
514  {
515  $tbl_grp->addColumn("", "", "1%");
516  }
517  $tbl_grp->addColumn($this->lng->txt('title'), 'title', '15%');
518  $tbl_grp->addColumn($this->lng->txt('description'), 'description', '15%');
519 
520  $tbl_grp->setDefaultOrderField('title');
521  $tbl_grp->setPrefix('grp_');
522  $tbl_grp->enable('select_all');
523  $tbl_grp->setSelectAllCheckbox('search_name_to_grp');
524  $tbl_grp->setFormName('recipients');
525 
526  $this->tpl->setVariable('TABLE_GRP', $tbl_grp->getHTML());
527  }
528  }
529  }
530 
531  if (count($users) || count($visible_groups) || count($entries))
532  {
533  $this->tpl->setVariable("IMG_ARROW", ilUtil::getImagePath("arrow_downright.png"));
534  $this->tpl->setVariable("ALT_ARROW", '');
535 
536  if($_GET["ref"] != "wsp")
537  {
538  $this->tpl->setVariable('BUTTON_ADOPT', $this->lng->txt('adopt'));
539  }
540  else
541  {
542  $this->tpl->setVariable('BUTTON_ADOPT', $this->lng->txt('wsp_share_with_users'));
543  }
544  }
545  else if (strlen(trim($_SESSION["mail_search_search"])) >= 3
546  && ($_SESSION['mail_search_type_addressbook'] != NULL || $_SESSION['mail_search_type_system'] != NULL))
547  {
548  $this->lng->loadLanguageModule('search');
549  ilUtil::sendInfo($this->lng->txt('search_no_match'));
550  }
551 
552  if($_GET["ref"] != "wsp")
553  {
554  $this->tpl->show();
555  }
556  }
557 
558  protected function addPermission($a_obj_ids)
559  {
560  if(!is_array($a_obj_ids))
561  {
562  $a_obj_ids = array($a_obj_ids);
563  }
564 
565  $existing = $this->wsp_access_handler->getPermissions($this->wsp_node_id);
566  $added = false;
567  foreach($a_obj_ids as $object_id)
568  {
569  if(!in_array($object_id, $existing))
570  {
571  $added = $this->wsp_access_handler->addPermission($this->wsp_node_id, $object_id);
572  }
573  }
574 
575  if($added)
576  {
577  ilUtil::sendSuccess($this->lng->txt("wsp_share_success"), true);
578  }
579  }
580 }
581 ?>