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

11 12
// TODO - refactor to ajax-services then KILME

13 14 15 16 17 18 19 20 21
// If you make changes here check also Tracker/Field/DynamicList.php
/**
 * Handler for (XHR) requests from the trackerfield type dynamic itemslist. This handler does not save or update anything.
 * It only updates the listfield when the $filterFieldValueHere changes, which happens if it is for example as selection change on a linkfield or category field.
 * 
 * @see lib/core/Tracker/Field/DynamicList.php for furher details.
 */


changi67's avatar
changi67 committed
22
require_once ('tiki-setup.php');
lphuberdeau's avatar
lphuberdeau committed
23
$trklib = TikiLib::lib('trk');
24
if ($prefs['feature_categories'] == 'y') {
25
	$categlib = TikiLib::lib('categ');
26
}
27
if ($prefs['feature_trackers'] != 'y') {
28 29
	die;
}
30 31 32 33 34 35 36 37 38 39 40 41 42
$access = TikiLib::lib('access');

$filterFieldIdHere =  isset($_GET["filterFieldIdHere"]) ? $_GET["filterFieldIdHere"] : null;
$trackerIdThere =  isset($_GET["trackerIdThere"]) ? $_GET["trackerIdThere"] : null;
$filterFieldValueHere =  isset($_GET["filterFieldValueHere"]) ? $_GET["filterFieldValueHere"] : null;
$filterFieldIdThere = isset($_GET["filterFieldIdThere"]) ? $_GET["filterFieldIdThere"] : null;
$listFieldIdThere = isset($_GET["listFieldIdThere"]) ? $_GET["listFieldIdThere"] : null;
$statusThere = isset($_GET["statusThere"]) ? $_GET["statusThere"] : null;
// needed when multiple fields are bound to the same selection i.e $filterFieldValueHere
$insertId = isset($_GET["insertId"]) ? $_GET["insertId"] : null;
// needed when the default should be passed back to the frontend
$originalValue = isset($_GET["originalValue"]) ? $_GET["originalValue"] : null;

43
header('Cache-Control: no-cache');
44
header('content-type: application/x-javascript');
45

46
$json_return = array();
47 48 49 50 51 52 53 54
// prepare response, we need to pass back some request data:
//  $insertId: their could be more than one dynamic list field bound to the same $filterFieldValueHere. Need to know Dynamic Item List field we refer to.
// $originalValue: to select the orginalValue (the value currently used for selecting the element), on changes of $filterFieldValueHere  
$json_return['request'] = array(
	'insertId' => $insertId,
	'originalValue' => $originalValue
);
$json_return['response'] = array();
55

56 57 58 59 60
// if we do not have something to compare with we return empty result
if (empty($filterFieldValueHere)) {
	$access->output_serialized($json_return);
	return;
}
61

62 63 64 65 66 67
// start pre processing
$filterFieldHere = $trklib->get_tracker_field($filterFieldIdHere);
$filterFieldThere = $trklib->get_tracker_field($filterFieldIdThere);
// set defaults - normalize $filterValueHere to $finalFilterValueHere, $filterFieldIdThere to $finalFilterFieldIdThere
$finalFilterFieldIdThere = $filterFieldIdThere;
$finalFilterValueHere = $filterFieldValueHere;
68

