Location: PHPKode > projects > PHPture > phpture/keyword.php
<?php

require_once('conf.php');

header('Content-Type: text/html; charset=UTF-8');

?><!DOCTYPE html>
<html>
<head>
<title>Keywords</title>
<link rel="stylesheet" href="style/keyword.css" type="text/css">

<script type="text/javascript" src="script/keyword.js"></script>
</head>
<body>
<ul id="keyword"><?php

$keyword_list = array();

/**
 * The offspring is all the children of a keyword and all their children and theirs, etc.
 * @param string $id The unique identifer of the keyword of which the desired offspring.
 * @return array A one dimensional array of unique ID's off all the offspring.
*/
function getKeywordOffspring($id) {

	global $keyword_list;

	$offspring_id_list = array();

	if(is_array($id)) {
		return array();
	}

	if(!is_null($keyword_list[$id]['children']) && is_array($keyword_list[$id]['children'])) {
		foreach($keyword_list[$id]['children'] as $child_id) {
			
			$offspring_id_list[] = $child_id;
			
			array_merge($offspring_id_list, getKeywordOffspring($keyword_list[$child_id]));
		}
	}
	
	$offspring_id_list = array_unique($offspring_id_list);

	return $offspring_id_list;
}

/**

*/
function getKeywordList() {

	global $keyword_list;
	
	$db = new PDO('sqlite:'.DATABASE_PATH);
	foreach ($db->query('SELECT ZUUID, ZNAME, ZPARENTUUID FROM ZRKKEYWORD ORDER BY ZNAME') as $row) {
	
		$keyword = array(
			'id' => $row['ZUUID'],
			'parent' => $row['ZPARENTUUID'],
			'name' => $row['ZNAME'],
			'children' => array()
		);
		
		if(isset($keyword_list[$keyword['id']]) && is_array($keyword_list[$keyword['id']]['children'])) {
		
			$keyword['children'] = $keyword_list[$keyword['id']]['children'];
		}
		
		$keyword_list[$keyword['parent']]['children'][] = $keyword['id'];
	
		$keyword_list[$keyword['id']] = $keyword;
	}
	
	return getKeywordChildren('', 'odd');
}

/**
 * @param string $parent_id The unique ID of the keyword of
 * @param string $class Optional name off the class, should alternate between 'even' and 'odd'.
 * @return string A string containg HTML-elements of the kind <li> and <ul> that form tree structure of clickable keywords.
*/
function getKeywordChildren($parent_id, $class = 'even') {

	global $keyword_list;

//print_r($keyword_list);

	$output = '';
	
	if(!isset($keyword_list[$parent_id]['children'])) {
		return;
	}

	$child_list = $keyword_list[$parent_id]['children'];

	for($i = 0; $i < count($child_list); $i++) {

		$keyword = $keyword_list[$child_list[$i]];
		
		$output_children = getKeywordChildren($keyword['id'], $class);

		$output .= '<li class="'.$class.'">';
				
		if(strlen($output_children) > 0) {
			$output .= '<img src="media/keyword/collapsed.png" class="expandCollapse" alt="" onclick="expandCollapse(this)">';
		} else {
			$output .= '<img src="media/empty.png" class="expandCollapse" alt="">';
		}
		
		$output.= '<span onclick="filterByKeyword(\''.$keyword['id'].'\')">'.$keyword['name'].'</span>';
		
		if($class == 'even') {
			$class = 'odd';
		} else {
			$class = 'even';
		}
		
		if(strlen($output_children) > 0) {
			$output .= '<span onclick="filterByKeyword(\''.$keyword['id'].','.implode(',', getKeywordOffspring($keyword['id'])).'\')" title="select this keyword and all of its children" class="addKeyword">(+ children)</span>';
		}
		//$output .= '<span onclick="filterByKeyword(\''.$keyword['id'].'\')" title="" class="addKeyword">+</span>';
		
		$output .= '<div style="display:none;">';
		
		if(strlen($output_children) > 0) {
			$output .= '<ul>'.$output_children.'</ul>';
		}
		
		$output .='</div>';

		$output .= '</li>';
	}

	return $output;
}


echo getKeywordList();

?></ul>
</body>
</html>
Return current item: PHPture