function wikiplugin_groupmailcore($data, $params)
{
    global $tikilib;
    require_once 'lib/wiki-plugins/wikiplugin_trackerlist.php';
    $trackerparams = array();
    $trackerparams['trackerId'] = $params['trackerId'];
    $trackerparams['fields'] = $params['fromFId'] . ':' . $params['operatorFId'] . ':' . $params['subjectFId'] . ':' . $params['datetimeFId'];
    $trackerparams['popup'] = $params['fromFId'] . ':' . $params['contentFId'];
    $trackerparams['filterfield'] = $params['fromFId'] . ':' . $params['accountFId'];
    $trackerparams['filtervalue'] = $params['fromEmail'] . ':' . $params['accountName'];
    $trackerparams['stickypopup'] = 'n';
    $trackerparams['showlinks'] = 'y';
    $trackerparams['shownbitems'] = 'n';
    $trackerparams['showinitials'] = 'n';
    $trackerparams['showstatus'] = 'n';
    $trackerparams['showcreated'] = 'n';
    $trackerparams['showlastmodif'] = 'n';
    $data = wikiplugin_trackerlist('', $trackerparams);
    return $data;
}
function wikiplugin_trackerfilter($data, $params)
{
	global $smarty, $prefs;
	global $trklib;	include_once('lib/trackers/trackerlib.php');
	static $iTrackerFilter = 0;
	if ($prefs['feature_trackers'] != 'y') {
		return $smarty->fetch("wiki-plugins/error_tracker.tpl");
	}
	$iTrackerFilter++;
	$default = array('noflipflop'=>'n', 'action'=>'Filter', 'line' => 'n', 'displayList' => 'n', 'export_action' => '',
					 'export_itemid' => 'y', 'export_status' => 'n', 'export_created' => 'n', 'export_modif' => 'n', 'export_charset' => 'UTF-8', 'status' => 'opc');

	if (isset($_REQUEST['reset_filter'])) {
		wikiplugin_trackerFilter_reset_filters($iTrackerFilter);
	} else if (!isset($_REQUEST['filter']) && isset($_REQUEST['session_filters']) && $_REQUEST['session_filters'] == 'y') {
		$params = array_merge($params, wikiplugin_trackerFilter_get_session_filters($iTrackerFilter));
	}
	if (isset($_REQUEST["mapview"]) && $_REQUEST["mapview"] == 'y' && !isset($_REQUEST["searchmap"]) && !isset($_REQUEST["searchlist"]) || isset($_REQUEST["searchmap"]) && !isset($_REQUEST["searchlist"])) {
		$params["googlemap"] = 'y';
	}
	if (isset($_REQUEST["mapview"]) && $_REQUEST["mapview"] == 'n' && !isset($_REQUEST["searchmap"]) && !isset($_REQUEST["searchlist"]) || isset($_REQUEST["searchlist"]) && !isset($_REQUEST["searchmap"]) ) {
		$params["googlemap"] = 'n';
	}
	$params = array_merge($default, $params);
	extract($params, EXTR_SKIP);
	$dataRes = '';

	if (isset($_REQUEST['msgTrackerFilter'])) {
		$smarty->assign('msgTrackerFilter', $_REQUEST['msgTrackerFilter']);
	}
	
	global $headerlib; include_once 'lib/headerlib.php';
	$headerlib->add_jq_onready(
					'/* Maintain state of other trackerfilter plugin forms */
					$(".trackerfilter form").submit( function () {
						var current_tracker = this;
						$(current_tracker).append("<input type=\"hidden\" name=\"tracker_filters[]\" value=\"" + $(current_tracker).serialize() + "\" />")
						$(".trackerfilter form").each( function() {
							if (current_tracker !== this && $("input[name=count_item]", this).val() > 0) {
								$(current_tracker).append("<input type=\"hidden\" name=\"tracker_filters[]\" value=\"" + $(this).serialize() + "\" />")
							}
						});
						return true;
					});'
	);

	if (!empty($_REQUEST['tracker_filters']) && count($_REQUEST['tracker_filters']) > 0) {
		foreach ($_REQUEST['tracker_filters'] as $tf_vals) {
			parse_str(urldecode($tf_vals), $vals);
			foreach ( $vals as $k => $v) {
				// if it's me and i had some items
				if ($k == 'iTrackerFilter' && $v == $iTrackerFilter && isset($vals['count_item']) && $vals['count_item'] > 0) {
					// unset request params for all the plugins (my one will be array_merged below)
					foreach ($_REQUEST['tracker_filters'] as $tf_vals2) {
						parse_str(urldecode($tf_vals2), $vals2);
						foreach ( $vals2 as $k2 => $v2) {
							unset($GLOBALS['_REQUEST'][$k2]);
						}
					}
			 		$_REQUEST = array_merge($_REQUEST, $vals);
				}
			}
		}
	}
	
	if (!isset($filters)) {
		if (empty($export_action)) {
			return tra('missing parameters').' filters';
		} else {
			$listfields = array();
			$filters = array();
			$formats = array();
		}
	} else {
	
		$listfields = wikiplugin_trackerFilter_split_filters($filters);
		foreach ($listfields as $i=>$f) {
			if (strchr($f, '/')) {
				list($fieldId, $format) = explode('/', $f);
				$listfields[$i] = $fieldId;
				$formats[$fieldId] = $format;
			} else {
				$formats[$f] = '';
			}
		}
	}
	if (empty($trackerId) && !empty($_REQUEST['trackerId'])) {
		 $trackerId = $_REQUEST['trackerId'];
	}
	
	$tracker_definition = Tracker_Definition::get($trackerId);
	
	if (empty($_REQUEST['filter']) && empty($export_action)) { // look if not coming from an initial and not exporting
		foreach ($_REQUEST as $key =>$val) {
			if (substr($key, 0, 2) == 'f_') {
				$_REQUEST['filter'] = 'y';
				break;
			}
		}
	}
	if (!isset($sortchoice)) {
		$sortchoice = '';
	} else {
		unset($params['sortchoice']);
		if (isset($_REQUEST["tr_sort_mode$iTrackerFilter"])) {
			$params['sort_mode'] = $_REQUEST["tr_sort_mode$iTrackerFilter"];
		}
		foreach ($sortchoice as $i=>$sc) {
			$sc = explode('|', $sc);
			$sortchoice[$i] = array('value'=>$sc[0], 'label'=>empty($sc[1])?$sc[0]: $sc[1]);
		}
	}
	if (empty($trackerId) || !($tracker = $trklib->get_tracker($trackerId))) {
		return $smarty->fetch("wiki-plugins/error_tracker.tpl");
	}
	$filters = wikiplugin_trackerFilter_get_filters($trackerId, $listfields, $formats, $status);
	if (empty($export_action)) {
		if (!is_array($filters)) {
			return $filters;
		}
	}
	if (($displayList == 'y' || isset($_REQUEST['filter']) || isset($_REQUEST['tr_offset']) || isset($_REQUEST['tr_sort_mode'])) &&
				(!isset($_REQUEST['iTrackerFilter']) || $_REQUEST['iTrackerFilter'] == $iTrackerFilter)) {
	  
		$ffs = array();
		$values = array();
		$exactValues = array();
		wikiplugin_trackerfilter_build_trackerlist_filter($_REQUEST, $formats, $ffs, $values, $exactValues, $tracker_definition);
		// echo '<pre>BUILD_FILTER'; print_r($ffs); print_r($exactValues); echo '</pre>';

		$params['fields'] = isset($fields) ? $fields : array();
		if (empty($params['trackerId'] )) {
			$params['trackerId'] = $trackerId;
		}
		if (!empty($ffs)) {
			if (empty($params['filterfield'])) {
				$params['filterfield'] = $ffs;
				$params['exactvalue'] = $exactValues;
				$params['filtervalue'] = $values;
			} else {
				$c = count($params['filterfield']);
				$params['filterfield'] = array_merge($params['filterfield'], $ffs);
				for ($i = 0; $i < $c; ++$i) {
					$params['exactvalue'][$i] = empty($params['exactvalue'][$i])?'':$params['exactvalue'][$i];
					$params['filtervalue'][$i] = empty($params['filtervalue'][$i])?'':$params['filtervalue'][$i];
				}
				$params['exactvalue'] = array_merge($params['exactvalue'], $exactValues);
				$params['filtervalue'] = array_merge($params['filtervalue'], $values);
			}
		}
		if (empty($params['max']))
			$params['max'] = $prefs['maxRecords'];
		if (!empty($_REQUEST['f_status']))
			$params['status'] = $_REQUEST['f_status'];
		wikiplugin_trackerFilter_save_session_filters($params, $iTrackerFilter);
		$smarty->assign('urlquery', wikiplugin_trackerFilter_build_urlquery($params));
		include_once('lib/wiki-plugins/wikiplugin_trackerlist.php');
		$dataRes .= wikiplugin_trackerlist($data, $params);
	} else {
		$data = '';
	}

	$smarty->assign_by_ref('sortchoice', $sortchoice);
	$smarty->assign_by_ref('filters', $filters);
	//echo '<pre>';print_r($filters); echo '</pre>';
	$smarty->assign_by_ref('trackerId', $trackerId);
	$smarty->assign('line', ($line == 'y' || $line == 'in')? 'y': 'n');
	$smarty->assign('indrop', $line == 'in'? 'y': 'n');
	$smarty->assign('iTrackerFilter', $iTrackerFilter);
	if (!empty($export_action)) {
		$smarty->assign('export_action', $export_action);
		$smarty->assign('export_fields', implode(':', $fields));
		$smarty->assign('export_itemid', $export_itemid == 'y' ? 'on' : '');
		$smarty->assign('export_status', $export_status == 'y' ? 'on' : '');
		$smarty->assign('export_created', $export_created == 'y' ? 'on' : '');
		$smarty->assign('export_modif', $export_modif == 'y' ? 'on' : '');
		$smarty->assign('export_charset', $export_charset);
		
		if (empty($params['filters'])) {
			if (!empty($filterfield)) { 	// convert param filters to export params
				$f_fields = array();
				for ($i = 0, $cfilterfield = count($filterfield); $i < $cfilterfield ; $i++) {
					if (!empty($exactvalue[$i])) {
						$f_fields['f_' . $filterfield[$i]] = $exactvalue[$i];
					} else if (!empty($filtervalue[$i])) {
						$f_fields['f_' . $filterfield[$i]] = $filtervalue[$i];
						$f_fields['x_' . $filterfield[$i]] = 't';	// x_ is for not exact?
					}
				}
				$smarty->assign_by_ref('f_fields', $f_fields);
			}
			$filters = array();	// clear out filters set up earlier which default to all fields if not exporting
		}
	}
	if ($displayList == 'n' || !empty($_REQUEST['filter']) || $noflipflop == 'y' || $prefs['javascript_enabled'] != 'y' || (isset($_SESSION['tiki_cookie_jar']["show_trackerFilter$iTrackerFilter"]) && $_SESSION['tiki_cookie_jar']["show_trackerFilter$iTrackerFilter"] == 'y')) {
		$open = 'y';
		$_SESSION['tiki_cookie_jar']["show_trackerFilter$iTrackerFilter"] = 'y';
	} else {
		$open = 'n';
	}
	$smarty->assign_by_ref('open', $open);
	$smarty->assign_by_ref('action', $action);
	$smarty->assign_by_ref('noflipflop', $noflipflop);
	$smarty->assign_by_ref('dataRes', $dataRes);
	
	if (isset($googlemapButtons)) {
		$smarty->assign('googlemapButtons', $googlemapButtons);
	}
	
	$dataF = $smarty->fetch('wiki-plugins/wikiplugin_trackerfilter.tpl');

	static $first = true;

	if ( $first ) {
		$first = false;
		global $headerlib;
		$headerlib->add_jq_onready(
						'$("a.prevnext", "#trackerFilter' . $iTrackerFilter . ' + .trackerfilter-result").click( function( e ) {
							e.preventDefault();
							$("#trackerFilter' . $iTrackerFilter . ' form")
							.attr("action", $(this).attr("href"))
							.submit();
						} );'
		);
	}

	return $data . $dataF;
}
function wikiplugin_trackerfilter($data, $params)
{
    global $smarty;
    global $trklib;
    include_once 'lib/trackers/trackerlib.php';
    extract($params, EXTR_SKIP);
    $dataRes = '';
    if (isset($_REQUEST['msgTrackerFilter'])) {
        $smarty->assign('msgTrackerFilter', $_REQUEST['msgTrackerFilter']);
    }
    if (isset($_REQUEST['filter']) || isset($_REQUEST['tr_offset']) || isset($_REQUEST['tr_sort_mode'])) {
        if (!isset($fields) && (empty($_REQUEST['trackerId']) || empty($trackerId))) {
            $smarty->assign('msg', tra("missing parameters"));
            return $smarty->fetch("error_simple.tpl");
        }
        foreach ($_REQUEST as $key => $val) {
            if (substr($key, 0, 2) == 'f_' && $val[0] != '') {
                $ffs[] = substr($key, 2);
                $values[] = $val;
            }
        }
        $params['fields'] = $fields;
        if (empty($params['trackerId'])) {
            $params['trackerId'] = $_REQUEST['trackerId'];
        }
        unset($params['filterfield']);
        unset($params['filtervalue']);
        if (!empty($ffs)) {
            $params['filterfield'] = $ffs;
            $params['exactvalue'] = $values;
        }
        include_once 'lib/wiki-plugins/wikiplugin_trackerlist.php';
        $dataRes .= wikiplugin_trackerlist($data, $params);
        $dataRes .= '<br />';
    }
    if (!isset($filters)) {
        $smarty->assign('msg', tra("missing parameters"));
        return $dataRes . $smarty->fetch("error_simple.tpl");
    }
    $listfields = split(':', $filters);
    $filters = array();
    if (!isset($trackerId)) {
        $trackerId = 0;
    }
    foreach ($listfields as $f) {
        if (strchr($f, '/')) {
            list($fieldId, $format) = split('/', $f);
        } else {
            $fieldId = $f;
            $format = 'r';
            // radio as default
        }
        $field = $trklib->get_tracker_field($fieldId);
        if ($trackerId) {
            if ($field['trackerId'] != $trackerId) {
                $smarty->assign('msg', tra('All fields must be from the same tracker'));
                return $dataRes . $smarty->fetch('error_simple.tpl');
            }
        } else {
            $trackerId = $field['trackerId'];
        }
        $selected = false;
        switch ($field['type']) {
            case 'e':
                // category
                global $categlib;
                include_once 'lib/categories/categlib.php';
                $res = $categlib->get_child_categories($field['options']);
                $opts = array();
                foreach ($res as $opt) {
                    $opt['id'] = $opt['categId'];
                    if (!empty($_REQUEST['f_' . $fieldId]) && in_array($opt['id'], $_REQUEST['f_' . $fieldId])) {
                        $opt['selected'] = 'y';
                        $selected = true;
                    } else {
                        $opt['selected'] = 'n';
                    }
                    $opts[] = $opt;
                }
                break;
            case 'd':
                // drop down list
                $res = split(',', $field['options']);
                $opts = array();
                foreach ($res as $val) {
                    $opt['id'] = $val;
                    $opt['name'] = $val;
                    if (!empty($_REQUEST['f_' . $fieldId]) && $_REQUEST['f_' . $fieldId][0] == $val) {
                        $opt['selected'] = 'y';
                        $selected = true;
                    } else {
                        $opt['selected'] = 'n';
                    }
                    $opts[] = $opt;
                }
                break;
            case 'n':
            case 't':
            case 'a':
            case 'm':
            case 'y':
                if (isset($status)) {
                    $res = $trklib->list_tracker_field_values($fieldId, $status);
                } else {
                    $res = $trklib->list_tracker_field_values($fieldId);
                }
                $opts = array();
                foreach ($res as $val) {
                    $opt['id'] = $val;
                    $opt['name'] = $val;
                    if (!empty($_REQUEST['f_' . $fieldId]) && ($_REQUEST['f_' . $fieldId][0] == $val || in_array($val, $_REQUEST['f_' . $fieldId]))) {
                        $opt['selected'] = 'y';
                        $selected = true;
                    } else {
                        $opt['selected'] = 'n';
                    }
                    $opts[] = $opt;
                }
                break;
            default:
                $smarty->assign('msg', tra("tracker field type not processed yet"));
                return $dataRes . $smarty->fetch("error_simple.tpl");
        }
        $filters[] = array('name' => $field['name'], 'fieldId' => $field['fieldId'], 'format' => $format, 'opts' => $opts, 'selected' => $selected);
    }
    $smarty->assign('filters', $filters);
    $smarty->assign('trackerId', $trackerId);
    $data = $smarty->fetch('wiki-plugins/wikiplugin_trackerfilter.tpl');
    return $dataRes . $data;
}
function wikiplugin_trackerfilter($data, $params)
{
    global $smarty, $prefs;
    global $trklib;
    include_once 'lib/trackers/trackerlib.php';
    if ($prefs['feature_trackers'] != 'y') {
        return $smarty->fetch("wiki-plugins/error_tracker.tpl");
    }
    extract($params, EXTR_SKIP);
    $dataRes = '';
    if (isset($_REQUEST['msgTrackerFilter'])) {
        $smarty->assign('msgTrackerFilter', $_REQUEST['msgTrackerFilter']);
    }
    if (!isset($filters)) {
        $smarty->assign('msg', tra("missing parameters"));
        return $dataRes . $smarty->fetch("error_simple.tpl");
    }
    $listfields = split(':', $filters);
    foreach ($listfields as $i => $f) {
        if (strchr($f, '/')) {
            list($fieldId, $format) = split('/', $f);
            $listfields[$i] = $fieldId;
            $formats[$fieldId] = $format;
        } else {
            $formats[$f] = '';
        }
    }
    if (!isset($displayList)) {
        $displayList = 'n';
    }
    if (empty($trackerId) && !empty($_REQUEST['trackerId'])) {
        $trackerId = $_REQUEST['trackerId'];
    }
    if (!isset($line)) {
        $line = 'n';
    }
    if (empty($_REQUEST['filter'])) {
        // look if not coming from an initial
        foreach ($_REQUEST as $key => $val) {
            if (substr($key, 0, 2) == 'f_') {
                $_REQUEST['filter'] = 'y';
                break;
            }
        }
    }
    if ($displayList == 'y' || isset($_REQUEST['filter']) || isset($_REQUEST['tr_offset']) || isset($_REQUEST['tr_sort_mode'])) {
        if (!isset($trackerId) || !($tracker = $trklib->get_tracker($trackerId))) {
            return $smarty->fetch("wiki-plugins/error_tracker.tpl");
        }
        if (!isset($fields)) {
            $smarty->assign('msg', tra("missing parameters"));
            return $msg;
        }
        foreach ($_REQUEST as $key => $val) {
            if (substr($key, 0, 2) == 'f_' && !empty($val) && (!is_array($val) || !empty($val[0]))) {
                $fieldId = substr($key, 2);
                $ffs[] = $fieldId;
                if (isset($formats[$fieldId]) && ($formats[$fieldId] == 't' || $formats[$fieldId] == 'i')) {
                    $exactValues[] = '';
                    $values[] = $formats[$fieldId] == 'i' ? "{$val}%" : $val;
                } else {
                    $exactValues[] = $val;
                    $values[] = '';
                }
            }
        }
        $params['fields'] = $fields;
        if (empty($params['trackerId'])) {
            $params['trackerId'] = $trackerId;
        }
        unset($params['filterfield']);
        unset($params['filtervalue']);
        if (!empty($ffs)) {
            $params['filterfield'] = $ffs;
            $params['exactvalue'] = $exactValues;
            $params['filtervalue'] = $values;
        }
        //echo '<pre>'; print_r($params);echo '</pre>';
        include_once 'lib/wiki-plugins/wikiplugin_trackerlist.php';
        $dataRes .= wikiplugin_trackerlist($data, $params);
        $dataRes .= '<br />';
    } else {
        $data = '';
    }
    $filters = wikiplugin_trackerFilter_get_filters($trackerId, $listfields, $formats);
    $smarty->assign_by_ref('filters', $filters);
    $smarty->assign_by_ref('trackerId', $trackerId);
    $smarty->assign_by_ref('line', $line);
    static $iTrackerFilter = 0;
    $smarty->assign('iTrackerFilter', $iTrackerFilter++);
    if ($displayList == 'n' || !empty($_REQUEST['filter'])) {
        $open = 'y';
    } else {
        $open = 'n';
    }
    $smarty->assign_by_ref('open', $open);
    if (!isset($action)) {
        $action = 'Filter';
        //tra('Filter')
    }
    $smarty->assign_by_ref('action', $action);
    $dataF = $smarty->fetch('wiki-plugins/wikiplugin_trackerfilter.tpl');
    return $data . $dataF . $dataRes;
}