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