1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5* This is a utility class for the yui tooltips.
6* this only works, if a parent has class="yui-skin-sam" attached.
7*/
8class ilTooltipGUI
9{
10    protected static $initialized = false;
11    protected static $library_initialized = false;
12
13    /**
14     * Adds a tooltip to an HTML element
15     *
16     * @param string $a_el_id element id
17     * @param string $a_el_id tooltip text
18     * @param string $a_el_id element id of container the tooltip should be added to
19     */
20    public static function addTooltip(
21        $a_el_id,
22        $a_text,
23        $a_container = "",
24        $a_my = "bottom center",
25        $a_at = "top center",
26        $a_use_htmlspecialchars = true
27    ) {
28        // to get rid of globals here, we need to change the
29        // process in learning modules, e.g. which does not work with $DIC (since it does not
30        // use the standard template)
31        $tpl = $GLOBALS["tpl"];
32
33        self::init();
34
35        $code = self::getTooltip(
36            $a_el_id,
37            $a_text,
38            $a_container,
39            $a_my,
40            $a_at,
41            $a_use_htmlspecialchars
42        );
43        $tpl->addOnLoadCode($code);
44    }
45
46    /**
47     * Get tooltip js code
48     *
49     * @param string $a_el_id element id
50     * @param string $a_el_id tooltip text
51     * @param string $a_el_id element id of container the tooltip should be added to
52     */
53    public static function getToolTip(
54        $a_el_id,
55        $a_text,
56        $a_container = "",
57        $a_my = "bottom center",
58        $a_at = "top center",
59        $a_use_htmlspecialchars = true
60    ) {
61        $addstr = "";
62
63        // not needed, just make sure the position plugin is included
64        //		$addstr.= ", position: {viewport: $('#fixed_content')}";
65
66        if ($a_container != "") {
67            $addstr .= ", container: '" . $a_container . "'";
68        }
69
70        if ($a_use_htmlspecialchars) {
71            $a_text = htmlspecialchars(str_replace(array("\n", "\r"), "", $a_text));
72        } else {
73            $a_text = str_replace(array("\n", "\r", "'", '"'), array("", "", "\'", '\"'), $a_text);
74        }
75        return 'il.Tooltip.add("' . $a_el_id . '", {' .
76            ' context:"' . $a_el_id . '",' .
77            ' my:"' . $a_my . '",' .
78            ' at:"' . $a_at . '",' .
79            ' text:"' . $a_text . '" ' . $addstr . '} );';
80    }
81
82    /**
83     * Initializes the needed tooltip libraries.
84     */
85    public static function init()
86    {
87        // for globals use, see comment above
88        $tpl = $GLOBALS["tpl"];
89
90        if (!self::$initialized) {
91            $tpl->addCss("./libs/bower/bower_components/qtip2/dist/jquery.qtip.min.css");
92            $tpl->addJavascript("./libs/bower/bower_components/qtip2/dist/jquery.qtip.min.js");
93            $tpl->addJavascript("./Services/UIComponent/Tooltip/js/ilTooltip.js");
94            $tpl->addOnLoadCode('il.Tooltip.init();', 3);
95            self::$initialized = true;
96        }
97    }
98}
99