Elgg  Version master
ActionMiddleware.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
12 
16  protected $request;
17 
21  protected $form_name;
22 
31  public function __invoke(\Elgg\Request $request): void {
32  $this->request = $request;
33 
34  $route = $request->getRoute();
35  list(, $action) = explode(':', $route, 2);
36 
37  // save sticky form values
38  $this->prepareStickyForm();
39 
40  $result = $request->elgg()->events->triggerResults('action:validate', $action, ['request' => $request], true);
41  if ($result === false) {
42  throw new ValidationException();
43  }
44 
45  // set the maximum execution time for actions
46  $action_timeout = $request->elgg()->config->action_time_limit;
47  if (isset($action_timeout)) {
48  set_time_limit($action_timeout);
49  }
50  }
51 
58  protected function prepareStickyForm(): void {
59  $this->form_name = $this->request->getParam('_elgg_sticky_form_name');
60 
61  if (empty($this->form_name)) {
62  return;
63  }
64 
65  // add user and system ignored fields
66  $ignored_fields = (string) $this->request->getParam('_elgg_sticky_ignored_fields');
67  $ignored_fields = elgg_string_to_array($ignored_fields);
68 
69  _elgg_services()->stickyForms->makeStickyForm($this->form_name, $ignored_fields);
70 
71  // register sticky value cleanup
72  $this->request->elgg()->events->registerHandler('response', 'all', [$this, 'cleanupStickyValues']);
73  }
74 
83  public function cleanupStickyValues(\Elgg\Event $event): void {
84  $response = $event->getValue();
85  if (!$response instanceof OkResponse) {
86  return;
87  }
88 
89  _elgg_services()->stickyForms->clearStickyForm($this->form_name);
90  }
91 }
Models an event passed to event handlers.
Definition: Event.php:11
OK response builder.
Definition: OkResponse.php:8
Request container.
Definition: Request.php:12
Some logic implemented before action is executed.
cleanupStickyValues(\Elgg\Event $event)
Automatically cleanup sticky form values after a successfull action.
__invoke(\Elgg\Request $request)
Pre-action logic.
prepareStickyForm()
Save the action input in sticky form values.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353
elgg_string_to_array(string $string)
Takes in a comma-separated string and returns an array of uniquely trimmed and stripped strings.
Definition: input.php:226
$action
Definition: subscribe.php:11
$response
Definition: content.php:10