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
8//this script may only be included - so its better to die if called directly.
9if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) {
10	header("location: index.php");
11	exit;
12}
13
14/**
15 * @return array
16 */
17function module_categories_info()
18{
19	return [
20		'name' => tra('Categories'),
21		'description' => tra('Displays links to categories as a tree.'),
22		'prefs' => ['feature_categories'],
23		'documentation' => 'Module categories',
24		'params' => [
25			'type' => [
26				'name' => tra('Object type filter'),
27				'description' => tra('Object type filter to apply when accessing a linked category. Example values:') . ' wiki page, article, faq, blog, image gallery, image, file gallery, tracker, trackerItem, quiz, poll, survey, sheet',
28				'filter' => 'striptags',
29			],
30			'deep' => [
31				'name' => tra('Deep'),
32				'description' => tra('Show subcategories objects when accessing a linked category. Possible values: on (default), off.'),
33				'filter' => 'word',
34			],
35			'categId' => [
36				'name' => tra('Category ID'),
37				'description' => tra('Limits displayed categories to a subtree of categories starting with the category with the given ID. Example value: 11. Default: 0 (don\'t limit display).'),
38				'filter' => 'int',
39				'profile_reference' => 'category',
40			],
41			'categParentIds' => [
42				'name' => tra('Show these categories and their children'),
43				'description' => tra('Show only these categories and the immediate child categories of these in the order the parameter specifies. Example values: 3,5,6.'),
44				'filter' => 'striptags',
45				'profile_reference' => 'category',
46			],
47			'selflink' => [
48				'name' => tra('Category links to a page named as the category'),
49				'description' => 'y|n .' . tra('If y, category links to a page named as the category'),
50				'filter' => 'alpha',
51			],
52			'hideEmpty' => [
53				'name' => tra('Hide Empty'),
54				'description' => 'y|n .' . tra('If y, only categories with child objects will be shown.'),
55				'filter' => 'alpha',
56			],
57			'onlyChildren' => [
58				'name' => tra('Only Children'),
59				'description' => 'y|n .' . tra('If y, only direct child categories will be shown. Default: n'),
60				'filter' => 'alpha',
61			],
62			'customURL' => [
63				'name' => tra('Custom URL'),
64				'description' => tra('Custom URL for link to send you. Use %catId% as placeholder for catId or %name% for name. E.g. "ProductBrowse?categ=%catId%" '),
65				'filter' => 'alpha',
66			],
67		],
68	];
69}
70
71/**
72 * @param $mod_reference
73 * @param $module_params
74 */
75function module_categories($mod_reference, &$module_params)
76{
77	global $prefs;
78	global $user;
79	$smarty = TikiLib::lib('smarty');
80	$categlib = TikiLib::lib('categ');
81	if (isset($module_params['type'])) {
82		$type = $module_params['type'];
83		$urlEnd = '&amp;type=' . urlencode($type);
84	} else {
85		$type = '';
86		$urlEnd = '';
87	}
88	if (isset($module_params['deep'])) {
89		$deep = $module_params['deep'];
90	} else {
91		$deep = 'on';
92	}
93	if ($deep === 'on') {
94		$urlEnd .= "&amp;deep=$deep";
95	}
96	$name = "";
97
98
99	if (isset($module_params['categId'])) {
100		$categId = $module_params['categId'];
101		if (isset($module_params['onlyChildren']) && $module_params['onlyChildren'] == 'y') {
102			$categories = $categlib->getCategories(['identifier' => $categId, 'type' => 'children']);
103		} else {
104			$categories = $categlib->getCategories(['identifier' => $categId, 'type' => 'descendants']);
105		}
106		foreach ($categories as $cat) {
107			if ($cat['categId'] == $categId) {
108				$name = $cat['name'];
109			}
110		}
111	} else {
112		$categories = $categlib->getCategories();
113		$categId = 0;
114	}
115	if (empty($categories)) {
116		$smarty->clearAssign('tree');
117		return;
118	}
119
120	if (isset($module_params['categParentIds'])) {
121		$categParentIds = explode(',', $module_params['categParentIds']);
122		$filtered_categories = [];
123		foreach ($categParentIds as $c) {
124			foreach ($categories as $cat) {
125				if ($cat['categId'] == $c || $cat['parentId'] == $c) {
126					$filtered_categories[] = $cat;
127				}
128			}
129		}
130		$categories = $filtered_categories;
131		unset($filtered_categories);
132	}
133
134	include_once('lib/tree/BrowseTreeMaker.php');
135	$tree_nodes = [];
136	include_once('tiki-sefurl.php');
137	foreach ($categories as $cat) {
138		if (! empty($module_params['hideEmpty']) && $module_params['hideEmpty'] === 'y' && $cat['objects'] == 0) {
139			$has_children = false;
140			foreach ($cat['children'] as $child) {
141				if (! empty($categories[$child]['objects'])) {
142					$has_children = true;
143					break;
144				}
145			}
146			if (! $has_children) {
147				continue;
148			}
149		}
150		if (isset($module_params['selflink']) && $module_params['selflink'] == 'y') {
151			$url = filter_out_sefurl('tiki-index.php?page=' . urlencode($cat['name']));
152		} elseif (isset($module_params['customURL'])) {
153			$url = str_replace("%catId%", $cat['categId'], $module_params['customURL']);
154			$url = str_replace("%name%", $cat['name'], $module_params['customURL']);
155		} else {
156			$url = filter_out_sefurl('tiki-browse_categories.php?parentId=' . $cat['categId'], 'category', $cat['name'], ! empty($urlEnd)) . $urlEnd;
157		}
158		$tree_nodes[] = [
159			"id" => $cat["categId"],
160			"parent" => $cat["parentId"],
161			'parentId' => $cat['parentId'],
162			'categId' => $cat['categId'],
163			"data" => '<span style="float: left; cursor: pointer; visibility: hidden;" class="ui-icon ui-icon-triangle-1-e"></span><a class="catname" href="' . $url . '">' . htmlspecialchars($cat['name']) . '</a><br />'
164		];
165	}
166	$res = '';
167	$tm = new BrowseTreeMaker('mod_categ' . $mod_reference['position'] . $mod_reference['ord']);
168	foreach ($categlib->findRoots($tree_nodes) as $node) {
169		$res .= $tm->make_tree($node, $tree_nodes);
170	}
171	$smarty->assign('tree', $res);
172}
173