]> dev.renevier.net Git - syj.git/blob - application/controllers/PathController.php
syj session controller helper
[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         $user = $this->_helper->SyjSession->user();
11         if (!$user) {
12             throw new Syj_Exception_Forbidden();
13         }
14
15         $decoder = new gisconverter\WKT();
16
17         try {
18             $geom = $decoder->geomFromText($formData["geom_data"]);
19         } catch (gisconverter\CustomException $e) {
20             throw new Syj_Exception_Request();
21         }
22
23         if ($geom::name != "LineString") {
24             throw new Syj_Exception_Request();
25         }
26
27         $path = new Syj_Model_Path();
28         $pathMapper = new Syj_Model_PathMapper();
29         if (isset ($formData["geom_id"]) and $formData["geom_id"]) {
30             if (!$pathMapper->find($formData["geom_id"], $path)) {
31                 throw new Syj_Exception_Request("unreferenced");
32             }
33         }
34         $path->geom = $geom;
35         if ($path->getId()) {
36             if ($path->owner->id != $user->id) {
37                 throw new Syj_Exception_Forbidden();
38             }
39         } else {
40             $path->owner = $user;
41         }
42         if (isset($formData["geom_title"])) {
43             $path->title = $formData["geom_title"];
44         }
45         try {
46             $pathMapper->save ($path);
47         } catch(Zend_Db_Statement_Exception $e) {
48             if ($e->getCode() == 23505) { // 23505: Unique violation throw new Syj_Exception_Request();
49                 $message = $e->getMessage();
50                 if (strpos($message, 'paths_geom_key') !== false) {
51                     throw new Syj_Exception_Request("uniquepath");
52                 } else {
53                     throw $e;
54                 }
55             } else {
56                 throw $e;
57             }
58         }
59
60         $this->_helper->SyjApi->setBody($path->id);
61     }
62 }