ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilCtrlContext.php
Go to the documentation of this file.
1 <?php
2 
18 declare(strict_types=1);
19 
22 
31 {
36 
41 
45  protected Refinery $refinery;
46 
51 
55  protected bool $is_async = false;
56 
60  protected string $target_script = 'ilias.php';
61 
65  protected ?string $cmd_mode = null;
66 
70  protected ?string $redirect = null;
71 
75  protected ?string $base_class = null;
76 
80  protected ?string $cmd_class = null;
81 
85  protected ?string $cmd = null;
86 
90  protected ?string $obj_type = null;
91 
95  protected ?int $obj_id = null;
96 
104  public function __construct(ilCtrlPathFactory $path_factory, RequestWrapper $request_wrapper, Refinery $refinery)
105  {
106  $this->path_factory = $path_factory;
107  $this->path = $path_factory->null();
108  $this->request_wrapper = $request_wrapper;
109  $this->refinery = $refinery;
110 
111  $this->adoptRequestParameters();
112  }
113 
117  public function isAsync(): bool
118  {
119  return $this->is_async;
120  }
121 
125  public function getRedirectSource(): ?string
126  {
127  return $this->redirect;
128  }
129 
133  public function getPath(): ilCtrlPathInterface
134  {
135  return $this->path;
136  }
137 
141  public function setCmdMode(string $mode): ilCtrlContextInterface
142  {
143  $this->cmd_mode = $mode;
144  return $this;
145  }
146 
150  public function getCmdMode(): ?string
151  {
152  return $this->cmd_mode;
153  }
154 
158  public function setBaseClass(string $base_class): ilCtrlContextInterface
159  {
160  $path = $this->path_factory->find($this, $base_class);
161 
162  // only set baseclass if it's a valid target.
163  if (null !== $path->getCidPath()) {
164  $this->base_class = $base_class;
165  // only update the path if the current one is null.
166  if (null === $this->path->getCidPath()) {
167  $this->path = $path;
168  }
169  }
170 
171  return $this;
172  }
173 
177  public function getBaseClass(): ?string
178  {
179  return $this->base_class;
180  }
181 
185  public function setTargetScript(string $target_script): ilCtrlContextInterface
186  {
187  $this->target_script = $target_script;
188  return $this;
189  }
190 
194  public function getTargetScript(): string
195  {
196  return $this->target_script;
197  }
198 
202  public function setCmdClass(?string $cmd_class): ilCtrlContextInterface
203  {
204  $path = $this->path_factory->find($this, $cmd_class);
205 
206  // only set command class if it's a valid target.
207  if (null !== $path->getCidPath()) {
208  $this->cmd_class = $cmd_class;
209  $this->path = $path;
210  }
211 
212  return $this;
213  }
214 
218  public function getCmdClass(): ?string
219  {
220  // if no cmd_class is set yet, the baseclass
221  // value can be returned.
222  return $this->cmd_class ?? $this->base_class;
223  }
224 
228  public function setCmd(?string $cmd): ilCtrlContextInterface
229  {
230  $this->cmd = $cmd;
231 
232  return $this;
233  }
234 
238  public function getCmd(): ?string
239  {
240  return $this->cmd;
241  }
242 
246  public function setObjType(string $obj_type): ilCtrlContextInterface
247  {
248  $this->obj_type = $obj_type;
249  return $this;
250  }
251 
255  public function getObjType(): ?string
256  {
257  return $this->obj_type;
258  }
259 
263  public function setObjId(int $obj_id): ilCtrlContextInterface
264  {
265  $this->obj_id = $obj_id;
266  return $this;
267  }
268 
272  public function getObjId(): ?int
273  {
274  return $this->obj_id;
275  }
276 
285  protected function adoptRequestParameters(): void
286  {
287  $this->redirect = $this->getQueryParam(ilCtrlInterface::PARAM_REDIRECT);
288  $this->cmd_mode = $this->getQueryParam(ilCtrlInterface::PARAM_CMD_MODE);
289  $this->cmd = $this->getQueryParam(ilCtrlInterface::PARAM_CMD);
290 
291  // the context is considered asynchronous if
292  // the command mode is 'async'.
293  if (ilCtrlInterface::CMD_MODE_ASYNC === $this->cmd_mode) {
294  $this->is_async = true;
295  }
296 
297  // if an existing path is provided use it by default.
298  $existing_path = $this->getQueryParam(ilCtrlInterface::PARAM_CID_PATH);
299  if (null !== $existing_path) {
300  $this->path = $this->path_factory->existing($existing_path);
301  }
302 
303  // set the provided baseclass, which might override the
304  // previously set existing path.
305  $base_class = $this->getQueryParam(ilCtrlInterface::PARAM_BASE_CLASS);
306  if (null !== $base_class) {
307  $this->setBaseClass($base_class);
308  }
309 
310  // set or append the provided command class, which might
311  // override the previously set path again.
312  $cmd_class = $this->getQueryParam(ilCtrlInterface::PARAM_CMD_CLASS);
313  if (null !== $cmd_class) {
314  $this->setCmdClass($cmd_class);
315  }
316  }
317 
324  protected function getQueryParam(string $parameter_name): ?string
325  {
326  if ($this->request_wrapper->has($parameter_name)) {
327  return $this->request_wrapper->retrieve(
328  $parameter_name,
329  $this->refinery->to()->string()
330  );
331  }
332 
333  return null;
334  }
335 }
__construct(ilCtrlPathFactory $path_factory, RequestWrapper $request_wrapper, Refinery $refinery)
ilCtrlContext Constructor
getCidPath()
Returns the CID path for the target class of the current instance.
Class ilCtrlPathFactory.
Class ilCtrlContext is responsible for holding the current context information.
setBaseClass(string $base_class)
RequestWrapper $request_wrapper
adoptRequestParameters()
Adopts context properties from the according ones delivered by the current request.
getQueryParam(string $parameter_name)
Helper function to retrieve request parameter values by name.
setObjType(string $obj_type)
setCmdClass(?string $cmd_class)
Interface RequestWrapper.
setTargetScript(string $target_script)
setCmd(?string $cmd)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilCtrlPathFactory $path_factory
setObjId(int $obj_id)
ilCtrlPathInterface $path
Interface ilCtrlPathInterface is responsible for holding and manipulating a valid ilCtrl class-path (...
setCmdMode(string $mode)