1<?php
2
3use ILIAS\GlobalScreen\Scope\MainMenu\Collector\Renderer\Hasher;
4use ILIAS\GlobalScreen\Scope\MainMenu\Factory\Item\Lost;
5
6/**
7 * Class ilMMSubItemTableGUI
8 * @author Fabian Schmid <fs@studer-raimann.ch>
9 */
10class ilMMSubItemTableGUI extends ilTable2GUI
11{
12    use Hasher;
13
14    const IDENTIFIER = 'identifier';
15    const F_TABLE_SHOW_INACTIVE = 'table_show_inactive';
16    const F_TABLE_ENTRY_STATUS = 'entry_status';
17    const F_TABLE_ALL_VALUE = 1;
18    const F_TABLE_ONLY_ACTIVE_VALUE = 2;
19    const F_TABLE_ONLY_INACTIVE_VALUE = 3;
20    /**
21     * @var ilObjMainMenuAccess
22     */
23    private $access;
24    /**
25     * @var array
26     */
27    private $filter;
28    /**
29     * @var ilMMCustomProvider
30     */
31    private $item_repository;
32
33    /**
34     * ilMMSubItemTableGUI constructor.
35     * @param ilMMSubItemGUI     $a_parent_obj
36     * @param ilMMItemRepository $item_repository
37     */
38    public function __construct(ilMMSubItemGUI $a_parent_obj, ilMMItemRepository $item_repository, ilObjMainMenuAccess $access)
39    {
40        $this->access = $access;
41        $this->setId(self::class);
42        $this->setExternalSorting(true);
43        $this->setExternalSegmentation(true);
44        parent::__construct($a_parent_obj);
45        $this->item_repository = $item_repository;
46        $this->lng             = $this->parent_obj->lng;
47        $this->addFilterItems();
48        $this->setData($this->resolveData());
49        $this->setFormAction($this->ctrl->getFormAction($this->parent_obj));
50        if ($this->access->hasUserPermissionTo('write')) {
51            $this->addCommandButton(ilMMSubItemGUI::CMD_SAVE_TABLE, $this->lng->txt('button_save'));
52        }
53        $this->initColumns();
54        $this->setRowTemplate('tpl.sub_items.html', 'Services/MainMenu');
55    }
56
57    protected function addFilterItems()
58    {
59        $table_entry_status = new ilSelectInputGUI($this->lng->txt(self::F_TABLE_ENTRY_STATUS), self::F_TABLE_ENTRY_STATUS);
60        $table_entry_status->setOptions(
61            array(
62                self::F_TABLE_ALL_VALUE           => $this->lng->txt("all"),
63                self::F_TABLE_ONLY_ACTIVE_VALUE   => $this->lng->txt("only_active"),
64                self::F_TABLE_ONLY_INACTIVE_VALUE => $this->lng->txt("only_inactive"),
65            )
66        );
67        $this->addAndReadFilterItem($table_entry_status);
68    }
69
70    /**
71     * @param $field
72     */
73    protected function addAndReadFilterItem(ilFormPropertyGUI $field)
74    {
75        $this->addFilterItem($field);
76        $field->readFromSession();
77        if ($field instanceof ilCheckboxInputGUI) {
78            $this->filter[$field->getPostVar()] = $field->getChecked();
79        } else {
80            $this->filter[$field->getPostVar()] = $field->getValue();
81        }
82    }
83
84    private function initColumns()
85    {
86        $this->addColumn($this->lng->txt('sub_parent'));
87        $this->addColumn($this->lng->txt('sub_position'));
88        $this->addColumn($this->lng->txt('sub_title'));
89        $this->addColumn($this->lng->txt('sub_type'));
90        $this->addColumn($this->lng->txt('sub_active'));
91        $this->addColumn($this->lng->txt('sub_status'));
92        $this->addColumn($this->lng->txt('sub_provider'));
93        $this->addColumn($this->lng->txt('sub_actions'));
94    }
95
96    /**
97     * @inheritDoc
98     */
99    protected function fillRow($a_set)
100    {
101        static $position;
102        static $current_parent;
103        $position++;
104        global $DIC;
105
106        $renderer = $DIC->ui()->renderer();
107        $factory  = $DIC->ui()->factory();
108        /**
109         * @var $item_facade ilMMItemFacadeInterface
110         */
111        $item_facade = $a_set['facade'];
112
113        if ($item_facade->isChild()) {
114            if (!$current_parent ||
115                $current_parent->getProviderIdentification()->serialize() !== $item_facade->getParentIdentificationString()) {
116                $current_parent = $this->item_repository->getSingleItem($item_facade->item()->getParent());
117                if ($current_parent instanceof Lost) {
118                }
119                $this->tpl->setVariable("PARENT_TITLE", $current_parent->getTitle());
120                $position = 1;
121            }
122        }
123        $this->tpl->setVariable('IDENTIFIER', self::IDENTIFIER);
124        $this->tpl->setVariable('ID', $this->hash($item_facade->getId()));
125        $this->tpl->setVariable('TITLE', $item_facade->getDefaultTitle());
126        $this->tpl->setVariable('PARENT', $this->getSelect($item_facade)->render());
127        $this->tpl->setVariable('STATUS', $item_facade->getStatus());
128        if ($item_facade->isActivated()) {
129            $this->tpl->touchBlock('is_active');
130        }
131        if ($item_facade->item()->isAlwaysAvailable() || !$item_facade->item()->isAvailable()) {
132            $this->tpl->touchBlock('is_active_blocked');
133        }
134
135        $this->tpl->setVariable('POSITION', $position * 10);
136        $this->tpl->setVariable('TYPE', $item_facade->getTypeForPresentation());
137        $this->tpl->setVariable('PROVIDER', $item_facade->getProviderNameForPresentation());
138
139        $this->ctrl->setParameterByClass(ilMMSubItemGUI::class, ilMMSubItemGUI::IDENTIFIER, $this->hash($a_set['identification']));
140        $this->ctrl->setParameterByClass(ilMMItemTranslationGUI::class, ilMMItemTranslationGUI::IDENTIFIER, $this->hash($a_set['identification']));
141
142        if ($this->access->hasUserPermissionTo('write')) {
143            $items[] = $factory->button()->shy($this->lng->txt(ilMMSubItemGUI::CMD_EDIT), $this->ctrl->getLinkTargetByClass(ilMMSubItemGUI::class, ilMMSubItemGUI::CMD_EDIT));
144            $items[] = $factory->button()->shy($this->lng->txt(ilMMTopItemGUI::CMD_TRANSLATE), $this->ctrl->getLinkTargetByClass(ilMMItemTranslationGUI::class, ilMMItemTranslationGUI::CMD_DEFAULT));
145
146            $rendered_modal = "";
147            if ($item_facade->isCustom()) {
148                $ditem  = $factory->modal()->interruptiveItem($this->hash($a_set['identification']), $item_facade->getDefaultTitle());
149                $action = $this->ctrl->getFormActionByClass(ilMMSubItemGUI::class, ilMMSubItemGUI::CMD_DELETE);
150                $m      = $factory->modal()
151                                  ->interruptive($this->lng->txt(ilMMSubItemGUI::CMD_DELETE), $this->lng->txt(ilMMSubItemGUI::CMD_CONFIRM_DELETE), $action)
152                                  ->withAffectedItems([$ditem]);
153
154                $items[] = $shy = $factory->button()->shy($this->lng->txt(ilMMSubItemGUI::CMD_DELETE), "")->withOnClick($m->getShowSignal());
155                // $items[] = $factory->button()->shy($this->lng->txt(ilMMSubItemGUI::CMD_DELETE), $this->ctrl->getLinkTargetByClass(ilMMSubItemGUI::class, ilMMSubItemGUI::CMD_CONFIRM_DELETE));
156                $rendered_modal = $renderer->render([$m]);
157            }
158
159            $this->tpl->setVariable('ACTIONS', $rendered_modal . $renderer->render([$factory->dropdown()->standard($items)->withLabel($this->lng->txt('sub_actions'))]));
160        }
161    }
162
163    /**
164     * @param ilMMItemFacadeInterface $child
165     * @return ilSelectInputGUI
166     */
167    private function getSelect(ilMMItemFacadeInterface $child) : ilSelectInputGUI
168    {
169        $s = new ilSelectInputGUI('', self::IDENTIFIER . "[{$this->hash($child->getId())}][parent]");
170        $s->setOptions($this->getPossibleParentsForFormAndTable());
171        $s->setValue($this->hash($child->getParentIdentificationString()));
172
173        return $s;
174    }
175
176    /**
177     * @return array
178     */
179    public function getPossibleParentsForFormAndTable() : array
180    {
181        $parents = [];
182        foreach ($this->item_repository->getPossibleParentsForFormAndTable() as $identification => $name) {
183            $parents[$this->hash($identification)] = $name;
184        }
185
186        return $parents;
187    }
188
189    private function resolveData() : array
190    {
191        global $DIC;
192        $sub_items_for_table = $this->item_repository->getSubItemsForTable();
193
194        foreach ($sub_items_for_table as $k => $item) {
195            $item_ident           = $DIC->globalScreen()->identification()->fromSerializedIdentification($item['identification']);
196            $item_facade          = $this->item_repository->repository()->getItemFacade($item_ident);
197            $parent_id            = $item['parent_identification'];
198            $parent_id_calculated = $item_facade->getParentIdentificationString();
199
200            $sub_items_for_table[$k]['facade'] = $item_facade;
201            if (isset($this->filter[self::F_TABLE_ENTRY_STATUS]) && $this->filter[self::F_TABLE_ENTRY_STATUS] !== self::F_TABLE_ALL_VALUE) {
202                if (($this->filter[self::F_TABLE_ENTRY_STATUS] == self::F_TABLE_ONLY_ACTIVE_VALUE && !$item_facade->isActivated())
203                    || ($this->filter[self::F_TABLE_ENTRY_STATUS] == self::F_TABLE_ONLY_INACTIVE_VALUE && $item_facade->isActivated())
204                ) {
205                    unset($sub_items_for_table[$k]);
206                }
207            }
208        }
209        return $sub_items_for_table;
210    }
211}
212