ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMailFolderTableGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'Services/Table/classes/class.ilTable2GUI.php';
5 require_once 'Services/Mail/classes/class.ilMailUserCache.php';
6 require_once 'Services/Mail/classes/class.ilMailBoxQuery.php';
7 
15 {
16  protected $lng = null;
17  protected $ctrl = null;
18 
19  protected $_folderNode = array();
20  protected $_parentObject = null;
21  protected $_currentFolderId = 0;
22  protected $_number_of_mails = 0;
23  protected $_selectedItems = array();
24  protected $_isTrashFolder = false;
25  protected $_isDraftsFolder = false;
26  protected $_isSentFolder = false;
27 
31  protected $filter = array();
32 
36  protected $sub_filter = array();
37 
46  public function __construct($a_parent_obj, $a_current_folder_id, $a_parent_cmd = '')
47  {
52  global $lng, $ilCtrl;
53 
54  $this->lng = $lng;
55  $this->ctrl = $ilCtrl;
56 
57  $this->_currentFolderId = $a_current_folder_id;
58  $this->_parentObject = $a_parent_obj;
59 
60  $this->setId('mail_folder_tbl_'.$a_current_folder_id);
61  $this->setPrefix('mtable');
62 
63  $this->setExternalSorting(true);
64  $this->setExternalSegmentation(true);
65  $this->setDefaultOrderField('send_time');
66  $this->setDefaultOrderDirection('desc');
67 
68  parent::__construct($a_parent_obj, $a_parent_cmd);
69 
70  $this->setFormAction($this->ctrl->getFormAction($this->_parentObject, 'showFolder'));
71 
72  $this->setEnableTitle(true);
73  $this->setSelectAllCheckbox('mail_id[]');
74  $this->setRowTemplate('tpl.mail_folder_row.html', 'Services/Mail');
75 
76  $this->setFilterCommand('applyFilter');
77  $this->setResetCommand('resetFilter');
78  }
79 
87  final public function prepareHTML()
88  {
89  global $ilUser;
90 
91  $this->addColumn('', '', '1px', true);
92  $this->addColumn($this->lng->txt('personal_picture'), '', '10%');
93  if($this->isDraftFolder() || $this->isSentFolder())
94  $this->addColumn($this->lng->txt('recipient'), 'rcp_to', '25%');
95  else
96  $this->addColumn($this->lng->txt('sender'), 'from', '25%');
97 
98  if($this->isLuceneSearchEnabled())
99  {
100  $this->addColumn($this->lng->txt('search_content'), '', '40%');
101  }
102  else
103  {
104  $this->addColumn($this->lng->txt('subject'), 'm_subject', '40%');
105  }
106  $this->addColumn($this->lng->txt('date'), 'send_time', '20%');
107 
108  // init folder data
109  $mtree = new ilTree($ilUser->getId());
110  $mtree->setTableNames('mail_tree', 'mail_obj_data');
111  $this->_folderNode = $mtree->getNodeData($this->_currentFolderId);
112 
113  // fetch table data
114  $this->fetchTableData();
115 
116  // command buttons
117  $this->initCommandButtons();
118 
119  // mail actions
120  $this->initMultiCommands($this->_parentObject->mbox->getActions($this->_currentFolderId));
121 
122  return $this;
123  }
124 
132  public function isDraftFolder($a_bool = null)
133  {
134  if(null === $a_bool)
135  {
136  return $this->_isDraftsFolder;
137  }
138 
139  $this->_isDraftsFolder = $a_bool;
140 
141  return $this;
142  }
143 
151  public function isSentFolder($a_bool = null)
152  {
153  if(null === $a_bool)
154  {
155  return $this->_isSentFolder;
156  }
157 
158  $this->_isSentFolder = $a_bool;
159 
160  return $this;
161  }
162 
170  public function isTrashFolder($a_bool = null)
171  {
172  if(null === $a_bool)
173  {
174  return $this->_isTrashFolder;
175  }
176 
177  $this->_isTrashFolder = $a_bool;
178 
179  return $this;
180  }
181 
189  private function initCommandButtons()
190  {
191  if($this->_folderNode['m_type'] == 'trash' && $this->getNumerOfMails() > 0)
192  {
193  $this->addCommandButton('askForEmptyTrash', $this->lng->txt('mail_empty_trash'));
194  }
195 
196  return $this;
197  }
198 
206  private function initMultiCommands($actions)
207  {
208  foreach($actions as $key => $action)
209  {
210  if($key == 'moveMails')
211  {
212  $folders = $this->_parentObject->mbox->getSubFolders();
213  foreach($folders as $folder)
214  {
215  if($folder['type'] != 'trash' ||
216  !$this->isTrashFolder()
217  )
218  {
219  if($folder['type'] != 'user_folder')
220  {
221  $label = $action . ' ' . $this->lng->txt('mail_' . $folder['title']) .
222  ($folder['type'] == 'trash' ? ' (' . $this->lng->txt('delete') . ')' : '');
223  $this->addMultiCommand($folder['obj_id'], $label);
224  }
225  else
226  $this->addMultiCommand($folder['obj_id'], $action . ' ' . $folder['title']);
227  }
228  }
229  }
230  else
231  {
232  if($key != 'deleteMails' || $this->isTrashFolder())
233  $this->addMultiCommand($key, $action);
234  }
235  }
236 
237  return $this;
238  }
239 
247  public function setSelectedItems($a_selected_items)
248  {
249  $this->_selectedItems = $a_selected_items;
250 
251  return $this;
252  }
253 
260  public function getSelectedItems()
261  {
262  return $this->_selectedItems;
263  }
264 
268  protected function isLuceneSearchEnabled()
269  {
270  include_once 'Services/Search/classes/class.ilSearchSettings.php';
271  if(ilSearchSettings::getInstance()->enabledLucene() && strlen($this->filter['mail_filter']))
272  {
273  return true;
274  }
275  }
276 
283  protected function fetchTableData()
284  {
288  global $ilUser;
289 
290  // table title
291  if($this->_folderNode['m_type'] == 'user_folder')
292  {
293  $txt_folder = $this->_folderNode['title'];
294  $img_folder = 'icon_user_folder.png';
295  }
296  else
297  {
298  $txt_folder = $this->lng->txt('mail_' . $this->_folderNode['title']);
299  $img_folder = 'icon' . substr($this->_folderNode['title'], 1) . '.png';
300  }
301 
302  try
303  {
304  if($this->isLuceneSearchEnabled())
305  {
306  include_once 'Services/Mail/classes/class.ilMailLuceneQueryParser.php';
307  $query_parser = new ilMailLuceneQueryParser($this->filter['mail_filter']);
308  $query_parser->setFields(array(
309  'title' => (bool)$this->filter['mail_filter_subject'],
310  'content' => (bool)$this->filter['mail_filter_body'],
311  'mattachment' => (bool)$this->filter['mail_filter_attach'],
312  'msender' => (bool)$this->filter['mail_filter_sender'],
313  'mrcp' => (bool)$this->filter['mail_filter_recipients']
314  ));
315  $query_parser->parse();
316 
317  require_once 'Services/Mail/classes/class.ilMailLuceneSearcher.php';
318  require_once 'Services/Mail/classes/class.ilMailSearchResult.php';
319  $result = new ilMailSearchResult();
320  $searcher = new ilMailLuceneSearcher($query_parser, $result);
321  $searcher->search($ilUser->getId(), $this->_currentFolderId);
322 
323  if(!$result->getIds())
324  {
325  throw new ilException('mail_search_empty_result');
326  }
327 
329  ilMailBoxQuery::$filter = array();
330  }
331  else
332  {
333  ilMailBoxQuery::$filter = (array)$this->filter;
334  }
335 
336  $this->determineOffsetAndOrder();
337 
339  ilMailBoxQuery::$userId = $ilUser->getId();
340  ilMailBoxQuery::$limit = $this->getLimit();
344  $data = ilMailBoxQuery::_getMailBoxListData();
345 
346  if(!count($data['set']) && $this->getOffset() > 0)
347  {
348  $this->resetOffset();
349 
350  ilMailBoxQuery::$limit = $this->getLimit();
352  $data = ilMailBoxQuery::_getMailBoxListData();
353  }
354  }
355  catch(Exception $e)
356  {
357  $this->setTitleData($txt_folder, 0, 0, $img_folder);
358 
359  if('mail_search_empty_result' == $e->getMessage())
360  {
361  $data['set'] = array();
362  $data['cnt'] = 0;
363  }
364  else
365  {
366  throw $e;
367  }
368  }
369 
370  if(!$this->isDraftFolder() && !$this->isSentFolder())
371  {
372  $user_ids = array();
373  foreach($data['set'] as $mail)
374  {
375  if($mail['sender_id'] && $mail['sender_id'] != ANONYMOUS_USER_ID)
376  {
377  $user_ids[$mail['sender_id']] = $mail['sender_id'];
378  }
379  }
380 
381  ilMailUserCache::preloadUserObjects($user_ids);
382  }
383 
384  $counter = 0;
385  foreach($data['set'] as $key => $mail)
386  {
387  ++$counter;
388 
389  if(is_array($this->getSelectedItems()) && in_array($mail['mail_id'], $this->getSelectedItems()))
390  {
391  $mail['checked'] = ' checked="checked" ';
392  }
393 
394  if($this->isDraftFolder() || $this->isSentFolder())
395  {
396  $mail['rcp_to'] = $mail['mail_login'] = $this->_parentObject->umail->formatNamesForOutput($mail['rcp_to']);
397  }
398  else
399  {
400  if($mail['sender_id'] == ANONYMOUS_USER_ID)
401  {
402  $mail['img_sender'] = ilUtil::getImagePath('HeaderIconAvatar.svg');
403  $mail['from'] = $mail['mail_login'] = $mail['alt_sender'] = ilMail::_getIliasMailerName();
404  }
405  else
406  {
407  $user = ilMailUserCache::getUserObjectById($mail['sender_id']);
408  if($user)
409  {
410  $mail['img_sender'] = $user->getPersonalPicturePath('xxsmall');
411  $mail['from'] = $mail['mail_login'] = $mail['alt_sender'] = $user->getPublicName();
412  }
413  else
414  {
415  $mail['from'] = $mail['mail_login'] = $mail['import_name'] . ' (' . $this->lng->txt('user_deleted') . ')';
416  }
417  }
418  }
419 
420  if($this->isDraftFolder())
421  {
422  $this->ctrl->setParameterByClass('ilmailformgui', 'mail_id', $mail['mail_id']);
423  $this->ctrl->setParameterByClass('ilmailformgui', 'type', 'draft');
424  $link_mark_as_read = $this->ctrl->getLinkTargetByClass('ilmailformgui');
425  $this->ctrl->clearParametersByClass('ilmailformgui');
426  }
427  else
428  {
429  $this->ctrl->setParameter($this->_parentObject, 'mail_id', $mail['mail_id']);
430  $link_mark_as_read = $this->ctrl->getLinkTarget($this->_parentObject, 'showMail');
431  $this->ctrl->clearParameters($this->_parentObject);
432  }
433  $css_class = $mail['m_status'] == 'read' ? 'mailread' : 'mailunread';
434 
435  if($this->isLuceneSearchEnabled())
436  {
437  $search_result = array();
438  foreach($result->getFields($mail['mail_id']) as $content)
439  {
440  if('title' == $content[0])
441  {
442  $mail['msr_subject_link_read'] = $link_mark_as_read;
443  $mail['msr_subject_mailclass'] = $css_class;
444  $mail['msr_subject'] = $content[1];
445  }
446  else
447  {
448  $search_result[] = $content[1];
449  }
450  }
451  $mail['msr_data'] = implode('', array_map(function($value) {
452  return '<p>'.$value.'</p>';
453  }, $search_result));
454 
455  if(!$mail['msr_subject'])
456  {
457  $mail['msr_subject_link_read'] = $link_mark_as_read;
458  $mail['msr_subject_mailclass'] = $css_class;
459  $mail['msr_subject'] = htmlspecialchars($mail['m_subject']);
460  }
461  }
462  else
463  {
464  $mail['mail_link_read'] = $link_mark_as_read;
465  $mail['mailclass'] = $css_class;
466  $mail['mail_subject'] = htmlspecialchars($mail['m_subject']);
467  }
468 
469  $mail['mail_date'] = ilDatePresentation::formatDate(new ilDateTime($mail['send_time'], IL_CAL_DATETIME));
470 
471  $data['set'][$key] = $mail;
472  }
473 
474  $this->setData($data['set']);
475  $this->setMaxCount($data['cnt']);
476  $this->setNumerOfMails($data['cnt']);
477 
478  $this->setTitleData($txt_folder, $data['cnt'], $data['cnt_unread'], $img_folder);
479 
480  return $this;
481  }
482 
493  protected function setTitleData($folderLabel, $mailCount, $unreadCount, $imgFolder)
494  {
495  $titleTemplate = new ilTemplate('tpl.mail_folder_title.html', true, true, 'Services/Mail');
496  $titleTemplate->setVariable('TXT_FOLDER', $folderLabel);
497  $titleTemplate->setVariable('MAIL_COUNT', $mailCount);
498  $titleTemplate->setVariable('TXT_MAIL_S', $this->lng->txt('mail_s'));
499  $titleTemplate->setVariable('MAIL_COUNT_UNREAD', $unreadCount);
500  $titleTemplate->setVariable('TXT_UNREAD', $this->lng->txt('unread'));
501 
502  parent::setTitle($titleTemplate->get(), $imgFolder);
503 
504  return $this;
505  }
506 
514  public function setNumerOfMails($a_number_of_mails)
515  {
516  $this->_number_of_mails = $a_number_of_mails;
517 
518  return $this;
519  }
520 
527  public function getNumerOfMails()
528  {
530  }
531 
538  public function fillRow($a_set)
539  {
540  foreach($a_set as $key => $value)
541  {
542  $this->tpl->setVariable(strtoupper($key), $value);
543  }
544  }
545 
546  public function initFilter()
547  {
548  $this->filter = array();
549 
550  include_once 'Services/Mail/classes/Form/class.ilMailQuickFilterInputGUI.php';
551  $ti = new ilMailQuickFilterInputGUI($this->lng->txt('mail_filter'), 'mail_filter');
552  $ti->setSubmitFormOnEnter(false);
553  $this->addFilterItem($ti);
554  $ti->readFromSession();
555  $this->filter['mail_filter'] = $ti->getValue();
556 
557  include_once 'Services/Form/classes/class.ilCheckboxInputGUI.php';
558 
559  if($this->isDraftFolder() || $this->isSentFolder())
560  {
561  $this->sub_filter[] = $ci = new ilCheckboxInputGUI($this->lng->txt('mail_filter_recipients'), 'mail_filter_recipients');
562  $ci->setOptionTitle($this->lng->txt('mail_filter_recipients'));
563  $ci->setValue(1);
564  $ti->addSubItem($ci);
565  $ci->setParent($this);
566  $ci->readFromSession();
567  $this->filter['mail_filter_recipients'] = (int)$ci->getChecked();
568  }
569  else
570  {
571  $this->sub_filter[] = $ci = new ilCheckboxInputGUI($this->lng->txt('mail_filter_sender'), 'mail_filter_sender');
572  $ci->setOptionTitle($this->lng->txt('mail_filter_sender'));
573  $ci->setValue(1);
574  $ti->addSubItem($ci);
575  $ci->setParent($this);
576  $ci->readFromSession();
577  $this->filter['mail_filter_sender'] = (int)$ci->getChecked();
578  }
579 
580  $this->sub_filter[] = $ci = new ilCheckboxInputGUI($this->lng->txt('mail_filter_subject'), 'mail_filter_subject');
581  $ci->setOptionTitle($this->lng->txt('mail_filter_subject'));
582  $ci->setValue(1);
583  $ti->addSubItem($ci);
584  $ci->setParent($this);
585  $ci->readFromSession();
586  $this->filter['mail_filter_subject'] = (int)$ci->getChecked();
587 
588  $this->sub_filter[] = $ci = new ilCheckboxInputGUI($this->lng->txt('mail_filter_body'), 'mail_filter_body');
589  $ci->setOptionTitle($this->lng->txt('mail_filter_body'));
590  $ci->setValue(1);
591  $ti->addSubItem($ci);
592  $ci->setParent($this);
593  $ci->readFromSession();
594  $this->filter['mail_filter_body'] = (int)$ci->getChecked();
595 
596  $this->sub_filter[] = $ci = new ilCheckboxInputGUI($this->lng->txt('mail_filter_attach'), 'mail_filter_attach');
597  $ci->setOptionTitle($this->lng->txt('mail_filter_attach'));
598  $ci->setValue(1);
599  $ti->addSubItem($ci);
600  $ci->setParent($this);
601  $ci->readFromSession();
602  $this->filter['mail_filter_attach'] = (int)$ci->getChecked();
603  }
604 
608  public function writeFilterToSession()
609  {
611 
612  foreach($this->sub_filter as $item)
613  {
614  if($item->checkInput())
615  {
616  $item->setValueByArray($_POST);
617  $item->writeToSession();
618  }
619  }
620  }
621 
625  public function resetFilter()
626  {
628 
629  foreach($this->sub_filter as $item)
630  {
631  if($item->checkInput())
632  {
633  $item->setValueByArray($_POST);
634  $item->clearFromSession();
635  }
636  }
637  }
638 }
639 ?>