2020-02-04 01:44:16 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Io;
|
|
|
|
|
|
|
|
use App\Ejaculation;
|
|
|
|
use App\Exceptions\CsvImportException;
|
2020-02-13 01:03:54 +09:00
|
|
|
use App\Rules\CsvDateTime;
|
2020-02-05 00:52:13 +09:00
|
|
|
use App\Tag;
|
2020-02-13 01:03:54 +09:00
|
|
|
use App\User;
|
2020-02-04 01:44:16 +09:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2020-02-13 01:03:54 +09:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2020-02-04 01:44:16 +09:00
|
|
|
use League\Csv\Reader;
|
|
|
|
|
|
|
|
class CheckinCsvImporter
|
|
|
|
{
|
2020-02-13 01:03:54 +09:00
|
|
|
/** @var User Target user */
|
|
|
|
private $user;
|
2020-02-04 01:44:16 +09:00
|
|
|
/** @var string CSV filename */
|
|
|
|
private $filename;
|
|
|
|
|
2020-02-13 01:03:54 +09:00
|
|
|
public function __construct(User $user, string $filename)
|
2020-02-04 01:44:16 +09:00
|
|
|
{
|
2020-02-13 01:03:54 +09:00
|
|
|
$this->user = $user;
|
2020-02-04 01:44:16 +09:00
|
|
|
$this->filename = $filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute()
|
|
|
|
{
|
|
|
|
// Guess charset
|
|
|
|
$head = file_get_contents($this->filename, false, null, 0, 1024);
|
|
|
|
$charset = mb_detect_encoding($head, ['ASCII', 'UTF-8', 'SJIS-win'], true);
|
|
|
|
if (array_search($charset, ['UTF-8', 'SJIS-win'], true) === false) {
|
|
|
|
throw new CsvImportException(['文字コード判定に失敗しました。UTF-8 (BOM無し) または Shift_JIS をお使いください。']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open CSV
|
|
|
|
$csv = Reader::createFromPath($this->filename, 'r');
|
|
|
|
$csv->setHeaderOffset(0);
|
|
|
|
if ($charset === 'SJIS-win') {
|
|
|
|
$csv->addStreamFilter('convert.mbstring.encoding.SJIS-win:UTF-8');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Import
|
|
|
|
DB::transaction(function () use ($csv) {
|
|
|
|
$errors = [];
|
|
|
|
|
|
|
|
if (!in_array('日時', $csv->getHeader(), true)) {
|
|
|
|
$errors[] = '日時列は必須です。';
|
|
|
|
throw new CsvImportException($errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($csv->getRecords() as $offset => $record) {
|
2020-02-16 14:28:30 +09:00
|
|
|
$line = $offset + 1;
|
2020-02-13 01:03:54 +09:00
|
|
|
$ejaculation = new Ejaculation(['user_id' => $this->user->id]);
|
2020-02-04 01:44:16 +09:00
|
|
|
|
2020-02-13 01:03:54 +09:00
|
|
|
$validator = Validator::make($record, [
|
|
|
|
'日時' => ['required', new CsvDateTime()],
|
|
|
|
'ノート' => 'nullable|string|max:500',
|
|
|
|
'オカズリンク' => 'nullable|url|max:2000',
|
|
|
|
]);
|
2020-02-05 00:52:13 +09:00
|
|
|
|
2020-02-13 01:03:54 +09:00
|
|
|
if ($validator->fails()) {
|
|
|
|
foreach ($validator->errors()->all() as $message) {
|
2020-02-16 14:28:30 +09:00
|
|
|
$errors[] = "{$line} 行 : {$message}";
|
2020-02-05 00:52:13 +09:00
|
|
|
}
|
2020-02-13 01:03:54 +09:00
|
|
|
continue;
|
2020-02-05 00:52:13 +09:00
|
|
|
}
|
|
|
|
|
2020-02-13 01:03:54 +09:00
|
|
|
$ejaculation->ejaculated_date = Carbon::createFromFormat('!Y/m/d H:i+', $record['日時']);
|
2020-02-16 15:29:06 +09:00
|
|
|
$ejaculation->note = str_replace(["\r\n", "\r"], "\n", $record['ノート'] ?? '');
|
2020-02-13 01:03:54 +09:00
|
|
|
$ejaculation->link = $record['オカズリンク'] ?? '';
|
2020-02-05 00:52:13 +09:00
|
|
|
|
|
|
|
$tagIds = [];
|
|
|
|
for ($i = 1; $i <= 32; $i++) {
|
|
|
|
$column = 'タグ' . $i;
|
|
|
|
if (empty($record[$column])) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
$tag = trim($record[$column]);
|
|
|
|
|
|
|
|
if (empty($tag)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (mb_strlen($tag) > 255) {
|
2020-02-16 14:28:30 +09:00
|
|
|
$errors[] = "{$line} 行 : {$column}列は255文字以内にしてください。";
|
2020-02-05 00:52:13 +09:00
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
$tag = Tag::firstOrCreate(['name' => $tag]);
|
|
|
|
$tagIds[] = $tag->id;
|
|
|
|
}
|
2020-02-04 01:44:16 +09:00
|
|
|
}
|
2020-02-05 00:52:13 +09:00
|
|
|
$ejaculation->tags()->sync($tagIds);
|
2020-02-04 01:44:16 +09:00
|
|
|
|
|
|
|
$ejaculation->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($errors)) {
|
|
|
|
throw new CsvImportException($errors);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|