diff --git a/src/Controller/BroodsController.php b/src/Controller/BroodsController.php index d9d2cd8..cbcb51c 100644 --- a/src/Controller/BroodsController.php +++ b/src/Controller/BroodsController.php @@ -116,4 +116,25 @@ class BroodsController extends AppController $this->redirect($this->referer()); } } + + public function downloadIndividual($brood_id, $individual_id) + { + $result = $this->Broods->downloadIndividual($brood_id, $individual_id); + $success = __('Individual fetched from remote.'); + $fail = __('Could not save the remote individual'); + if ($this->ParamHandler->isRest()) { + if ($result) { + return $this->RestResponse->saveSuccessResponse('Brood', 'downloadIndividual', $brood_id, 'json', $success); + } else { + return $this->RestResponse->saveFailResponse('Brood', 'downloadIndividual', $brood_id, $fail, 'json'); + } + } else { + if ($result) { + $this->Flash->success($success); + } else { + $this->Flash->error($fail); + } + $this->redirect($this->referer()); + } + } } diff --git a/src/Model/Table/BroodsTable.php b/src/Model/Table/BroodsTable.php index 51424f1..fcbacce 100644 --- a/src/Model/Table/BroodsTable.php +++ b/src/Model/Table/BroodsTable.php @@ -93,6 +93,31 @@ class BroodsTable extends AppTable } } + public function downloadAndCapture($brood_id, $object_id, $scope, $path) + { + $query = $this->find(); + $brood = $query->where(['id' => $brood_id])->first(); + if (empty($brood)) { + throw new NotFoundException(__('Brood not found')); + } + $http = new Client(); + $response = $http->get($brood['url'] . '/' . $scope . '/view/' . $org_id . '/index.json' , [], [ + 'headers' => [ + 'Authorization' => $brood['authkey'], + 'Accept' => 'Application/json', + 'Content-type' => 'Application/json' + ] + ]); + if ($response->isOk()) { + $org = $response->getJson(); + $this->Organisation = TableRegistry::getTableLocator()->get('Organisations'); + $result = $this->Organisation->captureOrg($org); + return $result; + } else { + return false; + } + } + public function downloadOrg($brood_id, $org_id) { $query = $this->find(); @@ -117,4 +142,29 @@ class BroodsTable extends AppTable return false; } } + + public function downloadIndividual($brood_id, $individual_id) + { + $query = $this->find(); + $brood = $query->where(['id' => $brood_id])->first(); + if (empty($brood)) { + throw new NotFoundException(__('Brood not found')); + } + $http = new Client(); + $response = $http->get($brood['url'] . '/individuals/view/' . $individual_id . '/index.json' , [], [ + 'headers' => [ + 'Authorization' => $brood['authkey'], + 'Accept' => 'Application/json', + 'Content-type' => 'Application/json' + ] + ]); + if ($response->isOk()) { + $org = $response->getJson(); + $this->Individual = TableRegistry::getTableLocator()->get('Individual'); + $result = $this->Individual->captureIndividual($individual); + return $result; + } else { + return false; + } + } }