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
8use Tiki\WikiPlugin\Reference;
9
10function wikiplugin_addreference_info()
11{
12	return [
13		'name' => tra('Add Reference'),
14		'description' => tra('Add a bibliography reference.'),
15		'format' => 'html',
16		'introduced' => 10,
17		'prefs' => ['wikiplugin_addreference', 'feature_references'],
18		'iconname' => 'pencil',
19		'params' => [
20			'biblio_code' => [
21				'required' => true,
22				'name' => tra('Biblio Code'),
23				'description' => tra('The code to be added as reference.'),
24				'default' => '',
25				'since' => '10.0',
26				'filter' => 'word',
27				'multiple' => true,
28				'separator' => ',',
29			],
30		],
31	];
32}
33
34function wikiplugin_addreference($data, $params)
35{
36	global $prefs;
37
38	if ($prefs['wikiplugin_addreference'] == 'y') {
39		/** @var ReferencesLib $referenceslib */
40		$referenceslib = TikiLib::lib('references');
41
42		if (! isset($GLOBALS['referencesData'])) {
43			$GLOBALS['referencesData'] = [];
44		}
45
46		$data = trim($data);
47
48		if (strstr($_SERVER['SCRIPT_NAME'], 'tiki-print.php')) {
49			$page = urldecode($_REQUEST['page']);
50			$page_id = TikiLib::lib('tiki')->get_page_id_from_name($page);
51			$page_info = TikiLib::lib('tiki')->get_page_info($page);
52		} else {
53			$object = current_object();
54			$page_id = TikiLib::lib('tiki')->get_page_id_from_name($object['object']);
55			$page_info = TikiLib::lib('tiki')->get_page_info($object['object']);
56		}
57
58		if (empty($params['biblio_code']) || (is_array($params['biblio_code']) && count($params['biblio_code']) == 0)) {
59			return;
60		}
61		if (! is_array($params['biblio_code'])) {
62			$params['biblio_code'] = [$params['biblio_code']];
63		}
64		$cleanBiblioCode = [];
65		foreach ($params['biblio_code'] as $code) {
66			$code = Reference::trimBibliographicCode($code);
67			if (! empty($code)) {
68				$cleanBiblioCode[] = $code;
69			}
70		}
71		$params['biblio_code'] = $cleanBiblioCode;
72
73		extract($params, EXTR_SKIP);
74
75		$matchedReferences = Reference::extractBibliographicCodesFromText($page_info['data']);
76
77		$temp = [];
78		$curr_matches = [];
79		$temp = array_unique($matchedReferences);
80		$i = 0;
81		foreach ($temp as $k => $v) {
82			if (strlen(trim($v)) > 0) {
83				$curr_matches[$i] = $v;
84				$i++;
85			}
86		}
87		unset($temp);
88
89		$matchedRecords = [];
90		foreach ($params['biblio_code'] as $code) {
91			$index = 0;
92			foreach ($curr_matches as $key => $val) {
93				if (strlen(trim($val)) > 0) {
94					if ($val == $code) {
95						$index = $key + 1;
96						break;
97					}
98				}
99			}
100			$matchedRecords[] = [
101				'biblio_code' => $code,
102				'index' => $index,
103			];
104		}
105
106		$GLOBALS['referencesData'] = $curr_matches;
107
108		$referenceStyle = Reference::getCitationStyle();
109		$displayPopover = (! empty($prefs['feature_references_popover']) && $prefs['feature_references_popover'] === 'y');
110
111		$referenceOutputList = [];
112		foreach ($matchedRecords as $biblioCode) {
113			$url = $GLOBALS['base_uri'] . "#" . $biblioCode['biblio_code'];
114
115			if ($displayPopover || $referenceStyle == Reference::STYLE_MLA) {
116				$referenceList = $referenceslib->get_reference_from_code_and_page(
117					[$biblioCode['biblio_code']],
118					$page_id
119				);
120			}
121
122			$extraTitle = "";
123			$class = "";
124			if ($displayPopover) {
125				$referenceText = Reference::parseTemplate(
126					Reference::getTagsToParse(),
127					$biblioCode['biblio_code'],
128					$referenceList['data'],
129					$referenceStyle
130				);
131				if (empty($referenceText)) {
132					$referenceText = $biblioCode['biblio_code'] . ': ' . tr('missing bibliography definition');
133				}
134				$baseLink = "<a href=&#039;" . $url . "&#039; title=&#039;" . $biblioCode['biblio_code'] . "&#039;>" . $referenceText . "</a>";
135				$extraTitle = '|' . $baseLink;
136				$class = "class='tips'";
137			}
138
139			if (! empty($biblioCode['biblio_code']) && $referenceStyle == Reference::STYLE_MLA) {
140				if (empty($referenceList['data'][$biblioCode['biblio_code']])) {
141					$referenceOutputList[] = "<a href='" . $url . "' " . $class . " title='" . $biblioCode['biblio_code'] . $extraTitle . "'>" . $biblioCode['biblio_code'] . "</a>";
142				} else {
143					$reference = $referenceList['data'][$biblioCode['biblio_code']];
144					$authorTemp = preg_split('/[\W]+/', $reference['author']);
145					$author = empty($authorTemp[0]) ? '' : $authorTemp[0];
146					$sep = (empty($author) || empty($reference['year'])) ? '' : ' ';
147
148					$referenceOutputList[] = "<a href='" . $url . "' " . $class . " title='" . $biblioCode['biblio_code'] . $extraTitle . "'>" . $author . $sep . $reference['year'] . "</a>";
149				}
150			} else {
151				$referenceOutputList[] = "<a href='" . $url . "' " . $class . " title='" . $biblioCode['biblio_code'] . $extraTitle . "'><sup>" . $biblioCode['index'] . "</sup></a>";
152			}
153		}
154
155		if (! empty($prefs['feature_references_style']) && $prefs['feature_references_style'] === 'mla') {
156			$data .= '(' . implode(', ', $referenceOutputList) . ')';
157		} else {
158			$data .= implode('<sup>,</sup> ', $referenceOutputList);
159		}
160
161		return $data;
162	}
163}
164