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