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