X-Git-Url: https://dev.renevier.net/?a=blobdiff_plain;f=application%2Fcontrollers%2FPathController.php;h=cdd067b6f4ba2b43722f7256d32d19fd2dd506e3;hb=c1aeb7538786d8c9f3b3337c0b71e21ef89d9c77;hp=bd4d33908bdfeb409b0a9eff47e654ae10908d2d;hpb=190fd621df4920c56a422c03663874cddaa67d64;p=syj.git diff --git a/application/controllers/PathController.php b/application/controllers/PathController.php index bd4d339..cdd067b 100644 --- a/application/controllers/PathController.php +++ b/application/controllers/PathController.php @@ -6,51 +6,90 @@ class PathController extends Zend_Controller_Action { public function indexAction() { $formData = $this->_helper->SyjPostData->getPostData('Syj_Form_Geom'); + $path = new Syj_Model_Path(); - $sessionStorage = Zend_Auth::getInstance()->getStorage(); - if ($sessionStorage->isEmpty()) { - throw new Syj_Exception_Forbidden(); - } - $sessionData = $sessionStorage->read(); - - $user = new Syj_Model_User(); - $userMapper = new Syj_Model_UserMapper(); - if (!$userMapper->find($sessionData['user'], $user)) { - // we could also throw a forbidden exception, but client session - // should not contain reference to a non existent user. So, it's considered a bug. - throw new Syj_Exception_Forbidden(); + $user = $this->_helper->SyjSession->user(); + if (!$user and !$formData["geom_accept"]) { + throw new Syj_Exception_Request(); } + $path->creator = $user; + $path->creatorIp = $this->getRequest()->getClientIp(true); - $decoder = new gisconverter\WKT(); + $this->save($path, $formData); - try { - $geom = $decoder->geomFromText($formData["geom_data"]); - } catch (gisconverter\CustomException $e) { - throw new Syj_Exception_Request(); + $redirecturl = "idx/" . (string)$path->id; + if ($this->getRequest()->isXmlHttpRequest()) { + $data = array('redirect' => $redirecturl); + $this->_helper->SyjApi->setCode(201)->setBodyJson($data); + } else { + $this->_helper->SyjApi->setRedirect($redirecturl, 303); } + } - if ($geom::name != "LineString") { - throw new Syj_Exception_Request(); - } + public function updateAction() { + $formData = $this->_helper->SyjPostData->getPostData('Syj_Form_Geom'); + $path = $this->getPath(); + $this->save($path, $formData); + $this->_helper->SyjApi->setCode(200); // we should use 204, but ie mangles 204 to 1223 + } + public function deleteAction() { + $path = $this->getPath(); + $pathMapper = new Syj_Model_PathMapper(); + $pathMapper->delete ($path); + $this->_helper->SyjApi->setCode(200); // we should use 204, but ie mangles 204 to 1223 + } + + public function getPath() { + $idx = $this->getRequest()->getUserParam('idx'); $path = new Syj_Model_Path(); $pathMapper = new Syj_Model_PathMapper(); - if (isset ($formData["geom_id"]) and $formData["geom_id"]) { - if (!$pathMapper->find($formData["geom_id"], $path)) { - throw new Syj_Exception_Request("unreferenced"); + if (!$pathMapper->find($idx, $path)) { + if ($pathMapper->hasexisted($idx)) { + throw new Syj_Exception_NotFound('Gone', 410); + } else { + throw new Syj_Exception_NotFound('Not Found', 404); } } - $path->geom = $geom; - if ($path->getId()) { - if ($path->owner->id != $user->id) { - throw new Syj_Exception_Forbidden(); + + $user = $this->_helper->SyjSession->user(); + if (!$path->isCreator($user)) { + throw new Syj_Exception_Forbidden(); + } + return $path; + } + + public function save(Syj_Model_Path $path, $formData) { + /* setting geom property */ + $geom = null; + foreach (array("WKT", "KML", "GPX", "geoJSON") as $dectype) { + $classname = 'gisconverter\\' . $dectype; + $decoder = new $classname(); + try { + $geom = $decoder->geomFromText($formData["geom_data"]); + } catch (Exception $e) { } - } else { - $path->owner = $user; + if ($geom) { + break; + } + } + if (!$geom) { + throw new Syj_Exception_InvalidGeomUpload(); } + + if ($geom::name != "LineString") { + throw new Syj_Exception_Request(); + } + $path->geom = $geom; + + /* setting title property */ if (isset($formData["geom_title"])) { $path->title = $formData["geom_title"]; } + + + /* now, saving !*/ + $pathMapper = new Syj_Model_PathMapper(); try { $pathMapper->save ($path); } catch(Zend_Db_Statement_Exception $e) { @@ -65,7 +104,6 @@ class PathController extends Zend_Controller_Action throw $e; } } - - $this->_helper->SyjApi->setBody($path->id); } + }