<commit_before><?php
// (c) Copyright 2002-2009 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.
//
// $Id$


/**
 * Tree Table Smarty func - smarty_function_treetable()
 * Renders a tree table (for use with http://plugins.jquery.com/project/treeTable)
 * 
 * Params
 * 
 * _data	:	array of data rows	 - 	e.g . with perms for now
 * 
 * 	array(
 * 		array('permName'=>'tiki_p_admin_newsletters', 'permDesc' => 'Can admin newsletters','level' => 'admin', 'type' => 'newsletters' etc...),
 * 		array('permName'=>'tiki_p_blahblah', etc...),
 * 	...)
 * 
 * _columns	: array of columns and headers array('permName' => tra('Permission Name'), 'permDesc' => tra('Permission Description'), etc
 * 				or a string like: '"permName"="Permission Name", "permDesc"="Description", etc'
 * 				if undefined it tries to guess (?)
 * 
 * _valueColumnIndex = 0	:	index of the col in the _data array above to use as the unique index
 * 
 * _sortColumn = ''			:	column to organise tree by (actually row key = e.g. 'type')
 * 
 * _checkbox = ''			: 	name of checkbox (auto-incrementing) - no checkboxes if not set
 * 								if comma delimted list then makes multiple checkboxes
 * 
 * _checkboxColumnIndex = 0	:	index of the col in the _data array above to use as the checkbox value
 * 								comma delimeted list (of ints) for multiple checkboxes as set above
 * 								if set needs to match number of checkboxes defines in _checkbox (or if not set uses 0,1,2 etc)
 * 
 * _checkboxTitles = ''		:	Comma delimited list of header titles for checkboxes (optional, but needs to match number of checkboxes above)
 * 
 * _listFilter = 'y'		:	include dynamic text filter
 * 
 * _filterMinRows = 12		:	don't show filter box if less than this number of rows
 * 
 * class = 'treeTable'		:	class of the table - will add 'sortable' if feature_jquery_sortable = y
 * id = 'treetable1'		:	id of the table (auto-incrementing)
 * 
 * _rowClasses = array('odd','even')	:	classes to cycle through for rows (tr's and td's)
 * 											can be a string for same class on each row
 * 											or empty string for not
 * 
 * _emptyDataMessage = {treetable}: '.tra('No rows found')	: message if there are no rows
 * 
 */

//this script may only be included - so its better to die if called directly.
if (strpos($_SERVER["SCRIPT_NAME"],basename(__FILE__)) !== false) {
  header("location: index.php");
  exit;
}

