1<?php
2/**
3 * Copyright (c) 2021. ComboStrap, Inc. and its affiliates. All Rights Reserved.
4 *
5 * This source code is licensed under the GPL license found in the
6 * COPYING  file in the root directory of this source tree.
7 *
8 * @license  GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html)
9 * @author   ComboStrap <support@combostrap.com>
10 *
11 */
12
13namespace ComboStrap;
14
15use dokuwiki\Menu\Item\AbstractItem;
16
17
18/**
19 * Class MenuItem
20 * *
21 * @package ComboStrap
22 *
23 * Inspiration:
24 * https://raw.githubusercontent.com/splitbrain/dokuwiki-plugin-dw2pdf/master/MenuItem.php
25 */
26class QualityMenuItem extends AbstractItem
27{
28
29
30    const CLASS_HTML = "combo-quality-item";
31    const CANONICAL = "quality";
32    const CLASS_HTML_LOW = "combo-quality-item-low";
33
34    /**
35     * @var MarkupPath
36     */
37    private $page;
38
39    /**
40     * QualityMenuItem constructor.
41     */
42    public function __construct()
43    {
44        $snippetManager = PluginUtility::getSnippetManager();
45        $snippetManager->attachJavascriptComboLibrary();
46        $snippetManager->attachJavascriptFromComponentId(self::CANONICAL);
47        $this->page = MarkupPath::createFromRequestedPage();
48        if($this->page->isLowQualityPage()){
49            $snippetManager->attachCssInternalStylesheet(self::CANONICAL);
50        }
51        parent::__construct();
52
53    }
54
55
56    /**
57     *
58     * @return string
59     */
60    public function getLabel(): string
61    {
62        $suffix = "";
63        if ($this->page->isLowQualityPage()) {
64            $suffix = "(Low)";
65        }
66        return "Page Quality $suffix";
67    }
68
69    public function getLinkAttributes($classprefix = 'menuitem '): array
70    {
71        $linkAttributes = parent::getLinkAttributes($classprefix);
72        /**
73         * A class and not an id
74         * because a menu item can be found twice on
75         * a page (For instance if you want to display it in a layout at a
76         * breakpoint and at another in another breakpoint
77         */
78        $linkAttributes['class'] = self::CLASS_HTML;
79        if ($this->page->isLowQualityPage()) {
80            $linkAttributes['class'] .= " ".self::CLASS_HTML_LOW;
81        }
82
83        return $linkAttributes;
84    }
85
86    public function getTitle(): string
87    {
88        $title = "Show the page quality";
89        if ($this->page->isLowQualityPage()) {
90            $title .= "\n(This page is a low quality page)";
91        } else {
92            $title .= "\n(This page has a normal quality)";
93        }
94        return htmlentities($title);
95    }
96
97    public function getSvg(): string
98    {
99
100        if ($this->page->isLowQualityPage()) {
101            /** @var string icon file */
102            return DirectoryLayout::getComboImagesDirectory()->resolve( 'quality-alert.svg')->toAbsoluteId();
103        } else {
104            /**
105             * @var string icon file
106             * !!! Same icon used in the landing page !!!
107             */
108            return DirectoryLayout::getComboImagesDirectory()->resolve('quality.svg')->toAbsoluteId();
109        }
110    }
111
112
113}
114