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

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

require_once('conf.php');

$db = new PDO('sqlite:'.DATABASE_PATH);

$root_uid = null;
$folders = array();
$tree_uid_list = array();
$tree_parent_uid_list = array();

function getExtension($str) {
	
	$list = explode('.', $str);
	
	return array_pop($list);
}

function getFolderTypeName($id) {
	
	switch($id) {
		case 1:
			return 'folder';
		case 2:
			return 'project';
		case 3:
			return 'projectfolder';
	}
}

function getAlbumTypeName($id) {
	
	switch($id) {
		case 1:
			return 'album';
		case 2:
			return 'book';
		case 3:
			return 'webgallery';
		case 4:
			return 'webjournal';
		case 5:
			return 'lighttable';
		default:
			return $id;
	}
}

function getName($name) {

	if($pos = strrpos($name, '.')) {
		return substr($name, 0, $pos);
	}
	
	return $name;
}

/**
	This function has been updated to use the UUID's instead of the ID's in order to work for Aperture 2.
*/	
function getTreeList() {

	global $db, $folders, $tree_uid_list, $tree_parent_uid_list, $root_uid;
	
	try {
	
		foreach ($db->query('SELECT Z_PK, ZUUID, ZVERSIONCOUNT, ZNAME, ZPARENTFOLDERUUID, ZFOLDERTYPE FROM ZRKFOLDER ORDER BY ZNAME') as $row) {
		
			// if id 0 continue;
		
			$folder = array(
				'id' => intval($row['Z_PK']),
				'uid' => $row['ZUUID'],
				'name' => $row['ZNAME'],
				'parent_uid' => $row['ZPARENTFOLDERUUID'],
				'photocount' => $row['ZVERSIONCOUNT'],
				'type' => $row['ZFOLDERTYPE']
			);
			
			//can't handle .Mac / Mobile me folders, so just skip it for now
			if($folder['type'] == 4) {
				continue;
			}
			
			if($folder['parent_uid'] == '') {
				$root_uid = $folder['uid'];
			}
		
			$tree_uid_list[] = $folder['uid'];
			$tree_parent_uid_list[] = $folder['parent_uid'];
			
			$folders[$folder['uid']] = $folder;
		}
	
	} catch(PDOException $exception) {
		
		die($exception->getMessage());
	}
	
	return getTreeChildren($root_uid);
}

function getFolderAlbums($uid) {
	
	global $db;
	
	$albums = array();

	foreach($db->query("SELECT ZUUID, ZVERSIONCOUNT, ZNAME, ZALBUMSUBCLASS, ZALBUMTYPE, ZUUID FROM ZRKPERSISTENTALBUM WHERE ZFOLDERUUID='".$uid."' ORDER BY ZNAME") as $row) {
	
		$album = array(
		'id' => $row['ZUUID'],
		'name' => $row['ZNAME'],
		'subclass' => $row['ZALBUMSUBCLASS'],
		'photocount' => $row['ZVERSIONCOUNT'],
		'type' => $row['ZALBUMTYPE']
		);
	
		$albums[] = $album;
	}
	
	return $albums;
}

function getAlbumOutput($albums) {

	$output = '';
	
	if(count($albums) > 0) {
			
		foreach($albums as $album) {
		
			if(getExtension($album['name']) == 'implicitAlbum') {
				continue;
			}
		
			$class = '';
			
			if($album['subclass'] == 2) {
				$class = 'smart';
			}
			
			$class .= getAlbumTypeName($album['type']); 
			
			$output .='<li class="'.$class.'" onclick="setSelected(event,this,\'album='.$album['id'].'&project=&folder=\')">';

			if(isset($album['photocount']) && $album['subclass'] != 2) {
				$output .= '<small>('.$album['photocount'].')</small>';
			}
			
			$output .= getName($album['name']).'</li>'."\n";
		}
		
		if($output != '') {
			$output = '<ul>'.$output.'</ul>'."\n";			
		}
	}
		
	return $output;
}

