<?php
	header('Content-type: text/xml');
	header('Content-Disposition: attachment; filename="text.gpx"');
?><?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<gpx version="1.1" xmlns="http://www.topografix.com/GPX/1/1" xmlns:geotracker="http://ilyabogdanovich.com/gpx/extensions/geotracker" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" creator="Find your road! https://wikipoints.ru">
	<metadata>
		<name><?= $trip->name ?></name>
		<desc><?= $trip->description ?></desc>
		<link href="https://wikipoints.ru" />
		<time><?= $trip->createdAt ?></time>
		<author>
			<name><?= App::$user->nick ?></name>
			<link href="https://wikipoints.ru/user/<?= App::$user->id ?>" />
		</author>
	</metadata>
<?php if ($waypoints): ?>
<?php foreach ($waypoints as $wpt): ?>
	<wpt lat="<?= $wpt->lat ?>" lon="<?= $wpt->lng ?>">
		<ele><?= $wpt->alt ?></ele>
		<time><?= $wpt->createdAt ?></time>
		<name><?= $wpt->name ?></name>
		<desc><?= $wpt->description ?></desc>
	</wpt>
<?php endforeach; ?>
<?php endif; ?>
<?php if ($tracks): ?>
	<trk>
		<name><?= $trip->name ?></name>
		<extensions>
			<geotracker:meta>
				<length><?= $trip->distance ?></length>
				<creationtime><?= $trip->createdAt ?></creationtime>
			</geotracker:meta>
		</extensions>
<?php foreach ($tracks as $track): ?>
<?php $trackPoints = TrackPoints::model()->getAll(array('where' => '`trackId` = ?', 'order'=>'id'), array($track->id)); ?>
		<trkseg>
<?php foreach ($trackPoints as $tpt): ?>
			<trkpt lat="<?= $tpt->lat ?>" lon="<?= $tpt->lng ?>">
				<ele><?= $tpt->alt ?></ele>
				<time><?= $tpt->createdAt ?></time>
				<extensions>
					<geotracker:meta s="<?= $tpt->speed ?>" />
				</extensions>
			</trkpt>
<?php endforeach; ?>
		</trkseg>
<?php endforeach; ?>
		</trk>
<?php endif; ?>
</gpx>