ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMailSearchGroupsGUI.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 require_once './Services/User/classes/class.ilObjUser.php';
6 require_once 'Services/Mail/classes/class.ilMailbox.php';
7 require_once 'Services/Mail/classes/class.ilFormatMail.php';
8 require_once 'Services/Contact/classes/class.ilAddressbook.php';
9 
17 {
18  private $tpl = null;
19  private $ctrl = null;
20  private $lng = null;
21 
22  private $umail = null;
23  private $abook = null;
24 
25  protected $mailing_allowed;
26 
27  public function __construct($wsp_access_handler = null, $wsp_node_id = null)
28  {
29  global $tpl, $ilCtrl, $lng, $ilUser, $rbacsystem;
30 
31  $this->tpl = $tpl;
32  $this->ctrl = $ilCtrl;
33  $this->lng = $lng;
34 
35  // personal workspace
36  $this->wsp_access_handler = $wsp_access_handler;
37  $this->wsp_node_id = $wsp_node_id;
38 
39  $this->ctrl->saveParameter($this, "mobj_id");
40  $this->ctrl->saveParameter($this, "ref");
41 
42  // check if current user may send mails
43  include_once "Services/Mail/classes/class.ilMail.php";
44  $mail = new ilMail($_SESSION["AccountId"]);
45  $this->mailing_allowed = $rbacsystem->checkAccess('mail_visible',$mail->getMailObjectReferenceId());
46 
47  $this->umail = new ilFormatMail($ilUser->getId());
48  $this->abook = new ilAddressbook($ilUser->getId());
49  }
50 
51  public function executeCommand()
52  {
53  $forward_class = $this->ctrl->getNextClass($this);
54  switch($forward_class)
55  {
56  default:
57  if (!($cmd = $this->ctrl->getCmd()))
58  {
59  $cmd = "showMyGroups";
60  }
61 
62  $this->$cmd();
63  break;
64  }
65  return true;
66  }
67 
68  function mail()
69  {
70  global $ilUser, $lng;
71 
72  if ($_GET["view"] == "mygroups")
73  {
74  $ids = ((int) $_GET['search_grp']) ? array((int)$_GET['search_grp']) : $_POST['search_grp'];
75  if ($ids)
76  {
77  $this->mailGroups();
78  }
79  else
80  {
81  ilUtil::sendInfo($lng->txt("mail_select_group"));
82  $this->showMyGroups();
83  }
84  }
85  else if ($_GET["view"] == "grp_members")
86  {
87  $ids = ((int) $_GET['search_members']) ? array((int)$_GET['search_members']) : $_POST['search_members'];
88  if ($ids)
89  {
90  $this->mailMembers();
91  }
92  else
93  {
94  ilUtil::sendInfo($lng->txt("mail_select_one_entry"));
95  $this->showMembers();
96  }
97  }
98  else
99  {
100  $this->showMyGroups();
101  }
102  }
103 
104  function mailGroups()
105  {
106  global $ilUser, $lng, $rbacreview;
107 
108  $members = array();
109 
110  if (!is_array($old_mail_data = $this->umail->getSavedData()))
111  {
112  $this->umail->savePostData(
113  $ilUser->getId(),
114  array(),
115  "",
116  "",
117  "",
118  "",
119  "",
120  "",
121  "",
122  ""
123  );
124  }
125 
126  require_once 'classes/class.ilObject.php';
127  $ids = ((int) $_GET['search_grp']) ? array((int)$_GET['search_grp']) : $_POST['search_grp'];
128  foreach ($ids as $grp_id)
129  {
130  $ref_ids = ilObject::_getAllReferences($grp_id);
131  foreach ($ref_ids as $ref_id)
132  {
133  $roles = $rbacreview->getAssignableChildRoles($ref_id);
134  foreach ($roles as $role)
135  {
136  if (substr($role['title'], 0, 14) == 'il_grp_member_' ||
137  substr($role['title'], 0, 13) == 'il_grp_admin_')
138  {
139  if(isset($old_mail_data['rcp_to']) &&
140  trim($old_mail_data['rcp_to']) != '')
141  {
142  $rcpt = $rbacreview->getRoleMailboxAddress($role['obj_id']);
143 
144  if(!$this->umail->doesRecipientStillExists($rcpt, $old_mail_data['rcp_to']))
145  // does not work if Pear is enabled and Mailbox Address contain special chars!!
146 // array_push($members, $rcpt);
147  // FIX for Mantis: 7523, 8061
148  array_push($members, '#'.$role['title']);
149 
150  unset($rcpt);
151  }
152  else
153  {
154  // does not work if Pear is enabled and Mailbox Address contain special chars!!
155 // array_push($members, $rbacreview->getRoleMailboxAddress($role['obj_id']));
156  // FIX for Mantis: 7523, 8061
157  array_push($members, '#'.$role['title']);
158  }
159  }
160  }
161  }
162  }
163 
164  if(count($members))
165  $mail_data = $this->umail->appendSearchResult($members, 'to');
166  else
167  $mail_data = $this->umail->getSavedData();
168 
169  $this->umail->savePostData(
170  $mail_data["user_id"],
171  $mail_data["attachments"],
172  $mail_data["rcp_to"],
173  $mail_data["rcp_cc"],
174  $mail_data["rcp_bcc"],
175  $mail_data["m_type"],
176  $mail_data["m_email"],
177  $mail_data["m_subject"],
178  $mail_data["m_message"],
179  $mail_data["use_placeholders"]
180  );
181 
182  ilUtil::redirect("ilias.php?baseClass=ilMailGUI&type=search_res");
183  }
184 
185  function mailMembers()
186  {
187  global $ilUser;
188 
189  $members = array();
190 
191  if (!is_array($this->umail->getSavedData()))
192  {
193  $this->umail->savePostData(
194  $ilUser->getId(),
195  array(),
196  "",
197  "",
198  "",
199  "",
200  "",
201  "",
202  "",
203  ""
204  );
205  }
206 
207  $ids = ((int) $_GET['search_members']) ? array((int)$_GET['search_members']) : $_POST['search_members'];
208 
209  foreach ($ids as $member)
210  {
211  $login = ilObjUser::_lookupLogin($member);
212  array_push($members, $login);
213  }
214  $mail_data = $this->umail->appendSearchResult($members,"to");
215 
216  $this->umail->savePostData(
217  $mail_data["user_id"],
218  $mail_data["attachments"],
219  $mail_data["rcp_to"],
220  $mail_data["rcp_cc"],
221  $mail_data["rcp_bcc"],
222  $mail_data["m_type"],
223  $mail_data["m_email"],
224  $mail_data["m_subject"],
225  $mail_data["m_message"],
226  $mail_data["use_placeholders"]
227  );
228 
229  ilUtil::redirect("ilias.php?baseClass=ilMailGUI&type=search_res");
230  }
231 
235  public function adoptMembers()
236  {
237  global $lng;
238 
239  $ids = ((int)$_GET['search_members']) ? array((int)$_GET['search_members']) : $_POST['search_members'];
240 
241  if ($ids )
242  {
243  $members = array();
244 
245  foreach ($ids as $member)
246  {
247  $login = ilObjUser::_lookupLogin($member);
248 
249  if (!$this->abook->checkEntry($login))
250  {
251  $name = ilObjUser::_lookupName($member);
252  $email = '';
253  if(ilObjUser::_lookupPref((int)$member, 'public_email') == 'y')
254  {
255  $email = ilObjUser::_lookupEmail($member);
256  }
257  $this->abook->addEntry(
258  $login,
259  $name["firstname"],
260  $name["lastname"],
261  $email
262  );
263  }
264  }
265  ilUtil::sendInfo($lng->txt("mail_members_added_addressbook"));
266  }
267  else
268  {
269  ilUtil::sendInfo($lng->txt("mail_select_one_entry"));
270  }
271 
272  $this->showMembers();
273  }
274 
278  function cancel()
279  {
280  if ($_GET["view"] == "mygroups" &&
281  $_GET["ref"] == "mail")
282  {
283  $this->ctrl->returnToParent($this);
284  }
285  else
286  {
287  $this->showMyGroups();
288  }
289  }
290 
294  public function showMyGroups()
295  {
296  global $lng, $ilUser, $ilObjDataCache, $tree;
297 
298  include_once 'Modules/Group/classes/class.ilGroupParticipants.php';
299 
300  $this->tpl->setTitle($this->lng->txt('mail'));
301 
302  $searchTpl = new ilTemplate('tpl.mail_search_template.html', true, true, 'Services/Contact');
303 
304  $_GET['view'] = 'mygroups';
305 
306  $lng->loadLanguageModule('crs');
307 
308  $this->ctrl->setParameter($this, 'view', 'mygroups');
309 
310  include_once 'Services/Contact/classes/class.ilMailSearchCoursesTableGUI.php';
311  $table = new ilMailSearchCoursesTableGUI($this, 'grp', $_GET["ref"]);
312  $table->setId('search_grps_tbl');
313  $grp_ids = ilGroupParticipants::_getMembershipByType($ilUser->getId(), 'grp');
314 
315  $counter = 0;
316  $tableData = array();
317  if (is_array($grp_ids) &&
318  count($grp_ids) > 0)
319  {
320 
321  include_once("./Services/UIComponent/AdvancedSelectionList/classes/class.ilAdvancedSelectionListGUI.php");
322  foreach($grp_ids as $grp_id)
323  {
325  {
326  $oGroupParticipants = ilGroupParticipants::_getInstanceByObjId($grp_id);
327  $grp_members = $oGroupParticipants->getParticipants();
328 
329  foreach ($grp_members as $key => $member)
330  {
331  $tmp_usr = new ilObjUser($member);
332 
333  if($tmp_usr->checkTimeLimit()== false || $tmp_usr->getActive() == false )
334  {
335  unset($grp_members[$key]);
336  }
337  }
338  unset($tmp_usr);
339 
340  $ref_ids = ilObject::_getAllReferences($grp_id);
341  $ref_id = current($ref_ids);
342  $path_arr = $tree->getPathFull($ref_id, $tree->getRootId());
343  $path_counter = 0;
344  $path = '';
345  foreach($path_arr as $data)
346  {
347  if($path_counter++)
348  {
349  $path .= " -> ";
350  }
351  $path .= $data['title'];
352  }
353  $path = $this->lng->txt('path').': '.$path;
354 
355  $current_selection_list = new ilAdvancedSelectionListGUI();
356  $current_selection_list->setListTitle($this->lng->txt("actions"));
357  $current_selection_list->setId("act_".$counter);
358 
359  $this->ctrl->setParameter($this, 'search_grp', $grp_id);
360  $this->ctrl->setParameter($this, 'view', 'mygroups');
361 
362  if($_GET["ref"] == "mail")
363  {
364  if ($this->mailing_allowed)
365  $current_selection_list->addItem($this->lng->txt("mail_members"), '', $this->ctrl->getLinkTarget($this, "mail"));
366  }
367  else if($_GET["ref"] == "wsp")
368  {
369  $current_selection_list->addItem($this->lng->txt("wsp_share_with_members"), '', $this->ctrl->getLinkTarget($this, "share"));
370  }
371  $current_selection_list->addItem($this->lng->txt("mail_list_members"), '', $this->ctrl->getLinkTarget($this, "showMembers"));
372 
373  $this->ctrl->clearParameters($this);
374 
375  $rowData = array
376  (
377  'CRS_ID' => $grp_id,
378  'CRS_NAME' => $ilObjDataCache->lookupTitle($grp_id),
379  'CRS_NO_MEMBERS' => count($grp_members),
380  'CRS_PATH' => $path,
381  'COMMAND_SELECTION_LIST' => $current_selection_list->getHTML()
382  );
383  $counter++;
384  $tableData[] = $rowData;
385  }
386  }
387  }
388  $table->setData($tableData);
389  if($counter > 0)
390  {
391  $this->tpl->setVariable('TXT_MARKED_ENTRIES',$lng->txt('marked_entries'));
392  }
393 
394  $searchTpl->setVariable('TABLE', $table->getHtml());
395  $this->tpl->setContent($searchTpl->get());
396 
397  if($_GET["ref"] != "wsp")
398  {
399  $this->tpl->show();
400  }
401  }
402 
406  public function showMembers()
407  {
408  global $lng, $ilUser;
409 
410  if ($_GET["search_grp"] != "")
411  {
412  $_POST["search_grp"] = explode(",", $_GET["search_grp"]);
413  }
414 
415  if (!is_array($_POST["search_grp"]) ||
416  count($_POST["search_grp"]) == 0)
417  {
418  ilUtil::sendInfo($lng->txt("mail_select_group"));
419  $this->showMyGroups();
420  }
421  else
422  {
423  $this->tpl->setTitle($this->lng->txt("mail"));
424  include_once 'Services/Contact/classes/class.ilMailSearchCoursesMembersTableGUI.php';
425  $context = $_GET["ref"] ? $_GET["ref"] : "mail";
426  $table = new ilMailSearchCoursesMembersTableGUI($this, 'grp', $context);
427  $table->setId('show_grps_mmbrs_tbl');
428  $lng->loadLanguageModule('crs');
429 
430  $counter = 0;
431  $tableData = array();
432  $searchTpl = new ilTemplate('tpl.mail_search_template.html', true, true, 'Services/Contact');
433 
434  foreach($_POST["search_grp"] as $grp_id)
435  {
436  $ref_ids = ilObject::_getAllReferences($grp_id);
437  $ref_id = current($ref_ids);
438 
439  if (is_object($group_obj = ilObjectFactory::getInstanceByRefId($ref_id,false)))
440  {
441  $grp_members = $group_obj->getGroupMemberData($group_obj->getGroupMemberIds());
442 
443  foreach($grp_members as $member)
444  {
445  $tmp_usr = new ilObjUser($member['id']);
446  if($tmp_usr->checkTimeLimit()== false || $tmp_usr->getActive() == false )
447  {
448  unset($tmp_usr);
449  continue;
450  }
451  unset($tmp_usr);
452 
453  $fullname = "";
454  if(in_array(ilObjUser::_lookupPref($member['id'], 'public_profile'), array("g", 'y')))
455  $fullname = $member['lastname'].', '.$member['firstname'];
456 
457  $rowData = array(
458  'MEMBERS_ID' => $member["id"],
459  'MEMBERS_LOGIN' => $member["login"],
460  'MEMBERS_NAME' => $fullname,
461  'MEMBERS_CRS_GRP' => $group_obj->getTitle(),
462  'MEMBERS_IN_ADDRESSBOOK' => $this->abook->checkEntryByLogin($member["login"]) ? $lng->txt("yes") : $lng->txt("no"),
463  'search_grp' => $grp_id
464  );
465  $tableData[] = $rowData;
466  }
467  }
468  }
469  $table->setData($tableData);
470  if (count($tableData))
471  {
472  //$table->addCommandButton('mail', $lng->txt("grp_mem_send_mail"));
473  //$table->addCommandButton('adoptMembers', $lng->txt("mail_into_addressbook"));
474  $searchTpl->setVariable("TXT_MARKED_ENTRIES",$lng->txt("marked_entries"));
475  }
476  $searchTpl->setVariable('TABLE', $table->getHtml());
477  $this->tpl->setContent($searchTpl->get());
478 
479  if($_GET["ref"] != "wsp")
480  {
481  $this->tpl->show();
482  }
483  }
484  }
485 
486  function share()
487  {
488  global $lng;
489 
490  if ($_GET["view"] == "mygroups")
491  {
492  $ids = $_REQUEST["search_grp"];
493  if (sizeof($ids))
494  {
495  $this->addPermission($ids);
496  }
497  else
498  {
499  ilUtil::sendInfo($lng->txt("mail_select_course"));
500  $this->showMyGroups();
501  }
502  }
503  else if ($_GET["view"] == "grp_members")
504  {
505  $ids = $_REQUEST["search_members"];
506  if (sizeof($ids))
507  {
508  $this->addPermission($ids);
509  }
510  else
511  {
512  ilUtil::sendInfo($lng->txt("mail_select_one_entry"));
513  $this->showMembers();
514  }
515  }
516  else
517  {
518  $this->showMyGroups();
519  }
520  }
521 
522  protected function addPermission($a_obj_ids)
523  {
524  if(!is_array($a_obj_ids))
525  {
526  $a_obj_ids = array($a_obj_ids);
527  }
528 
529  $existing = $this->wsp_access_handler->getPermissions($this->wsp_node_id);
530  $added = false;
531  foreach($a_obj_ids as $object_id)
532  {
533  if(!in_array($object_id, $existing))
534  {
535  $added = $this->wsp_access_handler->addPermission($this->wsp_node_id, $object_id);
536  }
537  }
538 
539  if($added)
540  {
541  ilUtil::sendSuccess($this->lng->txt("wsp_share_success"), true);
542  }
543  $this->ctrl->redirectByClass("ilworkspaceaccessgui", "share");
544  }
545 }
546 
547 ?>