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