chg: [events:index] Simplified endpoint

pull/8208/head
Sami Mokaddem 2022-03-10 12:10:37 +01:00
parent 3f9629ad0c
commit c83a7b0b5b
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
2 changed files with 33 additions and 23 deletions

View File

@ -3210,11 +3210,23 @@ class EventsController extends AppController
return $difference . " " . $periods[$j] . " ago";
}
public function restSearchExport($id=null)
public function restSearchExport($id=null, $returnFormat=null)
{
if ($this->request->is('post') || $this->request->is('put')) {
$returnFormat = empty($this->request->data['Event']['returnFormat']) ? 'json' : $this->request->data['Event']['returnFormat'];
$idList = !isset($this->request->data['Event']['id']) ? $id : $this->request->data['Event']['id'];
if (is_null($returnFormat)) {
if (is_numeric($id)) {
$idList = [$id];
} else {
$idList = json_decode($id, true);
}
if (empty($idList)) {
throw new NotFoundException(__('Invalid input.'));
}
$this->set('idList', $idList);
$this->set('exportFormats', array_keys($this->Event->validFormats));
$this->render('ajax/eventRestSearchExportConfirmationForm');
} else {
$returnFormat = empty($this->Event->validFormats[$returnFormat]) ? 'json' : $returnFormat;
$idList = $id;
if (!is_array($idList)) {
if (is_numeric($idList) || Validation::uuid($idList)) {
$idList = array($idList);
@ -3232,7 +3244,7 @@ class EventsController extends AppController
$elementCounter = 0;
$renderView = false;
$validFormat = $this->Event->validFormats[$returnFormat];
$responseType = empty($validFormat[0]) ? 'json' : $validFormat[0];
$responseType = $validFormat[0];
$final = $this->Event->restSearch($this->Auth->user(), $returnFormat, $filters, false, false, $elementCounter, $renderView);
if (!empty($renderView) && !empty($final)) {
$final = json_decode($final->intoString(), true);
@ -3243,20 +3255,12 @@ class EventsController extends AppController
$this->render('/Events/eventRestSearchExportResult');
} else {
$filename = $this->RestSearch->getFilename($filters, 'Event', $responseType);
return $this->RestResponse->viewData($final, $responseType, false, true, $filename, array('X-Result-Count' => $elementCounter, 'X-Export-Module-Used' => $returnFormat, 'X-Response-Format' => $responseType));
return $this->RestResponse->viewData($final, $responseType, false, true, $filename, [
'X-Result-Count' => $elementCounter,
'X-Export-Module-Used' => $returnFormat,
'X-Response-Format' => $responseType
]);
}
} else {
if (is_numeric($id)) {
$idList = [$id];
} else {
$idList = json_decode($id, true);
}
if (empty($idList)) {
throw new NotFoundException(__('Invalid input.'));
}
$this->request->data['Event']['id'] = json_encode($idList);
$this->set('exportFormats', array_keys($this->Event->validFormats));
$this->render('ajax/eventRestSearchExportConfirmationForm');
}
}

View File

@ -3,13 +3,9 @@ $modelForForm = 'Event';
echo $this->element('genericElements/Form/genericForm', [
'form' => $this->Form,
'data' => [
'title' => __('Export the %s selected events into the selected format', count($idArray)),
'title' => __('Export the selected events into the selected format'),
'model' => $modelForForm,
'fields' => [
[
'field' => 'id',
'type' => 'hidden',
],
[
'field' => 'returnFormat',
'label' => __('RestSearch Export Format'),
@ -21,6 +17,16 @@ echo $this->element('genericElements/Form/genericForm', [
],
'submit' => [
'action' => $this->request->params['action'],
'ajaxSubmit' => 'redirectToExportResult()'
],
],
]);
?>
<script>
function redirectToExportResult() {
var idListStr = '<?= json_encode($idList) ?>'
var returnFormat = $('#EventReturnFormat').val()
window.location = '<?= $baseurl ?>/events/restSearchExport/' + idListStr + '/' + returnFormat
}
</script>