ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
Radio.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2018 Nils Haagen <nils.haagen@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4 
6 
7 use ILIAS\UI\Component as C;
13 
17 class Radio extends Input implements C\Input\Field\Radio
18 {
20  use Triggerer;
21 
25  protected $options = [];
26 
30  protected $bylines = [];
31 
35  protected function isClientSideValueOk($value) : bool
36  {
37  return ($value === '' || array_key_exists($value, $this->getOptions()));
38  }
39 
43  protected function getConstraintForRequirement()
44  {
45  return null;
46  }
47 
51  public function withOption(string $value, string $label, string $byline = null) : C\Input\Field\Radio
52  {
53  $clone = clone $this;
54  $clone->options[$value] = $label;
55  if (!is_null($byline)) {
56  $clone->bylines[$value] = $byline;
57  }
58  return $clone;
59  }
60 
64  public function getOptions() : array
65  {
66  return $this->options;
67  }
68 
69 
70  public function getBylineFor(string $value)
71  {
72  if (!array_key_exists($value, $this->bylines)) {
73  return null;
74  }
75  return $this->bylines[$value];
76  }
77 
81  public function withInput(InputData $post_input)
82  {
83  if ($this->getName() === null) {
84  throw new \LogicException("Can only collect if input has a name.");
85  }
86  if (!$this->isDisabled()) {
87  $value = $post_input->getOr($this->getName(), "");
88  $clone = $this->withValue($value);
89  } else {
90  $value = $this->getValue();
91  $clone = $this;
92  }
93 
94  $clone->content = $this->applyOperationsTo($value);
95  if ($clone->content->isError()) {
96  return $clone->withError("" . $clone->content->error());
97  }
98 
99  $clone->content = $this->applyOperationsTo($value);
100 
101  if ($clone->getError()) {
102  $clone->content = $clone->data_factory->error($clone->getError());
103  }
104 
105  return $clone;
106  }
107 
111  public function getUpdateOnLoadCode() : \Closure
112  {
113  return function ($id) {
114  $code = "$('#$id').on('input', function(event) {
115  il.UI.input.onFieldUpdate(event, '$id', $('#$id input:checked').val());
116  });
117  il.UI.input.onFieldUpdate(event, '$id', $('#$id input:checked').val());";
118  return $code;
119  };
120  }
121 }
isDisabled()
Is this input disabled?
getOptions()
Get all options as value=>label.
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
getOr($name, $default)
Get a named value from the data and fallback to default if that name does not exist.
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:12
This is what a radio-input looks like.
Definition: Radio.php:10
withValue($value)
Get an input like this with another value displayed on the client side.
withOption(string $value, string $label, string $byline=null)
Definition: Radio.php:51
getUpdateOnLoadCode()
Get update code.This method has to return JS code that calls il.UI.filter.onFieldUpdate(event, &#39;$id&#39;, string_value);initially "onload" andon every input change. It must pass a readable string representation of its value in parameter &#39;string_value&#39;.string
Definition: Radio.php:111
getValue()
Get the value that is displayed in the input client side.