tiki-browse_categories.php 10.2 KB
Newer Older
lrargerich's avatar
lrargerich committed
1
<?php
2
// (c) Copyright 2002-2011 by authors of the Tiki Wiki CMS Groupware Project
3
// 
4 5
// 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.
6
// $Id$
7

8
$section = 'categories';
9 10 11
require_once ('tiki-setup.php');
include_once ('lib/categories/categlib.php');
include_once ('lib/tree/categ_browse_tree.php');
12 13 14
$access->check_feature('feature_categories');
$access->check_permission('tiki_p_view_category');

15 16
$prefsgroups = $prefs['feature_group_watches'];
global $prefsgroups, $tiki_p_admin_users, $tiki_p_admin;
17 18 19

$auto_query_args = array('deep', 'sort_mode', 'offset', 'find', 'type', 'parentId');

lrargerich's avatar
lrargerich committed
20
// Check for parent category or set to 0 if not present
21 22
if (!isset($_REQUEST['parentId'])) {
	$_REQUEST['parentId'] = 0;
lrargerich's avatar
lrargerich committed
23
}
24
$smarty->assign('parentId', $_REQUEST['parentId']);
25
if (isset($_REQUEST['maxRecords']) && ($_REQUEST['maxRecords'] >= 1 || $_REQUEST['maxRecords'] == -1)) {
26
	$maxRecords = $_REQUEST['maxRecords'];
27 28
} else {
	$maxRecords = $prefs['maxRecords'];
29
}
30
if (!isset($_REQUEST['sort_mode'])) {
31 32
	$sort_mode = 'name_asc';
} else {
33
	$sort_mode = $_REQUEST['sort_mode'];
34
}
35
if (!isset($_REQUEST['offset'])) {
36 37
	$offset = 0;
} else {
38
	$offset = $_REQUEST['offset'];
39 40
}
$smarty->assign_by_ref('offset', $offset);
41
if (!isset($_REQUEST['type'])) {
42 43
	$type = '';
} else {
44
	$type = $_REQUEST['type'];
45 46
}
$smarty->assign('type', $type);
47 48
if (isset($_REQUEST['find'])) {
	$find = $_REQUEST['find'];
49 50 51 52 53
} else {
	$find = '';
}
$smarty->assign('find', $find);
$smarty->assign_by_ref('sort_mode', $sort_mode);
54
if (isset($_REQUEST['deep']) && $_REQUEST['deep'] == 'on') {
55
	$deep = 'on';
56 57
	$smarty->assign('deep', 'on');
} else {
58
	$deep = 'off';
59 60
	$smarty->assign('deep', 'off');
}
61
$canView = false;
62
if (is_array($_REQUEST['parentId'])) {
63
	Perms::bulk( array( 'type' => 'category' ), 'object', $_REQUEST['parentId'] );
64
	foreach($_REQUEST['parentId'] as $p) {
65 66 67 68
		$perms = Perms::get( array( 'type' => 'category', 'object' => $p ) );
		if( $perms->view_category ) {
			$paths[] = $categlib->get_category_path($p);
			$p_info = $categlib->get_category($p);
69 70
			$canView = true;
		}
71 72
	}
	$smarty->assign('paths', $paths);
73
	$smarty->assign('headtitle', tra('Categories'));
lrargerich's avatar
lrargerich committed
74
} else {
75
	// If the parent category is not zero get the category path
76
	if ($_REQUEST['parentId']) {
77 78
		$perms = Perms::get( array( 'type' => 'category', 'object' => $_REQUEST['parentId'] ) );

79 80 81
		$path = $categlib->get_category_path($_REQUEST['parentId']);
		$p_info = $categlib->get_category($_REQUEST['parentId']);
		$father = $p_info['parentId'];
82
		$smarty->assign_by_ref('p_info', $p_info);
83
		$canView = $perms->view_category;
84
		$smarty->assign('headtitle', tra($p_info['name'])); //TODO Why translate ?
85
	} else {
86
		$path = tra('TOP');
87
		$father = 0;
88
		$canView = true;
89
		$smarty->assign('headtitle', tra('Categories'));
90 91 92
	}
	$smarty->assign('path', $path);
	$smarty->assign('father', $father);
lrargerich's avatar
lrargerich committed
93
}
94
if (!$canView) {
95
	$smarty->assign('errortype', 401);
96
	$smarty->assign('msg', tra('You do not have permission to view this page.'));
97
	$smarty->display('error.tpl');
98 99
	die;
}
100
//watches
101 102 103
if ($prefs['feature_user_watches'] == 'y') {
	if ($user && isset($_REQUEST['watch_event'])) {
		if ($_REQUEST['watch_action'] == 'add_desc') {
104
			$name = tra('Top');
105 106 107 108 109
			if ($_REQUEST['watch_object'] != 0) {
				$name = $categlib->get_category_path_string_with_root($_REQUEST['watch_object']);
			}
			$categlib->watch_category_and_descendants($user, $_REQUEST['watch_object'], $name);
		} else if ($_REQUEST['watch_action'] == 'add') {
110
			$name = tra('Top');
111 112 113 114 115 116 117 118 119 120 121
			if ($_REQUEST['watch_object'] != 0) {
				$name = $categlib->get_category_path_string_with_root($user, $_REQUEST['watch_object']);
			}
			$categlib->watch_category($user, $_REQUEST['watch_object'], $name);
		} else if ($_REQUEST['watch_action'] == 'remove_desc') {
			$categlib->unwatch_category_and_descendants($user, $_REQUEST['watch_object']);
		} else if ($_REQUEST['watch_action'] == 'remove') {
			$categlib->unwatch_category($user, $_REQUEST['watch_object']);
		}
	}
}
122 123

