references.php 7.18 KB
Newer Older
1
<?php
changi67's avatar
changi67 committed
2 3 4
/**
 * @package tikiwiki
 */
5
// (c) Copyright 2002-2015 by authors of the Tiki Wiki CMS Groupware Project
6
//
7 8 9 10 11 12 13 14 15
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$

require_once ('tiki-setup.php');

global $dbTiki;

if (!isset($prefs['feature_references']) && !$prefs['feature_references'] === 'y') {
16 17
	header('location: index.php');
	exit;
18 19
}

20
$referenceslib = TikiLib::lib('references');
21

22 23 24
if (!isset($_REQUEST['page'])) {
	$smarty->assign('msg', tra('No page indicated'));
	$smarty->display('error.tpl');
25 26 27
	die;
}

28 29
$smarty->assign('page', $_REQUEST['page']);
$page = $_REQUEST['page'];
30 31 32

$perms = Perms::get(array('wiki page', $page));

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
$page_id = TikiLib::lib('tiki')->get_page_id_from_name($_REQUEST['page']);

$action = $_REQUEST['action'];
$ref_id = $_REQUEST['ref_id'];
$ref_biblio_code = $_REQUEST['ref_biblio_code'];
$ref_author = $_REQUEST['ref_author'];
$ref_title = $_REQUEST['ref_title'];
$ref_part = $_REQUEST['ref_part'];
$ref_uri = $_REQUEST['ref_uri'];
$ref_code = $_REQUEST['ref_code'];
$ref_publisher = $_REQUEST['ref_publisher'];
$ref_location = $_REQUEST['ref_location'];
$ref_year = $_REQUEST['ref_year'];
$ref_style = $_REQUEST['ref_style'];
$ref_template = $_REQUEST['ref_template'];

if (isset($_REQUEST['addreference']) && $action='a_ref') {
50
	if (! $perms->edit_references) {
51 52 53 54 55 56 57 58 59
		echo json_encode(
			array(
				'result'=>tra('failure'),
				'message'=>tra('You do not have sufficient permissions to perform this action.')
			)
		);
		exit;
	}

60 61

	$errors = array();
62

63 64
	if (intval($page_id)) {
		if ($ref_biblio_code=='') {
65
			$errors[] = tra('Please enter Biblio Code.');
66 67
		}
		if (strlen($ref_biblio_code)>50) {
68
			$errors[] = tra('Biblio code must not exceed 50 characters.');
69
		}
70

71 72 73
		if (count($errors)<1) {
			$exists = $referenceslib->check_existence($page_id, $ref_biblio_code);
			if ($exists > 0) {
74
				echo json_encode(array('result'=>tra('failure'), 'id'=>-1));
75 76
			} else {
				$is_library = $referenceslib->check_lib_existence($ref_biblio_code);
77
				$id = $referenceslib->add_reference(
78 79 80 81 82 83 84 85 86 87 88 89
					$page_id,
					$ref_biblio_code,
					$ref_author,
					$ref_title,
					$ref_part,
					$ref_uri,
					$ref_code,
					$ref_year,
					$ref_style,
					$ref_template,
					$ref_publisher,
					$ref_location
90
				);
91
				echo json_encode(array('result'=>tra('success'), 'id'=>$id, 'is_library'=>$is_library));
92 93 94
			}
			exit;
		} else {
95
			foreach ($errors as $error) {
96 97 98 99 100
				echo json_encode(array('result'=>$error, 'id'=>''));
				exit;
			}
		}
	} else {
101
		$error = tra('Page not found. Please save the page first.');
102 103 104 105 106
		echo json_encode(array('result'=>$error, 'id'=>''));
		exit;
	}
}

107
if (isset($_REQUEST['addlibreference']) && $action = 'a_lib') {
108 109

	$errors = array();
110
	if (! $perms->use_references) {
111
		echo json_encode(
112 113 114 115
			array(
				'result'=>tra('failure'),
				'message'=>tra('You do not have sufficient permissions to perform this action.')
			)
116
		);
117 118
		exit;
	}
119

120
	if ($ref_biblio_code == '') {
121
		$errors[] = tra('Please enter Biblio Code.');
122
	}
123
	if (strlen($ref_biblio_code) > 50) {
124
		$errors[] = tra('Biblio code must not exceed 50 characters.');
125
	}
126

127
	if (count($errors) < 1) {
128 129
		$exists = $referenceslib->check_lib_existence($ref_biblio_code);
		if ($exists > 0) {
130
			echo json_encode(
131 132 133 134 135
				array(
					'result'=>tra('failure'),
					'message'=>tra('This reference already exists in the library.'),
					'is_library'=>$exists
				)
136
			);
137
		} else {
138
			$id = $referenceslib->add_reference(
139 140 141 142 143 144 145 146 147 148 149
				$ref_biblio_code,
				$ref_author,
				$ref_title,
				$ref_part,
				$ref_uri,
				$ref_code,
				$ref_year,
				$ref_style,
				$ref_template,
				$ref_publisher,
				$ref_location
150 151
			);
			echo json_encode(
152 153 154 155 156 157
				array(
					'result'=>tra('success'),
					'message'=>tra('Reference added to library.'),
					'id'=>$id,
					'is_library'=>$exists
				)
158
			);
159 160 161 162 163 164 165 166 167 168 169
		}
		exit;
	} else {
		foreach ($errors as $error) {
			echo json_encode(array('result'=>$error, 'message'=>$error));
			exit;
		}
	}
}

