diff --git a/controllers/JsonController.php b/controllers/JsonController.php
index 6535df9..0e27300 100644
--- a/controllers/JsonController.php
+++ b/controllers/JsonController.php
@@ -577,4 +577,72 @@ class JsonController extends Controller
));
}
+ static function actionTrack()
+ {
+ $id = intval(App::getParam('id'));
+
+ if (!$id) {
+ App::error404();
+ }
+
+ $trip = Trips::model()->getByPK($id);
+
+ if ($trip->isEmpty() || ($trip->status != Trips::STATUS_PUBLIC && $trip->owner != App::$user->id)) {
+ App::error404();
+ }
+
+ $res = array(
+ 'name' => $trip->name,
+ 'time' => $trip->createdAt,
+ );
+
+ $resWayPoints = array();
+ $resTracks = array();
+ $resLatlngs = array();
+
+
+ $wayPoints = WayPoints::model()->getAll(array('where' => '`tripId` = ?'), array($trip->id));
+ if ($wayPoints) {
+ foreach ($wayPoints as $block) {
+ $resWayPoints[] = array(
+ 'lat' => floatval($block->lat),
+ 'lng' => floatval($block->lng),
+ 'time' => strtotime($block->createdAt),
+ 'name' => (string) $block->name,
+ 'desc' => (string) $block->description,
+ );
+ }
+ }
+
+
+ $tracks = Tracks::model()->getAll(array('where' => '`tripId` = ?'), array($trip->id));
+ if ($tracks) {
+ foreach ($tracks as $track) {
+ $resLatlngs = array();
+ $trackPoints = TrackPoints::model()->getAll(array('where' => '`trackId` = ?'), array($track->id));
+ if ($trackPoints) {
+ foreach ($trackPoints as $trackPoint) {
+ $resLatlngs[] = array(
+ 'lat' => $trackPoint->lat,
+ 'lng' => $trackPoint->lng,
+ );
+ }
+ }
+
+ $resTracks[] = array(
+ 'name' => (string) $block->name,
+ 'length' => 0,
+ 'latlngs' => $resLatlngs,
+ );
+ }
+
+ }
+
+ $res['wayPoints'] = $resWayPoints;
+ $res['tracks'] = $resTracks;
+
+
+ self::renderPartial('json.php', $res);
+ }
+
}
diff --git a/controllers/TracksController.php b/controllers/TracksController.php
index c37d6fc..808ebe8 100644
--- a/controllers/TracksController.php
+++ b/controllers/TracksController.php
@@ -4,6 +4,10 @@ class TracksController extends Controller
{
function __construct()
{
+ if (!App::$user->id) {
+ App::redirect('/');
+ }
+
self::$layout = 'layouts/page.php';
self::addStyle('/css/style.css?ver=' . App::getConfig('version'));
self::addStyle('/css/pageStyle.css?ver=' . App::getConfig('version'));
@@ -15,9 +19,13 @@ class TracksController extends Controller
}
- function actionMy()
+ function actionIndex()
{
+ $trips = Trips::model()->getAll(array('where' => '`owner` = ?'), array(App::$user->id));
+ self::render('/tracks/index.php', array(
+ 'trips' => $trips,
+ ));
}
function actionFiles()
@@ -31,12 +39,11 @@ class TracksController extends Controller
function actionUploadfile()
{
-//var_dump($_POST, $_FILES);
$temp = pathinfo($_FILES['file']['name']);
$filename = $_FILES['file']['name'];
$extension = strtolower($temp['extension']);
$uploaddir = App::getBasedir().'tracks/';
-// var_dump($extension);die;
+
if ($extension !== 'gpx') {
App::error404('Неизвестный тип');
}
@@ -49,11 +56,104 @@ class TracksController extends Controller
));
$newName = 'track'.$tmp->id . '.' . $extension;
- if (move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir . $newName)) {
- var_dump($tmp);
- } else {
+ if (!move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir . $newName)) {
App::error404();
}
+
+ App::redirect('/tracks/files');
+ }
+
+ function actionImportgpx()
+ {
+ $id = (int)App::getParam('id');
+
+ if (!$id) {
+ App::error404();
+ }
+
+ $trackFile = TrackFiles::model()->getByPK($id);
+
+ if ($trackFile->isEmpty() || $trackFile->owner != App::$user->id) {
+ App::error404();
+ }
+
+ print_r($trackFile);
+
+ $gpxObject = simplexml_load_file(App::getBasedir().'tracks/track'.$trackFile->id.'.gpx');
+
+ $trip = Trips::model()->add(
+ array(
+ 'name' => isset($gpxObject->metadata->name) ? (string) $gpxObject->metadata->name : 'Track',
+ 'createdAt' => isset($gpxObject->metadata->time) ? $gpxObject->metadata->time : date('c'),
+ 'owner' => App::$user->id,
+ 'distance' => 0,
+ 'description' => 'Трек сздан '.date('d-m-Y H:i').' из файла '. $trackFile->filename,
+ 'status' => Trips::STATUS_PRIVATE,
+ )
+ );
+
+ $wayPoints = array();
+ $tracks = array();
+ $latlngs = array();
+ $tracksCounter = 1;
+
+ foreach ($gpxObject as $key => $block) {
+ if ($key == 'wpt') {
+ if (empty($block)) {
+ continue;
+ }
+
+ $wayPoint = WayPoints::model()->add(
+ array(
+ 'tripId' => $trip->id,
+ 'lat' => floatval($block['lat']),
+ 'lng' => floatval($block['lon']),
+ 'alt' => 0,
+ 'speed' => 0,
+ 'createdAt' => $block->time,
+ 'name' => (string) $block->name,
+ 'description' => (string) $block->desc,
+ )
+ );
+ }
+
+ if ($key == 'trk') {
+ if (empty($block->trkseg)) {
+ continue;
+ }
+
+ foreach ($block->trkseg as $trkpt) {
+ if ($trkpt) {
+ $track = Tracks::model()->add(
+ array(
+ 'tripId' => $trip->id,
+ 'name' => (string)$block->name ? (string)$block->name : 'Фрагмент трека №'.$tracksCounter,
+ 'distance' => 0,
+ 'description' => '',
+ )
+ );
+ $tracksCounter++;
+ $trackPoints = array();
+
+ foreach ($trkpt as $tpt) {
+
+ $trackPoint = TrackPoints::model()->add(
+ array(
+ 'trackId' => $track->id,
+ 'lat' => floatval($tpt['lat']),
+ 'lng' => floatval($tpt['lon']),
+ 'alt' => isset($tpt->ele) ? floatval($tpt->ele) : 0,
+ 'createdAt' => isset($tpt->time) ? $tpt->time : date('c'),
+ 'speed' => 0,
+ )
+ );
+ }
+ }
+ }
+ }
+ }
+
+ App::redirect('/tracks');
}
}
diff --git a/models/TrackPoints.php b/models/TrackPoints.php
new file mode 100644
index 0000000..6841536
--- /dev/null
+++ b/models/TrackPoints.php
@@ -0,0 +1,17 @@
+_tableName_ = 'trackPoints';
+ parent::__construct($fromArray);
+ }
+
+ static function model()
+ {
+ return new self();
+ }
+
+}
diff --git a/models/Tracks.php b/models/Tracks.php
new file mode 100644
index 0000000..fbbbf2a
--- /dev/null
+++ b/models/Tracks.php
@@ -0,0 +1,17 @@
+_tableName_ = 'tracks';
+ parent::__construct($fromArray);
+ }
+
+ static function model()
+ {
+ return new self();
+ }
+
+}
diff --git a/models/Trips.php b/models/Trips.php
new file mode 100644
index 0000000..97e46a7
--- /dev/null
+++ b/models/Trips.php
@@ -0,0 +1,21 @@
+_tableName_ = 'trips';
+ parent::__construct($fromArray);
+ }
+
+ static function model()
+ {
+ return new self();
+ }
+
+}
diff --git a/models/WayPoints.php b/models/WayPoints.php
new file mode 100644
index 0000000..1e1ac2e
--- /dev/null
+++ b/models/WayPoints.php
@@ -0,0 +1,17 @@
+_tableName_ = 'wayPoints';
+ parent::__construct($fromArray);
+ }
+
+ static function model()
+ {
+ return new self();
+ }
+
+}
diff --git a/public/js/map.js b/public/js/map.js
index 905eadf..134506d 100644
--- a/public/js/map.js
+++ b/public/js/map.js
@@ -577,27 +577,24 @@ function onLocationError(e) {
alert('Ошибка при определении координат: ' + e.message);
}
-function drawTrek()
+function drawTrek(id)
{
colors = ['red', 'green', 'blue'];
track = new Array();
$.ajax({
- url: "/index/test/",
+ url: "/json/track/id/"+id,
success: function (data) {
if (typeof (data.tracks) != 'undefined') {
for (var i = 0; i < data.tracks.length; i++) {
color = colors[Math.floor(Math.random() * colors.length)]
-// console.log(color);
track[i] = L.polyline(data.tracks[i].latlngs, {color: color, width: 4, opacity: 0.8});
track[i].addTo(myMap);
track[i].bindPopup(data.tracks[i].name);
}
-// myMap.fitBounds(track[i].getBounds());
}
if (typeof (data.wayPoints) != 'undefined') {
for (var i = 0; i < data.wayPoints.length; i++) {
-// console.log(data.wayPoints[i]);
L.marker([data.wayPoints[i]['lat'], data.wayPoints[i]['lng']], {icon: categories.marker, title: data.wayPoints[i]['name']}).addTo(myMap);
}
}
diff --git a/views/tracks/files.php b/views/tracks/files.php
index feca95d..e7da5b3 100644
--- a/views/tracks/files.php
+++ b/views/tracks/files.php
@@ -11,7 +11,7 @@
if ($files) {
foreach ($files as $file) {
- print $file->id . ' — ' . $file->name . ' ('. $file->filename.')
';
+ print $file->id . ' — ' . $file->name . ' ('. $file->filename.')
';
}
} else {
print 'Пока ничего не загружено';
diff --git a/views/tracks/index.php b/views/tracks/index.php
new file mode 100644
index 0000000..432080b
--- /dev/null
+++ b/views/tracks/index.php
@@ -0,0 +1,19 @@
+