ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMailAddressbookGUI.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 require_once "Services/Mail/classes/class.ilAddressbook.php";
28 require_once "Services/Mail/classes/class.ilAddressbookTableGUI.php";
29 
30 
39 {
40  private $tpl = null;
41  private $ctrl = null;
42  private $lng = null;
43  private $tabs_gui = null;
44 
45  private $umail = null;
46  private $abook = null;
47 
48  public function __construct()
49  {
50  global $tpl, $ilCtrl, $lng, $ilUser, $ilTabs;
51 
52  $this->tpl = $tpl;
53  $this->ctrl = $ilCtrl;
54  $this->lng = $lng;
55  $this->tabs_gui =& $ilTabs;
56 
57  $this->ctrl->saveParameter($this, "mobj_id");
58 
59  $this->umail = new ilFormatMail($ilUser->getId());
60  $this->abook = new ilAddressbook($ilUser->getId());
61  }
62 
63  public function executeCommand()
64  {
65  $this->showSubTabs();
66 
67  $forward_class = $this->ctrl->getNextClass($this);
68  switch($forward_class)
69  {
70  case 'ilmailformgui':
71  include_once 'Services/Mail/classes/class.ilMailFormGUI.php';
72 
73  $this->ctrl->forwardCommand(new ilMailFormGUI());
74  break;
75 
76  case 'ilmailsearchcoursesgui':
77  include_once 'Services/Mail/classes/class.ilMailSearchCoursesGUI.php';
78 
79  $this->tabs_gui->setSubTabActive('mail_my_courses');
80 
81  $this->ctrl->setReturn($this, "showAddressbook");
82  $this->ctrl->forwardCommand(new ilMailSearchCoursesGUI());
83  break;
84 
85  case 'ilmailsearchgroupsgui':
86  include_once 'Services/Mail/classes/class.ilMailSearchGroupsGUI.php';
87 
88  $this->tabs_gui->setSubTabActive('mail_my_groups');
89 
90  $this->ctrl->setReturn($this, "showAddressbook");
91  $this->ctrl->forwardCommand(new ilMailSearchGroupsGUI());
92  break;
93 
94  case 'ilmailinglistsgui':
95  include_once 'Services/Mail/classes/class.ilMailingListsGUI.php';
96 
97  $this->tabs_gui->setSubTabActive('mail_my_mailing_lists');
98 
99  $this->ctrl->setReturn($this, "showAddressbook");
100  $this->ctrl->forwardCommand(new ilMailingListsGUI());
101  break;
102 
103  default:
104  $this->tabs_gui->setSubTabActive('mail_my_entries');
105 
106  if (!($cmd = $this->ctrl->getCmd()))
107  {
108  $cmd = "showAddressbook";
109  }
110 
111  $this->$cmd();
112  break;
113  }
114  return true;
115  }
116 
120  function checkInput($addr_id = 0)
121  {
122  // check if user login and e-mail-address are empty
123  if (!strcmp(trim($_POST["login"]), "") &&
124  !strcmp(trim($_POST["email"]), ""))
125  {
126  ilUtil::sendInfo($this->lng->txt("mail_enter_login_or_email_addr"));
127  $error = true;
128  }
129  else if ($_POST["login"] != "" &&
130  !(ilObjUser::_lookupId(ilUtil::stripSlashes($_POST["login"]))))
131  {
132  ilUtil::sendInfo($this->lng->txt("mail_enter_valid_login"));
133  $error = true;
134  }
135  else if ($_POST["email"] &&
136  !(ilUtil::is_email($_POST["email"])))
137  {
138  ilUtil::sendInfo($this->lng->txt("mail_enter_valid_email_addr"));
139  $error = true;
140  }
141 
142  if (($this->existingEntry = $this->abook->checkEntryByLogin(ilUtil::stripSlashes($_POST["login"]))) > 0 &&
143  (($this->existingEntry != $addr_id && $addr_id > 0) || !$addr_id))
144  {
145  ilUtil::sendInfo($this->lng->txt("mail_entry_exists"));
146  $error = true;
147  }
148 
149  return $error ? false : true;
150  }
151 
155  public function saveEntry()
156  {
157  global $lng;
158 
159  if ($this->checkInput($_GET["addr_id"]))
160  {
161  if ($_GET["addr_id"])
162  {
163  $this->abook->updateEntry(ilUtil::stripSlashes($_GET["addr_id"]),
164  ilUtil::stripSlashes($_POST["login"]),
165  ilUtil::stripSlashes($_POST["firstname"]),
166  ilUtil::stripSlashes($_POST["lastname"]),
167  ilUtil::stripSlashes($_POST["email"]));
168  ilUtil::sendInfo($lng->txt("mail_entry_changed"));
169  }
170  else
171  {
172  $this->abook->addEntry(ilUtil::stripSlashes($_POST["login"]),
173  ilUtil::stripSlashes($_POST["firstname"]),
174  ilUtil::stripSlashes($_POST["lastname"]),
175  ilUtil::stripSlashes($_POST["email"]));
176  ilUtil::sendInfo($lng->txt("mail_entry_added"));
177  }
178 
179  unset($_SESSION['addr_search']);
180 
181  $this->showAddressbook();
182  }
183  else
184  {
185  $this->showAddressForm();
186  }
187 
188  return true;
189  }
190 
194  function confirmDelete()
195  {
196  global $lng;
197 
198  if (!isset($_POST['addr_id']))
199  {
200  ilUtil::sendInfo($this->lng->txt('mail_select_one_entry'));
201  $this->showAddressbook();
202  return true;
203  }
204 
205  include_once("Services/Utilities/classes/class.ilConfirmationGUI.php");
206  $c_gui = new ilConfirmationGUI();
207 
208  // set confirm/cancel commands
209  $c_gui->setFormAction($this->ctrl->getFormAction($this, "performDelete"));
210  $c_gui->setHeaderText($this->lng->txt("mail_sure_delete_entry"));
211  $c_gui->setCancel($this->lng->txt("cancel"), "showAddressbook");
212  $c_gui->setConfirm($this->lng->txt("confirm"), "performDelete");
213 
214  // add items to delete
215  foreach($_POST["addr_id"] as $addr_id)
216  {
217  $entry = $this->abook->getEntry($addr_id);
218  $c_gui->addItem("addr_id[]", $addr_id, $entry["login"] ? $entry["login"] : $entry["email"]);
219  }
220 
221  $this->tpl->setVariable("HEADER", $this->lng->txt("mail"));
222  $this->tpl->addBlockFile("ADM_CONTENT", "adm_content", "tpl.mail_addressbook.html", "Services/Mail");
223  $this->tpl->setVariable('DELETE_CONFIRMATION', $c_gui->getHTML());
224 
225  $this->tpl->show();
226 
227  return true;
228  }
229 
233  function performDelete()
234  {
235  global $lng;
236 
237  if (is_array($_POST['addr_id']))
238  {
239  if ($this->abook->deleteEntries($_POST['addr_id']))
240  {
241  ilUtil::sendInfo($lng->txt("mail_deleted_entry"));
242  }
243  else
244  {
245  ilUtil::sendInfo($lng->txt("mail_delete_error"));
246  }
247  }
248  else
249  {
250  ilUtil::sendInfo($lng->txt("mail_delete_error"));
251  }
252 
253  $this->showAddressbook();
254 
255  return true;
256  }
257 
261  function cancel()
262  {
263  $this->showAddressbook();
264  }
265 
266  public function showAddressForm()
267  {
268  global $rbacsystem, $lng, $ilUser;
269 
270  $this->tpl->setVariable("HEADER", $this->lng->txt("mail"));
271  $this->tpl->addBlockFile("ADM_CONTENT", "adm_content", "tpl.mail_addressbook_form.html", "Services/Mail");
272 
273  include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
274  $form = new ilPropertyFormGUI();
275  $form->setTitle($_GET['addr_id'] ? $lng->txt("mail_edit_entry") : $lng->txt("mail_new_entry"));
276 
277  if ($_GET['addr_id'])
278  {
279  $this->ctrl->setParameter($this, 'addr_id', $_GET['addr_id']);
280  }
281 
282  $entry = $this->abook->getEntry($_GET['addr_id']);
283  $form->setFormAction($this->ctrl->getFormAction($this, "saveEntry"));
284 
285  $formItem = new ilTextInputGUI($this->lng->txt("username"), "login");
286  $formItem->setValue(isset($_POST['login']) ? ilUtil::prepareFormOutput($_POST['login'], true) : ilUtil::prepareFormOutput($entry['login']));
287  $form->addItem($formItem);
288 
289  $formItem = new ilTextInputGUI($this->lng->txt("firstname"), "firstname");
290  $formItem->setValue(isset($_POST['firstname']) ? ilUtil::prepareFormOutput($_POST['firstname'], true) : ilUtil::prepareFormOutput($entry['firstname']));
291  $form->addItem($formItem);
292 
293  $formItem = new ilTextInputGUI($this->lng->txt("lastname"), "lastname");
294  $formItem->setValue(isset($_POST['lastname']) ? ilUtil::prepareFormOutput($_POST['lastname'], true) : ilUtil::prepareFormOutput($entry['lastname']));
295  $form->addItem($formItem);
296 
297  $formItem = new ilTextInputGUI($this->lng->txt("email"), "email");
298  $formItem->setValue(isset($_POST['email']) ? ilUtil::prepareFormOutput($_POST['email'], true) : ilUtil::prepareFormOutput($entry['email']));
299  $form->addItem($formItem);
300 
301  $form->addCommandButton('saveEntry',$this->lng->txt('save'));
302  $form->addCommandButton('cancel',$this->lng->txt('cancel'));
303 
304  $this->tpl->setVariable('FORM', $form->getHTML());
305 
306  $this->tpl->show();
307 
308  return true;
309  }
310 
311  public function mailToUsers()
312  {
313  global $ilUser;
314 
315  if (!isset($_POST['addr_id']))
316  {
317  ilUtil::sendInfo($this->lng->txt('mail_select_one_entry'));
318  $this->showAddressbook();
319  return true;
320  }
321 
322  $mail_data = $this->umail->getSavedData();
323  if(!is_array($mail_data))
324  {
325  $this->umail->savePostData($ilUser->getId(), array(), '', '', '', '', '', '', '', '');
326  }
327 
328  $members = array();
329  foreach ($_POST['addr_id'] as $addr_id)
330  {
331  $entry = $this->abook->getEntry($addr_id);
332 
333  if(strlen($entry['login']) && !$this->umail->doesRecipientStillExists($entry['login'], $mail_data['rcp_to'])) {
334  $members[] = $entry['login'];
335  } else if(strlen($entry['email']) && !$this->umail->doesRecipientStillExists($entry['email'], $mail_data['rcp_to'])) {
336  $members[] = $entry['email'];
337  }
338  }
339 
340  if(count($members))
341  {
342  $mail_data = $this->umail->appendSearchResult($members, 'to');
343  $this->umail->savePostData(
344  $mail_data['user_id'],
345  $mail_data['attachments'],
346  $mail_data['rcp_to'],
347  $mail_data['rcp_cc'],
348  $mail_data['rcp_bcc'],
349  $mail_data['m_type'],
350  $mail_data['m_email'],
351  $mail_data['m_subject'],
352  $mail_data['m_message'],
353  $mail_data['use_placeholders']
354  );
355  }
356 
357  ilUtil::redirect("ilias.php?baseClass=ilMailGUI&type=search_res");
358  }
359 
360  public function search()
361  {
362  $_SESSION['addr_search'] = $_POST['search_qry'];
363 
364  $this->showAddressbook();
365 
366  return true;
367  }
368 
372  public function showAddressbook()
373  {
374  global $rbacsystem, $lng, $ilUser;
375 
376  $this->tpl->setVariable("HEADER", $this->lng->txt("mail"));
377  $this->tpl->addBlockFile("ADM_CONTENT", "adm_content", "tpl.mail_addressbook.html", "Services/Mail");
378 
379  $this->tpl->setVariable('ACTION', $this->ctrl->getFormAction($this, "saveEntry"));
380  $this->tpl->setVariable("TXT_SEARCH_FOR",$this->lng->txt("search_for"));
381  $this->tpl->setVariable("BUTTON_SEARCH",$this->lng->txt("send"));
382  $this->tpl->setVariable("BUTTON_CANCEL",$this->lng->txt("reset"));
383  if (strlen(trim($_SESSION["addr_search"])) > 0)
384  {
385  $this->tpl->setVariable("VALUE_SEARCH_FOR", ilUtil::prepareFormOutput(trim($_SESSION["addr_search"]), true));
386  }
387 
388  $this->ctrl->setParameter($this, "cmd", "post");
389  $tbl = new ilAddressbookTableGUI($this);
390  $tbl->setTitle($lng->txt("mail_addr_entries"));
391  $tbl->setRowTemplate("tpl.mail_addressbook_row.html", "Services/Mail");
392 
393  $tbl->setDefaultOrderField('login');
394 
395  $result = array();
396  $this->abook->setSearchQuery($_SESSION['addr_search']);
397  $entries = $this->abook->getEntries();
398 
399  $tbl->addColumn('', 'check', '10%');
400  $tbl->addColumn($this->lng->txt('login'), 'login', '20%');
401  $tbl->addColumn($this->lng->txt('firstname'), 'firstname', '20%');
402  $tbl->addColumn($this->lng->txt('lastname'), 'lastname', '20%');
403  $tbl->addColumn($this->lng->txt('email'), 'email', '20%');
404  $tbl->addColumn('', 'edit', '10%');
405 
406  if (count($entries))
407  {
408  $tbl->enable('select_all');
409  $tbl->setSelectAllCheckbox('addr_id');
410 
411  $counter = 0;
412  foreach ($entries as $entry)
413  {
414  $result[$counter]['check'] = ilUtil::formCheckbox(0, 'addr_id[]', $entry["addr_id"]);
415 
416  if ($entry["login"] != "")
417  {
418  $this->ctrl->setParameterByClass("ilmailformgui", "type", "address");
419  $this->ctrl->setParameterByClass("ilmailformgui", "rcp", urlencode($entry["login"]));
420  $result[$counter]['login'] = "<a class=\"navigation\" href=\"" . $this->ctrl->getLinkTargetByClass("ilmailformgui") . "\">" . $entry["login"] . "</a>";
421  $this->ctrl->clearParametersByClass("ilmailformgui");
422  }
423 
424  $result[$counter]['firstname'] = $entry["firstname"];
425  $result[$counter]['lastname'] = $entry["lastname"];
426 
427  if ($rbacsystem->checkAccess("smtp_mail", $this->umail->getMailObjectReferenceId()))
428  {
429  $this->ctrl->setParameterByClass("ilmailformgui", "type", "address");
430  $this->ctrl->setParameterByClass("ilmailformgui", "rcp", urlencode($entry["email"]));
431  $result[$counter]['email'] = "<a class=\"navigation\" href=\"" . $this->ctrl->getLinkTargetByClass("ilmailformgui") . "\">" . $entry["email"] . "</a>";
432  $this->ctrl->clearParametersByClass("ilmailformgui");
433  }
434  else
435  {
436  $result[$counter]['email'] = $entry["email"];
437  }
438 
439  $this->ctrl->setParameter($this, 'addr_id', $entry['addr_id']);
440  $result[$counter]['edit_text'] = $this->lng->txt("edit");
441  $result[$counter]['edit_url'] = $this->ctrl->getLinkTarget($this, "showAddressForm");
442 
443  ++$counter;
444  }
445 
446  $tbl->addMultiCommand('mailToUsers', $this->lng->txt('send_mail_to'));
447  $tbl->addMultiCommand('confirmDelete', $this->lng->txt('delete'));
448  }
449  else
450  {
451  $tbl->disable('header');
452  $tbl->disable('footer');
453 
454  $tbl->setNoEntriesText($this->lng->txt('mail_search_no'));
455  }
456 
457  $tbl->setData($result);
458 
459  $tbl->addCommandButton('showAddressForm', $this->lng->txt('add'));
460 
461  $this->tpl->setVariable('TABLE', $tbl->getHTML());
462 
463  $this->tpl->show();
464 
465  return true;
466  }
467 
468  function showSubTabs()
469  {
470  $this->tabs_gui->addSubTabTarget('mail_my_entries',
471  $this->ctrl->getLinkTarget($this));
472  $this->tabs_gui->addSubTabTarget('mail_my_mailing_lists',
473  $this->ctrl->getLinkTargetByClass('ilmailinglistsgui'));
474  $this->tabs_gui->addSubTabTarget('mail_my_courses',
475  $this->ctrl->getLinkTargetByClass('ilmailsearchcoursesgui'));
476  $this->tabs_gui->addSubTabTarget('mail_my_groups',
477  $this->ctrl->getLinkTargetByClass('ilmailsearchgroupsgui'));
478  }
479 }
480 ?>