]> dev.renevier.net Git - syj.git/blobdiff - application/controllers/PathController.php
separate route to create a new path, and to update an existing one
[syj.git] / application / controllers / PathController.php
index bd4d33908bdfeb409b0a9eff47e654ae10908d2d..c3c085a8695ff1eddbed1874edea580e8b9644ed 100644 (file)
@@ -5,52 +5,62 @@
 class PathController extends Zend_Controller_Action
 {
     public function indexAction() {
+        return $this->save(new Syj_Model_Path());
+    }
+
+    public function updateAction() {
+        $idx = $this->getRequest()->getUserParam('idx');
+        $path = new Syj_Model_Path();
+        $pathMapper = new Syj_Model_PathMapper();
+        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);
+            }
+        }
+        return $this->save($path);
+    }
+
+    public function save(Syj_Model_Path $path) {
         $formData = $this->_helper->SyjPostData->getPostData('Syj_Form_Geom');
 
-        $sessionStorage = Zend_Auth::getInstance()->getStorage();
-        if ($sessionStorage->isEmpty()) {
-            throw new Syj_Exception_Forbidden();
+        /* authorization check */
+        $user = $this->_helper->SyjSession->user();
+        if (!$user and !$formData["geom_accept"]) {
+            throw new Syj_Exception_Request();
         }
-        $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();
+        /* setting creator property */
+        if ($path->getId()) {
+            if (!$path->isCreator($user)) {
+                throw new Syj_Exception_Request();
+            }
+        } else {
+            $path->creator = $user;
         }
+        $path->creatorIp = $this->getRequest()->getClientIp(true);
 
+        /* setting geom property */
         $decoder = new gisconverter\WKT();
-
         try {
             $geom = $decoder->geomFromText($formData["geom_data"]);
         } catch (gisconverter\CustomException $e) {
             throw new Syj_Exception_Request();
         }
-
         if ($geom::name != "LineString") {
             throw new Syj_Exception_Request();
         }
-
-        $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");
-            }
-        }
         $path->geom = $geom;
-        if ($path->getId()) {
-            if ($path->owner->id != $user->id) {
-                throw new Syj_Exception_Forbidden();
-            }
-        } else {
-            $path->owner = $user;
-        }
+
+        /* 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) {
@@ -68,4 +78,5 @@ class PathController extends Zend_Controller_Action
 
         $this->_helper->SyjApi->setBody($path->id);
     }
+
 }