<?php
//include top of the page
include_once('top.php');

//include functions
include_once('events.inc');

//functions
function getPerson($id) {
	global $db;
	$sql = 'SELECT * FROM people WHERE person_id='.$id;
	$result = $db->query($sql);
	$person = $result->fetch_assoc();
	return $person['nickname'] ? $person['nickname'] : $person['first_name'].' '.$person['last_name'];
}
function printFlier($data) {
	$size = getimagesize($data['picture']);
	$size_att = $size[3];
	echo '<div class="flier">';
	echo '<img src="'.$data['picture'].'" '.$size_att.' alt="flier" />';
	echo $data['description'] ? '<p>'.$data['description'].'</p>' : '';
	if($data['designer'] || $data['photographer'] || $data['model']) {
		echo '<ul>';
		if($data['designer']) {
			echo '<li>Designer: <span class="person">';
			echo getPerson($data['designer']);
			echo '<span></li>';
		}
		if($data['photographer']) {
			echo '<li>Photographer: <span class="person">';
			echo getPerson($data['photographer']);
			echo '</span></li>';
		}
		if($data['model']) {
			echo '<li>Model: <span class="person">';
			echo getPerson($data['model']);
			echo '</span></li>';
		}
		echo'</ul>';
	}
	echo '</div>';
}
// main
if($_GET['event_id']) {
	$current = getCurrent();
	// display top controls
	displayControls($current, 'fliers.php', 'topctrl');
//gather all data needed
	$sql = 'SELECT * FROM fliers WHERE event_id='.$_GET['event_id'].' ORDER BY side';
	$result = $db->query($sql);
	$front = $result->fetch_assoc();
	$back = $result->fetch_assoc();
	echo '<div id="fliers">';
	if($front['picture']) {
		printFlier($front);
	}
	if($back['picture']) {
		printFlier($back);
	}
	echo '</div>';
	// display bottom controls
	displayControls($current, 'fliers.php', 'bottomctrl');
} else {
	echo '<h1>&gt;&gt; Flyers &lt;&lt;</h1>';
	listEvents('fliers.php');
}

//include bottom
include_once('bottom.php');
?>