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