$ctall = $categlib->get_all_categories_respect_perms(null, 'view_category');
marclaporte's avatar
marclaporte committed
124

125
$descendants_curr = $categlib->get_category_descendants($_REQUEST['parentId']);
126
//user watches on current level
127 128 129
$usercatwatches_curr = $tikilib->get_user_watches($user, 'category_changed');
$eyes_curr = add_watch_icons ($descendants_curr, $usercatwatches_curr, $_REQUEST['parentId'], $_REQUEST['parentId'], $deep, $user);
$smarty->assign_by_ref('eyes_curr', $eyes_curr);
130

131 132 133 134 135 136 137 138
$i = 0;
foreach($ctall as $c) {
	$descendants = $categlib->get_category_descendants($c['categId']);
	$usercatwatches = $tikilib->get_user_watches($user, 'category_changed');
	$eyes = add_watch_icons ($descendants, $usercatwatches, $_REQUEST['parentId'], $c['categId'], $deep, $user);
	$ctall[$i]['eyes'] = $eyes;
	++$i;
}
139 140 141 142 143 144
$tree_nodes = array();
foreach($ctall as $c) {
	$tree_nodes[] = array(
		'id' => $c['categId'],
		'parent' => $c['parentId'],
		'data' => '<span class="object-count">'.$c['objects'].'</span>' . $c['eyes'].' <a class="catname" href="tiki-browse_categories.php?parentId=' . $c["categId"] . '&amp;deep=' . $deep . '&amp;type=' 
145
					. urlencode($type) . '">' . htmlspecialchars(tr($c['name'])) .'</a> ', 
146
	);
147
}
148 149 150 151
$tm = new CatBrowseTreeMaker('categ');
$res = $tm->make_tree($_REQUEST['parentId'], $tree_nodes);
$smarty->assign('tree', $res);

152
$objects = $categlib->list_category_objects($_REQUEST['parentId'], $offset, $maxRecords, $sort_mode, $type, $find, $deep == 'on', (!empty($_REQUEST['and'])) ? true : false);
153
if ($deep == 'on') {
154
	for ($i = count($objects['data']) - 1; $i >= 0; --$i) $objects['data'][$i]['categName'] = $tikilib->other_value_in_tab_line($ctall, $objects['data'][$i]['categId'], 'categId', 'name');
155
}
156 157


158 159
$smarty->assign_by_ref('objects', $objects['data']);
$smarty->assign_by_ref('cant_pages', $objects['cant']);
160
$smarty->assign_by_ref('maxRecords', $maxRecords);
161
include_once ('tiki-section_options.php');
162
ask_ticket('browse-categories');
163

164 165
// disallow robots to index page:
$smarty->assign('metatag_robots', 'NOINDEX, NOFOLLOW');
166 167 168
if (isset($_GET['plain'])) {
	header('Content-Type: text/plain');
	foreach($objects['data'] as $object) echo "{$object['categName']}\t{$object['type']}\t{$object['itemId']}\n";
169
	exit;
170
} else {
171 172
	// Display the template
	$smarty->assign('mid', 'tiki-browse_categories.tpl');
173
	$smarty->display('tiki.tpl');
174
}
175