function getTreeChildren($parent_uid) {

	global $folders, $root_uid;

//print_r($folders);

	$output = '';

	$child_list = getChildList($parent_uid);

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

		$folder = $folders[$child_list[$i]];
		
		if($folder['name'] == '   Built-in Smart Albums') {
			continue;
		}
		
		$albums = getFolderAlbums($folder['uid']);
		//$albums = array();
		
		$output_children = getTreeChildren($folder['uid']);
		
		$href = '';
		
		if(isset($folder['uid'])) {
			
			if($folder['type'] == 1) { 
				$href = 'folder='.$folder['uid'].'&project=&album=';
			} elseif($folder['type'] == 2) {
				$href = 'project='.$folder['uid'].'&folder=&album=';
			}
		}

		$output .= '<li class="'.getFolderTypeName($folder['type']).'"><div class="item" onclick="setSelected(event,this,\''.$href.'\')">';
		
		if(isset($folder['photocount'])) {
			$output .= '<small>('.$folder['photocount'].')</small>';
		}
		
		if(count($albums) > 1 || strlen($output_children) > 0) {
			$output .= '<img src="media/collapsed.png" class="expandCollapse" alt="" onclick="expandCollapse(this)">';
		} else {
			$output .= '<img src="media/empty.png" class="expandCollapse" alt="">';
		}
		
		if($folder['type'] == 1) {
			$output .= '<span>'.$folder['name'].'</span>';
		} else {
			$output .= '<span>'.getName($folder['name']).'</span>';
		}
		
		$output .= '</div>';

		$output .= '<div style="display:none;">';
		
		
		if(strlen($output_children) > 0) {
		
			$output .= '<ul>'.$output_children.'</ul>'."\n";
		}
		
		$output .= getAlbumOutput($albums);
		
		$output .= '</div>';
		
		$output .= '</li>'."\n";
	}
	
	//get albums that are not inside a project
	if($parent_uid == $root_uid) {
	
		$albums = getFolderAlbums($parent_uid);
		
		$output .= getAlbumOutput($albums);
	}

	return $output;
}

/**
	Return a list of children for a specific UID.
*/
function getChildList($uid) {

	global $tree_parent_uid_list, $tree_uid_list;

	$child_list = array();

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

		if($tree_parent_uid_list[$i] == $uid) {
			$child_list[] = $tree_uid_list[$i];
		}
	}
	
	return $child_list;
}

function getPropertyId($name) {

	global $db;
	
	foreach($db->query("'SELECT ZUUID FROM ZRKPROPERTYIDENTIFIER WHERE ZPROPERTYKEY='".sqlite_escape_string($name)."'") as $row) {
		return $row['ZUUID'];
	}
}

function getLocationList($id, $versions = null) {

	global $db;

	$locations = array();
	
	$query = '';
	
	if(!is_null($versions) && count($versions) > 0) {
		$query = " AND ZVERSIONID IN (".implode(", ", $versions).")";
	}

	foreach($db->query("SELECT DISTINCT ZPROPERTYSPECIFICSTRING FROM ZRKSEARCHABLEPROPERTY WHERE ZPROPERTYIDENTIFIERUUID='".$id."'".$query) as $row) {
				
		$locations[] = $row['ZPROPERTYSPECIFICSTRING'];
	}
	
	return $locations;
}

/**
	Get a list of all the photos (versions) that have a specific property ($id) and a value ($value) set. The ooptional $superset parameters contains a list version IDs that the returned version IDs must all be contained in.
*/
function getVersionList($id, $value, $superset = null) {
	
	global $db;
	
	$versions = array();

	$query = "SELECT ZVERSIONID FROM ZRKSEARCHABLEPROPERTY WHERE (ZPROPERTYIDENTIFIERUUID='".$id."' AND ZPROPERTYSPECIFICSTRING='".sqlite_escape_string($value)."')";

	foreach($db->query($query) as $row) {
		$versions[] = $row['ZVERSIONID'];		
	}

	//check if the versions are also in the superset to prevent locations with the same name in a different area appearing in the list	
	if(!is_null($superset)) {
		$versions = array_intersect($versions, $superset);
	}
	
	return $versions;
}

