]> dev.renevier.net Git - syj.git/blob - application/controllers/PathController.php
interface to manage list of created routes
[syj.git] / application / controllers / PathController.php
1 <?php
2 /*  This file is part of Syj, Copyright (c) 2010 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->SyjSession->user();
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         return $this->save($path, $formData);
19     }
20
21     public function updateAction() {
22         $formData = $this->_helper->SyjPostData->getPostData('Syj_Form_Geom');
23         return $this->save($this->getPath(), $formData);
24     }
25
26     public function deleteAction() {
27         $path = $this->getPath();
28         $pathMapper = new Syj_Model_PathMapper();
29         $pathMapper->delete ($path);
30         $this->_helper->SyjApi->setCode(204);
31     }
32
33     public function getPath() {
34         $idx = $this->getRequest()->getUserParam('idx');
35         $path = new Syj_Model_Path();
36         $pathMapper = new Syj_Model_PathMapper();
37         if (!$pathMapper->find($idx, $path)) {
38             if ($pathMapper->hasexisted($idx)) {
39                 throw new Syj_Exception_NotFound('Gone', 410);
40             } else {
41                 throw new Syj_Exception_NotFound('Not Found', 404);
42             }
43         }
44
45         $user = $this->_helper->SyjSession->user();
46         if (!$path->isCreator($user)) {
47             throw new Syj_Exception_Forbidden();
48         }
49         return $path;
50     }
51
52     public function save(Syj_Model_Path $path, $formData) {
53         /* setting geom property */
54         $decoder = new gisconverter\WKT();
55         try {
56             $geom = $decoder->geomFromText($formData["geom_data"]);
57         } catch (gisconverter\CustomException $e) {
58             throw new Syj_Exception_Request();
59         }
60         if ($geom::name != "LineString") {
61             throw new Syj_Exception_Request();
62         }
63         $path->geom = $geom;
64
65         /* setting title property */
66         if (isset($formData["geom_title"])) {
67             $path->title = $formData["geom_title"];
68         }
69
70
71         /* now, saving !*/
72         $pathMapper = new Syj_Model_PathMapper();
73         try {
74             $pathMapper->save ($path);
75         } catch(Zend_Db_Statement_Exception $e) {
76             if ($e->getCode() == 23505) { // 23505: Unique violation throw new Syj_Exception_Request();
77                 $message = $e->getMessage();
78                 if (strpos($message, 'paths_geom_key') !== false) {
79                     throw new Syj_Exception_Request("uniquepath");
80                 } else {
81                     throw $e;
82                 }
83             } else {
84                 throw $e;
85             }
86         }
87
88         $this->_helper->SyjApi->setBody($path->id);
89     }
90
91 }