Fixed CSV export format issues
added formatter for epoch and boolean values Change-Id: I4cacc7a5d0c4aed030c8a95d584808deea65083f
This commit is contained in:
parent
aeaee56d9a
commit
d0090ea4a6
@ -11,6 +11,8 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
use App\Http\Utils\BooleanCellFormatter;
|
||||
use App\Http\Utils\EpochCellFormatter;
|
||||
use App\Models\Foundation\Summit\PromoCodes\PromoCodesConstants;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Request;
|
||||
@ -301,7 +303,18 @@ final class OAuth2SummitPromoCodesApiController extends OAuth2ProtectedControlle
|
||||
$data = $this->promo_code_repository->getBySummit($summit, new PagingInfo($page, $per_page), $filter, $order);
|
||||
$filename = "promocodes-" . date('Ymd');
|
||||
$list = $data->toArray();
|
||||
return $this->export('csv', $filename, $list['data']);
|
||||
return $this->export
|
||||
(
|
||||
'csv',
|
||||
$filename,
|
||||
$list['data'],
|
||||
[
|
||||
'created' => new EpochCellFormatter,
|
||||
'last_edited' => new EpochCellFormatter,
|
||||
'redeemed' => new BooleanCellFormatter,
|
||||
'email_sent' => new BooleanCellFormatter,
|
||||
]
|
||||
);
|
||||
|
||||
}
|
||||
catch (ValidationException $ex1)
|
||||
|
@ -12,6 +12,8 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
use App\Http\Utils\BooleanCellFormatter;
|
||||
use App\Http\Utils\EpochCellFormatter;
|
||||
use App\Models\Foundation\Summit\Repositories\IPresentationSpeakerSummitAssistanceConfirmationRequestRepository;
|
||||
use Illuminate\Support\Facades\Input;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
@ -208,7 +210,19 @@ final class OAuth2SummitSpeakersAssistanceApiController extends OAuth2ProtectedC
|
||||
|
||||
$filename = "summit-speaker-assistances-" . date('Ymd');
|
||||
$list = $data->toArray();
|
||||
return $this->export('csv', $filename, $list['data']);
|
||||
return $this->export(
|
||||
'csv',
|
||||
$filename,
|
||||
$list['data'],
|
||||
[
|
||||
'created' => new EpochCellFormatter,
|
||||
'last_edited' => new EpochCellFormatter,
|
||||
'confirmation_date' => new EpochCellFormatter,
|
||||
'registered' => new BooleanCellFormatter,
|
||||
'is_confirmed' => new BooleanCellFormatter,
|
||||
'checked_in' => new BooleanCellFormatter,
|
||||
]
|
||||
);
|
||||
|
||||
} catch (ValidationException $ex1) {
|
||||
Log::warning($ex1);
|
||||
|
@ -126,20 +126,22 @@ abstract class JsonController extends Controller
|
||||
* @param string $format
|
||||
* @param string $filename
|
||||
* @param array $items
|
||||
* @param array $formatters
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
protected function export($format, $filename, array $items){
|
||||
if($format == 'csv') return $this->csv($filename, $items);
|
||||
protected function export($format, $filename, array $items, array $formatters = []){
|
||||
if($format == 'csv') return $this->csv($filename, $items, $formatters);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $filename
|
||||
* @param array $items
|
||||
* @param array $formatters
|
||||
* @param string $field_separator
|
||||
* @param string $mime_type
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
private function csv($filename, array $items, $field_separator = ",", $mime_type = 'application/vnd.ms-excel'){
|
||||
private function csv($filename, array $items, array $formatters = [], $field_separator = ",", $mime_type = 'application/vnd.ms-excel'){
|
||||
$headers = [
|
||||
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
|
||||
'Content-type' => $mime_type,
|
||||
@ -149,6 +151,6 @@ abstract class JsonController extends Controller
|
||||
'Pragma' => 'public',
|
||||
];
|
||||
|
||||
return Response::make(CSVExporter::getInstance()->export($items, $field_separator), 200, $headers);
|
||||
return Response::make(CSVExporter::getInstance()->export($items, $field_separator, [] , $formatters), 200, $headers);
|
||||
}
|
||||
}
|
30
app/Http/Utils/CSV/BooleanCellFormatter.php
Normal file
30
app/Http/Utils/CSV/BooleanCellFormatter.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php namespace App\Http\Utils;
|
||||
/**
|
||||
* Copyright 2018 OpenStack Foundation
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
/**
|
||||
* Class BooleanCellFormatter
|
||||
* @package App\Http\Utils
|
||||
*/
|
||||
final class BooleanCellFormatter implements ICellFormatter
|
||||
{
|
||||
|
||||
/**
|
||||
* @param string $val
|
||||
* @return string
|
||||
*/
|
||||
public function format($val)
|
||||
{
|
||||
return boolval($val) ? '1' : '0';
|
||||
}
|
||||
}
|
@ -47,7 +47,7 @@ final class CSVExporter
|
||||
* @param array $header
|
||||
* @return string
|
||||
*/
|
||||
public function export(array $items, $field_separator = ",", array $header = []){
|
||||
public function export(array $items, $field_separator = ",", array $header = [], array $formatters){
|
||||
$flag = false;
|
||||
$output = '';
|
||||
foreach ($items as $row) {
|
||||
@ -62,7 +62,10 @@ final class CSVExporter
|
||||
array_walk($row, array($this, 'cleanData'));
|
||||
$values = [];
|
||||
foreach ($header as $key){
|
||||
$values[] = isset($row[$key])? $row[$key] : '';
|
||||
$val = isset($row[$key])? $row[$key] : '';
|
||||
if(isset($formatters[$key]))
|
||||
$val = $formatters[$key]->format($val);
|
||||
$values[] = $val;
|
||||
}
|
||||
$output .= implode($field_separator, $values) . PHP_EOL;;
|
||||
}
|
||||
@ -72,10 +75,6 @@ final class CSVExporter
|
||||
function cleanData(&$str)
|
||||
{
|
||||
if (is_null($str)) {$str = ''; return;};
|
||||
if(is_bool($str)){
|
||||
$str = boolval($str) ? '1' : '0';
|
||||
return;
|
||||
}
|
||||
$str = preg_replace("/\t/", "\\t", $str);
|
||||
$str = preg_replace("/\r?\n/", "\\n", $str);
|
||||
$str = preg_replace("/,/", "-", $str);
|
46
app/Http/Utils/CSV/EpochCellFormatter.php
Normal file
46
app/Http/Utils/CSV/EpochCellFormatter.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php namespace App\Http\Utils;
|
||||
/**
|
||||
* Copyright 2018 OpenStack Foundation
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
use DateTime;
|
||||
use DateTimeZone;
|
||||
/**
|
||||
* Class EpochCellFormatter
|
||||
* @package App\Http\Utils
|
||||
*/
|
||||
final class EpochCellFormatter implements ICellFormatter
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $format;
|
||||
|
||||
/**
|
||||
* EpochCellFormatter constructor.
|
||||
* @param string $format
|
||||
*/
|
||||
public function __construct($format = 'Y-m-d H:i:s' )
|
||||
{
|
||||
$this->format = $format;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $val
|
||||
* @return string
|
||||
*/
|
||||
public function format($val)
|
||||
{
|
||||
if(empty($val)) return '';
|
||||
$date = new DateTime("@$val");
|
||||
return $date->format($this->format);
|
||||
}
|
||||
}
|
23
app/Http/Utils/CSV/ICellFormatter.php
Normal file
23
app/Http/Utils/CSV/ICellFormatter.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php namespace App\Http\Utils;
|
||||
/**
|
||||
* Copyright 2018 OpenStack Foundation
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
|
||||
interface ICellFormatter
|
||||
{
|
||||
/**
|
||||
* @param string $val
|
||||
* @return string
|
||||
*/
|
||||
public function format($val);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user