-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod-func-change_category.php
204 lines (186 loc) · 9.55 KB
/
mod-func-change_category.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
//
// 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.
/**
* @return array
*/
function module_change_category_info()
{
return [
'name' => tra('Change Category'),
'description' => tra('Enables to categorize an object.') . " " . tra('Some combinations of Multiple categories, Detailed, Unassign and Assign may challenge intuition or be simply broken.'),
'prefs' => ['feature_categories', 'feature_wiki'],
'documentation' => 'Module change_category',
'params' => [
'id' => [
'name' => tra('Category identifier'),
'description' => tra('Changes the root of the displayed categories from default "TOP" to the category with the given identifier.') . " " . tra('Note that the root category is not displayed.') . " " . tra('Example value: 13.') . " " . tra('Defaults to 0 (root).'),
'filter' => 'int',
'profile_reference' => 'category',
],
'notop' => [
'name' => tra('No top'),
'description' => tra('In non-detailed view, disallow uncategorizing. Example value: 1.') . " " . tra('Not set by default.'),
],
'path' => [
'name' => tra('Display path'),
'description' => tra('Unless set to "n", display relative category paths in the category tree rather than category names.') . " " . tra('Paths are relative to the root category, which is not displayed.') . " " . tra('Example value:') . ' "n". ' . tra('Not set by default.'),
],
'multiple' => [
'name' => tra('Multiple categories'),
'description' => tra('If set to "n", only allows categorizing in one category (from those displayed).') . " " . tra('Not set by default.'),
],
'categorize' => [
'name' => tra('Categorize'),
'description' => tra('String to display on the button to submit new categories, when multiple categories is enabled. Default value: Categorize.'),
],
'shy' => [
'name' => tra('Shy'),
'description' => tra('If set to "y", the module is not shown on pages which are not already categorized.') . " " . tra('Not set by default.'),
],
'detail' => [
'name' => tra('Detailed'),
'description' => tra('If set to "y", shows a list of categories in which the object is. If deletion is not disabled, it is done with the list.') . " " . tra('Not set by default.'),
],
'del' => [
'name' => tra('Unassign'),
'description' => tra('If set to "n", the detailed list of categories will not offer to unassign a category.') . " " . tra('Not set by default.'),
'depends' => 'detail'
],
'add' => [
'name' => tra('Assign'),
'description' => tra('If set to "y", allow to assign new categories.') . " " . tra('Example values: y, n.') . " " . tra('Default value: y.'),
],
'group' => [
'name' => tra('Group filter'),
'description' => tra('Very particular filter option. If set to "y", only categories with a name matching one of the user\'s groups are shown, and descendants of these matching categories.') . " " . tra('Example values: y, n.') . " " . tra('Default value: n.'),
],
'imgUrlNotIn' => [
'name' => tra('Image URL not in category'),
'description' => tra('Very particular parameter. If both this and "Image URL in category" are set and the root category contains a single child category, the module only displays an image with this URL if the object is not in the category.') . ' ' . tra('Example value:') . ' http://www.example.org/img/redcross.png.',
],
'imgUrlIn' => [
'name' => tra('Image URL in category'),
'description' => tra('Very particular parameter. If both this and "Image URL not in category" are set and the root category contains a single child category, the module only displays an image with this URL if the object is in the category.') . ' ' . tra('Example value:') . ' http://www.example.org/img/bigplus.png.',
],
],
];
}
/**
* @param $mod_reference
* @param $module_params
*/
function module_change_category($mod_reference, $module_params)
{
global $prefs;
$smarty = TikiLib::lib('smarty');
$modlib = TikiLib::lib('mod');
$smarty->assign('showmodule', false);
$object = current_object();
if (($object && $object['type'] && $object['object']) || $modlib->is_admin_mode(true)) {
$categlib = TikiLib::lib('categ');
if (! empty($module_params['id'])) {
$id = $module_params['id'];
$cat_parent = $categlib->get_category_name($id);
} else {
$id = 0;
$cat_parent = '';
}
if (! empty($module_params['shy']) && ! $modlib->is_admin_mode(true)) {
$shy = $module_params['shy'] === 'y';
} else {
$shy = false;
}
$detailed = isset($module_params['detail']) ? $module_params['detail'] : "n";
$smarty->assign('detailed', $detailed);
$add = isset($module_params['add']) ? $module_params['add'] : "y";
$smarty->assign('add', $add);
$multiple = isset($module_params['multiple']) ? $module_params['multiple'] : "y";
$smarty->assign('multiple', $multiple);
$cat_type = $object['type'] ?? '';
$cat_objid = $object['object'] ?? '';
$categories = $categlib->getCategories($id ? ['identifier' => $id, 'type' => 'descendants'] : null);
if (! empty($module_params['group']) && $module_params['group'] == 'y') {
global $user;
$userlib = TikiLib::lib('user');
if (! $user) {
return;
}
$userGroups = $userlib->get_user_groups_inclusion($user);
foreach ($categories as $i => $cat) {
if (isset($userGroups[$cat['name']])) {
continue;
}
$ok = false;
foreach ($cat['tepath'] as $c) {
if (isset($userGroups[$c])) {
$ok = true;
break;
}
}
if (! $ok) {
unset($categories[$i]);
}
}
}
$managedCategories = array_keys($categories);
if (isset($_REQUEST['remove']) && (! isset($module_params['del']) || $module_params['del'] != 'n')) {
$originalCategories = $categlib->get_object_categories($cat_type, $cat_objid);
// Check if the object is in the category to prevent infinite redirection.
if (in_array($_REQUEST['remove'], $originalCategories) && in_array($_REQUEST['remove'], $managedCategories)) {
$selectedCategories = [];
$managedCategories = array_intersect([(int) $_REQUEST['remove']], $managedCategories);
}
} elseif (isset($_REQUEST["modcatid"]) and $_REQUEST["modcatid"] == $id) {
if (! isset($_REQUEST['modcatchange'])) {
$selectedCategories = [];
} elseif (is_array($_REQUEST['modcatchange'])) {
$selectedCategories = $_REQUEST['modcatchange'];
} else {
$selectedCategories = [$_REQUEST['modcatchange']];
}
foreach ($selectedCategories as &$selectedCategory) {
$selectedCategory = (int) $selectedCategory;
}
if ($detailed != 'n') {
$managedCategories = array_intersect($selectedCategories, $managedCategories);
}
}
if (isset($selectedCategories)) {
$objectperms = Perms::get(['type' => $cat_type, 'object' => $cat_objid]);
if ($objectperms->modify_object_categories) {
$categlib->update_object_categories($selectedCategories, $cat_objid, $cat_type, null, null, null, $managedCategories);
}
header('Location: ' . $_SERVER['REQUEST_URI']);
die;
}
$objectCategories = $categlib->get_object_categories($cat_type, $cat_objid);
$isInAllManagedCategories = true;
foreach ($categories as &$category) {
if (in_array($category['categId'], $objectCategories)) {
$category['incat'] = 'y';
$shy = false;
} else {
$category['incat'] = 'n';
$isInAllManagedCategories = false;
}
}
if (count($categories) != 1) {
unset($module_params['imgUrlNotIn']);
unset($module_params['imgUrlIn']);
}
$smarty->assign('isInAllManagedCategories', $isInAllManagedCategories);
$smarty->assign('showmodule', ! $shy);
$objectlib = TikiLib::lib('object');
$title = $objectlib->get_title($cat_type, $cat_objid) ?? '';
if (empty($cat_parent)) {
$smarty->assign('tpl_module_title', sprintf(tra('Categorize %s'), htmlspecialchars($title)));
} else {
$smarty->assign('tpl_module_title', sprintf(tra('Categorize %s in %s'), htmlspecialchars($title), htmlspecialchars($cat_parent)));
}
$smarty->assign('modcatlist', $categories);
$smarty->assign('modcatid', $id);
}
}