ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilMailErrorFormatter.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2016 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
9 {
11  protected $lng;
12 
17  public function __construct(\ilLanguage $lng)
18  {
19  $this->lng = $lng;
20  }
21 
28  public function format(array $errors) : string
29  {
30  if (0 === count($errors)) {
31  return '';
32  }
33 
34  $errorsToDisplay = [];
35  foreach ($errors as $error) {
36  $translation = $this->lng->txt($error->getLanguageVariable());
37  if ($translation === '-' . $this->lng->txt($error->getLanguageVariable()) . '-') {
38  $translation = $error->getLanguageVariable();
39  }
40 
41  if (
42  0 === count($error->getPlaceHolderValues()) ||
43  $translation === $error->getLanguageVariable()
44  ) {
45  $errorsToDisplay[] = $translation;
46  } else {
47  $escapedPlaceholderValues = array_map(function (string $address) {
48  return \ilUtil::prepareFormOutput($address);
49  }, $error->getPlaceHolderValues());
50 
51  array_unshift($escapedPlaceholderValues, $translation);
52  $errorsToDisplay[] = call_user_func_array('sprintf', $escapedPlaceholderValues);
53  }
54  }
55 
56  if (0 === count($errorsToDisplay)) {
57  return '';
58  }
59 
60  $tpl = new \ilTemplate('tpl.mail_new_submission_errors.html', true, true, 'Services/Mail');
61  if (1 === count($errorsToDisplay)) {
62  $tpl->setCurrentBlock('single_error');
63  $tpl->setVariable('SINGLE_ERROR', current($errorsToDisplay));
64  $tpl->parseCurrentBlock();
65  } else {
66  $firstError = array_shift($errorsToDisplay);
67 
68  foreach ($errorsToDisplay as $error) {
69  $tpl->setCurrentBlock('error_loop');
70  $tpl->setVariable('ERROR', $error);
71  $tpl->parseCurrentBlock();
72  }
73 
74  $tpl->setCurrentBlock('multiple_errors');
75  $tpl->setVariable('FIRST_ERROR', $firstError);
76  $tpl->parseCurrentBlock();
77  }
78 
79  return $tpl->get();
80  }
81 }
Class ilMailErrorFormatter.
$tpl
Definition: ilias.php:10
__construct(\ilLanguage $lng)
ilMailErrorFormatter constructor.
format(array $errors)
Formats an error string based on the passed list of errors.
$errors
Definition: index.php:6
language handling