<?php
/**
 * Lookup ground-based observers.
 *
 * Ground based observers are stored either by their unique user ID,
 * or by their name, if they are not members of KASC.
 * This function will render a list, either as a comma-seperated
 * string or an array, of observers that are either IDs or names.
 * Will return an array where the users have been looked up.
 *
 * @param PDO $db Connection to the database.
 * @param string|array $arrObservers List of ground-based observers.
 * @return array Array with (name,id) of observers.
 * @author Rasmus Handberg <rasmush@phys.au.dk>
 */
function parse_observers(PDO $db, $arrObservers) {
	$write_observers = array();
	if (!is_null($arrObservers) && !empty($arrObservers)) {
		if (!is_array($arrObservers)) $arrObservers = explode(',', $arrObservers);
		$stmt = $db->prepare('SELECT name FROM users WHERE userid=:id;');
		$stmt->bindParam(':id', $observer, PDO::PARAM_INT);
		foreach ($arrObservers as $observer) {
			$observer = trim($observer);
			if (ctype_digit($observer)) {
				$stmt->execute();
				$row = $stmt->fetch();
				if ($row === false) continue;
				$write_observers[] = array('name' => $row['name'], 'id' => $observer);
			} else {
				$write_observers[] = array('name' => $observer, 'id' => $observer);
			}
		}
		$stmt->closeCursor();
	}
	return $write_observers;
}

function gb_instrument_type_image($inst_type) {
	switch ($inst_type) {
		case 'spectra':
			return "<img src=\"/Images/rainbow_small.png\" width=\"16\" height=\"16\" alt=\"Spectroscopy\" title=\"Spectroscopy\" style=\"width:16px;vertical-align:middle;\" />";
		case 'photo':
			return "<img src=\"/Images/lightbulb.png\" width=\"16\" height=\"16\" alt=\"Photometry\" title=\"Photometry\" style=\"width:16px;vertical-align:middle;\" />";
		case 'specpol':
			return "<img src=\"/Images/specpol.png\" width=\"16\" height=\"16\" alt=\"Spectropolarimetry\" title=\"Spectropolarimetry\" style=\"width:16px;vertical-align:middle;\" />";
		case 'interfero':
			return "<img src=\"/Images/interferometry.png\" width=\"16\" height=\"16\" alt=\"Interferometry\" title=\"Interferometry\" style=\"width:16px;vertical-align:middle;\" />";
		case 'imaging':
			return "<img src=\"/Images/image.png\" width=\"16\" height=\"16\" alt=\"Imaging\" title=\"Imaging\" style=\"width:16px;vertical-align:middle;\" />";
	}
	return "";
}

function gb_instrument_options(PDO $db, $selected=null) {

	// List of instrument types in the database:
	$inst_types = array(
		'spectra' => 'Spectroscopy',
		'photo' => 'Photometry',
		'imaging' => 'Imaging',
		'specpol' => 'Spectropolarimetry',
		'interfero' => 'Interferometry'
	);

	// Prepare statement querying the database for instruments with one type:
	$stmt = $db->prepare("SELECT instid,name FROM gb_instruments WHERE type=:type AND allow_uploads=1 ORDER BY name;");
	$stmt->bindParam(':type', $type, PDO::PARAM_STR);

	// Loop through the different types, adding each type as group
	// and each instrument as option to that group:
	$options = '';
	foreach ($inst_types as $type => $label) {
		$options .= "\t<optgroup label=\"$label\">\n";
		$stmt->execute();
		while ($row = $stmt->fetch()) {
			$options .= "\t\t<option value=\"{$row['instid']}\"";
			if (!is_null($selected) && $row['instid'] == $selected) $options .= " selected=\"selected\"";
			$options .= ">" .  htmlentities($row['name']) . "</option>\n";
		}
		$options .= "\t</optgroup>\n";
	}

	// Close the prepared statement again:
	$stmt->closeCursor();

	// Return results:
	return $options;
}