1<?php
2require_once("Services/Style/System/classes/Documentation/class.ilKSDocumentationExplorerGUI.php");
3require_once("Services/Style/System/classes/Documentation/class.ilKSDocumentationEntryGUI.php");
4require_once("libs/composer/vendor/geshi/geshi/src/geshi.php");
5
6
7use ILIAS\UI\Implementation\Crawler as Crawler;
8
9/**
10 *
11 * @author            Timon Amstutz <timon.amstutz@ilub.unibe.ch>
12 * @version           $Id$*
13 */
14class ilSystemStyleDocumentationGUI
15{
16    /**
17     * @var ilTemplate
18     */
19    protected $tpl;
20    /**
21     * @var ilCtrl $ctrl
22     */
23    protected $ctrl;
24
25    /**
26     * @var ilLanguage
27     */
28    protected $lng;
29
30    /**
31     * @var bool
32     */
33    protected $is_read_only = false;
34
35    const ROOT_FACTORY_PATH = "./Services/Style/System/data/abstractDataFactory.php";
36    const DATA_DIRECTORY = "./Services/Style/System/data";
37    const DATA_FILE = "data.json";
38    public static $DATA_PATH;
39
40    /**
41     * ilSystemStyleDocumentationGUI constructor.
42     * @param bool|false $read_only
43     */
44    public function __construct($read_only = false)
45    {
46        global $DIC;
47
48        $this->ctrl = $DIC->ctrl();
49        $this->lng = $DIC->language();
50        $this->tpl = $DIC["tpl"];
51
52        $this->setIsReadOnly($read_only);
53
54        self::$DATA_PATH = self::DATA_DIRECTORY . "/" . self::DATA_FILE;
55    }
56
57    /**
58     * Execute command
59     */
60    public function executeCommand()
61    {
62        $cmd = $this->ctrl->getCmd();
63
64        switch ($cmd) {
65            case 'parseEntries':
66                $this->$cmd();
67                $this->show();
68                break;
69            default:
70                if ($this->is_read_only) {
71                    $this->resetForReadOnly();
72                }
73                $this->addGotoLink();
74                $this->show();
75                break;
76        }
77    }
78
79    public function show()
80    {
81        $entries = $this->readEntries();
82        $content = "";
83
84        //The button to parse the entries from code should only be shown in DEVMODE. Other users do not need that.
85        if (DEVMODE == 1 && !$this->isReadOnly()) {
86            $toolbar = new ilToolbarGUI();
87            $reload_btn = ilLinkButton::getInstance();
88            $reload_btn->setCaption($this->lng->txt('refresh_entries'), false);
89            if ($_GET["node_id"]) {
90                $this->ctrl->saveParameter($this, "node_id");
91            }
92            $reload_btn->setUrl($this->ctrl->getLinkTarget($this, 'parseEntries'));
93            $toolbar->addButtonInstance($reload_btn);
94            $content .= $toolbar->getHTML();
95        }
96
97        $explorer = new ilKSDocumentationExplorerGUI($this, "entries", $entries, $_GET["node_id"]);
98        $this->tpl->setLeftNavContent($explorer->getHTML());
99        $entry_gui = new ilKSDocumentationEntryGUI($this, $explorer->getCurrentOpenedNode(), $entries);
100        $content .= $entry_gui->renderEntry();
101
102        $this->tpl->setContent($content);
103    }
104
105    protected function resetForReadOnly()
106    {
107        /**
108         * @var ILIAS\DI\Container $DIC
109         */
110        global $DIC;
111
112        $DIC->tabs()->clearTargets();
113
114        /**
115         * Since clearTargets also clears the help screen ids
116         */
117        $DIC->help()->setScreenIdComponent("sty");
118        $DIC->help()->setScreenId("system_styles");
119
120        $skin_id = $_GET["skin_id"];
121        $style_id = $_GET["style_id"];
122
123        $skin = ilSystemStyleSkinContainer::generateFromId($skin_id)->getSkin();
124        $style = $skin->getStyle($style_id);
125
126        $DIC["tpl"]->setTitle($DIC->language()->txt("documentation"));
127
128        if ($style->isSubstyle()) {
129            $DIC["tpl"]->setDescription(
130                $this->lng->txt("ks_documentation_of_substyle")
131                    . " '"
132                    . $style->getName() . "' " .
133                    $this->lng->txt("of_parent") . " '" . $skin->getStyle($style->getSubstyleOf())->getName() . "' " .
134                    $this->lng->txt("from_skin") . " " . $skin->getName()
135            );
136        } else {
137            $DIC["tpl"]->setDescription(
138                $this->lng->txt("ks_documentation_of_style") . " '" . $style->getName() . "' " .
139                    $this->lng->txt("from_skin") . " '" . $skin->getName() . "'"
140            );
141        }
142
143        $DIC["ilLocator"]->clearItems();
144        $DIC["tpl"]->setLocator();
145    }
146
147    protected function addGotoLink()
148    {
149        $this->tpl->setPermanentLink("stys", $_GET["ref_id"], "_" . $_GET["node_id"] . "_"
150                . $_GET["skin_id"] . "_" . $_GET["style_id"]);
151    }
152
153    /**
154     * @return Crawler\Entry\ComponentEntries
155     * @throws Crawler\Exception\CrawlerException
156     */
157    protected function parseEntries()
158    {
159        $crawler = new Crawler\FactoriesCrawler();
160        $entries = $crawler->crawlFactory(self::ROOT_FACTORY_PATH);
161        file_put_contents(self::$DATA_PATH, json_encode($entries));
162        ilUtil::sendSuccess($this->lng->txt("entries_reloaded"), true);
163        return $entries;
164    }
165
166    /**
167     * @return Crawler\Entry\ComponentEntries
168     */
169    protected function readEntries()
170    {
171        $entries_array = json_decode(file_get_contents(self::$DATA_PATH), true);
172
173        $entries = new Crawler\Entry\ComponentEntries();
174        foreach ($entries_array as $entry_array) {
175            $entry = new Crawler\Entry\ComponentEntry($entry_array);
176            $entries->addEntry($entry);
177        }
178
179        return $entries;
180    }
181
182    /**
183     * @return bool
184     */
185    public function isReadOnly()
186    {
187        return $this->is_read_only;
188    }
189
190    /**
191     * @param bool $is_read_only
192     */
193    public function setIsReadOnly($is_read_only)
194    {
195        $this->is_read_only = $is_read_only;
196    }
197}
198