ILIAS  release_4-3 Revision
 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  if(count($_POST['filename']) == 0)
86  {
87  ilUtil::sendFailure($this->lng->txt('mail_select_one_file'));
88  return $this->showAttachments();
89  }
90 
91  if(is_array($_POST['filename']) && count($_POST['filename']) > 0)
92  {
93  foreach($_POST['filename'] as $file)
94  {
95  if(file_exists($this->mfile->getMailPath() . '/' . basename($ilUser->getId() . '_' . urldecode($file))))
96  {
97  $files[] = urldecode($file);
98  }
99  }
100  }
101 
102  $this->umail->saveAttachments($files);
103 
104  $this->ctrl->returnToParent($this);
105  }
106 
107  public function cancelSaveAttachments()
108  {
109  $this->ctrl->setParameter($this, 'type', 'attach');
110  $this->ctrl->returnToParent($this);
111  }
112 
113  public function deleteAttachments()
114  {
115  if(!isset($_POST['filename']) || !is_array($_POST['filename']) || !$_POST['filename'])
116  {
117  ilUtil::sendFailure($this->lng->txt('mail_select_one_file'));
118  $this->showAttachments();
119  return;
120  }
121 
122  $this->tpl->setTitle($this->lng->txt('mail'));
123 
124  require_once 'Services/Utilities/classes/class.ilConfirmationGUI.php';
125  $confirmation = new ilConfirmationGUI();
126  $confirmation->setFormAction($this->ctrl->getFormAction($this, 'confirmDeleteAttachments'));
127  $confirmation->setConfirm($this->lng->txt('confirm'), 'confirmDeleteAttachments');
128  $confirmation->setCancel($this->lng->txt('cancel'), 'showAttachments');
129  $confirmation->setHeaderText($this->lng->txt('mail_sure_delete_file'));
130 
131  foreach($_POST['filename'] as $filename)
132  {
133  $confirmation->addItem('filename[]', ilUtil::stripSlashes($filename), ilUtil::stripSlashes(urldecode($filename)));
134  }
135 
136  $this->tpl->setContent($confirmation->getHtml());
137  $this->tpl->show();
138  }
139 
140  public function confirmDeleteAttachments()
141  {
142  if(!isset($_POST['filename']) || !is_array($_POST['filename']) || !$_POST['filename'])
143  {
144  ilUtil::sendInfo($this->lng->txt('mail_select_one_mail'));
145  $this->showAttachments();
146  return true;
147  }
148 
149  $files = array();
150  foreach($_POST['filename'] as $value)
151  {
152  $files[] = urldecode($value);
153  }
154 
155  if(strlen(($error = $this->mfile->unlinkFiles($files))))
156  {
157  ilUtil::sendFailure($this->lng->txt('mail_error_delete_file') . ' ' . $error);
158  }
159  else
160  {
161  $mailData = $this->umail->getSavedData();
162  if(is_array($mailData['attachments']))
163  {
164  $tmp = array();
165  for($i = 0; $i < count($mailData['attachments']); $i++)
166  {
167  if(!in_array($mailData['attachments'][$i], $files))
168  {
169  $tmp[] = $mailData['attachments'][$i];
170  }
171  }
172  $this->umail->saveAttachments($tmp);
173  }
174 
175  ilUtil::sendSuccess($this->lng->txt('mail_files_deleted'));
176  }
177 
178  $this->showAttachments();
179  }
180 
181  public function uploadFile()
182  {
183  if(strlen(trim($_FILES['userfile']['name'])))
184  {
185  if($this->mfile->storeUploadedFile($_FILES['userfile']) == 1)
186  {
187  ilUtil::sendFailure($this->lng->txt('mail_maxsize_attachment_error') . ' ' . ilFormat::formatSize($this->mfile->getUploadLimit()));
188  }
189  }
190 
191  $this->showAttachments();
192  }
193 
194  public function showAttachments()
195  {
199  global $ilToolbar;
200 
201  $this->tpl->setTitle($this->lng->txt('mail'));
202 
203  require_once 'Services/Form/classes/class.ilFileInputGUI.php';
204  $attachment = new ilFileInputGUI($this->lng->txt('upload'), 'userfile');
205  $attachment->setRequired(true);
206  $attachment->setSize(20);
207  $ilToolbar->setFormAction($this->ctrl->getFormAction($this, 'uploadFile'), true);
208  $ilToolbar->addInputItem($attachment);
209  $ilToolbar->addFormButton($this->lng->txt('upload'), 'uploadFile');
210 
211  require_once 'Services/Mail/classes/class.ilMailAttachmentTableGUI.php';
212  $table = new ilMailAttachmentTableGUI($this, 'showAttachments');
213 
214  $mailData = $this->umail->getSavedData();
215  $files = $this->mfile->getUserFilesData();
216  $data = array();
217  $counter = 0;
218  foreach($files as $file)
219  {
220  $checked = false;
221  if(is_array($mailData['attachments']) && in_array($file['name'], $mailData['attachments']))
222  {
223  $checked = true;
224  }
225 
226  $data[$counter] = array(
227  'checked' => $checked,
228  'filename' => $file['name'],
229  'filesize' => (int)$file['size'],
230  'filecreatedate'=> (int)$file['ctime']
231  );
232 
233  ++$counter;
234  }
235  $table->setData($data);
236 
237  $this->tpl->setContent($table->getHtml());
238  $this->tpl->show();
239  }
240 }