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