CT-01/02/03 공통 컴포넌트 구현 — 페이지네이션/엑셀/인쇄
CT-01: 페이지네이션 - 커스텀 Tailwind 페이저 뷰 (components/pager.php) - 18개 admin 컨트롤러 findAll() → paginate(20) 전환 - Bag 컨트롤러 7개 리스트도 paginate 적용 - 19개 admin index 뷰에 페이저 링크 추가 CT-02: 엑셀 저장 - export_helper.php (UTF-8 BOM CSV) - 발주/판매/지정판매소/재고 4개 엑셀 내보내기 라우트+메서드 - 해당 뷰에 "엑셀저장" 버튼 추가 CT-03: 인쇄 - print_header.php (지자체명/제목/결재란 컴포넌트) - admin/bag 레이아웃에 @media print CSS 추가 - 23개 뷰에 인쇄 버튼 + print_header 추가 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
69
app/Helpers/export_helper.php
Normal file
69
app/Helpers/export_helper.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* CSV 엑셀 내보내기 헬퍼
|
||||
*
|
||||
* UTF-8 BOM 포함으로 한글 엑셀 호환성 보장
|
||||
*/
|
||||
|
||||
if (! function_exists('export_csv')) {
|
||||
/**
|
||||
* CSV 파일을 브라우저로 다운로드 전송
|
||||
*
|
||||
* @param string $filename 파일명 (확장자 포함, 예: 'export.csv')
|
||||
* @param string[] $headers 컬럼 헤더 배열
|
||||
* @param array $rows 데이터 행 배열 (각 행은 배열)
|
||||
*/
|
||||
function export_csv(string $filename, array $headers, array $rows): void
|
||||
{
|
||||
// 파일명에 .csv 확장자 보장
|
||||
if (! str_ends_with($filename, '.csv')) {
|
||||
$filename .= '.csv';
|
||||
}
|
||||
|
||||
$response = service('response');
|
||||
$response->setHeader('Content-Type', 'text/csv; charset=UTF-8');
|
||||
$response->setHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
|
||||
$response->setHeader('Pragma', 'no-cache');
|
||||
$response->setHeader('Cache-Control', 'no-store, no-cache, must-revalidate');
|
||||
|
||||
// UTF-8 BOM (한글 엑셀 호환)
|
||||
$output = "\xEF\xBB\xBF";
|
||||
|
||||
// 헤더 행
|
||||
$output .= csv_encode_row($headers);
|
||||
|
||||
// 데이터 행
|
||||
foreach ($rows as $row) {
|
||||
$output .= csv_encode_row(array_values((array) $row));
|
||||
}
|
||||
|
||||
$response->setBody($output);
|
||||
$response->send();
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('csv_encode_row')) {
|
||||
/**
|
||||
* 배열 한 행을 CSV 문자열로 변환
|
||||
*
|
||||
* @param array $fields
|
||||
* @return string
|
||||
*/
|
||||
function csv_encode_row(array $fields): string
|
||||
{
|
||||
$escaped = [];
|
||||
foreach ($fields as $field) {
|
||||
$val = (string) ($field ?? '');
|
||||
// 쌍따옴표 이스케이프 및 감싸기
|
||||
if (str_contains($val, '"') || str_contains($val, ',') || str_contains($val, "\n") || str_contains($val, "\r")) {
|
||||
$val = '"' . str_replace('"', '""', $val) . '"';
|
||||
}
|
||||
$escaped[] = $val;
|
||||
}
|
||||
return implode(',', $escaped) . "\r\n";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user