]> dev.renevier.net Git - syj.git/blob - application/controllers/PathController.php
394da6921cac13042844cc78e0a5e4d99a839041
[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         if ($geom::name != "LineString") {
81             throw new Syj_Exception_InvalidGeomUpload();
82         }
83         $path->geom = $geom;
84
85         /* setting title property */
86         if (isset($formData["geom_title"])) {
87             $path->title = $formData["geom_title"];
88         }
89
90
91         /* now, saving !*/
92         $pathMapper = new Syj_Model_PathMapper();
93         $pathMapper->save ($path);
94     }
95
96 }