ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilEMailInputGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 {
28  protected string $value = "";
29  protected int $size = 30;
30  protected int $max_length = 80;
31  protected bool $allowRFC822 = false;
32  protected bool $retype = false;
33  protected string $retypevalue = '';
34 
35  public function __construct(
36  string $a_title = "",
37  string $a_postvar = ""
38  ) {
39  global $DIC;
40 
41  $this->lng = $DIC->language();
42  parent::__construct($a_title, $a_postvar);
43  $this->setRetype(false);
44  }
45 
46  public function setValue(string $a_value): void
47  {
48  $this->value = $a_value;
49  }
50 
51  public function getValue(): string
52  {
53  return $this->value;
54  }
55 
56  public function setValueByArray(array $a_values): void
57  {
58  $this->setValue($a_values[$this->getPostVar()] ?? "");
59  $this->setRetypeValue($a_values[$this->getPostVar() . '_retype'] ?? "");
60  }
61 
67  public function allowRFC822(bool $a_value): void
68  {
69  $this->allowRFC822 = $a_value;
70  }
71 
72  // get string parameter kindly
73  protected function sanitize($key): string
74  {
75  $t = $this->refinery->kindlyTo()->string();
76  return ilUtil::stripSlashes(
77  (string) ($this->getRequestParam($key, $t) ?? ""),
78  !$this->allowRFC822
79  );
80  }
81 
82  public function checkInput(): bool
83  {
84  $lng = $this->lng;
85 
86  if ($this->getRequired() && trim($this->str($this->getPostVar())) == "") {
87  $this->setAlert($lng->txt("msg_input_is_required"));
88  return false;
89  }
90  if ($this->getRetype() &&
91  ($this->sanitize($this->getPostVar()) != $this->sanitize($this->getPostVar() . '_retype'))) {
92  $this->setAlert($lng->txt('email_not_match'));
93  return false;
94  }
95  if (!ilUtil::is_email($this->sanitize($this->getPostVar())) &&
96  trim($this->sanitize($this->getPostVar())) != ""
97  ) {
98  $this->setAlert($lng->txt("email_not_valid"));
99  return false;
100  }
101  return true;
102  }
103 
104  public function getInput(): string
105  {
106  return trim($this->sanitize($this->getPostVar()));
107  }
108 
109  public function insert(ilTemplate $a_tpl): void
110  {
111  $lng = $this->lng;
112 
113  $ptpl = new ilTemplate('tpl.prop_email.html', true, true, 'components/ILIAS/Form');
114 
115  if ($this->getRetype()) {
116  $ptpl->setCurrentBlock('retype_email');
117  $ptpl->setVariable('RSIZE', $this->getSize());
118  $ptpl->setVariable('RID', $this->getFieldId());
119  $ptpl->setVariable('RMAXLENGTH', $this->getMaxLength());
120  $ptpl->setVariable('RPOST_VAR', $this->getPostVar());
121 
122  $retype_value = $this->getRetypeValue();
123  $ptpl->setVariable('PROPERTY_RETYPE_VALUE', ilLegacyFormElementsUtil::prepareFormOutput($retype_value));
124  if ($this->getDisabled()) {
125  $ptpl->setVariable('RDISABLED', ' disabled="disabled"');
126  }
127  $ptpl->setVariable('TXT_RETYPE', $lng->txt('form_retype_email'));
128  $ptpl->parseCurrentBlock();
129  }
130 
131  $ptpl->setVariable('POST_VAR', $this->getPostVar());
132  $ptpl->setVariable('ID', $this->getFieldId());
133  $ptpl->setVariable('PROPERTY_VALUE', ilLegacyFormElementsUtil::prepareFormOutput($this->getValue()));
134  $ptpl->setVariable('SIZE', $this->getSize());
135  $ptpl->setVariable('MAXLENGTH', $this->getMaxLength());
136  if ($this->getDisabled()) {
137  $ptpl->setVariable('DISABLED', ' disabled="disabled"');
138  $ptpl->setVariable('HIDDEN_INPUT', $this->getHiddenTag($this->getPostVar(), $this->getValue()));
139  }
140 
141  if ($this->getRequired()) {
142  $ptpl->setVariable("REQUIRED", "required=\"required\"");
143  }
144 
145  $a_tpl->setCurrentBlock('prop_generic');
146  $a_tpl->setVariable('PROP_GENERIC', $ptpl->get());
147  $a_tpl->parseCurrentBlock();
148  }
149 
150  public function setRetype(bool $a_val): void
151  {
152  $this->retype = $a_val;
153  }
154 
155  public function getRetype(): bool
156  {
157  return $this->retype;
158  }
159 
160  public function setRetypeValue(string $a_retypevalue): void
161  {
162  $this->retypevalue = $a_retypevalue;
163  }
164 
165  public function getRetypeValue(): string
166  {
167  return $this->retypevalue;
168  }
169 
170  public function setSize(int $size): void
171  {
172  $this->size = $size;
173  }
174 
175  public function getSize(): int
176  {
177  return $this->size;
178  }
179 
180  public function setMaxLength(int $max_length): void
181  {
182  $this->max_length = $max_length;
183  }
184 
185  public function getMaxLength(): int
186  {
187  return $this->max_length;
188  }
189 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
getRequestParam(string $key, Refinery\Transformation $t)
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
setRetypeValue(string $a_retypevalue)
insert(ilTemplate $a_tpl)
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
allowRFC822(bool $a_value)
Allow extended email address format.
setValue(string $a_value)
setMaxLength(int $max_length)
This class represents a email property in a property form.
static prepareFormOutput($a_str, bool $a_strip=false)
static is_email(string $a_email, ?ilMailRfc822AddressParserFactory $mailAddressParserFactory=null)
This preg-based function checks whether an e-mail address is formally valid.
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
global $DIC
Definition: shib_login.php:26
getHiddenTag(string $a_post_var, string $a_value)
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
__construct(string $a_title="", string $a_postvar="")
__construct(Container $dic, ilPlugin $plugin)
setValueByArray(array $a_values)