176
function add_watch_icons($descendants, $usercatwatches, $requestid, $categid, $deep, $user) {
177 178 179 180
	global $prefs;
	if (!$user || $prefs["feature_user_watches"] != 'y') {
		 return false;
	}
181 182 183 184 185
	global $prefsgroups, $tiki_p_admin_users, $tiki_p_admin, $categlib;
	$section = 'categories';
	require_once ('tiki-setup.php');
	include_once ('lib/categories/categlib.php');
	include_once ('lib/tree/categ_browse_tree.php');
186 187 188
	$nodesc = count($descendants);
	$watch_desc = 'n';
	$watch_this = 'n';
sylvieg's avatar
sylvieg committed
189
	$eyes = $eyesgroup = '';
190
	if ($categid == 0) {
191 192 193
		$tip_rem_desc = tra('Stop watching all categories');
		$tip_add_desc = tra('Watch all categories');
		$tip_group = tra('Group watches for all categories');
194
	} else {
195 196 197
		$tip_rem_desc = tra('Stop watching this category and its descendants');
		$tip_add_desc = tra('Watch this category and its descendants');
		$tip_group = tra('Group watches for this category');
198
	}
199
	$eye_rem_desc = '&nbsp;&nbsp;<a href="tiki-browse_categories.php?parentId=' . $requestid . '&amp;watch_event=category_changed&amp;watch_object=' . $categid . '&amp;deep=' . $deep . '&amp;watch_action=remove_desc" class="catname"><img src="pics/icons/no_eye_arrow_down.png" alt="' . $tip_rem_desc . '" style="margin-right:2px" width="14" height="14" title="' . $tip_rem_desc . '" class="catname" /></a>';
200
	$eye_rem = 	'<a href="tiki-browse_categories.php?parentId=' . $requestid . '&amp;watch_event=category_changed&amp;watch_object=' . $categid . '&amp;deep=' . $deep . '&amp;watch_action=remove" class="catname"><img src="pics/icons/no_eye.png" alt="'.tra("Stop watching this category").'" width="14" style="margin-right:3px" height="14" title="'.tra("Stop watching this category").'" class="catname" /></a>';
201
	$eye_add_desc = '&nbsp;&nbsp;<a href="tiki-browse_categories.php?parentId=' . $requestid . '&amp;watch_event=category_changed&amp;watch_object=' . $categid . '&amp;deep=' . $deep . '&amp;watch_action=add_desc" class="catname"><img src="pics/icons/eye_arrow_down.png" alt="' . $tip_add_desc . '" style="margin-right:2px" width="14" height="14" title="' . $tip_add_desc . '" class="catname" /></a>';
202
	$eye_add = 	'<a href="tiki-browse_categories.php?parentId=' . $requestid . '&amp;watch_event=category_changed&amp;watch_object=' . $categid . '&amp;deep=' . $deep . '&amp;watch_action=add" class="icon"><img src="pics/icons/eye.png" alt="'.tra("Watch this category").'" width="14" style="margin-right:3px;margin-bottom:0.052cm" height="14" title="'.tra("Watch this category").'" class="catname" /></a>';
203
	foreach ($descendants as $descendant) {
204 205 206
		if ($nodesc > 1) {
			//this category and descendants
			foreach ($usercatwatches as $usercatwatch) {
207
				if ($usercatwatch['object'] == $descendant || $descendant == 0) {
208
					$watch_desc = 'y';
209 210
					break;
				} else {
211
					$watch_desc = 'n';
212 213
				}
			}
214 215
			if ($watch_desc == 'n') {
				$eyes = $eye_add_desc; 
216 217
				break;
			} else {
218
				$eyes = $eye_rem_desc;
219 220
			}
		}
221 222
	}
	//this category only
223
	foreach ($usercatwatches as $usercatwatch) {
224 225 226
		if ($usercatwatch['object'] == $descendants[0]) {
			$watch_this = 'y';
			break;
227
		} else {
228
			$watch_this = 'n';
229
		}
230
	}
231 232 233
	if ($categid == 0) {
		$eyes .= '';
	} elseif ($watch_this == 'n') {
234 235 236 237 238
		$nodesc > 1 ? $eyes .= $eye_add : $eyes .= '&nbsp;&nbsp;' . $eye_add;
	} else {
		$nodesc > 1 ? $eyes .= $eye_rem : $eyes .= '&nbsp;&nbsp;' . $eye_rem;
	}
	//group watches
239 240 241 242 243 244
	if ($prefsgroups == 'y' && ( $tiki_p_admin_users == 'y' || $tiki_p_admin == 'y' )) {
		$objName = '';
		if ($categid == 0) {
			$objName = 'Top';
		} else {
			$objName = $categlib->get_category_path_string_with_root($categid);
245
		}
246
		$eyesgroup = '&nbsp;<a href="tiki-object_watches.php?objectId=' . $categid . '&amp;watch_event=category_changed&amp;objectType=Category&amp;objectName=' 
chealer's avatar
chealer committed
247
				. urlencode($objName) . '&amp;objectHref=tiki-browse_categories.php?parentId=' . $categid . '&amp;deep=' . $deep . '" >
248 249
				<img src="pics/icons/eye_group.png" alt="' . $tip_group . '" width="14" style="margin-bottom:2px" height="14" 
				title="' . $tip_group . '" class="catname" /></a>';
250 251
	}
	return $eyes . $eyesgroup;
252
}