if (isset($_REQUEST['editreference'])) {
170
	if (! $perms->edit_references) {
171 172 173 174 175 176 177 178 179
		echo json_encode(
			array(
				'result'=>tra('failure'),
				'message'=>tra('You do not have sufficient permissions to perform this action.')
			)
		);
		exit;
	}

180 181 182

	$errors = array();

183
	if ($ref_biblio_code == '') {
184
		$errors[] = tra('Please enter Biblio Code.');
185
	} elseif (strlen($ref_biblio_code) > 50) {
186
		$errors[] = tra('Biblio code must not exceed 50 characters.');
187 188
	} else {
		$ref_details = $referenceslib->get_reference_from_id($ref_id);
189

190 191 192 193
		/*If new code is not equal to previous code, check_existence*/
		if ($ref_details['data'][0]['biblio_code'] != $ref_biblio_code) {
			$count = $referenceslib->check_existence($page_id, $ref_biblio_code);
			if ($count > 0) {
194
				$errors[] = tra('This biblio code already exists.');
195 196 197 198 199
			}
		}
	}

	if (count($errors)<1) {
200
		$referenceslib->edit_reference(
201 202 203 204 205 206 207 208 209 210 211 212
			$ref_id,
			$ref_biblio_code,
			$ref_author,
			$ref_title,
			$ref_part,
			$ref_uri,
			$ref_code,
			$ref_year,
			$ref_style,
			$ref_template,
			$ref_publisher,
			$ref_location
213
		);
214
		$exists = $referenceslib->check_lib_existence($ref_biblio_code);
215
		echo json_encode(
216 217 218 219 220
			array(
				'result'=>tra('success'),
				'message'=>tra('Bibliography saved.'),
				'is_library'=>$exists
			)
221
		);
222 223
		exit;
	} else {
224
		foreach ($errors as $error) {
225
			echo json_encode(array('result'=>tra('failure'), 'message'=>$error));
226 227 228 229 230 231
		}
		exit;
	}
}

if (isset($_REQUEST['action']) && isset($ref_id)) {
232
	if (! $perms->use_references) {
233
		echo json_encode(
234 235 236 237
			array(
				'result'=>tra('failure'),
				'message'=>tra('You do not have sufficient permissions to perform this action.')
			)
238
		);
239 240 241 242 243 244 245
		exit;
	}

	if ($_REQUEST['action'] == 'u_lib') {
		$exists = $referenceslib->check_existence($page_id, $ref_biblio_code);
		$id = $referenceslib->add_lib_ref_to_page($ref_id, $page_id);
		if ($id == -1) {
246
			echo json_encode(array('result'=>tra('failure'), 'message'=>tra('Reference already exists.'), 'id'=>$id));
247 248 249 250 251 252 253 254 255 256
		} else {
			$details = $referenceslib->get_reference_from_id($id);
			foreach ($details['data'][0] as $key=>$data) {
				if ($details['data'][0][$key] == NULL) {
					if (!$details['data'][0][$key]) {
						$details['data'][0][$key] = '';
					}
				}
			}

arildb's avatar
arildb committed
257
			echo json_encode(
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
				array(
					'result'=>tra('success'),
					'message'=>tra('Reference added.'),
					'id'=>$id,
					'ref_biblio_code'=>$details['data'][0]['biblio_code'],
					'ref_author'=>$details['data'][0]['author'],
					'ref_title'=>$details['data'][0]['title'],
					'ref_year'=>$details['data'][0]['year'],
					'ref_part'=>$details['data'][0]['part'],
					'ref_uri'=>$details['data'][0]['uri'],
					'ref_code'=>$details['data'][0]['code'],
					'ref_publisher'=>$details['data'][0]['publisher'],
					'ref_location'=>$details['data'][0]['location'],
					'ref_style'=>$details['data'][0]['style'],
					'ref_template'=>$details['data'][0]['template']
				)
274
			);
275 276 277 278 279 280
		}
		exit;
	}
}

if (isset($_REQUEST['action']) && isset($ref_id)) {
281
	if (! $perms->edit_references) {
282 283 284 285 286 287 288 289 290
		echo json_encode(
			array(
				'result'=>tra('failure'),
				'message'=>tra('You do not have sufficient permissions to perform this action.')
			)
		);
		exit;
	}

291 292
	if ($_REQUEST['action'] == 'e_del') {
		$referenceslib->remove_reference($ref_id);
293
		echo tra('success');
294 295
	}
}