ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
Sequence.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24use ILIAS\UI\Implementation\Component\ComponentHelper;
25use Psr\Http\Message\ServerRequestInterface;
28use ILIAS\Data\Factory as DataFactory;
29use ILIAS\Refinery\Factory as Refinery;
34
36{
37 use ComponentHelper;
38
39 private const PARAM_POSITION = 'p';
40 public const STORAGE_ID_PREFIX = self::class . '_';
41
42 protected ?ServerRequestInterface $request = null;
43 protected ?URLBuilder $url_builder = null;
45 protected ?ViewControlContainer $viewcontrols = null;
46 protected ?array $actions = null;
47 protected int $position = 0;
48 protected ?string $id = null;
49
50 public function __construct(
51 protected DataFactory $data_factory,
52 protected Refinery $refinery,
53 protected Storage $storage,
54 protected ISequence\SegmentRetrieval $segment_retrieval,
55 protected ?string $title
56 ) {
57 }
58
59 public function getSegmentRetrieval(): ISequence\SegmentRetrieval
60 {
61 return $this->segment_retrieval;
62 }
63
64 public function getCurrentPosition(): int
65 {
66 return $this->position;
67 }
68
69 public function withCurrentPosition(int $position): static
70 {
71 $clone = clone $this;
72 $clone->position = $position;
73 return $clone;
74 }
75
76 public function withViewControls(ViewControlContainer $viewcontrols): static
77 {
78 $clone = clone $this;
79 $clone->viewcontrols = $viewcontrols;
80 return $clone;
81 }
82
83 public function getViewControls(): ?ViewControlContainer
84 {
86 }
87
88 public function withActions(...$actions): static
89 {
90 $clone = clone $this;
91 $clone->actions = $actions;
92 return $clone;
93 }
94
95 public function getActions(): ?array
96 {
97 return $this->actions;
98 }
99
100 protected function checkRequest(): void
101 {
102 if (! $this->request) {
103 throw new \LogicException('no request was set on the sequence');
104 }
105 }
106
107 public function withRequest(ServerRequestInterface $request): static
108 {
109 $clone = clone $this;
110 $clone->request = $request;
111 $clone->initFromRequest();
112 return $clone;
113 }
114
115 public function getRequest(): ServerRequestInterface
116 {
117 $this->checkRequest();
118 return $this->request;
119 }
120
121 protected function initFromRequest(): void
122 {
123 $base_uri = $this->data_factory->uri($this->request->getUri()->__toString());
124 $namespace = ['sequence_' . $this->getId() ?? ''];
125 $url_builder = new URLBuilder($base_uri);
126 list(
127 $this->url_builder,
128 $this->token_position
131 self::PARAM_POSITION
132 );
133
134 $query = new \ILIAS\HTTP\Wrapper\ArrayBasedRequestWrapper($this->request->getQueryParams());
135 $this->position = $query->retrieve(
136 $this->token_position->getName(),
137 $this->refinery->byTrying([
138 $this->refinery->kindlyTo()->int(),
139 $this->refinery->always(0)
140 ])
141 );
142
143 if ($this->viewcontrols) {
144 $this->viewcontrols = $this->applyValuesToViewcontrols(
145 $this->viewcontrols,
146 $this->request
147 );
148 }
149 }
150
151 public function getNext(int $direction): URI
152 {
153 $this->checkRequest();
154 return $this->url_builder
155 ->withParameter($this->token_position, (string) ($this->position + $direction))
156 ->buildURI();
157 }
158
159 protected function getStorageData(): ?array
160 {
161 if (null !== ($storage_id = $this->getStorageId())) {
162 return $this->storage[$storage_id] ?? null;
163 }
164 return null;
165 }
166
167 protected function setStorageData(array $data): void
168 {
169 if (null !== ($storage_id = $this->getStorageId())) {
170 $this->storage[$storage_id] = $data;
171 }
172 }
173
174 protected function getStorageId(): ?string
175 {
176 if (null !== ($id = $this->getId())) {
177 return static::STORAGE_ID_PREFIX . $id;
178 }
179 return null;
180 }
181
182 public function withId(string $id): static
183 {
184 $clone = clone $this;
185 $clone->id = $id;
186 return $clone;
187 }
188
189 protected function getId(): ?string
190 {
191 return $this->id;
192 }
193
194 public function getTitle(): ?string
195 {
196 return $this->title;
197 }
198
199 protected function applyValuesToViewcontrols(
200 ViewControlContainer $view_controls,
201 ServerRequestInterface $request
202 ): ViewControlContainer {
203 $stored_values = new ArrayInputData($this->getStorageData() ?? []);
204 $view_controls = $view_controls
205 ->withStoredInput($stored_values)
206 ->withRequest($request);
207 $this->setStorageData($view_controls->getComponentInternalValues());
208 return $view_controls;
209 }
210
211}
Builds data types.
Definition: Factory.php:36
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
applyValuesToViewcontrols(ViewControlContainer $view_controls, ServerRequestInterface $request)
Definition: Sequence.php:199
withRequest(ServerRequestInterface $request)
Rendering the sequence must be done using the current request: it (the request) will carry parameters...
Definition: Sequence.php:107
withId(string $id)
The Sequence comes with a storage to keep ViewControl-settings throughout requests.
Definition: Sequence.php:182
__construct(protected DataFactory $data_factory, protected Refinery $refinery, protected Storage $storage, protected ISequence\SegmentRetrieval $segment_retrieval, protected ?string $title)
Definition: Sequence.php:50
withViewControls(ViewControlContainer $viewcontrols)
You may add view controls to the sequences's player that alter the way, order or focus in which the s...
Definition: Sequence.php:76
acquireParameters(array $namespace, string ... $names)
Definition: URLBuilder.php:138
if($err=$client->getError()) $namespace
This describes a View Control Container.
Definition: ViewControl.php:30
Storage is simple key/value store without further schema definition.
Definition: Storage.php:30