69 70 71 72 73 74 75 76
// check which combination of filterFieldTypeHere and filterFieldTypeThere we have and how to deal with it.
// case 'xyz' - xyz tested means: this combinination has been tested and works for both, trackerlistview and item templateview.
switch ($filterFieldHere['type']) {
	case 'e': // category tested
		// only allow category with category. disallow any other category combination.
		if ($filterFieldThere['type'] != 'e') {
			$access->output_serialized($json_return);
			return;
77
		}
78 79 80 81 82 83 84 85 86 87 88 89 90 91
	break;
	
	case 'r': // r = itemlink - disallow itemlink/category, allow itemlink/itemlink, itemlink/simplefield types like text
		switch ($filterFieldThere['type']) {
			case 'r': // r = itemlink tested 
			break;
			
			case 't': // textfield tested
			default:
				$handler = $trklib->get_field_handler($filterFieldHere);
				$optTrackerId = $handler->getOption('trackerId');
				$optFieldId = $handler->getOption('fieldId');
				$finalFilterValueHere = $trklib->get_item_value($optTrackerId, $filterFieldValueHere, $optFieldId);
			break;
92
		}
93
	break;
94

95 96 97 98 99
	default:
		// if both field types are unknown but match, we assume that it could work
		if ($filterFieldHere['type'] != $filterFieldThere['type']) {
			$access->output_serialized($json_return);
			return;
100
		}
101 102
	break;
}
103

104 105 106 107 108 109 110 111 112 113 114 115 116 117
// start main processing - $trackerIdThere is not used at all in get_items_list()
$remoteItemIds = $trklib->get_items_list($trackerIdThere, $finalFilterFieldIdThere, $finalFilterValueHere, $statusThere);
$listFieldThere = $trklib->get_tracker_field($listFieldIdThere);
// special handling for itemList field. We would get always the same values on each iteration so we do it only one time.
$itemListFirstRun = true;
foreach ($remoteItemIds as $remoteItemId) {

	$itemInfo = $trklib->get_tracker_item($remoteItemId);

	// @TODO although it should be checked by the fieldhandler, verify that permissions on itemId level are respected. i.e restricted by a category field etc.
	// however: something like this does not work: $permObject = $tikilib->get_perm_object($remoteItemId, 'trackeritem');
	$hasPermission = true;
	if (!$hasPermission) {
		continue;
118
	}
119 120 121 122 123 124 125 126
	
	$listFieldThere = array_merge($listFieldThere, array('value' => $itemInfo[$listFieldIdThere]));
	$handler = $trklib->get_field_handler($listFieldThere, $itemInfo);
	// do not inherit showlinks settings from remote items.
	$context = array('showlinks' => 'n');
	
	// permissions are ok, now get the values depending on the fieldtype of $listFieldIdThere
	switch ($listFieldThere['type']) {
127

128 129 130 131 132 133
		case 'l': // itemlist tested
			// itemlink can have multiple matches and we would the same matches on each iteration.
			// so we only process one iteration and take the fields as selections out of it.
			if (!$itemListFirstRun) {
				break;
			}
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
			$valueFields = $handler->getFieldData();
			if (is_array($valueFields)) {
				// return each item of that list - requires match in DynamicList.php renderInnerOutput()
				// note: we save the itemId of the item selected out of the list, not the value. Allows to keep the links consistant on changing values.
				foreach ( $valueFields['items'] as $valueField => $labelField) {
					$json_return['response'][] = array($valueField, $labelField);
				}
			}
			$itemListFirstRun = false;
		break;

		case 'r': // itemlink tested
			$valueField = $handler->getFieldData();
			$labelField = $handler->renderOutput($context);
			$json_return['response'][] = array($valueField['value'], $labelField);
		break;
			
		case 'e': // category tested
			// array selected_categories etc.
			$valueField = $handler->getFieldData();
			// for some reason, need to apply the values back, oterwise renderOutput does not return a value - bug?
			$listFieldThere = array_merge($listFieldThere, $valueField);
			$handler = $trklib->get_field_handler($listFieldThere, $itemInfo);
			$labelField = $handler->renderOutput($context);
			// we return all categories per itemId, without html, comma separated
			$labelField = str_replace('<br/>', ', ', $labelField);
			$json_return['response'][] = array($remoteItemId, $labelField);
		break;
		
		// other fieldtypes
		case 't': // textfield tested
		default:		
			$labelField = $handler->renderOutput($context);
			$json_return['response'][] = array($remoteItemId, $labelField);
		break;
	} // switch

} // foreach
	
$access->output_serialized($json_return);
return;