/**
	Get the HTML-code consisting of nested <li>-elements that form a tree with all the locations of photos in the database. Locations are ordered from country to province, city and sublocation.
*/
function getLocationUl($name = null, $level = -1, $location_versions = null) {

	global $location_types, $levels, $country_cities, $parent_photo_count;
	
	if(!is_null($name)) {
		$location_versions = getVersionList($levels[$level], $name, $location_versions);
		
		$parent_photo_count[] = count($location_versions);
	}
			
	$location_list = getLocationList($levels[$level + 1], $location_versions);
	
	$output = '';
	$output_children = '';

	foreach($location_list as $location) {
	
			if($level == 1) {
				$country_cities[] = $location;
			}
			
			if($level < count($levels) - 2) {
				$output_children = getLocationUl($location, ($level+1), $location_versions);
			}
			
			$output .= '<li class="location '.$location_types[$level + 1].'"><div class="item" onclick="setSelected(event,this,\''.$location.'\')">';
			
			if(count($parent_photo_count) > 0) {
				$output .= '<small>('.$parent_photo_count[count($parent_photo_count) - 1].')</small>';
			}
			
			if(strlen($output_children) > 0) {
				$output .= '<img src="media/collapsed.png" class="expandCollapse" alt="" onclick="expandCollapse(this)">';
			} else {
				$output .= '<img src="media/empty.png" class="expandCollapse" alt="">';
			}
			
			$output .= '<span>'.$location.'</span>';
			
			$output .= '</div>';
	
			$output .= '<div style="display:none;">';			
				
			if(strlen($output_children) > 0) {
			
				$output .= '<ul>'.$output_children.'</ul>'."\n";
			}
			
			$output .= '</div>';
					
			$output .= '</li>'."\n";
	}
	
	if($level == 0) {
	
		$output_children = '';

		$location_list = getLocationList($levels[$level + 2], $location_versions);
		
		//echo('<li>'.implode(', ', $locations).' done: '.implode(', ', $country_cities).' diff: '.implode(', ', array_diff($locations, $country_cities)).'</li>');
		
		$location_list = array_diff($location_list, $country_cities);
		
		foreach($location_list as $location) {
		
			if($level < count($levels) - 2) {
				$output_children = getLocationUl($location, ($level+2), $location_versions);
			}
			
			if(strlen($output_children) > 0) {
				$class = '';
			} else {
				$class = 'final ';
			}
		
			$output .= '<li class="location"><div class="item" onclick="setSelected(event,this,\''.$location.'\')">';
			
			if(!is_null($location_versions)) {
				$output .= '<small>('.count($location_versions).')</small>';
			}
			
			if($class != 'final' && strlen($output_children) > 0) {
				$output .= '<img src="media/collapsed.png" class="expandCollapse" alt="" onclick="expandCollapse(this)">';
			} else {
				$output .= '<img src="media/empty.png" class="expandCollapse" alt="">';
			}

			$output .= '<span>'.$location.'</span>';			
			
			$output .= '</div>';
	
			$output .= '<div style="display:none;">';			
				
			if(strlen($output_children) > 0) {
			
				$output .= '<ul>'.$output_children.'</ul>'."\n";
			}
			
			$output .= '</div>';
			
			$output .= '</li>'."\n";
		}
		
		$country_cities = array();
	}
		
	
	return $output;
}

/**
	Get the UUID's that are used in this database for the location properties and return them in an array, ordered in the same order as the location types, which should be from large (countries) to small (sublocation).
*/
function getLocationLevels($locations) {
	
	global $db;
	
	$levels = array();
	
	$query = "SELECT ZUUID, ZPROPERTYKEY FROM ZRKPROPERTYIDENTIFIER WHERE ZPROPERTYKEY IN ('".implode("','", $locations)."')";
	foreach($db->query($query) as $row) {

		$key = array_search($row['ZPROPERTYKEY'], $locations);
		
		if($key !== false) {
			
			$levels[$key] = $row['ZUUID'];
		}
	}
	
	return $levels;
}

/*
$location_types = array('Country/PrimaryLocationName', 'Province/State', 'City', 'SubLocation');
$levels = getLocationLevels($location_types);
$country_cities = array();
*/
$parent_photo_count = array();

?><!DOCTYPE html>
<html>
<head>
<title>projects</title>
<link rel="stylesheet" href="style/projects.css" type="text/css">
<script type="text/javascript" src="script/projects.js"></script>
</head>
<body id="sidebar">

<ul>
<?php echo(getTreeList()); ?>
<?php /*
<li class="location"><div class="item" onclick="setSelected(event,this,'')"><img src="media/collapsed.png" class="expandCollapse" alt="" onclick="expandCollapse(this)"><span>Locations</span></div><div style="display:none;"><ul><?php echo(getLocationUl()); ?></ul></div></li>*/ ?>
</ul>

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