]> dev.renevier.net Git - syj.git/blob - application/controllers/PathController.php
separate route to create a new path, and to update an existing one
[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         return $this->save(new Syj_Model_Path());
9     }
10
11     public function updateAction() {
12         $idx = $this->getRequest()->getUserParam('idx');
13         $path = new Syj_Model_Path();
14         $pathMapper = new Syj_Model_PathMapper();
15         if (!$pathMapper->find($idx, $path)) {
16             if ($pathMapper->hasexisted($idx)) {
17                 throw new Syj_Exception_NotFound('Gone', 410);
18             } else {
19                 throw new Syj_Exception_NotFound('Not Found', 404);
20             }
21         }
22         return $this->save($path);
23     }
24
25     public function save(Syj_Model_Path $path) {
26         $formData = $this->_helper->SyjPostData->getPostData('Syj_Form_Geom');
27
28         /* authorization check */
29         $user = $this->_helper->SyjSession->user();
30         if (!$user and !$formData["geom_accept"]) {
31             throw new Syj_Exception_Request();
32         }
33
34         /* setting creator property */
35         if ($path->getId()) {
36             if (!$path->isCreator($user)) {
37                 throw new Syj_Exception_Request();
38             }
39         } else {
40             $path->creator = $user;
41         }
42         $path->creatorIp = $this->getRequest()->getClientIp(true);
43
44         /* setting geom property */
45         $decoder = new gisconverter\WKT();
46         try {
47             $geom = $decoder->geomFromText($formData["geom_data"]);
48         } catch (gisconverter\CustomException $e) {
49             throw new Syj_Exception_Request();
50         }
51         if ($geom::name != "LineString") {
52             throw new Syj_Exception_Request();
53         }
54         $path->geom = $geom;
55
56         /* setting title property */
57         if (isset($formData["geom_title"])) {
58             $path->title = $formData["geom_title"];
59         }
60
61
62         /* now, saving !*/
63         $pathMapper = new Syj_Model_PathMapper();
64         try {
65             $pathMapper->save ($path);
66         } catch(Zend_Db_Statement_Exception $e) {
67             if ($e->getCode() == 23505) { // 23505: Unique violation throw new Syj_Exception_Request();
68                 $message = $e->getMessage();
69                 if (strpos($message, 'paths_geom_key') !== false) {
70                     throw new Syj_Exception_Request("uniquepath");
71                 } else {
72                     throw $e;
73                 }
74             } else {
75                 throw $e;
76             }
77         }
78
79         $this->_helper->SyjApi->setBody($path->id);
80     }
81
82 }