ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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
12
17{
19 use Triggerer;
20
21 const DEPENDANT_FIELD_ERROR = 'ilradio_dependant_field_error';
22
26 protected $options = [];
27
31 protected $bylines = [];
32
36 protected $dependant_fields = [];
37
41 protected function isClientSideValueOk($value)
42 {
43 return ($value === '' || array_key_exists($value, $this->getOptions()));
44 }
45
49 protected function getConstraintForRequirement()
50 {
51 return null;
52 }
53
58 {
59 $clone = parent::withNameFrom($source);
60
61 foreach ($clone->dependant_fields as $option_value => $fields) {
62 $named_inputs = [];
63 foreach ($fields as $key => $input) {
64 $named_inputs[$key] = $input->withNameFrom($source);
65 }
66 $clone->dependant_fields[$option_value] = $named_inputs;
67 }
68 return $clone;
69 }
70
74 public function withOption(string $value, string $label, string $byline = null, $dependant_fields = null) : C\Input\Field\Radio
75 {
76 $clone = clone $this;
77 $clone->options[$value] = $label;
78 if (!is_null($byline)) {
79 $clone->bylines[$value] = $byline;
80 }
81 if (!is_null($dependant_fields)) {
82 $clone->dependant_fields[$value] = $dependant_fields;
83 }
84 return $clone;
85 }
86
90 public function getOptions() : array
91 {
92 return $this->options;
93 }
94
95
96 public function getBylineFor(string $value)
97 {
98 if (!array_key_exists($value, $this->bylines)) {
99 return null;
100 }
101 return $this->bylines[$value];
102 }
103
104
108 public function getDependantFieldsFor(string $value)
109 {
110 if (!array_key_exists($value, $this->dependant_fields)) {
111 return null;
112 }
113 return $this->dependant_fields[$value];
114 }
115
119 public function withInput(PostData $post_input)
120 {
121 if ($this->getName() === null) {
122 throw new \LogicException("Can only collect if input has a name.");
123 }
124 $value = $post_input->getOr($this->getName(), "");
125
126 $clone = $this->withValue($value);
127
128 $clone->content = $this->applyOperationsTo($value);
129 if ($clone->content->isError()) {
130 return $clone->withError("" . $clone->content->error());
131 }
132
133 $dep_fields = $this->getDependantFieldsFor($value);
134 if (is_null($dep_fields)) {
135 $clone->content = $this->applyOperationsTo($value);
136 } else {
137 $values = [
138 'value' => $value,
139 'group_values' => []
140 ];
141
142 foreach ($dep_fields as $name => $field) {
143 $filled = $field->withInput($post_input);
144 $content = $filled->getContent();
145
146 if ($content->isOk()) {
147 $values['group_values'][$name] = $content->value();
148 } else {
149 $clone = $clone->withError(self::DEPENDANT_FIELD_ERROR);
150 }
151
152 $clone->dependant_fields[$value][$name] = $filled;
153 }
154 $clone->content = $clone->applyOperationsTo($values);
155 }
156
157 if ($clone->getError()) {
158 $clone->content = $clone->data_factory->error($clone->getError());
159 }
160
161 return $clone;
162 }
163}
$source
Definition: linkback.php:22
An exception for terminatinating execution or to throw for unit testing.
withOption(string $value, string $label, string $byline=null, $dependant_fields=null)
Definition: Radio.php:74
$key
Definition: croninfo.php:18
This describes commonalities between all inputs.
Definition: Input.php:31
withValue($value)
Get an input like this with another value displayed on the client side.
This is what a radio-input looks like.
Definition: Radio.php:11
getOptions()
Get all options as value=>label.
getDependantFieldsFor(string $value)
Get dependant fields for a single option.
Interface to be extended by components that have the possibility to bind to Javascript.
Describes a source for input names.
Definition: NameSource.php:11
Describes how Input-Elements want to interact with posted data.
Definition: PostData.php:13
getOr($name, $default)
Get a named value from the data and fallback to default if that name does not exist.
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
$values