インポート処理のコントローラーとか実装

This commit is contained in:
shibafu 2020-05-21 22:33:27 +09:00
parent 15c462449f
commit fa4827f382
5 changed files with 107 additions and 0 deletions

View File

@ -3,7 +3,9 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\DeactivatedUser; use App\DeactivatedUser;
use App\Exceptions\CsvImportException;
use App\Services\CheckinCsvExporter; use App\Services\CheckinCsvExporter;
use App\Services\CheckinCsvImporter;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
@ -72,6 +74,36 @@ class SettingController extends Controller
return redirect()->route('setting.privacy')->with('status', 'プライバシー設定を更新しました。'); return redirect()->route('setting.privacy')->with('status', 'プライバシー設定を更新しました。');
} }
public function import()
{
return view('setting.import');
}
public function storeImport(Request $request)
{
$validated = $request->validate([
'file' => 'required|file'
], [], [
'file' => 'ファイル'
]);
$file = $request->file('file');
if (!$file->isValid()) {
return redirect()->route('setting.import')->withErrors(['file' => 'ファイルのアップロードに失敗しました。']);
}
try {
set_time_limit(0);
$importer = new CheckinCsvImporter(Auth::user(), $file->path());
$importer->execute();
return redirect()->route('setting.import')->with('status', 'インポートに性交しました。');
} catch (CsvImportException $e) {
return redirect()->route('setting.import')->with('import_errors', $e->getErrors());
}
}
public function export() public function export()
{ {
return view('setting.export'); return view('setting.export');

View File

@ -92,4 +92,43 @@ class Formatter
return implode(',', $srcset); return implode(',', $srcset);
} }
/**
* php.ini書式のデータサイズを正規化します。
* @param mixed $val データサイズ
* @return string
*/
public function normalizeIniBytes($val)
{
$val = trim($val);
$last = strtolower(substr($val, -1, 1));
if (ord($last) < 0x30 || ord($last) > 0x39) {
$bytes = substr($val, 0, -1);
switch ($last) {
case 'g':
$bytes *= 1024;
// fall through
// no break
case 'm':
$bytes *= 1024;
// fall through
// no break
case 'k':
$bytes *= 1024;
break;
}
} else {
$bytes = $val;
}
if ($bytes >= (1 << 30)) {
return ($bytes >> 30) . 'GB';
} elseif ($bytes >= (1 << 20)) {
return ($bytes >> 20) . 'MB';
} elseif ($bytes >= (1 << 10)) {
return ($bytes >> 10) . 'KB';
}
return $bytes . 'B';
}
} }

View File

@ -10,6 +10,8 @@
href="{{ route('setting') }}"><span class="oi oi-person mr-1"></span> プロフィール</a> href="{{ route('setting') }}"><span class="oi oi-person mr-1"></span> プロフィール</a>
<a class="list-group-item list-group-item-action {{ Route::currentRouteName() === 'setting.privacy' ? 'active' : '' }}" <a class="list-group-item list-group-item-action {{ Route::currentRouteName() === 'setting.privacy' ? 'active' : '' }}"
href="{{ route('setting.privacy') }}"><span class="oi oi-shield mr-1"></span> プライバシー</a> href="{{ route('setting.privacy') }}"><span class="oi oi-shield mr-1"></span> プライバシー</a>
<a class="list-group-item list-group-item-action {{ Route::currentRouteName() === 'setting.import' ? 'active' : '' }}"
href="{{ route('setting.import') }}"><span class="oi oi-data-transfer-upload mr-1"></span> データのインポート</a>
<a class="list-group-item list-group-item-action {{ Route::currentRouteName() === 'setting.export' ? 'active' : '' }}" <a class="list-group-item list-group-item-action {{ Route::currentRouteName() === 'setting.export' ? 'active' : '' }}"
href="{{ route('setting.export') }}"><span class="oi oi-data-transfer-download mr-1"></span> データのエクスポート</a> href="{{ route('setting.export') }}"><span class="oi oi-data-transfer-download mr-1"></span> データのエクスポート</a>
<a class="list-group-item list-group-item-action {{ Route::currentRouteName() === 'setting.deactivate' ? 'active' : '' }}" <a class="list-group-item list-group-item-action {{ Route::currentRouteName() === 'setting.deactivate' ? 'active' : '' }}"

View File

@ -0,0 +1,32 @@
@extends('setting.base')
@section('title', 'データのインポート')
@section('tab-content')
<h3>データのインポート</h3>
<hr>
<p>外部で作成したチェックインデータをTissueに取り込むことができます。</p>
<form class="mt-4" action="{{ route('setting.import') }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<strong>取り込むファイルを選択してください。</strong>
<small class="form-text text-muted">{{ Formatter::normalizeIniBytes(ini_get('upload_max_filesize')) }}までのCSVファイル、文字コードは Shift_JIS UTF-8 (BOMなし) に対応しています。</small>
<input name="file" type="file" class="form-control-file {{ $errors->has('file') ? ' is-invalid' : '' }} mt-2">
@if ($errors->has('file'))
<div class="invalid-feedback">{{ $errors->first('file') }}</div>
@endif
</div>
@if (session('import_errors'))
<div class="alert alert-danger">
<p class="alert-heading"><span class="oi oi-warning"></span> <strong>インポートに失敗しました</strong></p>
@foreach (session('import_errors') as $err)
<p class="mb-0">{{ $err }}</p>
@endforeach
</div>
@endif
<button type="submit" class="btn btn-primary mt-2">アップロード</button>
</form>
@endsection
@push('script')
@endpush

View File

@ -36,6 +36,8 @@ Route::middleware('auth')->group(function () {
Route::post('/setting/profile', 'SettingController@updateProfile')->name('setting.profile.update'); Route::post('/setting/profile', 'SettingController@updateProfile')->name('setting.profile.update');
Route::get('/setting/privacy', 'SettingController@privacy')->name('setting.privacy'); Route::get('/setting/privacy', 'SettingController@privacy')->name('setting.privacy');
Route::post('/setting/privacy', 'SettingController@updatePrivacy')->name('setting.privacy.update'); Route::post('/setting/privacy', 'SettingController@updatePrivacy')->name('setting.privacy.update');
Route::get('/setting/import', 'SettingController@import')->name('setting.import');
Route::post('/setting/import', 'SettingController@storeImport')->name('setting.import');
Route::get('/setting/export', 'SettingController@export')->name('setting.export'); Route::get('/setting/export', 'SettingController@export')->name('setting.export');
Route::get('/setting/export/csv', 'SettingController@exportToCsv')->name('setting.export.csv'); Route::get('/setting/export/csv', 'SettingController@exportToCsv')->name('setting.export.csv');
Route::get('/setting/deactivate', 'SettingController@deactivate')->name('setting.deactivate'); Route::get('/setting/deactivate', 'SettingController@deactivate')->name('setting.deactivate');