2 /* This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
3 and is published under the AGPL license. */
5 class PathController extends Zend_Controller_Action
7 public function indexAction() {
8 $formData = $this->_helper->SyjPostData->getPostData('Syj_Form_Geom');
9 $path = new Syj_Model_Path();
11 $user = $this->_helper->SyjSession->user();
12 if (!$user and !$formData["geom_accept"]) {
13 throw new Syj_Exception_Request();
15 $path->creator = $user;
16 $path->creatorIp = trim(end(split(',', $this->getRequest()->getClientIp(true))));
18 $this->save($path, $formData);
19 $data = array('redirect' => "idx/" . (string)$path->id);
20 $this->_helper->SyjApi->setCode(201)->setBodyJson($data);
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
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
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);
45 throw new Syj_Exception_NotFound('Not Found', 404);
49 $user = $this->_helper->SyjSession->user();
50 if (!$path->isCreator($user)) {
51 throw new Syj_Exception_Forbidden();
56 public function save(Syj_Model_Path $path, $formData) {
57 /* setting geom property */
58 $decoder = new gisconverter\WKT();
60 $geom = $decoder->geomFromText($formData["geom_data"]);
61 } catch (gisconverter\CustomException $e) {
62 throw new Syj_Exception_Request();
64 if ($geom::name != "LineString") {
65 throw new Syj_Exception_Request();
69 /* setting title property */
70 if (isset($formData["geom_title"])) {
71 $path->title = $formData["geom_title"];
76 $pathMapper = new Syj_Model_PathMapper();
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");