tiki-admin_keywords.php 3.87 KB
Newer Older
1
<?php
changi67's avatar
changi67 committed
2 3 4
/**
 * @package tikiwiki
 */
5
// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
rjsmelo's avatar
rjsmelo committed
6
//
changi67's avatar
changi67 committed
7 8 9
// 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$
10

rjsmelo's avatar
rjsmelo committed
11
require_once('tiki-setup.php');
12

13 14 15 16 17 18
/***
 * @var \TikiAccessLib  $access
 * @var \Smarty_Tiki    $smarty
 * 
 */ 

changi67's avatar
changi67 committed
19 20
$access->check_feature('wiki_keywords');
$access->check_permission('tiki_p_admin_wiki');
21

22
/**
23
 * @param        $page
24
 * @param string $keywords
25 26 27
 *
 * @return TikiDb_Pdo_Result|TikiDb_Adodb_Result
 * @throws Exception
28
 */
rjsmelo's avatar
rjsmelo committed
29
function set_keywords($page, $keywords = "")
changi67's avatar
changi67 committed
30
{
31
	global $tikilib;
rjsmelo's avatar
rjsmelo committed
32

33
	$query = "UPDATE `tiki_pages` SET `keywords`=? WHERE `pageName`=? LIMIT 1";
rjsmelo's avatar
rjsmelo committed
34
	$bindvars = [ $keywords, $page ];
changi67's avatar
changi67 committed
35
	$result = $tikilib->query($query, $bindvars);
rjsmelo's avatar
rjsmelo committed
36

37
	if ($result && $result->numRows()) {
38 39 40 41
		/***
		* 
		* @var \UnifiedSearchLib $searchlib
		*/
42 43 44
		$searchlib = TikiLib::lib('unifiedsearch');
		$searchlib->invalidateObject('wiki page', $page);
		$searchlib->processUpdateQueue();
rjsmelo's avatar
rjsmelo committed
45 46
	}

47
	return $result;
48 49
}

50 51 52 53
/**
 * @param $page
 * @return bool
 */
rjsmelo's avatar
rjsmelo committed
54
function get_keywords($page)
changi67's avatar
changi67 committed
55
{
56
	global $tikilib;
changi67's avatar
changi67 committed
57
	return $tikilib->get_page_info($page);
58 59
}

60 61 62 63 64 65
/**
 * @param int $limit
 * @param int $offset
 * @param string $page
 * @return array
 */
rjsmelo's avatar
rjsmelo committed
66
function get_all_keywords($limit = 0, $offset = 0, $page = "")
changi67's avatar
changi67 committed
67
{
68
	global $tikilib;
rjsmelo's avatar
rjsmelo committed
69 70
	$query = "FROM `tiki_pages` WHERE `keywords` IS NOT NULL and `keywords` <> '' ";
	if ($page) {
71
		$query .= " and `pageName` LIKE ?";
rjsmelo's avatar
rjsmelo committed
72
		$bindvars = [ "%$page%" ];
73
	} else {
rjsmelo's avatar
rjsmelo committed
74
		$bindvars = [];
75
	}
rjsmelo's avatar
rjsmelo committed
76 77 78

	$ret = [
		'pages' => $tikilib->fetchAll("SELECT `keywords`, `pageName` as page " . $query, $bindvars, $limit, $offset),
changi67's avatar
changi67 committed
79
		'cant' => $tikilib->getOne('SELECT COUNT(*) ' . $query, $bindvars),
rjsmelo's avatar
rjsmelo committed
80 81
	];

82 83 84 85 86 87 88 89 90 91 92
	return $ret;
}

//Init variables for limit and offset
$limit = $prefs['maxRecords'];
$offset = 0;

//Check for offset, see if it's a multiple of the limit
//This is done to stop arbitrary offsets being entered
$offset = (int)$_REQUEST['offset'];

93 94 95
if ((isset($_REQUEST['save_keywords']) && isset($_REQUEST['new_keywords']) && isset($_REQUEST['page']) && $access->checkCsrf())
	|| (isset($_REQUEST['remove_keywords']) && isset($_REQUEST['page']) && $access->checkCsrfForm(tr('Remove keywords?')))
	) {
rjsmelo's avatar
rjsmelo committed
96
	//Set page and new_keywords var for both remove_keywords and
97
	//save_keywords actions at the same time
rjsmelo's avatar
rjsmelo committed
98 99 100
	( isset($_REQUEST['page']) ) ? $page = $_REQUEST['page'] : $page = $_REQUEST['page'];
	( isset($_REQUEST['new_keywords']) ) ? $new_keywords = $_REQUEST['new_keywords'] : $new_keywords = "";

101
	$result = set_keywords($page, $new_keywords);
rjsmelo's avatar
rjsmelo committed
102

103 104 105 106 107 108 109 110
	if ($result && $result->numRows()) {
		$msg = isset($_REQUEST['save_keywords'])
			? tr('Keywords for page "%0" saved', htmlspecialchars($_REQUEST['page']))
			: tr('Keywords for page "%0" removed', htmlspecialchars($_REQUEST['page']));
		Feedback::success($msg);
	} else {
		Feedback::error(tr('Keywords were not updated'));
	}
111 112
}

rjsmelo's avatar
rjsmelo committed
113
if (isset($_REQUEST['page']) && ! $_REQUEST['remove_keywords']) {
114
	$page_keywords = get_keywords($_REQUEST['page']);
rjsmelo's avatar
rjsmelo committed
115

changi67's avatar
changi67 committed
116 117
	$smarty->assign('edit_keywords', $page_keywords['keywords']);
	$smarty->assign('edit_keywords_page', $page_keywords['pageName']);
118 119 120
	$smarty->assign('edit_on', 'y');
}

rjsmelo's avatar
rjsmelo committed
121
if (isset($_REQUEST['q']) && ! $_REQUEST['remove_keywords'] && ! $_REQUEST['save_keywords']) {
122
	$existing_keywords = get_all_keywords($limit, $offset, $_REQUEST['q']);
123 124
	$smarty->assign('search_on', 'y');
	$smarty->assign('search_cant', $existing_keywords['cant']);
125 126
}

rjsmelo's avatar
rjsmelo committed
127
if (! isset($existing_keywords['cant'])) {
128 129 130
	$existing_keywords = get_all_keywords($limit, $offset);
}

rjsmelo's avatar
rjsmelo committed
131
if ($existing_keywords['cant'] > 0) {
132
	$smarty->assign('existing_keywords', $existing_keywords['pages']);
rjsmelo's avatar
rjsmelo committed
133

changi67's avatar
changi67 committed
134
	$pages_cant = ceil($existing_keywords['cant'] / $limit);
135 136
	$smarty->assign('pages_cant', $pages_cant);
	$smarty->assign('offset', $offset);
rjsmelo's avatar
rjsmelo committed
137
}
138

139
$smarty->assign('mid', 'tiki-admin_keywords.tpl');
changi67's avatar
changi67 committed
140
$smarty->display('tiki.tpl');