1<?php
2
3
4namespace ComboStrap;
5
6
7use syntax_plugin_combo_button;
8use syntax_plugin_combo_link;
9use syntax_plugin_combo_tooltip;
10
11class Tooltip
12{
13
14
15    public const TOOLTIP_ATTRIBUTE = "tooltip";
16    public const CALLSTACK = "callstack";
17    public const POSITION_ATTRIBUTE = "position";
18
19    public static function processTooltip(TagAttributes &$tagAttributes)
20    {
21
22        $tooltip = $tagAttributes->getValueAndRemove(self::TOOLTIP_ATTRIBUTE);
23        if ($tooltip === null) {
24            return;
25        }
26
27        if(!is_array($tooltip)){
28            LogUtility::msg("The tooltip value ($tooltip) is not an array.");
29            return;
30        }
31
32        /**
33         * Tooltip
34         */
35        $dataAttributeNamespace = Bootstrap::getDataNamespace();
36
37
38        /**
39         * Old tooltip syntax
40         */
41        $title = $tooltip[syntax_plugin_combo_tooltip::TEXT_ATTRIBUTE];
42        if ($title === null) {
43
44            $callStack = $tooltip[Tooltip::CALLSTACK];
45            if ($callStack !== null) {
46                try {
47                    $title = PluginUtility::renderInstructionsToXhtml($callStack);
48                } catch (ExceptionCombo $e) {
49                    $title = LogUtility::wrapInRedForHtml("Error while rendering the tooltip. Error: {$e->getMessage()}");
50                }
51
52                /**
53                 * New Syntax
54                 * (The new syntax add the attributes to the previous element
55                 */
56                $tagAttributes->addOutputAttributeValue("data{$dataAttributeNamespace}-html", "true");
57
58            }
59        }
60
61
62
63
64        if (empty($title)) {
65            $title = LogUtility::wrapInRedForHtml("The tooltip is empty");
66        }
67        $tagAttributes->addOutputAttributeValue("title", $title);
68
69        /**
70         * Snippet
71         */
72        Tooltip::addToolTipSnippetIfNeeded();
73        $tagAttributes->addOutputAttributeValue("data{$dataAttributeNamespace}-toggle", "tooltip");
74
75        /**
76         * Position
77         */
78        $position = $tooltip[Tooltip::POSITION_ATTRIBUTE];
79        if ($position === null) {
80            $position = "top";
81        }
82        $tagAttributes->addOutputAttributeValue("data{$dataAttributeNamespace}-placement", "${position}");
83
84
85        /**
86         * Keyboard user and assistive technology users
87         * If not button or link (ie span), add tabindex to make the element focusable
88         * in order to see the tooltip
89         * Not sure, if this is a good idea
90         *
91         * Arbitrary HTML elements (such as <span>s) can be made focusable by adding the tabindex="0" attribute
92         */
93        $logicalTag = $tagAttributes->getLogicalTag();
94        if (!in_array($logicalTag, [syntax_plugin_combo_link::TAG, syntax_plugin_combo_button::TAG])) {
95            $tagAttributes->addOutputAttributeValue("tabindex", "0");
96        }
97
98
99    }
100
101    /**
102     * tooltip is used also in page protection
103     */
104    public
105    static function addToolTipSnippetIfNeeded()
106    {
107        PluginUtility::getSnippetManager()->attachInternalJavascriptForSlot(syntax_plugin_combo_tooltip::TAG);
108        PluginUtility::getSnippetManager()->attachCssInternalStyleSheetForSlot(syntax_plugin_combo_tooltip::TAG);
109    }
110}
111