ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilBackgroundPositionInputGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 {
31  protected array $dirs = [];
32  protected string $verticalvalue = "";
33  protected string $horizontalvalue = "";
34  protected ilObjUser $user;
35  protected string $value = "";
36 
37  public function __construct(string $a_title = "", string $a_postvar = "")
38  {
39  global $DIC;
40 
41  $this->lng = $DIC->language();
42  $this->user = $DIC->user();
43  parent::__construct($a_title, $a_postvar);
44  $this->setType("border_width");
45  $this->dirs = array("horizontal", "vertical");
46  }
47 
48  public function setHorizontalValue(string $a_horizontalvalue): void
49  {
50  $this->horizontalvalue = $a_horizontalvalue;
51  }
52 
53  public function getHorizontalValue(): string
54  {
56  }
57 
58  public function setVerticalValue(string $a_verticalvalue): void
59  {
60  $this->verticalvalue = $a_verticalvalue;
61  }
62 
63  public function getVerticalValue(): string
64  {
65  return $this->verticalvalue;
66  }
67 
68  public function getValue(): string
69  {
70  if ($this->getHorizontalValue() != "") {
71  if ($this->getVerticalValue() != "") {
72  return $this->getHorizontalValue() . " " . $this->getVerticalValue();
73  } else {
74  return $this->getHorizontalValue();
75  }
76  } else {
77  if ($this->getVerticalValue() != "") {
78  return "left " . $this->getVerticalValue();
79  }
80  }
81  return "";
82  }
83 
84  public function setValue(string $a_val): void
85  {
86  $a_val = trim($a_val);
87  $a_val_arr = explode(" ", $a_val);
88  $hor = trim($a_val_arr[0]);
89  $ver = trim($a_val_arr[1] ?? "");
90  if ($hor == "center" && $ver == "") {
91  $ver = "center";
92  }
93  $this->setHorizontalValue($hor);
94  $this->setVerticalValue($ver);
95  }
96 
97  public function checkInput(): bool
98  {
99  $lng = $this->lng;
100 
101  $input = $this->getInput();
102 
103  foreach ($this->dirs as $dir) {
104  $type = $input[$dir]["type"];
105  $num_value = $input[$dir]["num_value"];
106  $num_unit = $input[$dir]["num_unit"];
107  $pre_value = $input[$dir]["pre_value"];
108 
109  if (!is_numeric($num_value) && trim($num_value) != "") {
110  $this->setAlert($lng->txt("sty_msg_input_must_be_numeric"));
111  return false;
112  }
113 
114  $value = "";
115  if ($type == "numeric") {
116  if ($num_value != "") {
117  $value = $num_value . $num_unit;
118  }
119  } else {
120  $value = $pre_value;
121  }
122 
123  if (trim($value) != "") {
124  switch ($dir) {
125  case "horizontal": $this->setHorizontalValue($value);
126  break;
127  case "vertical": $this->setVerticalValue($value);
128  break;
129  }
130  }
131  }
132 
133  return true;
134  }
135 
136  public function getInput(): array
137  {
138  return $this->arrayArray($this->getPostVar());
139  }
140 
141  public function insert(ilTemplate $a_tpl): void
142  {
143  $lng = $this->lng;
144 
145  $layout_tpl = new ilTemplate("tpl.prop_hv_layout.html", true, true, "components/ILIAS/Style/Content");
146 
147  foreach ($this->dirs as $dir) {
148  $value = "";
149  $current_unit = "";
150  $tpl = new ilTemplate("tpl.prop_background_position.html", true, true, "components/ILIAS/Style/Content");
152  $pre_options = ilObjStyleSheet::_getStyleParameterValues("background-position");
153  $pre_options = $pre_options[$dir];
154  switch ($dir) {
155  case "horizontal": $value = strtolower(trim($this->getHorizontalValue()));
156  break;
157  case "vertical": $value = strtolower(trim($this->getVerticalValue()));
158  break;
159  }
160 
161  if (in_array($value, $pre_options)) {
162  $current_type = "pre";
163  $tpl->setVariable("PREDEFINED_SELECTED", 'checked="checked"');
164  } else {
165  $current_type = "unit";
166  $tpl->setVariable("NUMERIC_SELECTED", 'checked="checked"');
167 
168  foreach ($unit_options as $u) {
169  if (substr($value, strlen($value) - strlen($u)) == $u) {
170  $current_unit = $u;
171  }
172  }
173  $disp_val = substr($value, 0, strlen($value) - strlen($current_unit));
174  if ($current_unit == "") {
175  $current_unit = "px";
176  }
177  $tpl->setVariable("VAL_NUM", $disp_val);
178  }
179 
180  foreach ($unit_options as $option) {
181  $tpl->setCurrentBlock("unit_option");
182  $tpl->setVariable("VAL_UNIT", $option);
183  $tpl->setVariable("TXT_UNIT", $option);
184  if ($current_type == "unit" && $current_unit == $option) {
185  $tpl->setVariable("UNIT_SELECTED", 'selected="selected"');
186  }
187  $tpl->parseCurrentBlock();
188  }
189 
190  foreach ($pre_options as $option) {
191  $tpl->setCurrentBlock("pre_option");
192  $tpl->setVariable("VAL_PRE", $option);
193  $tpl->setVariable("TXT_PRE", $option);
194  if ($current_type == "pre" && $value == $option) {
195  $tpl->setVariable("PRE_SELECTED", 'selected="selected"');
196  }
197  $tpl->parseCurrentBlock();
198  }
199 
200  $tpl->setVariable("POSTVAR", $this->getPostVar());
201  $tpl->setVariable("TXT_DIR", $lng->txt("sty_$dir"));
202  $tpl->setVariable("DIR", $dir);
203 
204  $layout_tpl->setVariable(strtoupper($dir), $tpl->get());
205  }
206  $layout_tpl->setVariable("COLSPAN", "2");
207 
208  $a_tpl->setCurrentBlock("prop_generic");
209  $a_tpl->setVariable("PROP_GENERIC", $layout_tpl->get());
210  $a_tpl->parseCurrentBlock();
211  }
212 
213  public function setValueByArray(array $a_values): void
214  {
215  if ($a_values[$this->getPostVar()]["horizontal"]["type"] == "predefined") {
216  $this->setHorizontalValue($a_values[$this->getPostVar()]["horizontal"]["pre_value"]);
217  } else {
218  $this->setHorizontalValue($a_values[$this->getPostVar()]["horizontal"]["num_value"] .
219  $a_values[$this->getPostVar()]["horizontal"]["num_unit"]);
220  }
221  if ($a_values[$this->getPostVar()]["vertical"]["type"] == "predefined") {
222  $this->setVerticalValue($a_values[$this->getPostVar()]["vertical"]["pre_value"]);
223  } else {
224  $this->setVerticalValue($a_values[$this->getPostVar()]["vertical"]["num_value"] .
225  $a_values[$this->getPostVar()]["vertical"]["num_unit"]);
226  }
227  }
228 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
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...
static _getStyleParameterNumericUnits(bool $a_no_percentage=false)
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
global $DIC
Definition: shib_login.php:26
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
__construct(Container $dic, ilPlugin $plugin)
static _getStyleParameterValues(string $par)
This class represents a background position in a property form.
__construct(string $a_title="", string $a_postvar="")