From 006a922e9fd522aa57a4eb3b9aecaad61449334a Mon Sep 17 00:00:00 2001 From: iglocska Date: Tue, 14 Aug 2018 23:38:01 +0200 Subject: [PATCH] chg: [API] further work on the new CSV export --- app/Controller/EventsController.php | 6 +++--- app/Lib/Export/CsvExport.php | 10 ++++++---- app/Model/Attribute.php | 11 +++++++++-- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/app/Controller/EventsController.php b/app/Controller/EventsController.php index ba87bed3f..4b4b39aea 100644 --- a/app/Controller/EventsController.php +++ b/app/Controller/EventsController.php @@ -2695,8 +2695,6 @@ class EventsController extends AppController if ($user === false) { return $exception; } - App::uses('CsvExport', 'Export'); - $export = new CsvExport(); // if it's a search, grab the attributeIDList from the session and get the IDs from it. Use those as the condition // We don't need to look out for permissions since that's filtered by the search itself // We just want all the attributes found by the search @@ -2775,11 +2773,13 @@ class EventsController extends AppController 'requested_attributes' => $requested_attributes, 'includeContext' => $includeContext ); + App::uses('CsvExport', 'Export'); + $export = new CsvExport(); $final = $export->header($options); while ($continue) { $attributes = $this->Event->csv($user, $params, false, $continue); $params['page'] += 1; - $final .= $export->handler($attributes, $final); + $final .= $export->handler($attributes, $final, $options); $final .= $export->separator($attributes, $final); } $export->footer(); diff --git a/app/Lib/Export/CsvExport.php b/app/Lib/Export/CsvExport.php index 69db4934b..cd6fdf82c 100644 --- a/app/Lib/Export/CsvExport.php +++ b/app/Lib/Export/CsvExport.php @@ -16,6 +16,7 @@ class CsvExport public function handler($attributes, $final, $options = array()) { + $result = array(); foreach ($attributes as $attribute) { $line1 = ''; $line2 = ''; @@ -29,7 +30,7 @@ class CsvExport $line2 = rtrim($line2, ","); $line = $line1 . ',' . $line2; $line = rtrim($line, ","); - if ($includeContext) { + if (!empty($options['includeContext'])) { foreach ($this->Event->csv_event_context_fields_to_fetch as $header => $field) { if ($field['object']) { $line .= ',' . $attribute['Event'][$field['object']][$field['var']]; @@ -38,9 +39,10 @@ class CsvExport } } } - $final .= $line; + $result[] = $line; } - return $final; + $result = implode(PHP_EOL, $result); + return $result; } public function header($options = array()) @@ -60,7 +62,7 @@ class CsvExport $headers[$k] = 'date'; } } - $headers = implode(',', $headers); + $headers = implode(',', $headers) . PHP_EOL; return $headers; } diff --git a/app/Model/Attribute.php b/app/Model/Attribute.php index 9c081131c..70c3bfa46 100644 --- a/app/Model/Attribute.php +++ b/app/Model/Attribute.php @@ -2837,10 +2837,14 @@ class Attribute extends AppModel $continue = false; continue; } - } else { - $continue = false; } $results = $this->find('all', $params); + if (!$loop) { + if (!empty($params['limit']) && count($results) < $params['limit']) { + $continue = false; + } + $break = true; + } // return false if we're paginating if (isset($options['limit']) && empty($results)) { return array(); @@ -2869,6 +2873,9 @@ class Attribute extends AppModel } $attributes[] = $results[$key]; } + if ($break) { + break; + } } return $attributes; }