]> dev.renevier.net Git - syj.git/blob - application/controllers/PathController.php
af9d104fb2efb3bb5e78d9d3dfc0ef8492d11e12
[syj.git] / application / controllers / PathController.php
1 <?php
2 /*  This file is part of Syj, Copyright (c) 2010-2011 Arnaud Renevier,
3     and is published under the AGPL license. */
4
5 class PathController extends Zend_Controller_Action
6 {
7     public function indexAction() {
8         $formData = $this->_helper->SyjPostData->getPostData('Syj_Form_Geom');
9         $path = new Syj_Model_Path();
10
11         $user = $this->_helper->SyjUserManager->current();
12         if (!$user and !$formData["geom_accept"]) {
13             throw new Syj_Exception_Request();
14         }
15         $path->creator = $user;
16         $path->creatorIp = $this->getRequest()->getClientIp(true);
17
18         $this->save($path, $formData);
19
20         $redirecturl = "idx/" . (string)$path->id;
21         if ($this->getRequest()->isXmlHttpRequest()) {
22             $data = array('redirect' => $redirecturl);
23             $this->_helper->SyjApi->setCode(201)->setBodyJson($data);
24         } else {
25             $this->_helper->SyjApi->setRedirect($redirecturl, 303);
26         }
27     }
28
29     public function updateAction() {
30         $formData = $this->_helper->SyjPostData->getPostData('Syj_Form_Geom');
31         $path = $this->getPath();
32         $this->save($path, $formData);
33         $this->_helper->SyjApi->setCode(200); // we should use 204, but ie mangles 204 to 1223
34     }
35
36     public function deleteAction() {
37         $path = $this->getPath();
38         $pathMapper = new Syj_Model_PathMapper();
39         $pathMapper->delete ($path);
40         $this->_helper->SyjApi->setCode(200); // we should use 204, but ie mangles 204 to 1223
41     }
42
43     public function getPath() {
44         $idx = $this->getRequest()->getUserParam('idx');
45         $path = new Syj_Model_Path();
46         $pathMapper = new Syj_Model_PathMapper();
47         if (!$pathMapper->find($idx, $path)) {
48             if ($pathMapper->hasexisted($idx)) {
49                 throw new Syj_Exception_NotFound('Gone', 410);
50             } else {
51                 throw new Syj_Exception_NotFound('Not Found', 404);
52             }
53         }
54
55         $user = $this->_helper->SyjUserManager->current();
56         if (!$path->isCreator($user)) {
57             throw new Syj_Exception_Forbidden();
58         }
59         return $path;
60     }
61
62     public function save(Syj_Model_Path $path, $formData) {
63         /* setting geom property */
64         $geom = null;
65         foreach (array("WKT", "KML", "GPX", "geoJSON") as $dectype) {
66             $classname = 'gisconverter\\' . $dectype;
67             $decoder = new $classname();
68             try {
69                 $geom = $decoder->geomFromText($formData["geom_data"]);
70             } catch (Exception $e) {
71             }
72             if ($geom) {
73                 break;
74             }
75         }
76         if (!$geom) {
77             throw new Syj_Exception_InvalidGeomUpload();
78         }
79
80         // merge linestrings for gpx containting multiple trkseg elements.
81         if ($classname == 'gisconverter\\GPX' && $geom::name == 'GeometryCollection') {
82           $geomstring = "";
83           foreach (array_filter($geom->components, function ($geom) {
84             return $geom::name == "LineString";
85           }) as $linestring) {
86             $geomstring .= str_replace("<trkseg>", "",
87                               str_replace("</trkseg>", "", $linestring->toGPX()));
88           }
89           $geom = $decoder->geomFromText("<trkseg>" . $geomstring . "</trkseg>");
90         }
91
92         if ($geom::name != "LineString") {
93             throw new Syj_Exception_InvalidGeomUpload();
94         }
95         $path->geom = $geom;
96
97         /* setting title property */
98         if (isset($formData["geom_title"])) {
99             $path->title = $formData["geom_title"];
100         }
101
102
103         /* now, saving !*/
104         $pathMapper = new Syj_Model_PathMapper();
105         $pathMapper->save ($path);
106     }
107
108 }