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