ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
data_processing.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
6 
20 function data_processing()
21 {
22  //Step 0: Declare dependencies
23  global $DIC;
24  $ui = $DIC->ui()->factory();
25  $renderer = $DIC->ui()->renderer();
26  $request = $DIC->http()->request();
27  $refinery = $DIC->refinery();
28  //Step 1: Define transformations
29  $sum = $refinery->custom()->transformation(function ($vs) {
30  list($l, $r) = $vs;
31  $s = $l + $r;
32  return "$l + $r = $s";
33  });
34 
35  $from_name = $refinery->custom()->transformation(function ($v) {
36  switch ($v) {
37  case "one": return 1;
38  case "two": return 2;
39  case "three": return 3;
40  case "four": return 4;
41  case "five": return 5;
42  case "six": return 6;
43  case "seven": return 7;
44  case "eight": return 8;
45  case "nine": return 9;
46  case "ten": return 10;
47  }
48  throw new \LogicException("PANIC!");
49  });
50 
51  //Step 2: Define custom constraint
52  $valid_number = $refinery->custom()->constraint(function ($v) {
53  return in_array($v, ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]);
54  }, "This is not a number I know...");
55 
56  //Step 3: Define the input field and attach the previously defined constraint an
57  // validation.
58  $number_input = $ui->input()->field()
59  ->text("number", "Put in the name of a number from one to ten.")
60  ->withAdditionalTransformation($valid_number)
61  ->withAdditionalTransformation($from_name);
62 
63  //Step 4: Define the form action to target the input processing
64  $DIC->ctrl()->setParameterByClass(
65  'ilsystemstyledocumentationgui',
66  'example_name',
67  'data_processing'
68  );
69  $form_action = $DIC->ctrl()->getFormActionByClass('ilsystemstyledocumentationgui');
70 
71  //Step 5: Define the form, plugin the inputs and attach some transformation acting
72  // on the complete input of the form.
73  $form = $ui->input()->container()->form()->standard(
74  $form_action,
75  [ $number_input->withLabel("Left")
76  , $number_input->withLabel("Right")
77  ]
79 
80  //Step 6: Define some data processing.
81  if ($request->getMethod() == "POST"
82  && array_key_exists('example_name', $request->getQueryParams())
83  && $request->getQueryParams()['example_name'] == 'data_processing') {
84  $form = $form->withRequest($request);
85  $result = $form->getData();
86  } else {
87  $result = "No result yet.";
88  }
89 
90  //Step 7: Render the form and the result of the data processing
91  return
92  "<pre>" . print_r($result, true) . "</pre><br/>" .
93  $renderer->render($form);
94 }
$renderer
data_processing()
description: > Example showing how constraints and transformation can be attached to a form...
global $DIC
Definition: shib_login.php:25
$r