1<?php
2/**
3 * @package tikiwiki
4 */
5// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
6//
7// All Rights Reserved. See copyright.txt for details and a complete list of authors.
8// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
9// $Id$
10
11require_once('tiki-setup.php');
12$access->check_feature('feature_references');
13$access->check_permission(['tiki_p_edit_references'], tra('Edit Library References'));
14
15global $dbTiki;
16$referenceslib = TikiLib::lib('references');
17
18$getInput = function ($request, $key, $default = '') {
19	return empty($request[$key]) ? $default : $request[$key];
20};
21
22$page = $getInput($_REQUEST, 'page');
23$smarty->assign('page', $page);
24
25
26$page_id = TikiLib::lib('tiki')->get_page_id_from_name($page);
27$action = $getInput($_REQUEST, 'action');
28$ref_id = $getInput($_REQUEST, 'referenceId');
29$ref_auto_biblio_code = empty($_REQUEST['ref_auto_biblio_code']) ? 'off' : $_REQUEST['ref_auto_biblio_code'];
30$ref_biblio_code = $getInput($_REQUEST, 'ref_biblio_code');
31$ref_author = $getInput($_REQUEST, 'ref_author');
32$ref_title = $getInput($_REQUEST, 'ref_title');
33$ref_part = $getInput($_REQUEST, 'ref_part');
34$ref_uri = $getInput($_REQUEST, 'ref_uri');
35$ref_code = $getInput($_REQUEST, 'ref_code');
36$ref_publisher = $getInput($_REQUEST, 'ref_publisher');
37$ref_location = $getInput($_REQUEST, 'ref_location');
38$ref_year = $getInput($_REQUEST, 'ref_year');
39$ref_style = $getInput($_REQUEST, 'ref_style');
40$ref_template = $getInput($_REQUEST, 'ref_template');
41
42$find = empty($_REQUEST['find']) ? '' : $_REQUEST['find'];
43$maxRecords = empty($_REQUEST['maxRecords']) ? 25 : $_REQUEST['maxRecords'];
44$offset = empty($_REQUEST['offset']) ? 0 : $_REQUEST['offset'];
45
46if (isset($_REQUEST['addreference'])) {
47	$errors = [];
48
49	if ($ref_auto_biblio_code !== 'on') {
50		if ($ref_biblio_code == '') {
51			$errors[] = 'Please enter Biblio Code.';
52		}
53
54		$exists = $referenceslib->check_lib_existence($ref_biblio_code);
55		if ($exists > 0) {
56			$errors[] = 'This reference already exists.';
57		}
58	}
59
60	if (count($errors) < 1 && $access->checkCsrf()) {
61		$id = $referenceslib->add_reference(
62			null,
63			$ref_biblio_code,
64			$ref_author,
65			$ref_title,
66			$ref_part,
67			$ref_uri,
68			$ref_code,
69			$ref_year,
70			$ref_style,
71			$ref_template,
72			$ref_publisher,
73			$ref_location
74		);
75		$record = $referenceslib->get_reference_from_id($id);
76		$record = array_shift($record['data']);
77		$record['success'] = true;
78		$record['id'] = $record['ref_id'];
79		if ($_REQUEST['response'] == 'json') {
80			echo json_encode($record);
81			return;
82		}
83		$cookietab = 1;
84	} else {
85		foreach ($errors as $error) {
86			$msg .= tra($error);
87		}
88		if ($_REQUEST['response'] == 'json') {
89			echo json_encode([
90				'success' => false,
91				'msg' => $msg
92			]);
93			return;
94		}
95		Feedback::error(['mes' => $msg]);
96	}
97}
98
99if (isset($_REQUEST['editreference'])) {
100	$errors = [];
101
102	if ($ref_id == '') {
103		$errors[] = 'Reference not found.';
104	}
105	if ($ref_biblio_code == '') {
106		$errors[] = 'Please enter Biblio Code.';
107	}
108	if (isset($prefs['feature_library_references']) && $prefs['feature_library_references'] === 'y') {
109		$currentlibreference = $referenceslib->get_reference_from_id($ref_id);
110		$linkedreferences = $referenceslib->get_reference_from_code($currentlibreference['data'][0]['biblio_code']);
111
112		foreach ($linkedreferences['data'] as $ref) {
113			if (count($errors) < 1 && $access->checkCsrf()) {
114				$referenceslib->edit_reference(
115					$ref['ref_id'],
116					$ref_biblio_code,
117					$ref_author,
118					$ref_title,
119					$ref_part,
120					$ref_uri,
121					$ref_code,
122					$ref_year,
123					$ref_style,
124					$ref_template,
125					$ref_publisher,
126					$ref_location
127				);
128				$cookietab = 1;
129			} else {
130				foreach ($errors as $error) {
131					$msg .= tra($error);
132				}
133				Feedback::error(['mes' => $msg]);
134			}
135		}
136	} else {
137		if (count($errors) < 1 && $access->checkCsrf()) {
138			$referenceslib->edit_reference(
139				$ref_id,
140				$ref_biblio_code,
141				$ref_author,
142				$ref_title,
143				$ref_part,
144				$ref_uri,
145				$ref_code,
146				$ref_year,
147				$ref_style,
148				$ref_template,
149				$ref_publisher,
150				$ref_location
151			);
152			$cookietab = 1;
153		} else {
154			foreach ($errors as $error) {
155				$msg .= tra($error);
156			}
157			Feedback::error(['mes' => $msg]);
158		}
159	}
160}
161
162if (isset($_REQUEST['action']) && isset($ref_id)) {
163	$errors = [];
164	if (isset($prefs['feature_library_references']) && $prefs['feature_library_references'] === 'y') {
165		$currentlibreference = $referenceslib->get_reference_from_id($ref_id);
166		$linkedreferences = $referenceslib->get_reference_from_code($currentlibreference['data'][0]['biblio_code']);
167
168		if (count($linkedreferences['data']) === 1) {
169			if ($_REQUEST['action'] == 'delete' && $access->checkCsrfForm(tra('Delete reference?'))) {
170				$referenceslib->remove_reference($ref_id);
171			}
172		} else {
173			$errors[] = 'This library reference can not be deleted because is still being used in some pages, please unlink the reference from the pages first.';
174			foreach ($errors as $error) {
175				$msg .= tra($error);
176			}
177			Feedback::error(['mes' => $msg]);
178		}
179	} else {
180		if ($_REQUEST['action'] == 'delete' && $access->checkCsrfForm(tra('Delete reference?'))) {
181			$referenceslib->remove_reference($ref_id);
182		}
183	}
184}
185
186$references = $referenceslib->list_lib_references($find, $maxRecords, $offset);
187$smarty->assign('references', $references['data']);
188$smarty->assign('cant', $references['cant']);
189if (! empty($ref_id)) {
190	$currentlibreference = $referenceslib->get_reference_from_id($ref_id);
191	if (isset($currentlibreference['data']) && isset($currentlibreference['data'][0]) && $currentlibreference['data'][0]['page_id'] == null) {
192		$referenceInfo = $currentlibreference['data'][0];
193		$smarty->assign('referenceinfo', $referenceInfo);
194		$pageReferences = $referenceslib->get_references_from_biblio($currentlibreference['data'][0]['biblio_code']);
195		if (! empty($pageReferences['data'])) {
196			/** @var TikiLib $tikiLib */
197			$tikiLib = TikiLib::lib('tiki');
198			$pagesNames = [];
199			foreach ($pageReferences['data'] as $pageReference) {
200				if ($pageReference['page_id']) {
201					$page = $tikiLib->get_page_name_from_id($pageReference['page_id']);
202					if (! empty($page)) {
203						$pagesNames[] = ['pageName' => $page];
204					}
205				}
206			}
207			$pagesNames = Perms::filter([ 'type' => 'wiki page' ], 'object', $pagesNames, ['object' => 'pageName'], ['view', 'wiki_view_ref']);
208			$smarty->assign('pagereferences', $pagesNames);
209		}
210	}
211} else {
212	if (! empty($_REQUEST['addreference']) && count($errors) > 0) {
213		$referenceInfo = [
214			'biblio_code' => $ref_biblio_code,
215			'author' => $ref_author,
216			'title' => $ref_title,
217			'part' => $ref_part,
218			'uri' => $ref_uri,
219			'code' => $ref_code,
220			'year' => $ref_year,
221			'publisher' => $ref_publisher,
222			'location' => $ref_location,
223			'style' => $ref_style,
224			'template' => $ref_template
225		];
226		$smarty->assign('referenceinfo', $referenceInfo);
227	}
228}
229
230if ($getInput($_REQUEST, 'details')) {
231	$cookietab = '2';
232} elseif ($getInput($_REQUEST, 'usage')) {
233	$cookietab = '3';
234}
235
236$smarty->assign('find', $find);
237$smarty->assign('maxRecords', $maxRecords);
238$smarty->assign('offset', $offset);
239
240// Display the template
241$smarty->assign('mid', 'tiki-references.tpl');
242$smarty->display('tiki.tpl');
243