]> dev.renevier.net Git - syj.git/blobdiff - application/controllers/GeomController.php
exported path: use title instead of idx for filename
[syj.git] / application / controllers / GeomController.php
index 5e0a0340ea887ef1ba6af94b22816446d44aa7ca..b01837c8c50c218e20cb452c59ffc6ff3bebe4dc 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-/*  This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
+/*  This file is part of Syj, Copyright (c) 2010-2011 Arnaud Renevier,
     and is published under the AGPL license. */
 
 class GeomController extends Zend_Controller_Action
@@ -10,26 +10,91 @@ class GeomController extends Zend_Controller_Action
         $response = $this->getResponse();
 
         $idx = $request->idx;
+
         $pathMapper = new Syj_Model_PathMapper();
         $path = new Syj_Model_Path();
 
         $api = $this->_helper->SyjApi;
 
-        if (!$pathMapper->find($idx, $path)) {
-            if ($pathMapper->hasexisted($idx)) {
-                $api->setCode(410);
+        $ext = "";
+        $parts = explode('.', $idx);
+        if (count($parts) >= 2) {
+            $ext = end($parts);
+            if (in_array($ext, array('kml', 'gpx', 'json'))) {
+                $idx = implode('.', explode('.', $idx, -1));
             } else {
-                $api->setCode(404);
+                $ext = "";
+            }
+        }
+
+        if (!$pathMapper->find($idx, $path)) {
+            if (!$pathMapper->findByTitle($idx, $path)) {
+                if ($pathMapper->hasexisted($idx)) {
+                    $api->setCode(410);
+                } else {
+                    $api->setCode(404);
+                }
+                return;
             }
-            return;
         }
 
-        $data = array('geom' => (string)$path->geom,
-                  'title' => (string)$path->displayTitle);
+        switch ($ext) {
+            case 'kml':
+                $this->kml($path);
+            break;
+            case 'gpx':
+                $this->gpx($path);
+            break;
+            case 'json':
+            default:
+                $this->json($path);
+                return;
+            break;
+        }
+    }
+
+    protected function kml(Syj_Model_Path $path) {
+        $data = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;   // <? <-- vim syntax goes crazy
+        $data .= '<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">';
+        $data .= '<Placemark>';
+        if ($path->creator) {
+            $data .= '<atom:author><atom:name>'
+                        . htmlspecialchars($path->creator->pseudo, ENT_COMPAT, "UTF-8")
+                        . '</atom:name></atom:author>';
+        }
+        $data .= '<name>' . htmlspecialchars($path->displayTitle, ENT_COMPAT, "UTF-8") . '</name>';
+        $data .= $path->geom->toKML();
+        $data .= '</Placemark>';
+        $data .= '</kml>';
+
+        $api = $this->_helper->SyjApi;
+        $api->setCheckIfNoneMatch(true)->setContentType('application/vnd.google-earth.kml+xml')->setBody($data);
+    }
+
+    protected function gpx(Syj_Model_Path $path) {
+        $data = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;   // <? <-- vim syntax goes crazy
+        $data .= '<gpx creator="syj" version="1.0" xmlns="http://www.topografix.com/GPX/1/0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">';
+        $data .= '<trk>';
         if ($path->creator) {
-            $data['creator'] = (string)$path->creator->pseudo;
+            $data .= '<author>' . htmlspecialchars($path->creator->pseudo, ENT_COMPAT, "UTF-8") . '</author>';
         }
+        $data .= '<name>' . htmlspecialchars($path->displayTitle, ENT_COMPAT, "UTF-8") . '</name>';
+        $data .= $path->geom->toGPX();
+        $data .= '</trk>';
+        $data .= '</gpx>';
 
+        $api = $this->_helper->SyjApi;
+        $api->setCheckIfNoneMatch(true)->setContentType('application/gpx+xml')->setBody($data);
+    }
+
+    protected function json(Syj_Model_Path $path) {
+        $data = json_decode($path->geom->toGeoJSON());
+        $data->title = (string)$path->displayTitle;
+        if ($path->creator) {
+            $data->creator = (string)$path->creator->pseudo;
+        }
+        $api = $this->_helper->SyjApi;
         $api->setCheckIfNoneMatch(true)->setBodyJson($data);
     }
+
 }