2 /* This file is part of Syj, Copyright (c) 2010-2011 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->SyjUserManager->current();
12 if (!$user and !$formData["geom_accept"]) {
13 throw new Syj_Exception_Request();
15 $path->creator = $user;
16 $path->creatorIp = $this->getRequest()->getClientIp(true);
18 $this->save($path, $formData);
20 $redirecturl = "idx/" . (string)$path->id;
21 if ($this->getRequest()->isXmlHttpRequest()) {
22 $data = array('redirect' => $redirecturl);
23 $this->_helper->SyjApi->setCode(201)->setBodyJson($data);
25 $this->_helper->SyjApi->setRedirect($redirecturl, 303);
29 public function updateAction() {
30 $formData = $this->_helper->SyjPostData->getPostData('Syj_Form_Geom');
31 $path = $this->getPath();
32 $this->save($path, $formData);
33 $this->_helper->SyjApi->setCode(200); // we should use 204, but ie mangles 204 to 1223
36 public function deleteAction() {
37 $path = $this->getPath();
38 $pathMapper = new Syj_Model_PathMapper();
39 $pathMapper->delete ($path);
40 $this->_helper->SyjApi->setCode(200); // we should use 204, but ie mangles 204 to 1223
43 public function getPath() {
44 $idx = $this->getRequest()->getUserParam('idx');
45 $path = new Syj_Model_Path();
46 $pathMapper = new Syj_Model_PathMapper();
47 if (!$pathMapper->find($idx, $path)) {
48 if ($pathMapper->hasexisted($idx)) {
49 throw new Syj_Exception_NotFound('Gone', 410);
51 throw new Syj_Exception_NotFound('Not Found', 404);
55 $user = $this->_helper->SyjUserManager->current();
56 if (!$path->isCreator($user)) {
57 throw new Syj_Exception_Forbidden();
62 public function save(Syj_Model_Path $path, $formData) {
63 /* setting geom property */
65 foreach (array("WKT", "KML", "GPX", "geoJSON") as $dectype) {
66 $classname = 'gisconverter\\' . $dectype;
67 $decoder = new $classname();
69 $geom = $decoder->geomFromText($formData["geom_data"]);
70 } catch (Exception $e) {
77 throw new Syj_Exception_InvalidGeomUpload();
80 if ($geom::name != "LineString") {
81 throw new Syj_Exception_InvalidGeomUpload();
85 /* setting title property */
86 if (isset($formData["geom_title"])) {
87 $path->title = $formData["geom_title"];
92 $pathMapper = new Syj_Model_PathMapper();
93 $pathMapper->save ($path);