ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMailAttachmentGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once "Services/Mail/classes/class.ilFormatMail.php";
5 require_once "Services/Mail/classes/class.ilFileDataMail.php";
6 
13 {
17  private $tpl;
18 
22  private $ctrl;
23 
27  private $lng;
28 
32  private $umail;
33 
37  private $mfile;
38 
39  public function __construct()
40  {
47  global $tpl, $ilCtrl, $lng, $ilUser;
48 
49  $this->tpl = $tpl;
50  $this->ctrl = $ilCtrl;
51  $this->lng = $lng;
52 
53  $this->ctrl->saveParameter($this, 'mobj_id');
54 
55  $this->umail = new ilFormatMail($ilUser->getId());
56  $this->mfile = new ilFileDataMail($ilUser->getId());
57  }
58 
59  public function executeCommand()
60  {
61  $forward_class = $this->ctrl->getNextClass($this);
62  switch($forward_class)
63  {
64  default:
65  if(!($cmd = $this->ctrl->getCmd()))
66  {
67  $cmd = 'showAttachments';
68  }
69 
70  $this->$cmd();
71  break;
72  }
73  return true;
74  }
75 
76  public function saveAttachments()
77  {
81  global $ilUser;
82 
83  $files = array();
84 
85  // Important: Do not check for uploaded files here, otherwise it is no more possible to remove files (please ignore bug reports like 10137)
86 
87  $size_of_selected_files = 0;
88  if(is_array($_POST['filename']) && count($_POST['filename']) > 0)
89  {
90  foreach($_POST['filename'] as $file)
91  {
92  if(file_exists($this->mfile->getMailPath() . '/' . basename($ilUser->getId() . '_' . urldecode($file))))
93  {
94  $files[] = urldecode($file);
95  $size_of_selected_files += filesize($this->mfile->getMailPath() . '/' . basename($ilUser->getId() . '_' . urldecode($file)));
96  }
97  }
98  }
99 
100  if(
101  null !== $this->mfile->getAttachmentsTotalSizeLimit() &&
102  $files && $size_of_selected_files > $this->mfile->getAttachmentsTotalSizeLimit()
103  )
104  {
105  ilUtil::sendFailure($this->lng->txt('mail_max_size_attachments_total_error') . ' ' . ilFormat::formatSize($this->mfile->getAttachmentsTotalSizeLimit()));
106  return $this->showAttachments();
107  }
108 
109  $this->umail->saveAttachments($files);
110 
111  $this->ctrl->returnToParent($this);
112  }
113 
114  public function cancelSaveAttachments()
115  {
116  $this->ctrl->setParameter($this, 'type', 'attach');
117  $this->ctrl->returnToParent($this);
118  }
119 
120  public function deleteAttachments()
121  {
122  if(!isset($_POST['filename']) || !is_array($_POST['filename']) || !$_POST['filename'])
123  {
124  ilUtil::sendFailure($this->lng->txt('mail_select_one_file'));
125  $this->showAttachments();
126  return;
127  }
128 
129  $this->tpl->setTitle($this->lng->txt('mail'));
130 
131  require_once 'Services/Utilities/classes/class.ilConfirmationGUI.php';
132  $confirmation = new ilConfirmationGUI();
133  $confirmation->setFormAction($this->ctrl->getFormAction($this, 'confirmDeleteAttachments'));
134  $confirmation->setConfirm($this->lng->txt('confirm'), 'confirmDeleteAttachments');
135  $confirmation->setCancel($this->lng->txt('cancel'), 'showAttachments');
136  $confirmation->setHeaderText($this->lng->txt('mail_sure_delete_file'));
137 
138  foreach($_POST['filename'] as $filename)
139  {
140  $confirmation->addItem('filename[]', ilUtil::stripSlashes($filename), ilUtil::stripSlashes(urldecode($filename)));
141  }
142 
143  $this->tpl->setContent($confirmation->getHtml());
144  $this->tpl->show();
145  }
146 
147  public function confirmDeleteAttachments()
148  {
149  if(!isset($_POST['filename']) || !is_array($_POST['filename']) || !$_POST['filename'])
150  {
151  ilUtil::sendInfo($this->lng->txt('mail_select_one_mail'));
152  $this->showAttachments();
153  return true;
154  }
155 
156  $files = array();
157  foreach($_POST['filename'] as $value)
158  {
159  $files[] = urldecode($value);
160  }
161 
162  if(strlen(($error = $this->mfile->unlinkFiles($files))))
163  {
164  ilUtil::sendFailure($this->lng->txt('mail_error_delete_file') . ' ' . $error);
165  }
166  else
167  {
168  $mailData = $this->umail->getSavedData();
169  if(is_array($mailData['attachments']))
170  {
171  $tmp = array();
172  for($i = 0; $i < count($mailData['attachments']); $i++)
173  {
174  if(!in_array($mailData['attachments'][$i], $files))
175  {
176  $tmp[] = $mailData['attachments'][$i];
177  }
178  }
179  $this->umail->saveAttachments($tmp);
180  }
181 
182  ilUtil::sendSuccess($this->lng->txt('mail_files_deleted'));
183  }
184 
185  $this->showAttachments();
186  }
187 
191  protected function getToolbarForm()
192  {
193  require_once 'Services/Form/classes/class.ilPropertyFormGUI.php';
194  $form = new ilPropertyFormGUI();
195  $attachment = new ilFileInputGUI($this->lng->txt('upload'), 'userfile');
196  $attachment->setRequired(true);
197  $attachment->setSize(20);
198  $form->addItem($attachment);
199  return $form;
200  }
201 
202  public function uploadFile()
203  {
207  global $lng;
208 
209  if(strlen(trim($_FILES['userfile']['name'])))
210  {
211  $form = $this->getToolbarForm();
212  if($form->checkInput())
213  {
214  $this->mfile->storeUploadedFile($_FILES['userfile']);
215  ilUtil::sendSuccess($lng->txt('saved_successfully'));
216  }
217  else
218  {
219  if($form->getItemByPostVar('userfile')->getAlert() != $lng->txt("form_msg_file_size_exceeds"))
220  {
221  ilUtil::sendFailure($form->getItemByPostVar('userfile')->getAlert());
222  }
223  else
224  {
225  ilUtil::sendFailure($this->lng->txt('mail_maxsize_attachment_error') . ' ' . ilFormat::formatSize($this->mfile->getUploadLimit()));
226  }
227  }
228  }
229  else
230  {
231  ilUtil::sendFailure($this->lng->txt('mail_select_one_file'));
232  }
233 
234  $this->showAttachments();
235  }
236 
237  public function showAttachments()
238  {
242  global $ilToolbar;
243 
244  $this->tpl->setTitle($this->lng->txt('mail'));
245 
246  require_once 'Services/Form/classes/class.ilFileInputGUI.php';
247  $attachment = new ilFileInputGUI($this->lng->txt('upload'), 'userfile');
248  $attachment->setRequired(true);
249  $attachment->setSize(20);
250  $ilToolbar->setFormAction($this->ctrl->getFormAction($this, 'uploadFile'), true);
251  $ilToolbar->addInputItem($attachment);
252  $ilToolbar->addFormButton($this->lng->txt('upload'), 'uploadFile');
253 
254  require_once 'Services/Mail/classes/class.ilMailAttachmentTableGUI.php';
255  $table = new ilMailAttachmentTableGUI($this, 'showAttachments');
256 
257  $mailData = $this->umail->getSavedData();
258  $files = $this->mfile->getUserFilesData();
259  $data = array();
260  $counter = 0;
261  foreach($files as $file)
262  {
263  $checked = false;
264  if(is_array($mailData['attachments']) && in_array($file['name'], $mailData['attachments']))
265  {
266  $checked = true;
267  }
268 
269  $data[$counter] = array(
270  'checked' => $checked,
271  'filename' => $file['name'],
272  'filesize' => (int)$file['size'],
273  'filecreatedate'=> (int)$file['ctime']
274  );
275 
276  ++$counter;
277  }
278  $table->setData($data);
279 
280  $this->tpl->setContent($table->getHtml());
281  $this->tpl->show();
282  }
283 }