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

require_once 'tiki-setup.php';
12
$categlib = TikiLib::lib('categ');
13 14
require_once 'lib/transitionlib.php';

rjsmelo's avatar
rjsmelo committed
15
$auto_query_args = [];
16

17
$access->check_permission('tiki_p_admin');
18 19

// Init
rjsmelo's avatar
rjsmelo committed
20
if (isset($_SESSION['transition'])) {
21 22 23 24
	$transition_mode = $_SESSION['transition']['mode'];
	$available_states = $_SESSION['transition']['states'];
} else {
	$transition_mode = 'category';
rjsmelo's avatar
rjsmelo committed
25
	$available_states = [];
26 27 28
}

$selected_transition = null;
rjsmelo's avatar
rjsmelo committed
29
$to_add = [];
30 31 32

// Action handling

rjsmelo's avatar
rjsmelo committed
33
switch ($jitRequest->action->alpha()) {
34 35
	case 'subset':
		$transition_mode = $_REQUEST['transition_mode'];
36

rjsmelo's avatar
rjsmelo committed
37
		if ($transition_mode == 'category') {
38
			$jitPost->replaceFilter('cat_categories', 'int');
rjsmelo's avatar
rjsmelo committed
39
			if ($selection = $jitPost->asArray('cat_categories')) {
40
				$available_states = array_combine(
41
					$selection,
rjsmelo's avatar
rjsmelo committed
42
					array_map([ $categlib, 'get_category_name' ], $selection)
43 44
				);
			} else {
rjsmelo's avatar
rjsmelo committed
45
				$available_states = [];
46
			}
47
		} else {
48
			$jitPost->replaceFilter('groups', 'groupname');
rjsmelo's avatar
rjsmelo committed
49
			if ($selection = $jitPost->asArray('groups')) {
50 51
				$available_states = array_combine($selection, $selection);
			} else {
rjsmelo's avatar
rjsmelo committed
52
				$available_states = [];
53
			}
54
		}
55 56 57 58 59 60 61 62 63 64 65 66
		break;
	case 'new':
		$transitionlib = new TransitionLib($transition_mode);
		$transitionlib->addTransition(
			$_REQUEST['from'],
			$_REQUEST['to'],
			$_REQUEST['label'],
			isset($_REQUEST['preserve'])
		);
		break;
	case 'edit':
		$transitionlib = new TransitionLib($transition_mode);
67

rjsmelo's avatar
rjsmelo committed
68
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
69 70 71 72 73 74 75 76
			$transitionlib->updateTransition(
				$_REQUEST['transitionId'],
				$_REQUEST['from'],
				$_REQUEST['to'],
				$_REQUEST['label'],
				isset($_REQUEST['preserve'])
			);
		} else {
77
			$selected_transition = $transitionlib->getTransition((int) $_REQUEST['transitionId']);
78 79 80 81 82
		}
		break;
	case 'addguard':
		$transitionlib = new TransitionLib($transition_mode);
		$selected_transition = $transitionlib->getTransition((int) $_REQUEST['transitionId']);
83

rjsmelo's avatar
rjsmelo committed
84 85
		if ($selection = $jitPost->asArray('states')) {
			$selected_transition['guards'][] = [
86 87 88
						$_REQUEST['type'],
						(int) $_REQUEST['count'],
						$selection,
rjsmelo's avatar
rjsmelo committed
89
			];
90
			$transitionlib->updateGuards((int) $selected_transition['transitionId'], $selected_transition['guards']);
91 92 93 94 95 96
		}
		break;
	case 'removeguard':
		$transitionlib = new TransitionLib($transition_mode);
		$selected_transition = $transitionlib->getTransition((int) $_REQUEST['transitionId']);

rjsmelo's avatar
rjsmelo committed
97
		unset($selected_transition['guards'][ (int) $_REQUEST['guard'] ]);
