ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilCSSRectInputGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 {
28  protected string $top = "";
29  protected string $left = "";
30  protected string $right = "";
31  protected string $bottom = "";
32  protected int $size = 0;
33  protected bool $useUnits = false;
34 
41  public function __construct($a_title = "", $a_postvar = "")
42  {
43  global $DIC;
44 
45  $this->lng = $DIC->language();
46  parent::__construct($a_title, $a_postvar);
47  $this->size = 6;
48  $this->useUnits = true;
49  }
50 
51  public function setValue(array $valueArray): void
52  {
53  $this->top = $valueArray['top'];
54  $this->left = $valueArray['left'];
55  $this->right = $valueArray['right'];
56  $this->bottom = $valueArray['bottom'];
57  }
58 
59  public function setUseUnits(bool $a_value): void
60  {
61  $this->useUnits = $a_value;
62  }
63 
64  public function useUnits(): bool
65  {
66  return $this->useUnits;
67  }
68 
69  public function setTop(string $a_value): void
70  {
71  $this->top = $a_value;
72  }
73 
74  public function getTop(): string
75  {
76  return $this->top;
77  }
78 
79  public function setBottom(string $a_value): void
80  {
81  $this->bottom = $a_value;
82  }
83 
84  public function getBottom(): string
85  {
86  return $this->bottom;
87  }
88 
89  public function setLeft(string $a_value): void
90  {
91  $this->left = $a_value;
92  }
93 
94  public function getLeft(): string
95  {
96  return $this->left;
97  }
98 
99  public function setRight(string $a_value): void
100  {
101  $this->right = $a_value;
102  }
103 
104  public function getRight(): string
105  {
106  return $this->right;
107  }
108 
109  public function setSize(int $a_size): void
110  {
111  $this->size = $a_size;
112  }
113 
114  public function setValueByArray(array $a_values): void
115  {
116  $postVar = $this->getPostVar();
117 
118  $positions = ['top', 'left', 'right', 'bottom'];
119  $values = [
120  'top' => '',
121  'bottom' => '',
122  'right' => '',
123  'left' => '',
124  ];
125 
126  foreach ($positions as $position) {
127  if (isset($a_values[$postVar . '_' . $position])) {
128  $values[$position] = $a_values[$postVar . '_' . $position];
129  } elseif (isset($a_values[$postVar][$position])) {
130  $values[$position] = $a_values[$postVar][$position];
131  }
132  }
133 
134  $this->setValue($values);
135  }
136 
137  public function getSize(): int
138  {
139  return $this->size;
140  }
141 
142  public function checkInput(): bool
143  {
144  $lng = $this->lng;
145 
146  $val = $this->getInput();
147  if (
148  $this->getRequired() &&
149  (
150  ($val[$this->getPostVar() . '_top'] == "")
151  || ($val[$this->getPostVar() . '_bottom'] == "")
152  || ($val[$this->getPostVar() . '_left'] == "")
153  || ($val[$this->getPostVar() . '_right'] == "")
154  )
155  ) {
156  $this->setAlert($lng->txt("msg_input_is_required"));
157  return false;
158  }
159 
160  foreach (['top', 'right', 'bottom', 'left'] as $side) {
161  $value = $val[$this->getPostVar() . '_' . $side];
162 
163  if (($this->getRequired() || $value !== "") && !preg_match('/^(([1-9]+|([1-9]+[0]*[\.,]{0,1}[\d]+))|(0[\.,](0*[1-9]+[\d]*))|0).*?$/si', $value)) {
164  $this->setAlert($lng->txt('msg_invalid_value_css_rect_input'));
165  return false;
166  }
167  }
168 
169  if ($this->useUnits()) {
170  if ((!preg_match('/^(([1-9]+|([1-9]+[0]*[\.,]{0,1}[\d]+))|(0[\.,](0*[1-9]+[\d]*))|0)(cm|mm|in|pt|pc|px|em)$/is', $val[$this->getPostVar() . '_top'])) ||
171  (!preg_match('/^(([1-9]+|([1-9]+[0]*[\.,]{0,1}[\d]+))|(0[\.,](0*[1-9]+[\d]*))|0)(cm|mm|in|pt|pc|px|em)$/is', $val[$this->getPostVar() . '_right'])) ||
172  (!preg_match('/^(([1-9]+|([1-9]+[0]*[\.,]{0,1}[\d]+))|(0[\.,](0*[1-9]+[\d]*))|0)(cm|mm|in|pt|pc|px|em)$/is', $val[$this->getPostVar() . '_bottom'])) ||
173  (!preg_match('/^(([1-9]+|([1-9]+[0]*[\.,]{0,1}[\d]+))|(0[\.,](0*[1-9]+[\d]*))|0)(cm|mm|in|pt|pc|px|em)$/is', $val[$this->getPostVar() . '_left']))) {
174  $this->setAlert($lng->txt("msg_unit_is_required"));
175  return false;
176  }
177  }
178  return $this->checkSubItemsInput();
179  }
180 
181  public function getInput(): array
182  {
183  $val = $this->strArray($this->getPostVar());
184  $ret[$this->getPostVar() . '_top'] = trim($val['top']);
185  $ret[$this->getPostVar() . '_right'] = trim($val['right']);
186  $ret[$this->getPostVar() . '_bottom'] = trim($val['bottom']);
187  $ret[$this->getPostVar() . '_left'] = trim($val['left']);
188  return $ret;
189  }
190 
191  public function insert(ilTemplate $a_tpl): void
192  {
193  $lng = $this->lng;
194 
195  if (strlen($this->getTop())) {
196  $a_tpl->setCurrentBlock("cssrect_value_top");
197  $a_tpl->setVariable("CSSRECT_VALUE", ilLegacyFormElementsUtil::prepareFormOutput($this->getTop()));
198  $a_tpl->parseCurrentBlock();
199  }
200  if (strlen($this->getBottom())) {
201  $a_tpl->setCurrentBlock("cssrect_value_bottom");
202  $a_tpl->setVariable("CSSRECT_VALUE", ilLegacyFormElementsUtil::prepareFormOutput($this->getBottom()));
203  $a_tpl->parseCurrentBlock();
204  }
205  if (strlen($this->getLeft())) {
206  $a_tpl->setCurrentBlock("cssrect_value_left");
207  $a_tpl->setVariable("CSSRECT_VALUE", ilLegacyFormElementsUtil::prepareFormOutput($this->getLeft()));
208  $a_tpl->parseCurrentBlock();
209  }
210  if (strlen($this->getRight())) {
211  $a_tpl->setCurrentBlock("cssrect_value_right");
212  $a_tpl->setVariable("CSSRECT_VALUE", ilLegacyFormElementsUtil::prepareFormOutput($this->getRight()));
213  $a_tpl->parseCurrentBlock();
214  }
215  $a_tpl->setCurrentBlock("cssrect");
216  $a_tpl->setVariable("ID", $this->getFieldId());
217  $a_tpl->setVariable("SIZE", $this->getSize());
218  $a_tpl->setVariable("POST_VAR", $this->getPostVar());
219  $a_tpl->setVariable("TEXT_TOP", $lng->txt("pos_top"));
220  $a_tpl->setVariable("TEXT_RIGHT", $lng->txt("pos_right"));
221  $a_tpl->setVariable("TEXT_BOTTOM", $lng->txt("pos_bottom"));
222  $a_tpl->setVariable("TEXT_LEFT", $lng->txt("pos_left"));
223  if ($this->getDisabled()) {
224  $a_tpl->setVariable(
225  "DISABLED",
226  " disabled=\"disabled\""
227  );
228  }
229  $a_tpl->parseCurrentBlock();
230  }
231 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
setValueByArray(array $a_values)
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...
setBottom(string $a_value)
static prepareFormOutput($a_str, bool $a_strip=false)
This class represents a text property in a property form.
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:546
global $DIC
Definition: shib_login.php:25
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
insert(ilTemplate $a_tpl)
__construct(Container $dic, ilPlugin $plugin)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setValue(array $valueArray)
__construct($a_title="", $a_postvar="")
Constructor.