ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilMMTopItemGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26
34{
35 public const CMD_RESTORE = 'restore';
36 public const CMD_SELECT_PARENT = 'selectParent';
37 public const CMD_MOVE = 'move';
38
40
41 public function __construct(Pons $pons)
42 {
44 $this->top_token = $this->pons->in()->buildToken('top', 'id');
45 }
46
47 #[\Override]
48 public function executeCommand(): bool
49 {
50 if ($this->pons->handle('top_items')) {
51 return true;
52 }
53
54 $command = $this->flow->getCommand(self::CMD_DEFAULT);
55 match ($command) {
56 self::CMD_SELECT_PARENT => $this->selectParent(),
57 self::CMD_MOVE => $this->move(),
58 self::CMD_RESTORE => $this->restore(),
59 default => parent::executeCommand()
60 };
61 return true;
62 }
63
64 public function getMutlipleItems(): array
65 {
66 $ids = $this->pons->in()->getAllFromRequest($this->top_token->token());
67 if (($ids[0] ?? null) === Input::ALL_OBJECTS) {
68 return array_map(
69 fn(array $data): \ilMMItemFacadeInterface => $this->repository->getItemFacadeForIdentificationString($data['identification']),
70 $this->repository->getTopItems()
71 );
72 }
73 return $this->repository->getItemFacadesForIdentificationStrings($ids);
74 }
75
77 {
78 return $this->repository->getItemFacadeForIdentificationString(
79 $this->pons->in()->getFirstFromRequest($this->top_token->token())
80 );
81 }
82
83 public function getTokensToKeep(): array
84 {
85 return [
86 $this->top_token->token(),
87 ];
88 }
89
90 protected function buildForm(): ilMMTopItemFormGUI
91 {
92 $item = $this->repository->getItemFacadeForIdentificationString(
93 $this->pons->in()->getFirstFromRequest($this->top_token->token())
94 );
95 return new ilMMTopItemFormGUI(
96 $this->flow->ctrl(),
97 $this->ui->factory(),
98 $this->ui->renderer(),
99 $this->lng,
100 $this->pons->in()->request(),
101 $item,
102 $this->repository
103 );
104 }
105
106 #[Command('write')]
107 protected function form(bool $save = false): void
108 {
109 if (!$save) {
110 $this->pons->in()->keep($this->top_token->token());
111 }
112
113 $form = $this->buildForm();
114
115 if ($save && $form->save()) {
116 $this->pons->out()->success($this->lng->t('item_updated'), true);
117 $this->pons->flow()->redirect(self::CMD_DEFAULT);
118 }
119
120 $this->pons->out()->outAsyncAsModal(
121 $this->pons->i18n()->txt('edit'),
122 $this->flow->getLinkTarget($this, self::CMD_UPDATE),
123 $form->get()
124 );
125 }
126
127 #[Command('read')]
128 protected function index(): void
129 {
130 $write_access = $this->access->hasUserPermissionTo('write');
131 if ($write_access) {
132 // ADD NEW
133 $form = $this->buildForm();
134 $add_modal = $this->ui->factory()->modal()->roundtrip(
135 $this->lng->t('topitem_add'),
136 null,
137 $form->get()->getInputs(),
138 $this->flow->getLinkTarget($this, self::CMD_CREATE)
139 );
140
141 $btn_add = $this->ui->factory()->button()->primary(
142 $this->lng->t('topitem_add'),
143 $add_modal->getShowSignal()
144 );
145 $this->toolbar->addComponent($add_modal);
146 $this->toolbar->addComponent($btn_add);
147
148 // RESTORE
149 $restoration_modal = $this->ui->factory()->modal()->interruptive(
150 $this->lng->t(self::CMD_RESTORE),
151 $this->lng->t('msg_restore_confirm'),
152 $this->flow->getLinkTarget($this, self::CMD_RESTORE)
153 )->withActionButtonLabel($this->lng->t('restore'));
154 $btn_restore = $this->ui->factory()->button()->standard(
155 $this->lng->t(self::CMD_RESTORE),
156 ''
157 )->withOnClick(
158 $restoration_modal->getShowSignal()
159 );
160 $this->toolbar->addComponent($btn_restore);
161 $this->toolbar->addComponent($restoration_modal);
162
163 // REMOVE LOST ITEMS
164 if ($this->repository->hasLostItems()) {
165 $btn_flush = $this->ui->factory()->button()->standard(
166 $this->lng->txt(self::CMD_FLUSH),
167 $this->flow->getLinkTarget($this, self::CMD_FLUSH)
168 );
169 $this->toolbar->addComponent($btn_flush);
170 }
171 }
172
173 $table = new ilMMTopItemTableComponent(
174 $this->pons,
175 $this->top_token,
176 $this->repository,
177 $write_access
178 );
179
180 $this->pons->out()->out(...$table->get());
181 }
182
183 #[Command('write')]
184 private function selectParent(): string
185 {
186 $this->keepTokens();
187
188 $this->pons->out()->outAsyncAsModal(
189 $this->lng->txt('select_parent'),
190 $this->flow->getLinkTarget($this, self::CMD_MOVE),
191 $this->getMoveForm()
192 );
193 }
194
195 #[Command('write')]
196 private function move(): void
197 {
198 $item = $this->getCurrentItem();
199
200 $data = $this->getMoveForm()
201 ->withRequest($this->pons->in()->request())
202 ->getData();
203
204 if (isset($data[0]) && $item->isInterchangeable()) {
205 $f = $this->repository->getItemFacadeForIdentificationString($data[0]);
206 $item->setParent($data[0]);
207 $this->repository->updateItem($item);
208 $this->pons->out()->success($this->lng->txt('msg_moved'), true);
209 } else {
210 $this->pons->out()->error($this->lng->txt('msg_not_moved'), true);
211 }
212
213 $this->pons->flow()->redirect(self::CMD_DEFAULT);
214 }
215
216 private function getMoveForm(): Standard
217 {
218 $f = $this->ui->factory();
219 $parent = $f->input()
220 ->field()
221 ->select(
222 $this->lng->txt('select_parent'),
223 $this->repository->getPossibleParentsForFormAndTable()
224 )
225 ->withRequired(true);
226
227 return $f->input()
228 ->container()
229 ->form()
230 ->standard(
231 $this->flow->getLinkTarget($this, self::CMD_MOVE),
232 [$parent]
233 );
234 }
235
236 #[Command('write')]
237 private function restore(): void
238 {
243
244 $this->pons->out()->success($this->lng->txt('msg_restored'), true);
245 $this->flow->redirect(self::CMD_DEFAULT);
246 }
247}
static flushDB()
@depracated never use in ILIAS Core, Plugins only
Class ilMMTopItemFormGUI.
@ilCtrl_IsCalledBy ilMMTopItemGUI: ilObjMainMenuGUI @ilCtrl_Calls ilMMTopItemGUI: ILIAS\GlobalScreen\...
form(bool $save=false)
TokenContainer $top_token
This describes a standard form.
Definition: Standard.php:29
Interface ilMMItemFacadeInterface.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc