ILIAS  trunk Revision v12.0_alpha-16-g3e876e53c80
Form.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24use ILIAS\GlobalScreen\GUI\I18n\Translator;
25use Psr\Http\Message\ServerRequestInterface;
27use Generator;
33
37class Form implements toComponent
38{
39 public const string F_ALIAS = 'alias';
40 public const string F_ACTIVE = 'active';
41 public const string ACTION_TOGGLE = 'toggle';
42 public const string ACTION_DELETE = 'delete';
43 public const string ACTION_EDIT = 'edit';
44 public const string F_TARGET_REF_ID = 'target_ref_id';
46 private Translator $i18n;
47 private ServerRequestInterface $request;
48 private \ILIAS\Refinery\Factory $refinery;
50
51 public function __construct(
52 Pons $pons,
53 private Repository $repository,
54 private URI $target,
55 private Shortlink $shortlink,
56 ) {
57 $this->ui_factory = $pons->out()->ui()->factory();
58 $this->i18n = $pons->i18n();
59 $this->request = $pons->in()->request();
60 $this->refinery = $pons->in()->refinery();
61 $this->validator = new Validator();
62 }
63
64 protected function getFields(): array
65 {
66 // Tsrget Ref-ID Selection
67 $node_retrieval = new \NodeRetrievalGUI();
68 $current_ref_id_value = $this->shortlink->getTargetData()['ref_id'] ?? null;
69 $tree_select = $this
70 ->ui_factory
71 ->input()
72 ->field()
73 ->treeSelect(
74 $node_retrieval->getNodeRetrieval(),
75 $this->i18n->t(self::F_TARGET_REF_ID),
76 $this->i18n->t(self::F_TARGET_REF_ID, 'info'),
77 )
78 // ->withRequired(true) //Currently not possible due to tree select implementation
79 ->withAdditionalTransformation(
80 $this->refinery->custom()->constraint(
81 fn($d): bool => !empty($d),
82 $this->i18n->t('target_ref_id_required')
83 )
84 )
85 ->withAdditionalTransformation(
86 $this->refinery->custom()->transformation(
87 fn($d): Shortlink => $this->shortlink = $this
88 ->shortlink
89 ->withTargetData(
90 $this
92 ->typeDataRevolver()
93 ->resolveForRefId(
94 (int) ($d[0])
95 )
96 )
97 )
98 );
99
100 if (!empty($current_ref_id_value)) {
101 $tree_select = $tree_select->withValue(
102 $current_ref_id_value
103 );
104 }
105
106 // Alias aka Shortlink-name
107 $alias = $this
108 ->ui_factory
109 ->input()
110 ->field()
111 ->text(
112 $this->i18n->t(self::F_ALIAS),
113 $this->i18n->t(self::F_ALIAS, 'info')
114 )
115 ->withValue(
116 $this->shortlink->getAlias()
117 );
118
119 if (empty($this->shortlink->getId())) {
120 $alias = $alias
121 ->withAdditionalTransformation(
122 $this->refinery->custom()->constraint(
123 fn($d): bool => !$this->repository->has($d),
124 $this->i18n->t('alias_already_exists')
125 )
126 );
127 }
128
129 $alias = $alias
130 ->withAdditionalTransformation(
131 $this->refinery->custom()->constraint(
132 fn($d): bool => $this->validator->isValid($d),
133 $this->i18n->t('alias_invalid')
134 )
135 )
136 ->withAdditionalTransformation(
137 $this->refinery->custom()->transformation(
138 fn($d): Shortlink => $this->shortlink = $this->shortlink->withAlias($d)
139 )
140 );
141
142 // Active status
143 $active = $this
144 ->ui_factory
145 ->input()
146 ->field()
147 ->checkbox(
148 $this->i18n->t(self::F_ACTIVE),
149 $this->i18n->t(self::F_ACTIVE, 'info')
150 )
151 ->withValue(
152 $this->shortlink->isActive()
153 )
154 ->withAdditionalTransformation(
155 $this->refinery->custom()->transformation(
156 fn($d): Shortlink => $this->shortlink = $this->shortlink->withActive($d)
157 )
158 );
159
160 return [
161 self::F_ALIAS => $alias,
162 self::F_TARGET_REF_ID => $tree_select,
163 self::F_ACTIVE => $active
164 ];
165 }
166
167 protected function getForm(): Standard
168 {
169 return $this
170 ->ui_factory
171 ->input()
172 ->container()
173 ->form()
174 ->standard(
175 (string) $this->target,
176 $this->getFields()
178 $this->refinery->custom()->transformation(fn($d): Shortlink => $this->shortlink)
179 );
180 }
181
182 public function save(): ?Standard
183 {
184 $form = $this
185 ->getForm()
186 ->withRequest(
187 $this->request
188 );
189
190 $data = $form->getData();
191 if ($data instanceof Shortlink) {
192 $this->repository->store(
193 $data
194 );
195 return null;
196 }
197 return $form;
198 }
199
200 public function get(): \Generator
201 {
202 yield $this->getForm();
203 }
204
205}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
withRequest(ServerRequestInterface $request)
Get a form like this where data from the request is attached.
withAdditionalTransformation(Transformation $trafo)
Apply a transformation to the data of the form.
This describes a standard form.
Definition: Standard.php:30
This is how the factory for UI elements looks.
Definition: Factory.php:38