ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
All Data Structures Namespaces Files Functions Variables Typedefs Modules Pages
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 
4 require_once 'Services/Form/classes/class.ilPropertyFormGUI.php';
5 require_once 'Services/Mail/classes/class.ilMailTemplateDataProvider.php';
6 require_once 'Services/Mail/classes/class.ilMailTemplate.php';
7 require_once 'Services/Mail/classes/class.ilMailTemplateService.php';
8 require_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  protected $rbacsystem;
47 
51  protected $parentObject;
52 
56  protected $error;
57 
62  {
63  global $DIC;
64 
65  $this->parentObject = $parentObject;
66 
67  $this->tpl = $DIC->ui()->mainTemplate();
68  $this->ctrl = $DIC->ctrl();
69  $this->lng = $DIC->language();
70  $this->toolbar = $DIC->toolbar();
71  $this->rbacsystem = $DIC->rbac()->system();
72  $this->error = $DIC['ilErr'];
73 
74  $this->lng->loadLanguageModule('meta');
75 
76  $this->provider = new ilMailTemplateDataProvider();
77  }
78 
82  private function isEditingAllowed()
83  {
84  return $this->rbacsystem->checkAccess('write', $this->parentObject->getRefId());
85  }
86 
90  public function executeCommand()
91  {
92  $next_class = $this->ctrl->getNextClass($this);
93  $cmd = $this->ctrl->getCmd();
94 
95  switch ($next_class) {
96  default:
97  if (!$cmd || !method_exists($this, $cmd)) {
98  $cmd = 'showTemplates';
99  }
100 
101  $this->$cmd();
102  break;
103  }
104  }
105 
109  protected function showTemplates()
110  {
111  require_once 'Services/UIComponent/Button/classes/class.ilLinkButton.php';
112  require_once 'Services/Mail/classes/class.ilMailTemplateTableGUI.php';
113 
115  if (count($contexts) <= 1) {
116  ilUtil::sendFailure($this->lng->txt('mail_template_no_context_available'));
117  } elseif ($this->isEditingAllowed()) {
118  $create_tpl_button = ilLinkButton::getInstance();
119  $create_tpl_button->setCaption('mail_new_template');
120  $create_tpl_button->setUrl($this->ctrl->getLinkTarget($this, 'showInsertTemplateForm'));
121  $this->toolbar->addButtonInstance($create_tpl_button);
122  }
123 
124  $tbl = new ilMailTemplateTableGUI($this, 'showTemplates', !$this->isEditingAllowed());
125  $tbl->setData($this->provider->getTableData());
126 
127  $this->tpl->setContent($tbl->getHTML());
128  }
129 
133  protected function insertTemplate()
134  {
135  if (!$this->isEditingAllowed()) {
136  $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
137  }
138 
139  $form = $this->getTemplateForm();
140 
141  if (!$form->checkInput()) {
142  $form->setValuesByPost();
144  return;
145  }
146 
147  $generic_context = new ilMailTemplateGenericContext();
148  if ($form->getInput('context') == $generic_context->getId()) {
149  $form->getItemByPostVar('context')->setAlert($this->lng->txt('mail_template_no_valid_context'));
150  $form->setValuesByPost();
152  return;
153  }
154 
155  try {
156  $context = ilMailTemplateService::getTemplateContextById($form->getInput('context'));
157  $template = new ilMailTemplate();
158  $template->setTitle($form->getInput('title'));
159  $template->setContext($context->getId());
160  $template->setLang($form->getInput('lang'));
161  $template->setSubject($form->getInput('m_subject'));
162  $template->setMessage($form->getInput('m_message'));
163  $template->insert();
164 
165  ilUtil::sendSuccess($this->lng->txt('saved_successfully'), true);
166  $this->ctrl->redirect($this, 'showTemplates');
167  } catch (Exception $e) {
168  $form->getItemByPostVar('context')->setAlert($this->lng->txt('mail_template_no_valid_context'));
169  ilUtil::sendFailure($this->lng->txt('form_input_not_valid'));
170  }
171 
172  $form->setValuesByPost();
174  }
175 
179  protected function showInsertTemplateForm(ilPropertyFormGUI $form = null)
180  {
181  if (!($form instanceof ilPropertyFormGUI)) {
182  $form = $this->getTemplateForm();
183  }
184 
185  $this->tpl->setContent($form->getHTML());
186  }
187 
191  protected function updateTemplate()
192  {
193  if (!$this->isEditingAllowed()) {
194  $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
195  }
196 
197  if (!isset($_POST['tpl_id']) || !strlen($_POST['tpl_id'])) {
198  ilUtil::sendFailure($this->lng->txt('mail_template_missing_id'));
199  $this->showTemplates();
200  return;
201  }
202 
203  $template = $this->provider->getTemplateById((int) $_POST['tpl_id']);
204  if (!($template instanceof ilMailTemplate)) {
205  ilUtil::sendFailure($this->lng->txt('mail_template_missing_id'));
206  $this->showTemplates();
207  return;
208  }
209 
210  $form = $this->getTemplateForm();
211  if (!$form->checkInput()) {
212  $form->setValuesByPost();
213  $this->showEditTemplateForm($form);
214  return;
215  }
216 
217  $generic_context = new ilMailTemplateGenericContext();
218  if ($form->getInput('context') == $generic_context->getId()) {
219  $form->getItemByPostVar('context')->setAlert($this->lng->txt('mail_template_no_valid_context'));
220  $form->setValuesByPost();
221  $this->showEditTemplateForm($form);
222  return;
223  }
224 
225  try {
226  $context = ilMailTemplateService::getTemplateContextById($form->getInput('context'));
227  $template->setTitle($form->getInput('title'));
228  $template->setContext($context->getId());
229  $template->setLang($form->getInput('lang'));
230  $template->setSubject($form->getInput('m_subject'));
231  $template->setMessage($form->getInput('m_message'));
232  $template->update();
233 
234  ilUtil::sendSuccess($this->lng->txt('saved_successfully'), true);
235  $this->ctrl->redirect($this, 'showTemplates');
236  } catch (Exception $e) {
237  $form->getItemByPostVar('context')->setAlert($this->lng->txt('mail_template_no_valid_context'));
238  ilUtil::sendFailure($this->lng->txt('form_input_not_valid'));
239  }
240 
241  $form->setValuesByPost();
242  $this->showEditTemplateForm($form);
243  }
244 
248  protected function showEditTemplateForm(ilPropertyFormGUI $form = null)
249  {
250  if (!($form instanceof ilPropertyFormGUI)) {
251  if (!isset($_GET['tpl_id']) || !strlen($_GET['tpl_id'])) {
252  ilUtil::sendFailure($this->lng->txt('mail_template_missing_id'));
253  $this->showTemplates();
254  return;
255  }
256 
257  $template = $this->provider->getTemplateById((int) $_GET['tpl_id']);
258  if (!($template instanceof ilMailTemplate)) {
259  ilUtil::sendFailure($this->lng->txt('mail_template_missing_id'));
260  $this->showTemplates();
261  return;
262  }
263 
264  $form = $this->getTemplateForm($template);
266  }
267 
268  $this->tpl->setContent($form->getHTML());
269  }
270 
276  {
277  $form->setValuesByArray(array(
278  'tpl_id' => $template->getTplId(),
279  'title' => $template->getTitle(),
280  'context' => $template->getContext(),
281  'lang' => $template->getLang(),
282  'm_subject' => $template->getSubject(),
283  'm_message' => $template->getMessage()
284  ));
285  }
286 
290  protected function confirmDeleteTemplate()
291  {
292  if (!$this->isEditingAllowed()) {
293  $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
294  }
295 
296  if (isset($_POST['tpl_id']) && is_array($_POST['tpl_id']) && count($_POST['tpl_id']) > 0) {
297  $tpl_ids = array_filter(array_map('intval', $_POST['tpl_id']));
298  } else {
299  if (isset($_GET['tpl_id']) && strlen($_GET['tpl_id'])) {
300  $tpl_ids = array_filter(array((int) $_GET['tpl_id']));
301  } else {
302  $tpl_ids = array();
303  }
304  }
305 
306  if (count($tpl_ids) == 0) {
307  ilUtil::sendFailure($this->lng->txt('select_one'));
308  $this->showTemplates();
309  return;
310  }
311 
312  require_once 'Services/Utilities/classes/class.ilConfirmationGUI.php';
313  $confirm = new ilConfirmationGUI();
314  $confirm->setFormAction($this->ctrl->getFormAction($this, 'deleteTemplate'));
315  $confirm->setHeaderText($this->lng->txt('mail_sure_delete_entry'));
316  $confirm->setConfirm($this->lng->txt('confirm'), 'deleteTemplate');
317  $confirm->setCancel($this->lng->txt('cancel'), 'showTemplates');
318 
319  foreach ($tpl_ids as $tpl_id) {
320  $template = $this->provider->getTemplateById((int) $tpl_id);
321  $confirm->addItem('tpl_id[]', $tpl_id, $template->getTitle());
322  }
323  $this->tpl->setContent($confirm->getHTML());
324  }
325 
329  protected function deleteTemplate()
330  {
331  if (!$this->isEditingAllowed()) {
332  $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
333  }
334 
335  if (isset($_POST['tpl_id']) && is_array($_POST['tpl_id']) && count($_POST['tpl_id']) > 0) {
336  $tpl_ids = array_filter(array_map('intval', $_POST['tpl_id']));
337  if (0 == count($tpl_ids)) {
338  ilUtil::sendFailure($this->lng->txt('select_one'));
339  $this->showTemplates();
340  return;
341  }
342  } else {
343  ilUtil::sendFailure($this->lng->txt('select_one'));
344  $this->showTemplates();
345  return;
346  }
347 
348  $this->provider->deleteTemplates($tpl_ids);
349 
350  if (1 == count($tpl_ids)) {
351  ilUtil::sendSuccess($this->lng->txt('mail_tpl_deleted_s'), true);
352  } else {
353  ilUtil::sendSuccess($this->lng->txt('mail_tpl_deleted_p'), true);
354  }
355  $this->ctrl->redirect($this, 'showTemplates');
356  }
357 
358  public function getAjaxPlaceholdersById()
359  {
360  $context_id = ilUtil::stripSlashes($_GET['triggerValue']);
361  require_once 'Services/Mail/classes/Form/class.ilManualPlaceholderInputGUI.php';
362  $placeholders = new ilManualPlaceholderInputGUI('m_message');
363  $placeholders->setInstructionText($this->lng->txt('mail_nacc_use_placeholder'));
364  $placeholders->setAdviseText(sprintf($this->lng->txt('placeholders_advise'), '<br />'));
365  $context = ilMailTemplateService::getTemplateContextById($context_id);
366  foreach ($context->getPlaceholders() as $key => $value) {
367  $placeholders->addPlaceholder($value['placeholder'], $value['label']);
368  }
369  $placeholders->render(true);
370  exit();
371  }
372 
377  protected function getTemplateForm(ilMailTemplate $template = null)
378  {
379  $form = new ilPropertyFormGUI();
380 
381  $title = new ilTextInputGUI($this->lng->txt('mail_template_title'), 'title');
382  $title->setRequired(true);
383  $title->setDisabled(!$this->isEditingAllowed());
384  $form->addItem($title);
385 
386  $context = new ilRadioGroupInputGUI($this->lng->txt('mail_template_context'), 'context');
387  $context->setDisabled(!$this->isEditingAllowed());
389 
390  if (count($contexts) <= 1) {
391  ilUtil::sendFailure($this->lng->txt('mail_template_no_context_available'), true);
392  $this->ctrl->redirect($this, 'showTemplates');
393  }
394 
395  $context_sort = array();
396  $context_options = array();
397  $generic_context = new ilMailTemplateGenericContext();
398  foreach ($contexts as $ctx) {
399  if ($ctx->getId() != $generic_context->getId()) {
400  $context_options[$ctx->getId()] = $ctx;
401  $context_sort[$ctx->getId()] = $ctx->getTitle();
402  }
403  }
404  asort($context_sort);
405  $first = null;
406  foreach ($context_sort as $id => $title) {
407  $ctx = $context_options[$id];
408  $option = new ilRadioOption($ctx->getTitle(), $ctx->getId());
409  $option->setInfo($ctx->getDescription());
410  $context->addOption($option);
411 
412  if (!$first) {
413  $first = $id;
414  }
415  }
416  $context->setValue($first);
417  $context->setRequired(true);
418  $form->addItem($context);
419 
420  $hidde_language = new ilHiddenInputGUI('lang');
421  $hidde_language->setValue($this->lng->getLangKey());
422  $form->addItem($hidde_language);
423 
424  $subject = new ilTextInputGUI($this->lng->txt('subject'), 'm_subject');
425  $subject->setDisabled(!$this->isEditingAllowed());
426  $subject->setRequired(true);
427  $subject->setSize(50);
428  $form->addItem($subject);
429 
430  $message = new ilTextAreaInputGUI($this->lng->txt('message'), 'm_message');
431  $message->setDisabled(!$this->isEditingAllowed());
432  $message->setRequired(true);
433  $message->setCols(60);
434  $message->setRows(10);
435  $form->addItem($message);
436 
437  require_once 'Services/Mail/classes/Form/class.ilManualPlaceholderInputGUI.php';
438  $placeholders = new ilManualPlaceholderInputGUI('m_message');
439  $placeholders->setDisabled(!$this->isEditingAllowed());
440  $placeholders->setInstructionText($this->lng->txt('mail_nacc_use_placeholder'));
441  $placeholders->setAdviseText(sprintf($this->lng->txt('placeholders_advise'), '<br />'));
442  $placeholders->supportsRerenderSignal(
443  'context',
444  $this->ctrl->getLinkTarget($this, 'getAjaxPlaceholdersById', '', true, false)
445  );
446  if ($template === null) {
447  $context_id = $generic_context->getId();
448  } else {
449  $context_id = $template->getContext();
450  }
451  $context = ilMailTemplateService::getTemplateContextById($context_id);
452  foreach ($context->getPlaceholders() as $key => $value) {
453  $placeholders->addPlaceholder($value['placeholder'], $value['label']);
454  }
455  $form->addItem($placeholders);
456  if ($template instanceof ilMailTemplate && $template->getTplId() > 0) {
457  $id = new ilHiddenInputGUI('tpl_id');
458  $form->addItem($id);
459 
460  $form->setTitle($this->lng->txt('mail_edit_tpl'));
461  $form->setFormAction($this->ctrl->getFormaction($this, 'updateTemplate'));
462 
463  if ($this->isEditingAllowed()) {
464  $form->addCommandButton('updateTemplate', $this->lng->txt('save'));
465  }
466  } else {
467  $form->setTitle($this->lng->txt('mail_create_tpl'));
468  $form->setFormAction($this->ctrl->getFormaction($this, 'insertTemplate'));
469 
470  if ($this->isEditingAllowed()) {
471  $form->addCommandButton('insertTemplate', $this->lng->txt('save'));
472  }
473  }
474 
475  if ($this->isEditingAllowed()) {
476  $form->addCommandButton('showTemplates', $this->lng->txt('cancel'));
477  } else {
478  $form->addCommandButton('showTemplates', $this->lng->txt('back'));
479  }
480 
481  return $form;
482  }
483 }
static sendSuccess($a_info="", $a_keep=false)
Send Success Message to Screen.
This class represents an option in a radio group.
__construct(\ilObject $parentObject)
showEditTemplateForm(ilPropertyFormGUI $form=null)
$template
This class represents a property form user interface.
global $DIC
Definition: saml.php:7
Class ilMailTemplateGUI.
$_GET["client_id"]
$tbl
Definition: example_048.php:81
getTemplateForm(ilMailTemplate $template=null)
if(!array_key_exists('StateId', $_REQUEST)) $id
Class ilMailTemplate.
setInfo($a_info)
Set Info.
Class ilMailTemplateDataProvider.
static getTemplateContexts($a_id=null)
Returns an array of mail template contexts, the key of each entry matches its id. ...
Class ilMailTemplateTableGUI.
Class ilManualPlaceholderInputGUI.
This class represents a hidden form property in a property form.
catch(Exception $e) $message
This class represents a property in a property form.
populateFormWithTemplate(ilPropertyFormGUI $form, ilMailTemplate $template)
This class represents a text property in a property form.
showInsertTemplateForm(ilPropertyFormGUI $form=null)
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
Create styles array
The data for the language used.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
This class represents a text area property in a property form.
$key
Definition: croninfo.php:18
setDisabled($a_disabled)
Set Disabled.
$_POST["username"]
setValuesByArray($a_values, $a_restrict_to_value_keys=false)
Set form values from an array.
Confirmation screen class.