diff --git a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitPromoCodesApiController.php b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitPromoCodesApiController.php index 1d364e36..1899b7e2 100644 --- a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitPromoCodesApiController.php +++ b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitPromoCodesApiController.php @@ -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) diff --git a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitSpeakersAssistanceApiController.php b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitSpeakersAssistanceApiController.php index 634779e4..7fe4348d 100644 --- a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitSpeakersAssistanceApiController.php +++ b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitSpeakersAssistanceApiController.php @@ -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); diff --git a/app/Http/Controllers/JsonController.php b/app/Http/Controllers/JsonController.php index 15d69e5f..851fcc72 100644 --- a/app/Http/Controllers/JsonController.php +++ b/app/Http/Controllers/JsonController.php @@ -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); } } \ No newline at end of file diff --git a/app/Http/Utils/CSV/BooleanCellFormatter.php b/app/Http/Utils/CSV/BooleanCellFormatter.php new file mode 100644 index 00000000..e5fd1680 --- /dev/null +++ b/app/Http/Utils/CSV/BooleanCellFormatter.php @@ -0,0 +1,30 @@ +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); diff --git a/app/Http/Utils/CSV/EpochCellFormatter.php b/app/Http/Utils/CSV/EpochCellFormatter.php new file mode 100644 index 00000000..da24e2b5 --- /dev/null +++ b/app/Http/Utils/CSV/EpochCellFormatter.php @@ -0,0 +1,46 @@ +format = $format; + } + + /** + * @param string $val + * @return string + */ + public function format($val) + { + if(empty($val)) return ''; + $date = new DateTime("@$val"); + return $date->format($this->format); + } +} \ No newline at end of file diff --git a/app/Http/Utils/CSV/ICellFormatter.php b/app/Http/Utils/CSV/ICellFormatter.php new file mode 100644 index 00000000..74075f3d --- /dev/null +++ b/app/Http/Utils/CSV/ICellFormatter.php @@ -0,0 +1,23 @@ +