1<?php
2
3namespace ILIAS\OrgUnit\Provider;
4
5use ILIAS\GlobalScreen\Scope\Tool\Provider\AbstractDynamicToolProvider;
6use ILIAS\GlobalScreen\ScreenContext\Stack\CalledContexts;
7use ILIAS\GlobalScreen\ScreenContext\Stack\ContextCollection;
8use ILIAS\UI\Component\Tree\Tree;
9use ILIAS\UI\Component\Tree\TreeRecursion;
10use ilObjOrgUnit;
11use ilObjOrgUnitGUI;
12use ilOrgUnitExplorerGUI;
13use ilOrgUnitExtension;
14use ilTree;
15
16/**
17 * Class OrgUnitToolProvider
18 * @author Fabian Schmid <fs@studer-raimann.ch>
19 */
20class OrgUnitToolProvider extends AbstractDynamicToolProvider
21{
22    public const SHOW_ORGU_TREE = 'show_orgu_tree';
23
24    /**
25     * @inheritDoc
26     */
27    public function isInterestedInContexts() : ContextCollection
28    {
29        return $this->context_collection->main()->administration();
30    }
31
32    /**
33     * @inheritDoc
34     */
35    public function getToolsForContextStack(CalledContexts $called_contexts) : array
36    {
37        $tools = [];
38
39        if ($called_contexts->current()->getAdditionalData()->is(self::SHOW_ORGU_TREE, true)) {
40            $iff = function (string $id) {
41                return $this->identification_provider->contextAwareIdentifier($id);
42            };
43
44            $t = function (string $key) : string {
45                return $this->dic->language()->txt($key);
46            };
47
48            $tools[] = $this->factory->treeTool($iff('tree_new'))
49                                     ->withTitle($t('tree'))
50                                     ->withSymbol($this->dic->ui()->factory()->symbol()->icon()->standard('orgu', 'Orgu'))
51                                     ->withTree($this->getTree());
52        }
53
54        return $tools;
55    }
56
57    private function getTree() : Tree
58    {
59        global $DIC;
60        $lng = $DIC->language();
61        $tree = $this->getTreeRecursion();
62
63        $parent_node_id = $DIC->repositoryTree()->getParentId(ilObjOrgUnit::getRootOrgRefId());
64
65        return $this->dic->ui()->factory()->tree()->expandable($lng->txt("org_units"), $tree)
66            ->withData($tree->getChildsOfNode($parent_node_id));
67    }
68
69    private function getTreeRecursion() : TreeRecursion
70    {
71        $tree = new ilOrgUnitExplorerGUI("orgu_explorer", ilObjOrgUnitGUI::class, "showTree", new ilTree(1));
72        $tree->setTypeWhiteList($this->getTreeWhiteList());
73        $tree->setRootId(ilObjOrgUnit::getRootOrgRefId());
74        $tree->setPathOpen($_GET['item_ref_id'] ?? $_GET['ref_id'] ?? '');
75
76        return $tree;
77    }
78
79    private function getTreeWhiteList() : array
80    {
81        $whiteList = array('orgu');
82        $pls = ilOrgUnitExtension::getActivePluginIdsForTree();
83
84        return array_merge($whiteList, $pls);
85    }
86}
87