1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8function wikiplugin_alink_info()
9{
10	return [
11		'name' => tra('Anchor Link'),
12		'documentation' => 'PluginAlink',
13		'description' => tra('Create a link to an anchor'),
14		'prefs' => ['wikiplugin_alink'],
15		'body' => tra('Anchor link label.'),
16		'introduced' => 1,
17		'iconname' => 'link',
18		'tags' => [ 'basic' ],
19		'params' => [
20			'aname' => [
21				'required' => true,
22				'name' => tra('Anchor Name'),
23				'description' => tra('The anchor name as defined in the Aname plugin.'),
24				'default' => '',
25				'since' => '1',
26			],
27			'pagename' => [
28				'required' => false,
29				'name' => tra('Page Name'),
30				'description' => tra('The name of the wiki page containing the anchor. If empty, the anchor name will be searched for on the wiki page where the plugin is used.'),
31				'filter' => 'pagename',
32				'default' => '',
33				'profile_reference' => 'wiki_page',
34				'since' => '1',
35			],
36		],
37	];
38}
39
40function wikiplugin_alink($data, $params)
41{
42	global $prefs;
43	$multilinguallib = TikiLib::lib('multilingual');
44	$tikilib = TikiLib::lib('tiki');
45	extract($params, EXTR_SKIP);
46
47	if (! isset($aname)) {
48		return ("<b>missing parameter for aname</b><br />");
49	}
50
51	// the following replace is necessary to maintain compliance with XHTML 1.0 Transitional
52	// and the same behavior as tikilib.php. This will change when the world arrives at XHTML 1.0 Strict.
53	$aname = preg_replace('/[^a-zA-Z0-9]+/', '_', $aname);
54
55	if (isset($pagename) && $pagename) {
56		// Stolen, with some modifications, from tikilib.php line 4717-4723
57		if ($desc = $tikilib->page_exists_desc($pagename)) {
58		// to choose the best page language
59			$bestLang = ($prefs['feature_multilingual'] == 'y' && $prefs['feature_best_language'] == 'y') ? "&amp;bl" : "";
60		// $bestLang = $prefs['feature_best_language'] == 'y' ? "&amp;bl" : "";
61
62			return "<a title=\"$desc\" href='tiki-index.php?page=" . urlencode($pagename) .
63			$bestLang . "#" . $aname . "' class='wiki'>$data</a>";
64		} else {
65			return $data . '<a href="tiki-editpage.php?page=' . urlencode($pagename) .
66			'" title="' . tra("Create page:") . ' ' . urlencode($pagename) .
67			'"  class="wiki wikinew">?</a>';
68		}
69	} elseif (isset($_REQUEST['page'])) {
70		$urlPrefix = "tiki-index.php?page=";
71		if ($prefs['feature_sefurl'] == 'y') {
72			$urlPrefix = "";
73		}
74		return "<a href=\"" . $urlPrefix . $_REQUEST["page"] . "#$aname\">$data</a>";
75	} else {
76		return "<a href=\"#$aname\">$data</a>";
77	}
78}
79