98 99 100 101 102 103
		$selected_transition['guards'] = array_values($selected_transition['guards']);
		$transitionlib->updateGuards((int) $selected_transition['transitionId'], $selected_transition['guards']);
		break;
	case 'remove':
		$transitionlib = new TransitionLib($transition_mode);
		check_ticket('remove_transition');
104

105 106
		$transitionlib->removeTransition($_REQUEST['transitionId']);
		break;
107 108 109
}

// Obtain data
110
$categories = $categlib->getCategories();
111
$cat_tree = $categlib->generate_cat_tree($categories, true, array_keys($available_states));
112

113 114
$transitionlib = new TransitionLib($transition_mode);
$transitions = $transitionlib->listTransitions(array_keys($available_states));
115

rjsmelo's avatar
rjsmelo committed
116
if ($selected_transition) {
117
	// When a transition is selected, make sure all of its endpoints are listed in the edit panel
rjsmelo's avatar
rjsmelo committed
118
	$to_add = [ $selected_transition['from'], $selected_transition['to'] ];
119

rjsmelo's avatar
rjsmelo committed
120
	foreach ($selected_transition['guards'] as $guard) {
121
		$to_add = array_merge($to_add, $guard[2]);
122 123 124
	}
}

rjsmelo's avatar
rjsmelo committed
125
foreach ($transitions as & $trans) {
126 127
	$trans['from_label'] = transition_label_finder($trans['from']);
	$trans['to_label'] = transition_label_finder($trans['to']);
128 129 130
}

// Setup Smarty & Session
rjsmelo's avatar
rjsmelo committed
131
$_SESSION['transition'] = [
132 133
				'mode' => $transition_mode,
				'states' => $available_states,
rjsmelo's avatar
rjsmelo committed
134
];
135

rjsmelo's avatar
rjsmelo committed
136
foreach ($to_add as $v) {
137
	$available_states[ $v ] = transition_label_finder($v);
138
}
139

rjsmelo's avatar
rjsmelo committed
140 141 142 143
$guards = [];
if ($selected_transition) {
	foreach ($selected_transition['guards'] as $guard) {
		$guards[] = [
144 145 146
						'type' => $guard[0],
						'count' => $guard[1],
						'members' => array_map('transition_label_finder', $guard[2]),
rjsmelo's avatar
rjsmelo committed
147
		];
148 149 150
	}
}

151 152 153 154 155
$smarty->assign('transition_mode', $transition_mode);
$smarty->assign('available_states', $available_states);
$smarty->assign('transitions', $transitions);
$smarty->assign('guards', $guards);
$smarty->assign('selected_transition', $selected_transition);
156

157
$smarty->assign('cat_tree', $cat_tree);
158

159
// Graph setup
rjsmelo's avatar
rjsmelo committed
160 161 162 163
if (count($available_states) > 0) {
	$edges = [];
	foreach ($transitions as $tr) {
		$edges[] = [
164 165 166 167
						'from' => $tr['from_label'],
						'to' => $tr['to_label'],
						'label' => $tr['name'],
						'preserve' => (bool) $tr['preserve']
rjsmelo's avatar
rjsmelo committed
168
		];
169 170
	}

171 172
	$smarty->assign('graph_nodes', json_encode(array_values($available_states)));
	$smarty->assign('graph_edges', json_encode($edges));
173 174
}

175 176
$smarty->assign('mid', 'tiki-admin_transitions.tpl');
$smarty->display('tiki.tpl');
177

178 179 180 181
/**
 * @param $state
 * @return mixed|string
 */
rjsmelo's avatar
rjsmelo committed
182
function transition_label_finder($state)
183
{
184 185
	global $available_states, $transition_mode;
	$categlib = TikiLib::lib('categ');
186

rjsmelo's avatar
rjsmelo committed
187
	if (isset($available_states[$state])) {
188
		return $available_states[$state];
rjsmelo's avatar
rjsmelo committed
189
	} elseif ($transition_mode == 'category') {
190
		return $categlib->get_category_name($state);
191 192 193 194
	} else {
		return $state;
	}
}