ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
EntryForm.php
Go to the documentation of this file.
1 <?php
2 
20 
26 
27 class EntryForm
28 {
29  private readonly Factory $ui_factory;
30  private readonly \ILIAS\Refinery\Factory $refinery;
31  private ?Standard $form = null;
32 
33  public function __construct(
34  private readonly EntriesRepository $repository,
35  private readonly Translator $translator,
36  private readonly Group $group,
37  private ?Entry $entry = null,
38  ) {
39  global $DIC;
40  $this->ui_factory = $DIC->ui()->factory();
41  $this->refinery = $DIC->refinery();
42  $this->entry ??= $this->repository->blank();
43  }
44 
45  protected function getInputs(): array
46  {
47  $ff = $this->ui_factory->input()->field();
48 
49  $inputs = [
50  'title' => $ff
51  ->text($this->translator->translate('title', 'entry'))
52  ->withValue($this->entry->getTitle())
54  $this->refinery->custom()->transformation(function (string $value): string {
55  $this->entry = $this->entry->withTitle($value);
56  return $value;
57  })
58  )
59  ->withRequired(true)
60  ->withMaxLength(255),
61  'active' => $ff
62  ->checkbox(
63  $this->translator->translate('active', 'entry'),
64  $this->translator->translate('active_info', 'entry')
65  )
66  ->withValue($this->entry->isActive())
68  $this->refinery->custom()->transformation(function (bool $value): bool {
69  $this->entry = $this->entry->withActive($value);
70  return $value;
71  })
72  ),
73  'action' => $ff
74  ->url(
75  $this->translator->translate('action', 'entry'),
76  $this->translator->translate('action_info', 'entry')
77  )
78  ->withRequired(true)
79  ->withValue($this->entry->getAction())
81  $this->refinery->custom()->transformation(function (string $value): string {
82  $this->entry = $this->entry->withAction($value);
83  return $value;
84  })
85  ),
86  'external' => $ff
87  ->checkbox($this->translator->translate('external', 'entry'))
88  ->withValue($this->entry->isExternal())
90  $this->refinery->custom()->transformation(function (bool $value): bool {
91  $this->entry = $this->entry->withExternal($value);
92  return $value;
93  })
94  ),
95  ];
96 
97  return [
98  $ff->section($inputs, $this->translator->translate($this->entry->getId() === '' ? 'add' : 'edit', 'entry'))
99  ];
100  }
101 
102  public function get(string $target): Standard
103  {
104  return $this->form ?? $this->ui_factory
105  ->input()
106  ->container()
107  ->form()
108  ->standard($target, $this->getInputs())
109  ->withAdditionalTransformation(
110  $this->refinery->custom()->transformation(fn(array $value): ?Entry => $this->entry)
111  );
112  }
113 
114  public function store(RequestInterface $request, string $target): bool
115  {
116  $this->form = $this->get($target)->withRequest($request);
117  $data = $this->form->getData();
118  if ($data !== null) {
119  $this->entry = $this->entry->withParent($this->group->getId());
120  $this->repository->store($this->entry);
121  return true;
122  }
123  return false;
124  }
125 
126 }
repository()
description: > Example for rendering a repository card
Definition: repository.php:33
__construct(private readonly EntriesRepository $repository, private readonly Translator $translator, private readonly Group $group, private ?Entry $entry=null,)
Definition: EntryForm.php:33
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
store(RequestInterface $request, string $target)
Definition: EntryForm.php:114
This is how the factory for UI elements looks.
Definition: Factory.php:37
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:22
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:61
This describes a standard form.
Definition: Standard.php:28
form( $class_path, string $cmd, string $submit_caption="")
readonly ILIAS Refinery Factory $refinery
Definition: EntryForm.php:30