function smarty_function_treetable($params, &$smarty) {
	global $headerlib, $tree_table_id, $prefs;
	
	extract($params);
	
	$_emptyDataMessage = empty($_emptyDataMessage) ? '{treetable}: ' . tra('No rows found') : $_emptyDataMessage;
	if (empty($_data)) {
		return $_emptyDataMessage;
	}
	
	$_checkbox = empty($_checkbox) ? '' : $_checkbox;
	$_checkboxTitles = empty($_checkboxTitles) ? '' : $_checkboxTitles;
	
	if (strpos($_checkbox, ',') !== false) {
		$_checkbox = split(',', trim($_checkbox));
		if (isset($_checkboxColumnIndex)) {
			if (strpos($_checkboxColumnIndex, ',') !== false) {
				$_checkboxColumnIndex = split(',', trim($_checkboxColumnIndex));
			}
			if (count($_checkbox) != count($_checkboxColumnIndex)) {
				return tra('{treetable}: Number of items in _checkboxColumnIndex doesn not match items in _checkbox');
			}
		}
		if (!empty($_checkboxTitles)) {
			if (strpos($_checkboxTitles, ',') !== false) {
				$_checkboxTitles = split(',', trim($_checkboxTitles));
			}
			if (count($_checkbox) != count($_checkboxTitles)) {
				return tra('{treetable}: Number of items in _checkboxTitles doesn not match items in _checkbox');
			}
		}
	}
	$_checkboxColumnIndex = empty($_checkboxColumnIndex) ? 0 : $_checkboxColumnIndex;
	$_valueColumnIndex = empty($_valueColumnIndex) ? 0 : $_valueColumnIndex;
	
	if (!empty($_checkbox) && !is_array($_checkbox)) {
			$_checkbox = array($_checkbox);
			$_checkboxColumnIndex = array($_checkboxColumnIndex);
	}
	
	$html = '';
	$nl = "\n";
	
	// some defaults
	$_listFilter = empty($_listFilter) ? 'y' : $_listFilter;
	$_filterMinRows = empty($_filterMinRows) ? 12 : $_filterMinRows;
	$_rowClasses = !isset($_rowClasses) ? array('odd','even') : 
		(is_array($_rowClasses) ? $_rowClasses : array($_rowClasses));
	
	if (!empty($_rowClasses)) {
		$rowCounter = 0;
	} else {
		$rowCounter = -1;
	}
	
	// auto-increment val for unique id's etc
	if (empty($id)) {
		if (!isset($tree_table_id)) {
			$tree_table_id = 1;
		} else {
			$tree_table_id++;
		}
		$id = 'treetable_'. $tree_table_id;
	} else {
		$id;
	}
	// TODO - check this? add key/val pairs?
	if (empty($_columns)) {
		$keys =  array_keys($_data[0]);
		$_columns = array();
		foreach($keys as $key) {
			if (!is_numeric($key)) {
				$_columns[$key] = htmlentities($key);
			}
		}
	} else if (is_string($_columns)) {
		$ar = split(',', $_columns);
		$_columns = array();
		foreach ($ar as $str) {
			$ar2 = split('=', trim($str));
			$_columns[trim($ar2[0],' "')] = trim($ar2[1],' "');
		}
		unset($ar, $ar2);
	}
	
	$_sortColumn = empty($_sortColumn) ? '' : $_sortColumn;
	
	if ($_sortColumn) {
		sort2d($_data, $_sortColumn);
		$headerlib->add_jq_onready('$jq("#'.$id.'").treeTable({clickableNodeNames:true});');
		// TODO refilter when .parent is opened - seems to prevent the click propagating
//		$headerlib->add_jq_onready('$jq("tr.parent").click(function(event) {
//if ($jq("#'.$id.'_filter").val()) {
//	$jq("#'.$id.'_filter").trigger("keyup");
//	if (event.isPropagationStopped() || event.isImmediatePropagationStopped()) {
//		$jq(this).trigger("click");
//	}
//}
//		});');
	}
	
	$class = empty($class) ? 'treeTable' : $class;	// treetable
	
	if ($prefs['feature_jquery_tablesorter'] == 'y' && strpos($class, 'sortable') === false) {
		 //$class .= ' sortable';
	}
	
	if ($_listFilter == 'y' && count($_data) > $_filterMinRows) {
		include_once('lib/smarty_tiki/function.listfilter.php');
		$html .= smarty_function_listfilter(
			array('id' => $id.'_filter',
				  'selectors' => "#$id tbody tr:not(.parent)",
				  'parentSelector' => "#$id tbody .parent",
				  'exclude' => ".subHeader",
				  $smarty));
	}
	
	// start writing the table
	$html .= $nl.'<table id="'.$id.'" class="'.$class.'">'.$nl;
	
	// write the table header
	$html .= '<thead><tr>';
	if (!empty($_checkbox)) {
		include_once('lib/smarty_tiki/function.select_all.php');
		for ($i = 0; $i < count($_checkbox); $i++) {
			$html .= '<th class="checkBoxHeader">';
			$html .= smarty_function_select_all(
						array('checkbox_names'=>$_checkbox[$i].'[]',
							  'label' => empty($_checkboxTitles) ? '' : $_checkboxTitles[$i]), $smarty);
			$html .= '</th>';
		}
	}
	
	foreach ($_columns as $column => $columnName) {
		$html .= '<th>';
		$html .= htmlentities($columnName);
		$html .= '</th>';
	}
	$html .= '</tr></thead>'.$nl;
	$html .= '<tbody>'.$nl;
	
	$treeColumnsAdded = array();
	
		// for each row
	foreach ($_data as &$row) {
		// set up tree hierarchy<commit_msg>[FIX] Warning (typo) and notice (type)<commit_after><?php
// (c) Copyright 2002-2009 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.
//
// $Id$


/**
 * Tree Table Smarty func - smarty_function_treetable()
 * Renders a tree table (for use with http://plugins.jquery.com/project/treeTable)
 * 
 * Params
 * 
 * _data	:	array of data rows	 - 	e.g . with perms for now
 * 
 * 	array(
 * 		array('permName'=>'tiki_p_admin_newsletters', 'permDesc' => 'Can admin newsletters','level' => 'admin', 'type' => 'newsletters' etc...),
 * 		array('permName'=>'tiki_p_blahblah', etc...),
 * 	...)
 * 
 * _columns	: array of columns and headers array('permName' => tra('Permission Name'), 'permDesc' => tra('Permission Description'), etc
 * 				or a string like: '"permName"="Permission Name", "permDesc"="Description", etc'
 * 				if undefined it tries to guess (?)
 * 
 * _valueColumnIndex = 0	:	index of the col in the _data array above to use as the unique index
 * 
 * _sortColumn = ''			:	column to organise tree by (actually row key = e.g. 'type')
 * 
 * _checkbox = ''			: 	name of checkbox (auto-incrementing) - no checkboxes if not set
 * 								if comma delimted list then makes multiple checkboxes
 * 
 * _checkboxColumnIndex = 0	:	index of the col in the _data array above to use as the checkbox value
 * 								comma delimeted list (of ints) for multiple checkboxes as set above
 * 								if set needs to match number of checkboxes defines in _checkbox (or if not set uses 0,1,2 etc)
 * 
 * _checkboxTitles = ''		:	Comma delimited list of header titles for checkboxes (optional, but needs to match number of checkboxes above)
 * 
 * _listFilter = 'y'		:	include dynamic text filter
 * 
 * _filterMinRows = 12		:	don't show filter box if less than this number of rows
 * 
 * class = 'treeTable'		:	class of the table - will add 'sortable' if feature_jquery_sortable = y
 * id = 'treetable1'		:	id of the table (auto-incrementing)
 * 
 * _rowClasses = array('odd','even')	:	classes to cycle through for rows (tr's and td's)
 * 											can be a string for same class on each row
 * 											or empty string for not
 * 
 * _emptyDataMessage = {treetable}: '.tra('No rows found')	: message if there are no rows
 * 
 */

//this script may only be included - so its better to die if called directly.
if (strpos($_SERVER["SCRIPT_NAME"],basename(__FILE__)) !== false) {
  header("location: index.php");
  exit;
}

function smarty_function_treetable($params, &$smarty) {
	global $headerlib, $tree_table_id, $prefs;
	
	extract($params);
	
	$_emptyDataMessage = empty($_emptyDataMessage) ? '{treetable}: ' . tra('No rows found') : $_emptyDataMessage;
	if (empty($_data)) {
		return $_emptyDataMessage;
	}
	
	$_checkbox = empty($_checkbox) ? '' : $_checkbox;
	$_checkboxTitles = empty($_checkboxTitles) ? '' : $_checkboxTitles;
	
	if (strpos($_checkbox, ',') !== false) {
		$_checkbox = split(',', trim($_checkbox));
		if (isset($_checkboxColumnIndex)) {
			if (is_string($_checkboxColumnIndex) && strpos($_checkboxColumnIndex, ',') !== false) {
				$_checkboxColumnIndex = split(',', trim($_checkboxColumnIndex));
			}
			if (count($_checkbox) != count($_checkboxColumnIndex)) {
				return tra('{treetable}: Number of items in _checkboxColumnIndex doesn not match items in _checkbox');
			}
		}
		if (!empty($_checkboxTitles)) {
			if (strpos($_checkboxTitles, ',') !== false) {
				$_checkboxTitles = split(',', trim($_checkboxTitles));
			}
			if (count($_checkbox) != count($_checkboxTitles)) {
				return tra('{treetable}: Number of items in _checkboxTitles doesn not match items in _checkbox');
			}
		}
	}
	$_checkboxColumnIndex = empty($_checkboxColumnIndex) ? 0 : $_checkboxColumnIndex;
	$_valueColumnIndex = empty($_valueColumnIndex) ? 0 : $_valueColumnIndex;
	
	if (!empty($_checkbox) && !is_array($_checkbox)) {
			$_checkbox = array($_checkbox);
			$_checkboxColumnIndex = array($_checkboxColumnIndex);
	}
	
	$html = '';
	$nl = "\n";
	
	// some defaults
	$_listFilter = empty($_listFilter) ? 'y' : $_listFilter;
	$_filterMinRows = empty($_filterMinRows) ? 12 : $_filterMinRows;
	$_rowClasses = !isset($_rowClasses) ? array('odd','even') : 
		(is_array($_rowClasses) ? $_rowClasses : array($_rowClasses));
	
	if (!empty($_rowClasses)) {
		$rowCounter = 0;
	} else {
		$rowCounter = -1;
	}
	
	// auto-increment val for unique id's etc
	if (empty($id)) {
		if (!isset($tree_table_id)) {
			$tree_table_id = 1;
		} else {
			$tree_table_id++;
		}
		$id = 'treetable_'. $tree_table_id;
	} else {
		$id;
	}
	// TODO - check this? add key/val pairs?
	if (empty($_columns)) {
		$keys =  array_keys($_data[0]);
		$_columns = array();
		foreach($keys as $key) {
			if (!is_numeric($key)) {
				$_columns[$key] = htmlentities($key);
			}
		}
	} else if (is_string($_columns)) {
		$ar = split(',', $_columns);
		$_columns = array();
		foreach ($ar as $str) {
			$ar2 = split('=', trim($str));
			$_columns[trim($ar2[0],' "')] = trim($ar2[1],' "');
		}
		unset($ar, $ar2);
	}
	
	$_sortColumn = empty($_sortColumn) ? '' : $_sortColumn;
	
	if ($_sortColumn) {
		sort2d($_data, $_sortColumn);
		$headerlib->add_jq_onready('$jq("#'.$id.'").treeTable({clickableNodeNames:true});');
		// TODO refilter when .parent is opened - seems to prevent the click propagating
//		$headerlib->add_jq_onready('$jq("tr.parent").click(function(event) {
//if ($jq("#'.$id.'_filter").val()) {
//	$jq("#'.$id.'_filter").trigger("keyup");
//	if (event.isPropagationStopped() || event.isImmediatePropagationStopped()) {
//		$jq(this).trigger("click");
//	}
//}
//		});');
	}
	
	$class = empty($class) ? 'treeTable' : $class;	// treetable
	
	if ($prefs['feature_jquery_tablesorter'] == 'y' && strpos($class, 'sortable') === false) {
		 //$class .= ' sortable';
	}
	
	if ($_listFilter == 'y' && count($_data) > $_filterMinRows) {
		include_once('lib/smarty_tiki/function.listfilter.php');
		$html .= smarty_function_listfilter(
			array('id' => $id.'_filter',
				  'selectors' => "#$id tbody tr:not(.parent)",
				  'parentSelector' => "#$id tbody .parent",
				  'exclude' => ".subHeader"),  $smarty);
	}
	
	// start writing the table
	$html .= $nl.'<table id="'.$id.'" class="'.$class.'">'.$nl;
	
	// write the table header
	$html .= '<thead><tr>';
	if (!empty($_checkbox)) {
		include_once('lib/smarty_tiki/function.select_all.php');
		for ($i = 0; $i < count($_checkbox); $i++) {
			$html .= '<th class="checkBoxHeader">';
			$html .= smarty_function_select_all(
						array('checkbox_names'=>$_checkbox[$i].'[]',
							  'label' => empty($_checkboxTitles) ? '' : $_checkboxTitles[$i]), $smarty);
			$html .= '</th>';
		}
	}
	
	foreach ($_columns as $column => $columnName) {
		$html .= '<th>';
		$html .= htmlentities($columnName);
		$html .= '</th>';
	}
	$html .= '</tr></thead>'.$nl;
	$html .= '<tbody>'.$nl;
	
	$treeColumnsAdded = array();
	
		// for each row
	foreach ($_data as &$row) {
		// set up tree hierarchy
{'fraction_non_alphanumeric': 0.1771513774495882, 'fraction_numerical': 0.004544163589889236, 'mean_word_length': 3.835221421215242, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1067520', 'n_tokens_mistral': 5224, 'n_tokens_neox': 4718, 'n_words': 1219}