ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilColorPickerInputGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
28 {
29  protected string $hex = "";
30  protected bool $acceptnamedcolors = false;
31  protected string $defaultcolor = "";
32 
33  public function __construct(
34  string $a_title = "",
35  string $a_postvar = ""
36  ) {
37  parent::__construct($a_title, $a_postvar);
38  $this->setType("color");
39  $this->setDefaultColor("04427e");
40  }
41 
42  public function checkInput(): bool
43  {
44  if ($this->getRequired() && !strlen($this->getInput())
45  ) {
46  $this->setAlert($this->lng->txt("msg_input_is_required"));
47  return false;
48  }
49 
50  return true;
51  }
52 
53  public function getInput(): string
54  {
55  $value = trim($this->str($this->getPostVar()));
56  if ($this->getAcceptNamedColors() && substr($value, 0, 1) == "!") {
57  return $value;
58  }
59  return $this->determineHexcode($value);
60  }
61 
62  public function setValueByArray(array $a_values): void
63  {
64  $this->setValue($a_values[$this->getPostVar()]);
65  }
66 
67  public function setValue($a_value): void
68  {
69  $a_value = trim($a_value);
70  if ($this->getAcceptNamedColors() && substr($a_value, 0, 1) == "!") {
71  parent::setValue($a_value);
72  } else {
73  $this->hex = ilColorPickerInputGUI::determineHexcode($a_value);
74  parent::setValue($this->getHexcode());
75  }
76  }
77 
78  public function setDefaultColor(string $a_defaultcolor): void
79  {
80  $this->defaultcolor = $a_defaultcolor;
81  }
82 
83  public function getDefaultColor(): string
84  {
85  return $this->defaultcolor;
86  }
87 
88  // Set Accept Named Colors (Leading '!').
89  public function setAcceptNamedColors(bool $a_acceptnamedcolors): void
90  {
91  $this->acceptnamedcolors = $a_acceptnamedcolors;
92  }
93 
94  public function getAcceptNamedColors(): bool
95  {
97  }
98 
99  public function getHexcode(): string
100  {
101  if (strpos($this->hex, '#') === 0) {
102  return substr($this->hex, 1);
103  }
104  return $this->hex ?: $this->getDefaultColor();
105  }
106 
107  public static function determineHexcode(string $a_value): string
108  {
109  $a_value = trim(strtolower($a_value));
110 
111  // remove leading #
112  if (strpos($a_value, '#') === 0) {
113  $a_value = substr($a_value, 1);
114  }
115 
116  // handle standard color names (no leading (!))
117  switch ($a_value) {
118  // html4 colors
119  case "black": $a_value = "000000";
120  break;
121  case "maroon": $a_value = "800000";
122  break;
123  case "green": $a_value = "008000";
124  break;
125  case "olive": $a_value = "808000";
126  break;
127  case "navy": $a_value = "000080";
128  break;
129  case "purple": $a_value = "800080";
130  break;
131  case "teal": $a_value = "008080";
132  break;
133  case "silver": $a_value = "C0C0C0";
134  break;
135  case "gray": $a_value = "808080";
136  break;
137  case "red": $a_value = "ff0000";
138  break;
139  case "lime": $a_value = "00ff00";
140  break;
141  case "yellow": $a_value = "ffff00";
142  break;
143  case "blue": $a_value = "0000ff";
144  break;
145  case "fuchsia": $a_value = "ff00ff";
146  break;
147  case "aqua": $a_value = "00ffff";
148  break;
149  case "white": $a_value = "ffffff";
150  break;
151 
152  // other colors used by ILIAS, supported by modern browsers
153  case "brown": $a_value = "a52a2a";
154  break;
155  }
156 
157  // handle rgb values
158  if (substr($a_value, 0, 3) == "rgb") {
159  $pos1 = strpos($a_value, "(");
160  $pos2 = strpos($a_value, ")");
161  $rgb = explode(",", substr($a_value, $pos1 + 1, $pos2 - $pos1 - 1));
162  $r = str_pad(dechex((int) $rgb[0]), 2, "0", STR_PAD_LEFT);
163  $g = str_pad(dechex((int) $rgb[1]), 2, "0", STR_PAD_LEFT);
164  $b = str_pad(dechex((int) $rgb[2]), 2, "0", STR_PAD_LEFT);
165  $a_value = $r . $g . $b;
166  }
167 
168  $a_value = trim(strtolower($a_value));
169 
170  // expand three digit hex numbers
171  if (preg_match("/^[a-f0-9]{3}/", $a_value) && strlen($a_value) == 3) {
172  $a_value = "" . $a_value;
173  $a_value = $a_value[0] . $a_value[0] . $a_value[1] . $a_value[1] . $a_value[2] . $a_value[2];
174  }
175 
176  if (!preg_match("/^[a-f0-9]{6}/", $a_value)) {
177  $a_value = "";
178  }
179 
180  return strtoupper($a_value);
181  }
182 
183  public function insert(ilTemplate $a_tpl): void
184  {
185  $tpl = new ilTemplate('tpl.prop_color.html', true, true, 'components/ILIAS/Form');
186  $tpl->setVariable('COLOR_ID', $this->getFieldId());
188  if ($ic == "") {
189  $ic = "FFFFFF";
190  }
191  $tpl->setVariable('INIT_COLOR_SHORT', $ic);
192  $tpl->setVariable('POST_VAR', $this->getPostVar());
193 
194  if ($this->getDisabled()) {
195  $a_tpl->setVariable('COLOR_DISABLED', 'disabled="disabled"');
196  }
197 
198  $tpl->setVariable("POST_VAR", $this->getPostVar());
199  $tpl->setVariable("PROP_COLOR_ID", $this->getFieldId());
200 
201  if (substr(trim($this->getValue() ?? ""), 0, 1) == "!" && $this->getAcceptNamedColors()) {
202  $tpl->setVariable(
203  "PROPERTY_VALUE_COLOR",
205  );
206  } else {
207  $tpl->setVariable("PROPERTY_VALUE_COLOR", ilLegacyFormElementsUtil::prepareFormOutput($this->getHexcode()));
208  $tpl->setVariable('INIT_COLOR', '#' . $this->getHexcode());
209  }
210 
211  $a_tpl->setCurrentBlock("prop_generic");
212  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
213  $a_tpl->parseCurrentBlock();
214  }
215 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
setAcceptNamedColors(bool $a_acceptnamedcolors)
Color picker form for selecting color hexcodes using yui library.
setDefaultColor(string $a_defaultcolor)
__construct(string $a_title="", string $a_postvar="")
static prepareFormOutput($a_str, bool $a_strip=false)
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
static determineHexcode(string $a_value)
__construct(Container $dic, ilPlugin $plugin)
$r