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