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