]> dev.renevier.net Git - syj.git/blob - application/controllers/PathController.php
version 0.1
[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
10         $sessionStorage = Zend_Auth::getInstance()->getStorage();
11         if ($sessionStorage->isEmpty()) {
12             throw new Syj_Exception_Forbidden();
13         }
14         $sessionData = $sessionStorage->read();
15
16         $user = new Syj_Model_User();
17         $userMapper = new Syj_Model_UserMapper();
18         if (!$userMapper->find($sessionData['user'], $user)) {
19             // we could also throw a forbidden exception, but client session
20             // should not contain reference to a non existent user. So, it's considered a bug.
21             throw new Syj_Exception_Forbidden();
22         }
23
24         $decoder = new gisconverter\WKT();
25
26         try {
27             $geom = $decoder->geomFromText($formData["geom_data"]);
28         } catch (gisconverter\CustomException $e) {
29             throw new Syj_Exception_Request();
30         }
31
32         if ($geom::name != "LineString") {
33             throw new Syj_Exception_Request();
34         }
35
36         $path = new Syj_Model_Path();
37         $pathMapper = new Syj_Model_PathMapper();
38         if (isset ($formData["geom_id"]) and $formData["geom_id"]) {
39             if (!$pathMapper->find($formData["geom_id"], $path)) {
40                 throw new Syj_Exception_Request("unreferenced");
41             }
42         }
43         $path->geom = $geom;
44         if ($path->getId()) {
45             if ($path->owner->id != $user->id) {
46                 throw new Syj_Exception_Forbidden();
47             }
48         } else {
49             $path->owner = $user;
50         }
51         if (isset($formData["geom_title"])) {
52             $path->title = $formData["geom_title"];
53         }
54         try {
55             $pathMapper->save ($path);
56         } catch(Zend_Db_Statement_Exception $e) {
57             if ($e->getCode() == 23505) { // 23505: Unique violation throw new Syj_Exception_Request();
58                 $message = $e->getMessage();
59                 if (strpos($message, 'paths_geom_key') !== false) {
60                     throw new Syj_Exception_Request("uniquepath");
61                 } else {
62                     throw $e;
63                 }
64             } else {
65                 throw $e;
66             }
67         }
68
69         $this->_helper->SyjApi->setBody($path->id);
70     }
71 }