ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilMailTemplateGUI.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Services/Form/classes/class.ilPropertyFormGUI.php';
5require_once 'Services/Mail/classes/class.ilMailTemplateDataProvider.php';
6require_once 'Services/Mail/classes/class.ilMailTemplate.php';
7require_once 'Services/Mail/classes/class.ilMailTemplateService.php';
8require_once 'Services/Mail/classes/class.ilMailTemplateGenericContext.php';
9
17{
21 protected $form;
22
26 protected $tpl;
27
31 protected $ctrl;
32
36 protected $lng;
37
41 protected $toolbar;
42
46 public function __construct()
47 {
54 global $tpl, $ilCtrl, $lng, $ilToolbar;
55
56 $this->tpl = $tpl;
57 $this->ctrl = $ilCtrl;
58 $this->lng = $lng;
59 $this->toolbar = $ilToolbar;
60
61 $this->lng->loadLanguageModule('meta');
62
63 $this->provider = new ilMailTemplateDataProvider();
64 }
65
69 public function executeCommand()
70 {
71 $next_class = $this->ctrl->getNextClass($this);
72 $cmd = $this->ctrl->getCmd();
73
74 switch($next_class)
75 {
76 default:
77 if(!$cmd || !method_exists($this, $cmd))
78 {
79 $cmd = 'showTemplates';
80 }
81
82 $this->$cmd();
83 break;
84 }
85 }
86
90 protected function showTemplates()
91 {
92 require_once 'Services/UIComponent/Button/classes/class.ilLinkButton.php';
93 require_once 'Services/Mail/classes/class.ilMailTemplateTableGUI.php';
94
96 if(count($contexts) <= 1)
97 {
98 ilUtil::sendFailure($this->lng->txt('mail_template_no_context_available'));
99 }
100 else
101 {
102 $create_tpl_button = ilLinkButton::getInstance();
103 $create_tpl_button->setCaption('mail_new_template');
104 $create_tpl_button->setUrl($this->ctrl->getLinkTarget($this, 'showInsertTemplateForm'));
105 $this->toolbar->addButtonInstance($create_tpl_button);
106 }
107
108 $tbl = new ilMailTemplateTableGUI($this, 'showTemplates');
109 $tbl->setData($this->provider->getTableData());
110
111 $this->tpl->setContent($tbl->getHTML());
112 }
113
117 protected function insertTemplate()
118 {
119 $form = $this->getTemplateForm();
120
121 if(!$form->checkInput())
122 {
123 $form->setValuesByPost();
125 return;
126 }
127
128 $generic_context = new ilMailTemplateGenericContext();
129 if($form->getInput('context') == $generic_context->getId())
130 {
131 $form->getItemByPostVar('context')->setAlert($this->lng->txt('mail_template_no_valid_context'));
132 $form->setValuesByPost();
134 return;
135 }
136
137 try
138 {
139 $context = ilMailTemplateService::getTemplateContextById($form->getInput('context'));
140 $template = new ilMailTemplate();
141 $template->setTitle($form->getInput('title'));
142 $template->setContext($context->getId());
143 $template->setLang($form->getInput('lang'));
144 $template->setSubject($form->getInput('m_subject'));
145 $template->setMessage($form->getInput('m_message'));
146 $template->insert();
147
148 ilUtil::sendSuccess($this->lng->txt('saved_successfully'), true);
149 $this->ctrl->redirect($this, 'showTemplates');
150 }
151 catch(Exception $e)
152 {
153 $form->getItemByPostVar('context')->setAlert($this->lng->txt('mail_template_no_valid_context'));
154 ilUtil::sendFailure($this->lng->txt('form_input_not_valid'));
155 }
156
157 $form->setValuesByPost();
159 }
160
165 {
166 if(!($form instanceof ilPropertyFormGUI))
167 {
168 $form = $this->getTemplateForm();
169 }
170 $this->tpl->setContent($form->getHTML());
171 }
172
176 protected function updateTemplate()
177 {
178 if(!isset($_POST['tpl_id']) || !strlen($_POST['tpl_id']))
179 {
180 ilUtil::sendFailure($this->lng->txt('mail_template_missing_id'));
181 $this->showTemplates();
182 return;
183 }
184
185 $template = $this->provider->getTemplateById((int)$_POST['tpl_id']);
186 if(!($template instanceof ilMailTemplate))
187 {
188 ilUtil::sendFailure($this->lng->txt('mail_template_missing_id'));
189 $this->showTemplates();
190 return;
191 }
192
193 $form = $this->getTemplateForm();
194 if(!$form->checkInput())
195 {
196 $form->setValuesByPost();
198 return;
199 }
200
201 $generic_context = new ilMailTemplateGenericContext();
202 if($form->getInput('context') == $generic_context->getId())
203 {
204 $form->getItemByPostVar('context')->setAlert($this->lng->txt('mail_template_no_valid_context'));
205 $form->setValuesByPost();
207 return;
208 }
209
210 try
211 {
212 $context = ilMailTemplateService::getTemplateContextById($form->getInput('context'));
213 $template->setTitle($form->getInput('title'));
214 $template->setContext($context->getId());
215 $template->setLang($form->getInput('lang'));
216 $template->setSubject($form->getInput('m_subject'));
217 $template->setMessage($form->getInput('m_message'));
218 $template->update();
219
220 ilUtil::sendSuccess($this->lng->txt('saved_successfully'), true);
221 $this->ctrl->redirect($this, 'showTemplates');
222 }
223 catch(Exception $e)
224 {
225 $form->getItemByPostVar('context')->setAlert($this->lng->txt('mail_template_no_valid_context'));
226 ilUtil::sendFailure($this->lng->txt('form_input_not_valid'));
227 }
228
229 $form->setValuesByPost();
231 }
232
237 {
238 if(!($form instanceof ilPropertyFormGUI))
239 {
240 if(!isset($_GET['tpl_id']) || !strlen($_GET['tpl_id']))
241 {
242 ilUtil::sendFailure($this->lng->txt('mail_template_missing_id'));
243 $this->showTemplates();
244 return;
245 }
246
247 $template = $this->provider->getTemplateById((int)$_GET['tpl_id']);
248 if(!($template instanceof ilMailTemplate))
249 {
250 ilUtil::sendFailure($this->lng->txt('mail_template_missing_id'));
251 $this->showTemplates();
252 return;
253 }
254
255 $form = $this->getTemplateForm($template);
256 $this->populateFormWithTemplate($form, $template);
257 }
258
259 $this->tpl->setContent($form->getHTML());
260 }
261
267 {
268 $form->setValuesByArray(array(
269 'tpl_id' => $template->getTplId(),
270 'title' => $template->getTitle(),
271 'context' => $template->getContext(),
272 'lang' => $template->getLang(),
273 'm_subject' => $template->getSubject(),
274 'm_message' => $template->getMessage()
275 ));
276 }
277
281 protected function confirmDeleteTemplate()
282 {
283 if(isset($_POST['tpl_id']) && is_array($_POST['tpl_id']) && count($_POST['tpl_id']) > 0)
284 {
285 $tpl_ids = array_filter(array_map('intval', $_POST['tpl_id']));
286 }
287 else if(isset($_GET['tpl_id']) && strlen($_GET['tpl_id']))
288 {
289 $tpl_ids = array_filter(array((int)$_GET['tpl_id']));
290 }
291 else
292 {
293 $tpl_ids = array();
294 }
295
296 if(count($tpl_ids) == 0)
297 {
298 ilUtil::sendFailure($this->lng->txt('select_one'));
299 $this->showTemplates();
300 return;
301 }
302
303 require_once 'Services/Utilities/classes/class.ilConfirmationGUI.php';
304 $confirm = new ilConfirmationGUI();
305 $confirm->setFormAction($this->ctrl->getFormAction($this, 'deleteTemplate'));
306 $confirm->setHeaderText($this->lng->txt('mail_sure_delete_entry'));
307 $confirm->setConfirm($this->lng->txt('confirm'), 'deleteTemplate');
308 $confirm->setCancel($this->lng->txt('cancel'), 'showTemplates');
309
310 foreach($tpl_ids as $tpl_id)
311 {
312 $template = $this->provider->getTemplateById((int)$tpl_id);
313 $confirm->addItem('tpl_id[]', $tpl_id, $template->getTitle());
314 }
315 $this->tpl->setContent($confirm->getHTML());
316 }
317
321 protected function deleteTemplate()
322 {
323 if(isset($_POST['tpl_id']) && is_array($_POST['tpl_id']) && count($_POST['tpl_id']) > 0)
324 {
325 $tpl_ids = array_filter(array_map('intval', $_POST['tpl_id']));
326 if(0 == count($tpl_ids))
327 {
328 ilUtil::sendFailure($this->lng->txt('select_one'));
329 $this->showTemplates();
330 return;
331 }
332 }
333 else
334 {
335 ilUtil::sendFailure($this->lng->txt('select_one'));
336 $this->showTemplates();
337 return;
338 }
339
340 $this->provider->deleteTemplates($tpl_ids);
341
342 if(1 == count($tpl_ids))
343 {
344 ilUtil::sendSuccess($this->lng->txt('mail_tpl_deleted_s'), true);
345 }
346 else
347 {
348 ilUtil::sendSuccess($this->lng->txt('mail_tpl_deleted_p'), true);
349 }
350 $this->ctrl->redirect($this, 'showTemplates');
351 }
352
353 public function getAjaxPlaceholdersById()
354 {
355 $context_id = ilUtil::stripSlashes($_GET['context_id']);
356 require_once 'Services/Mail/classes/Form/class.ilManualPlaceholderInputGUI.php';
357 $placeholders = new ilManualPlaceholderInputGUI($this->ctrl->getLinkTarget($this, 'getAjaxPlaceholdersById', '', true, false));
358 $context = ilMailTemplateService::getTemplateContextById($context_id);
359 foreach( $context->getPlaceholders() as $key => $value)
360 {
361 $placeholders->addPlaceholder($value['placeholder'], $value['label'] );
362 }
363 $placeholders->render(true);
364 exit();
365 }
366
371 protected function getTemplateForm(ilMailTemplate $template = null)
372 {
373 $form = new ilPropertyFormGUI();
374
375 $title = new ilTextInputGUI($this->lng->txt('mail_template_title'), 'title');
376 $title->setRequired(true);
377 $form->addItem($title);
378
379 $context = new ilRadioGroupInputGUI($this->lng->txt('mail_template_context'), 'context');
381
382 if(count($contexts) <= 1)
383 {
384 ilUtil::sendFailure($this->lng->txt('mail_template_no_context_available'), true);
385 $this->ctrl->redirect($this, 'showTemplates');
386 }
387
388 $context_sort = array();
389 $context_options = array();
390 $generic_context = new ilMailTemplateGenericContext();
391 foreach($contexts as $ctx)
392 {
393 if($ctx->getId() != $generic_context->getId())
394 {
395 $context_options[$ctx->getId()] = $ctx;
396 $context_sort[$ctx->getId()] = $ctx->getTitle();
397 }
398 }
399 asort($context_sort);
400 $first = null;
401 foreach($context_sort as $id => $title)
402 {
403 $ctx = $context_options[$id];
404 $option = new ilRadioOption($ctx->getTitle(), $ctx->getId());
405 $option->setInfo($ctx->getDescription());
406 $context->addOption($option);
407
408 if(!$first)
409 {
410 $first = $id;
411 }
412 }
413 $context->setValue($first);
414 $context->setRequired(true);
415 $form->addItem($context);
416
417 /*$lang = new ilSelectInputGUI($this->lng->txt('language'), 'lang');
418 $options = array();
419 foreach($this->lng->getInstalledLanguages() as $iso2code)
420 {
421 $options[$iso2code] = $this->lng->txt('meta_l_' . $iso2code);
422 }
423 asort($options);
424 $lang->setOptions(array('' => $this->lng->txt('please_choose')) + $options);
425 $lang->setRequired(true);
426 $form->addItem($lang);*/
427 $hidde_language = new ilHiddenInputGUI('lang');
428 $hidde_language->setValue($this->lng->getLangKey());
429 $form->addItem($hidde_language);
430
431 $subject = new ilTextInputGUI($this->lng->txt('subject'), 'm_subject');
432 $subject->setSize(50);
433 $form->addItem($subject);
434
435 $message = new ilTextAreaInputGUI($this->lng->txt('message'), 'm_message');
436 $message->setRequired(true);
437 $message->setCols(60);
438 $message->setRows(10);
439 $form->addItem($message);
440
441 require_once 'Services/Mail/classes/Form/class.ilManualPlaceholderInputGUI.php';
442 $placeholders = new ilManualPlaceholderInputGUI($this->ctrl->getLinkTarget($this, 'getAjaxPlaceholdersById', '', true, false));
443
444 if( $template === null )
445 {
446 $context_id = $generic_context->getId();
447 }
448 else
449 {
450 $context_id = $template->getContext();
451 }
452 $context = ilMailTemplateService::getTemplateContextById($context_id);
453 foreach( $context->getPlaceholders() as $key => $value)
454 {
455 $placeholders->addPlaceholder($value['placeholder'], $value['label'] );
456 }
457 $form->addItem($placeholders);
458 if($template instanceof ilMailTemplate && $template->getTplId() > 0)
459 {
460 $id = new ilHiddenInputGUI('tpl_id');
461 $form->addItem($id);
462
463 $form->setTitle($this->lng->txt('mail_edit_tpl'));
464 $form->setFormAction($this->ctrl->getFormaction($this, 'updateTemplate'));
465 $form->addCommandButton('updateTemplate', $this->lng->txt('save'));
466 }
467 else
468 {
469 $form->setTitle($this->lng->txt('mail_create_tpl'));
470 $form->setFormAction($this->ctrl->getFormaction($this, 'insertTemplate'));
471 $form->addCommandButton('insertTemplate', $this->lng->txt('save'));
472 }
473
474 $form->addCommandButton('showTemplates', $this->lng->txt('cancel'));
475 return $form;
476 }
477}
$_GET["client_id"]
Confirmation screen class.
This class represents a hidden form property in a property form.
static getInstance()
Factory.
Class ilMailTemplateDataProvider.
Class ilMailTemplateGUI.
getTemplateForm(ilMailTemplate $template=null)
showInsertTemplateForm(ilPropertyFormGUI $form=NULL)
showEditTemplateForm(ilPropertyFormGUI $form=NULL)
populateFormWithTemplate(ilPropertyFormGUI $form, ilMailTemplate $template)
static getTemplateContexts($a_id=null)
Returns an array of mail template contexts, the key of each entry matches its id.
Class ilMailTemplateTableGUI.
Class ilMailTemplate.
Class ilManualPlaceholderInputGUI.
This class represents a property form user interface.
This class represents a property in a property form.
This class represents an option in a radio group.
This class represents a text area property in a property form.
This class represents a text property in a property form.
static sendSuccess($a_info="", $a_keep=false)
Send Success Message to Screen.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
$_POST['username']
Definition: cron.php:12
$tbl
Definition: example_048.php:81
global $ilCtrl
Definition: ilias.php:18
exit
Definition: login.php:54
$cmd
Definition: sahs_server.php:35