chg: [API] further work on the new CSV export

pull/3608/head
iglocska 2018-08-14 23:38:01 +02:00
parent 1d5ff2f146
commit 006a922e9f
3 changed files with 18 additions and 9 deletions

View File

@ -2695,8 +2695,6 @@ class EventsController extends AppController
if ($user === false) { if ($user === false) {
return $exception; 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 // 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 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 // We just want all the attributes found by the search
@ -2775,11 +2773,13 @@ class EventsController extends AppController
'requested_attributes' => $requested_attributes, 'requested_attributes' => $requested_attributes,
'includeContext' => $includeContext 'includeContext' => $includeContext
); );
App::uses('CsvExport', 'Export');
$export = new CsvExport();
$final = $export->header($options); $final = $export->header($options);
while ($continue) { while ($continue) {
$attributes = $this->Event->csv($user, $params, false, $continue); $attributes = $this->Event->csv($user, $params, false, $continue);
$params['page'] += 1; $params['page'] += 1;
$final .= $export->handler($attributes, $final); $final .= $export->handler($attributes, $final, $options);
$final .= $export->separator($attributes, $final); $final .= $export->separator($attributes, $final);
} }
$export->footer(); $export->footer();

View File

@ -16,6 +16,7 @@ class CsvExport
public function handler($attributes, $final, $options = array()) public function handler($attributes, $final, $options = array())
{ {
$result = array();
foreach ($attributes as $attribute) { foreach ($attributes as $attribute) {
$line1 = ''; $line1 = '';
$line2 = ''; $line2 = '';
@ -29,7 +30,7 @@ class CsvExport
$line2 = rtrim($line2, ","); $line2 = rtrim($line2, ",");
$line = $line1 . ',' . $line2; $line = $line1 . ',' . $line2;
$line = rtrim($line, ","); $line = rtrim($line, ",");
if ($includeContext) { if (!empty($options['includeContext'])) {
foreach ($this->Event->csv_event_context_fields_to_fetch as $header => $field) { foreach ($this->Event->csv_event_context_fields_to_fetch as $header => $field) {
if ($field['object']) { if ($field['object']) {
$line .= ',' . $attribute['Event'][$field['object']][$field['var']]; $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()) public function header($options = array())
@ -60,7 +62,7 @@ class CsvExport
$headers[$k] = 'date'; $headers[$k] = 'date';
} }
} }
$headers = implode(',', $headers); $headers = implode(',', $headers) . PHP_EOL;
return $headers; return $headers;
} }

View File

@ -2837,10 +2837,14 @@ class Attribute extends AppModel
$continue = false; $continue = false;
continue; continue;
} }
} else {
$continue = false;
} }
$results = $this->find('all', $params); $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 // return false if we're paginating
if (isset($options['limit']) && empty($results)) { if (isset($options['limit']) && empty($results)) {
return array(); return array();
@ -2869,6 +2873,9 @@ class Attribute extends AppModel
} }
$attributes[] = $results[$key]; $attributes[] = $results[$key];
} }
if ($break) {
break;
}
} }
return $attributes; return $attributes;
} }