xref: /plugin/autotooltip/helper.php (revision 6bfd3f23b88eaf72b526530268de105d7e747a15)
1*6bfd3f23SEli Fenton<?php
2*6bfd3f23SEli Fentonif(!defined('DOKU_INC')) die();
3*6bfd3f23SEli Fenton
4*6bfd3f23SEli Fenton/**
5*6bfd3f23SEli Fenton * Auto-Tooltip DokuWiki plugin
6*6bfd3f23SEli Fenton *
7*6bfd3f23SEli Fenton * @license    MIT
8*6bfd3f23SEli Fenton * @author     Eli Fenton
9*6bfd3f23SEli Fenton */
10*6bfd3f23SEli Fentonclass helper_plugin_autotooltip extends DokuWiki_Admin_Plugin {
11*6bfd3f23SEli Fenton	private $localRenderer;
12*6bfd3f23SEli Fenton
13*6bfd3f23SEli Fenton	public function __construct() {
14*6bfd3f23SEli Fenton		$this->localRenderer = new Doku_Renderer_xhtml;
15*6bfd3f23SEli Fenton	}
16*6bfd3f23SEli Fenton
17*6bfd3f23SEli Fenton
18*6bfd3f23SEli Fenton	/**
19*6bfd3f23SEli Fenton	 * Return a simple tooltip.
20*6bfd3f23SEli Fenton	 *
21*6bfd3f23SEli Fenton	 * @param string $content - The on-page content. May contain HTML.
22*6bfd3f23SEli Fenton	 * @param string $tooltip - Tooltip content. May contain HTML.
23*6bfd3f23SEli Fenton	 * @param string $classes - CSS classes to add to this tooltip.
24*6bfd3f23SEli Fenton	 * @return string
25*6bfd3f23SEli Fenton	 */
26*6bfd3f23SEli Fenton	function forText($content, $tooltip, $classes = '') {
27*6bfd3f23SEli Fenton		if (!$classes) {
28*6bfd3f23SEli Fenton			$classes = 'plugin-autotooltip__default';
29*6bfd3f23SEli Fenton		}
30*6bfd3f23SEli Fenton
31*6bfd3f23SEli Fenton		$textclass = strstr($content, '<a ') !== FALSE ? '' : 'plugin-autotooltip__simple';
32*6bfd3f23SEli Fenton
33*6bfd3f23SEli Fenton		return '<div class="plugin-autotooltip_linked ' . $textclass . '" onmouseover="autotooltip.show(this)" onmouseout="autotooltip.hide()">' .
34*6bfd3f23SEli Fenton			$content .
35*6bfd3f23SEli Fenton			'<div class="plugin-autotooltip-hidden-classes">' . $classes . '</div>' .
36*6bfd3f23SEli Fenton			'<div class="plugin-autotooltip-hidden-tip">' . $this->_formatTT($tooltip) . '</div>' .
37*6bfd3f23SEli Fenton		'</div>';
38*6bfd3f23SEli Fenton	}
39*6bfd3f23SEli Fenton
40*6bfd3f23SEli Fenton
41*6bfd3f23SEli Fenton	/**
42*6bfd3f23SEli Fenton	 * Render a tooltip, with the title and abstract of a page.
43*6bfd3f23SEli Fenton	 *
44*6bfd3f23SEli Fenton	 * @param string $id - A page id.
45*6bfd3f23SEli Fenton	 * @param string $content - The on-page content. May contain HTML.
46*6bfd3f23SEli Fenton	 * @param string $classes - CSS classes to add to this tooltip.
47*6bfd3f23SEli Fenton	 * @return string
48*6bfd3f23SEli Fenton	 */
49*6bfd3f23SEli Fenton	function forWikilink($id, $content, $classes = '') {
50*6bfd3f23SEli Fenton		if (!$classes) {
51*6bfd3f23SEli Fenton			$classes = 'plugin-autotooltip__default';
52*6bfd3f23SEli Fenton		}
53*6bfd3f23SEli Fenton
54*6bfd3f23SEli Fenton		$title = p_get_metadata($id, 'title');
55*6bfd3f23SEli Fenton		$abstract = p_get_metadata($id, 'description abstract');
56*6bfd3f23SEli Fenton		try {
57*6bfd3f23SEli Fenton			// By default, the abstract starts with the title. Remove it so it's not displayed twice, but still fetch
58*6bfd3f23SEli Fenton			// both pieces of metadata, in case another plugin rewrote the abstract.
59*6bfd3f23SEli Fenton			$abstract = preg_replace('/^' . preg_quote($title) . '(\r?\n)+/', '', $abstract);
60*6bfd3f23SEli Fenton		} catch(\Exception $e) {
61*6bfd3f23SEli Fenton			// Ignore.
62*6bfd3f23SEli Fenton		}
63*6bfd3f23SEli Fenton
64*6bfd3f23SEli Fenton		$link = $this->localRenderer->internallink($id, $content, null, true);
65*6bfd3f23SEli Fenton
66*6bfd3f23SEli Fenton		if (page_exists($id)) {
67*6bfd3f23SEli Fenton			// Remove the title attribute, since we have a better tooltip.
68*6bfd3f23SEli Fenton			$link = preg_replace('/title="[^"]*"/', '', $link);
69*6bfd3f23SEli Fenton
70*6bfd3f23SEli Fenton			return '<div class="plugin-autotooltip_linked" onmouseover="autotooltip.show(this)" onmouseout="autotooltip.hide()">' .
71*6bfd3f23SEli Fenton				$link .
72*6bfd3f23SEli Fenton				'<div class="plugin-autotooltip-hidden-classes">plugin-autotooltip_big ' . $classes . '</div>' .
73*6bfd3f23SEli Fenton				'<div class="plugin-autotooltip-hidden-tip">' .
74*6bfd3f23SEli Fenton				'  <h3>' . $title . '</h3>' .
75*6bfd3f23SEli Fenton				'  <div class="level3">' .
76*6bfd3f23SEli Fenton				'    <p class="plugin-autotooltip_abstract">' . $this->_formatTT($abstract) . '</p>' .
77*6bfd3f23SEli Fenton				'  </div>' .
78*6bfd3f23SEli Fenton				'</div>' .
79*6bfd3f23SEli Fenton				'</div>';
80*6bfd3f23SEli Fenton		}
81*6bfd3f23SEli Fenton		else {
82*6bfd3f23SEli Fenton			return $link;
83*6bfd3f23SEli Fenton		}
84*6bfd3f23SEli Fenton	}
85*6bfd3f23SEli Fenton
86*6bfd3f23SEli Fenton
87*6bfd3f23SEli Fenton	/**
88*6bfd3f23SEli Fenton	 * Format tooltip text.
89*6bfd3f23SEli Fenton	 *
90*6bfd3f23SEli Fenton	 * @param string $tt - Tooltip text.
91*6bfd3f23SEli Fenton	 * @return string
92*6bfd3f23SEli Fenton	 */
93*6bfd3f23SEli Fenton	private function _formatTT($tt) {
94*6bfd3f23SEli Fenton		return preg_replace('/\r?\n/', '<br>', $tt);
95*6bfd3f23SEli Fenton	}
96*6bfd3f23SEli Fenton}
97