From e1d9d5339028a3dcb7d9241946a6fa3a3b40d0f7 Mon Sep 17 00:00:00 2001 From: mokaddem Date: Wed, 24 Jul 2019 10:51:58 +0200 Subject: [PATCH] new: [decaying] Added models import and export feature --- app/Controller/DecayingModelController.php | 47 ++++++++++++++++++- app/Model/DecayingModel.php | 3 +- app/View/DecayingModel/import.ctp | 30 ++++++++++++ app/View/DecayingModel/index.ctp | 1 + .../genericElements/SideMenu/side_menu.ctp | 4 ++ 5 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 app/View/DecayingModel/import.ctp diff --git a/app/Controller/DecayingModelController.php b/app/Controller/DecayingModelController.php index a498cd735..de45589f3 100644 --- a/app/Controller/DecayingModelController.php +++ b/app/Controller/DecayingModelController.php @@ -27,13 +27,58 @@ class DecayingModelController extends AppController } else { $this->Flash->success($message); $this->redirect(array('controller' => 'decayingModel', 'action' => 'index')); - // return $this->RestResponse->viewData($message, $this->response->type()); } } else { throw new MethodNotAllowedException(__("This method is not allowed")); } } + public function export($model_id) + { + $model = $this->DecayingModel->checkAuthorisation($this->Auth->user(), $model_id, true); + if (!$this->_isSiteAdmin() && !$decModel) { + throw new MethodNotAllowedException(__('No Decaying Model with the provided ID exists, or you are not authorised to view it.')); + } + unset($model['DecayingModel']['id']); + unset($model['DecayingModel']['org_id']); + unset($model['DecayingModelMapping']); + return $this->RestResponse->viewData($model, $this->response->type()); + } + + public function import() + { + if ($this->request->is('post') || $this->request->is('put')) { + $data = $this->request->data['DecayingModel']; + if ($data['submittedjson']['name'] != '' && $data['json'] != '') { + throw new MethodNotAllowedException(__('Only one import field can be used')); + } + if ($data['submittedjson']['size'] > 0) { + $filename = basename($data['submittedjson']['name']); + $file_content = file_get_contents($data['submittedjson']['tmp_name']); + if ((isset($data['submittedjson']['error']) && $data['submittedjson']['error'] == 0) || + (!empty($data['submittedjson']['tmp_name']) && $data['submittedjson']['tmp_name'] != '') + ) { + if (!$file_content) { + throw new InternalErrorException(__('PHP says file was not uploaded. Are you attacking me?')); + } + } + $text = $file_content; + } else { + $text = $data['json']; + } + $json = json_decode($text, true); + if ($json === null) { + throw new MethodNotAllowedException(__('Error while decoding JSON')); + } + if ($this->DecayingModel->save($json)) { + $this->Flash->success(__('The model has been saved.')); + } else { + $this->Flash->error(__('Error while saving model.')); + } + $this->redirect(array('action' => 'index')); + } + } + public function view($id) { if (!$this->request->is('get')) { diff --git a/app/Model/DecayingModel.php b/app/Model/DecayingModel.php index 8211599fd..bf0dd1177 100644 --- a/app/Model/DecayingModel.php +++ b/app/Model/DecayingModel.php @@ -158,8 +158,9 @@ class DecayingModel extends AppModel $decayingModels = $this->find('all', array( 'conditions' => $conditions, 'recursive' => -1, - 'contain' => 'DecayingModelMapping', + 'contain' => 'DecayingModelMapping', // not needed )); + // #FIXME should be already done foreach ($decayingModels as $i => $decayingModel) { $decayingModels[$i]['DecayingModel']['attribute_types'] = Hash::extract($decayingModels[$i]['DecayingModelMapping'], '{n}.attribute_type'); unset($decayingModels[$i]['DecayingModelMapping']); diff --git a/app/View/DecayingModel/import.ctp b/app/View/DecayingModel/import.ctp new file mode 100644 index 000000000..fbc8918f4 --- /dev/null +++ b/app/View/DecayingModel/import.ctp @@ -0,0 +1,30 @@ +
+Form->create('DecayingModel', array('enctype' => 'multipart/form-data'));?> +
+ +

+
+ Form->input('json', array( + 'div' => 'input clear', + 'label' => __('JSON'), + 'placeholder' => __('Model JSON'), + 'class' => 'form-control span6', + 'type' => 'textarea', + 'rows' => 18 + )); + echo $this->Form->input('submittedjson', array( + 'div' => 'input clear', + 'label' => __('JSON file'), + 'type' => 'file' + )); + ?> +
+
+ Form->button(__('Add'), array('class' => 'btn btn-primary')); + echo $this->Form->end(); + ?> +
+element('/genericElements/SideMenu/side_menu', array('menuList' => 'decayingModel', 'menuItem' => 'import')); diff --git a/app/View/DecayingModel/index.ctp b/app/View/DecayingModel/index.ctp index 7e7b2a382..930467456 100644 --- a/app/View/DecayingModel/index.ctp +++ b/app/View/DecayingModel/index.ctp @@ -50,6 +50,7 @@ foreach ($decayingModel as $item): ?> Html->link('', array('action' => 'view', $item['DecayingModel']['id']), array('class' => 'icon-list-alt', 'title' => 'View'));?> Html->link('', array('action' => 'edit', $item['DecayingModel']['id']), array('class' => 'icon-edit', 'title' => 'Edit'));?> + Html->link('', array('action' => 'export', $item['DecayingModel']['id'] . '.json'), array('download' => true, 'class' => 'fa fa-cloud-download-alt', 'title' => __('Download model')));?> Form->postLink('', array('action' => 'delete', $item['DecayingModel']['id']), array('class' => 'icon-trash', 'title' => 'Delete'), __('Are you sure you want to delete DecayingModel #' . $item['DecayingModel']['id'] . '?'));?> diff --git a/app/View/Elements/genericElements/SideMenu/side_menu.ctp b/app/View/Elements/genericElements/SideMenu/side_menu.ctp index 4684ceaf1..d392c13c9 100644 --- a/app/View/Elements/genericElements/SideMenu/side_menu.ctp +++ b/app/View/Elements/genericElements/SideMenu/side_menu.ctp @@ -899,6 +899,10 @@ 'text' => __('Force Update Default Models') )); } + echo $this->element('/genericElements/SideMenu/side_menu_link', array( + 'url' => '/decayingModel/import', + 'text' => __('Import Decaying Model') + )); echo $this->element('/genericElements/SideMenu/side_menu_link', array( 'url' => '/decayingModel/add', 'text' => __('Add Decaying Model')