From e4dafd3882dfd6e8dc9c2ab49c411566b0411be3 Mon Sep 17 00:00:00 2001 From: noud Date: Mon, 10 Dec 2012 11:32:40 +0100 Subject: [PATCH] RESTfull sync RESTfull attribute add, edit and view, to be usefull in sync. --- app/Config/routes.php | 2 +- app/Controller/AttributesController.php | 26 ++++++++++++++++++++----- app/View/Attributes/xml/view.ctp | 24 +++++++++++++++++++++++ tools/curl/addAttribute.sh | 2 ++ tools/curl/editAttribute.sh | 7 +++++++ tools/curl/viewAttribute.sh | 2 ++ 6 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 app/View/Attributes/xml/view.ctp create mode 100755 tools/curl/addAttribute.sh create mode 100644 tools/curl/editAttribute.sh create mode 100755 tools/curl/viewAttribute.sh diff --git a/app/Config/routes.php b/app/Config/routes.php index 82aa26118..663bbbfa2 100644 --- a/app/Config/routes.php +++ b/app/Config/routes.php @@ -31,7 +31,7 @@ Router::connect('/users/admin_index/*', array('controller' => 'users', 'action' => 'index', 'admin' => true)); // Activate REST - Router::mapResources(array('events')); + Router::mapResources(array('events', 'attributes')); Router::parseExtensions('xml'); /** diff --git a/app/Controller/AttributesController.php b/app/Controller/AttributesController.php index 06887015b..1a6b52013 100644 --- a/app/Controller/AttributesController.php +++ b/app/Controller/AttributesController.php @@ -49,7 +49,7 @@ class AttributesController extends AppController { return true; } // Only on own attributes for these actions - if (in_array($this->action, array('edit', 'delete'))) { + if (in_array($this->action, array('edit', 'delete', 'view'))) { $attributeid = $this->request->params['pass'][0]; return $this->Attribute->isOwnedByOrg($attributeid, $this->Auth->user('org')); } @@ -77,6 +77,16 @@ class AttributesController extends AppController { $this->set('categoryDefinitions', $this->Attribute->categoryDefinitions); } + public function view($id = null) { + $this->Attribute->id = $id; + if (!$this->Attribute->exists()) { + throw new NotFoundException(__('Invalid attribute')); + } + $this->Attribute->read(null, $id); + + $this->set('attribute', $this->Attribute->data); + } + /** * add method * @@ -101,7 +111,7 @@ class AttributesController extends AppController { // // multiple attributes in batch import // - if ($this->request->data['Attribute']['batch_import'] == 1) { + if ((isset($this->request->data['Attribute']['batch_import']) && $this->request->data['Attribute']['batch_import'] == 1)) { // make array from value field $attributes = explode("\n", $this->request->data['Attribute']['value']); @@ -141,9 +151,15 @@ class AttributesController extends AppController { $this->Attribute->create(); if ($this->Attribute->save($this->request->data)) { - // inform the user and redirect - $this->Session->setFlash(__('The attribute has been saved')); - $this->redirect(array('controller' => 'events', 'action' => 'view', $this->request->data['Attribute']['event_id'])); + if ($this->_isRest()) { + // REST users want to see the newly created event + $this->view($this->Attribute->getId()); + $this->render('view'); + } else { + // inform the user and redirect + $this->Session->setFlash(__('The attribute has been saved')); + $this->redirect(array('controller' => 'events', 'action' => 'view', $this->request->data['Attribute']['event_id'])); + } } else { $this->Session->setFlash(__('The attribute could not be saved. Please, try again.')); } diff --git a/app/View/Attributes/xml/view.ctp b/app/View/Attributes/xml/view.ctp new file mode 100644 index 000000000..b788b0acd --- /dev/null +++ b/app/View/Attributes/xml/view.ctp @@ -0,0 +1,24 @@ + 'tags')); +echo $xmlObject->asXML(); diff --git a/tools/curl/addAttribute.sh b/tools/curl/addAttribute.sh new file mode 100755 index 000000000..7fb7064c9 --- /dev/null +++ b/tools/curl/addAttribute.sh @@ -0,0 +1,2 @@ +curl -i -H "Accept: application/xml" -H "content-type: text/xml" -H "Authorization: vlf4o42bYSVVWLm28jLB85my4HBZWXTri8vGdySb" \ +--data "@input/33529.xml" -X POST http://localhost/attributes diff --git a/tools/curl/editAttribute.sh b/tools/curl/editAttribute.sh new file mode 100644 index 000000000..655ca85a2 --- /dev/null +++ b/tools/curl/editAttribute.sh @@ -0,0 +1,7 @@ +#curl -H "Accept: application/xml" -H "content-type: text/xml" -H "Authorization: vlf4o42bYSVVWLm28jLB85my4HBZWXTri8vGdySb" \ +#--data "@input/event.xml" -X PUT http://localhost/events/14' +#http://bel_mod1.local.net:80/events/29 + +# POST can be used as well.. +curl -H "Accept: application/xml" -H "content-type: text/xml" -H "Authorization: vlf4o42bYSVVWLm28jLB85my4HBZWXTri8vGdySb" \ +--data "@input/33529.xml" -X POST http://localhost/attributes/33529 diff --git a/tools/curl/viewAttribute.sh b/tools/curl/viewAttribute.sh new file mode 100755 index 000000000..b3f2a2ff8 --- /dev/null +++ b/tools/curl/viewAttribute.sh @@ -0,0 +1,2 @@ +curl -H "Accept: application/xml" -H "Authorization: vlf4o42bYSVVWLm28jLB85my4HBZWXTri8vGdySb" \ +-X GET http://localhost/attributes/$1