2017-08-27 04:44:53 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2019-01-15 00:05:01 +09:00
|
|
|
use App\Ejaculation;
|
2018-06-07 23:46:40 +09:00
|
|
|
use App\Events\LinkDiscovered;
|
2018-01-08 08:50:22 +09:00
|
|
|
use App\Tag;
|
2017-11-03 17:48:58 +09:00
|
|
|
use App\User;
|
2017-11-05 19:14:36 +09:00
|
|
|
use Carbon\Carbon;
|
2017-08-27 04:44:53 +09:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2020-08-10 00:10:06 +09:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2019-01-15 00:05:01 +09:00
|
|
|
use Validator;
|
2017-08-27 04:44:53 +09:00
|
|
|
|
|
|
|
class EjaculationController extends Controller
|
|
|
|
{
|
2018-03-06 22:43:49 +09:00
|
|
|
public function create(Request $request)
|
2017-08-27 04:44:53 +09:00
|
|
|
{
|
2018-03-06 22:43:49 +09:00
|
|
|
$defaults = [
|
|
|
|
'date' => $request->input('date', date('Y/m/d')),
|
|
|
|
'time' => $request->input('time', date('H:i')),
|
|
|
|
'link' => $request->input('link', ''),
|
|
|
|
'tags' => $request->input('tags', ''),
|
|
|
|
'note' => $request->input('note', ''),
|
2019-09-07 19:57:44 +09:00
|
|
|
'is_private' => $request->input('is_private', 0) == 1,
|
|
|
|
'is_too_sensitive' => $request->input('is_too_sensitive', 0) == 1
|
2018-03-06 22:43:49 +09:00
|
|
|
];
|
|
|
|
|
|
|
|
return view('ejaculation.checkin')->with('defaults', $defaults);
|
2017-08-27 04:44:53 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
2018-01-02 23:37:27 +09:00
|
|
|
$inputs = $request->all();
|
|
|
|
|
2018-03-06 22:43:49 +09:00
|
|
|
$validator = Validator::make($inputs, [
|
2017-08-27 04:44:53 +09:00
|
|
|
'date' => 'required|date_format:Y/m/d',
|
|
|
|
'time' => 'required|date_format:H:i',
|
|
|
|
'note' => 'nullable|string|max:500',
|
2019-01-13 23:58:11 +09:00
|
|
|
'link' => 'nullable|url|max:2000',
|
2018-01-08 08:50:22 +09:00
|
|
|
'tags' => 'nullable|string',
|
2018-01-02 23:37:27 +09:00
|
|
|
])->after(function ($validator) use ($request, $inputs) {
|
2017-08-27 04:44:53 +09:00
|
|
|
// 日時の重複チェック
|
2017-11-05 19:14:36 +09:00
|
|
|
if (!$validator->errors()->hasAny(['date', 'time'])) {
|
2018-01-02 23:37:27 +09:00
|
|
|
$dt = $inputs['date'] . ' ' . $inputs['time'];
|
2017-11-05 19:14:36 +09:00
|
|
|
if (Ejaculation::where(['user_id' => Auth::id(), 'ejaculated_date' => $dt])->count()) {
|
|
|
|
$validator->errors()->add('datetime', '既にこの日時にチェックインしているため、登録できません。');
|
|
|
|
}
|
2017-08-27 04:44:53 +09:00
|
|
|
}
|
2018-03-06 22:43:49 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
return redirect()->route('checkin')->withErrors($validator)->withInput();
|
|
|
|
}
|
2017-08-27 04:44:53 +09:00
|
|
|
|
2020-08-10 00:10:06 +09:00
|
|
|
$ejaculation = DB::transaction(function () use ($request, $inputs) {
|
|
|
|
$ejaculation = Ejaculation::create([
|
|
|
|
'user_id' => Auth::id(),
|
|
|
|
'ejaculated_date' => Carbon::createFromFormat('Y/m/d H:i', $inputs['date'] . ' ' . $inputs['time']),
|
|
|
|
'note' => $inputs['note'] ?? '',
|
|
|
|
'link' => $inputs['link'] ?? '',
|
|
|
|
'source' => Ejaculation::SOURCE_WEB,
|
|
|
|
'is_private' => $request->has('is_private') ?? false,
|
|
|
|
'is_too_sensitive' => $request->has('is_too_sensitive') ?? false
|
|
|
|
]);
|
|
|
|
|
|
|
|
$tagIds = [];
|
|
|
|
if (!empty($inputs['tags'])) {
|
|
|
|
$tags = explode(' ', $inputs['tags']);
|
|
|
|
foreach ($tags as $tag) {
|
|
|
|
if ($tag === '') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$tag = Tag::firstOrCreate(['name' => $tag]);
|
|
|
|
$tagIds[] = $tag->id;
|
2019-11-09 23:19:28 +09:00
|
|
|
}
|
2018-01-08 15:23:58 +09:00
|
|
|
}
|
2020-08-10 00:10:06 +09:00
|
|
|
$ejaculation->tags()->sync($tagIds);
|
|
|
|
|
|
|
|
return $ejaculation;
|
|
|
|
});
|
2018-01-08 08:50:22 +09:00
|
|
|
|
2018-06-07 23:46:40 +09:00
|
|
|
if (!empty($ejaculation->link)) {
|
|
|
|
event(new LinkDiscovered($ejaculation->link));
|
|
|
|
}
|
|
|
|
|
2017-11-05 23:06:03 +09:00
|
|
|
return redirect()->route('checkin.show', ['id' => $ejaculation->id])->with('status', 'チェックインしました!');
|
2017-08-27 04:44:53 +09:00
|
|
|
}
|
|
|
|
|
2017-11-05 21:49:27 +09:00
|
|
|
public function show($id)
|
2017-08-27 04:44:53 +09:00
|
|
|
{
|
2019-04-05 23:10:26 +09:00
|
|
|
$ejaculation = Ejaculation::where('id', $id)
|
|
|
|
->withLikes()
|
|
|
|
->firstOrFail();
|
2017-11-05 21:49:27 +09:00
|
|
|
$user = User::findOrFail($ejaculation->user_id);
|
|
|
|
|
|
|
|
// 1つ前のチェックインからの経過時間を求める
|
|
|
|
$previousEjaculation = Ejaculation::select('ejaculated_date')
|
|
|
|
->where('user_id', $ejaculation->user_id)
|
|
|
|
->where('ejaculated_date', '<', $ejaculation->ejaculated_date)
|
|
|
|
->orderByDesc('ejaculated_date')
|
|
|
|
->first();
|
|
|
|
if (!empty($previousEjaculation)) {
|
|
|
|
$ejaculatedSpan = $ejaculation->ejaculated_date
|
|
|
|
->diff($previousEjaculation->ejaculated_date)
|
|
|
|
->format('%a日 %h時間 %i分');
|
|
|
|
} else {
|
|
|
|
$ejaculatedSpan = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('ejaculation.show')->with(compact('user', 'ejaculation', 'ejaculatedSpan'));
|
2017-08-27 04:44:53 +09:00
|
|
|
}
|
|
|
|
|
2018-01-04 16:53:33 +09:00
|
|
|
public function edit($id)
|
2017-08-27 04:44:53 +09:00
|
|
|
{
|
2018-01-04 16:53:33 +09:00
|
|
|
$ejaculation = Ejaculation::findOrFail($id);
|
2019-01-15 00:05:01 +09:00
|
|
|
|
2019-10-14 02:24:39 +09:00
|
|
|
$this->authorize('edit', $ejaculation);
|
2019-10-14 02:03:57 +09:00
|
|
|
|
2018-01-04 16:53:33 +09:00
|
|
|
return view('ejaculation.edit')->with(compact('ejaculation'));
|
2017-08-27 04:44:53 +09:00
|
|
|
}
|
|
|
|
|
2018-01-04 16:53:33 +09:00
|
|
|
public function update(Request $request, $id)
|
2017-08-27 04:44:53 +09:00
|
|
|
{
|
2018-01-04 16:53:33 +09:00
|
|
|
$ejaculation = Ejaculation::findOrFail($id);
|
|
|
|
|
2019-10-14 02:24:39 +09:00
|
|
|
$this->authorize('edit', $ejaculation);
|
2019-10-14 02:03:57 +09:00
|
|
|
|
2018-01-04 16:53:33 +09:00
|
|
|
$inputs = $request->all();
|
|
|
|
|
2018-03-06 22:43:49 +09:00
|
|
|
$validator = Validator::make($inputs, [
|
2018-01-04 16:53:33 +09:00
|
|
|
'date' => 'required|date_format:Y/m/d',
|
|
|
|
'time' => 'required|date_format:H:i',
|
|
|
|
'note' => 'nullable|string|max:500',
|
2019-01-13 23:58:11 +09:00
|
|
|
'link' => 'nullable|url|max:2000',
|
2018-01-08 08:56:17 +09:00
|
|
|
'tags' => 'nullable|string',
|
2018-01-04 16:53:33 +09:00
|
|
|
])->after(function ($validator) use ($id, $request, $inputs) {
|
|
|
|
// 日時の重複チェック
|
|
|
|
if (!$validator->errors()->hasAny(['date', 'time'])) {
|
|
|
|
$dt = $inputs['date'] . ' ' . $inputs['time'];
|
|
|
|
if (Ejaculation::where(['user_id' => Auth::id(), 'ejaculated_date' => $dt])->where('id', '<>', $id)->count()) {
|
|
|
|
$validator->errors()->add('datetime', '既にこの日時にチェックインしているため、登録できません。');
|
|
|
|
}
|
|
|
|
}
|
2018-03-06 22:43:49 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
2019-01-17 01:01:42 +09:00
|
|
|
return redirect()->route('checkin.edit', ['id' => $id])->withErrors($validator)->withInput();
|
2018-03-06 22:43:49 +09:00
|
|
|
}
|
2018-01-04 16:53:33 +09:00
|
|
|
|
2020-08-10 00:10:06 +09:00
|
|
|
DB::transaction(function () use ($ejaculation, $request, $inputs) {
|
|
|
|
$ejaculation->fill([
|
|
|
|
'ejaculated_date' => Carbon::createFromFormat('Y/m/d H:i', $inputs['date'] . ' ' . $inputs['time']),
|
|
|
|
'note' => $inputs['note'] ?? '',
|
|
|
|
'link' => $inputs['link'] ?? '',
|
|
|
|
'is_private' => $request->has('is_private') ?? false,
|
|
|
|
'is_too_sensitive' => $request->has('is_too_sensitive') ?? false
|
|
|
|
])->save();
|
|
|
|
|
|
|
|
$tagIds = [];
|
|
|
|
if (!empty($inputs['tags'])) {
|
|
|
|
$tags = explode(' ', $inputs['tags']);
|
|
|
|
foreach ($tags as $tag) {
|
|
|
|
if ($tag === '') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$tag = Tag::firstOrCreate(['name' => $tag]);
|
|
|
|
$tagIds[] = $tag->id;
|
2019-11-09 23:19:28 +09:00
|
|
|
}
|
2018-01-08 15:23:58 +09:00
|
|
|
}
|
2020-08-10 00:10:06 +09:00
|
|
|
$ejaculation->tags()->sync($tagIds);
|
|
|
|
});
|
2018-01-08 08:56:17 +09:00
|
|
|
|
2018-06-07 23:46:40 +09:00
|
|
|
if (!empty($ejaculation->link)) {
|
|
|
|
event(new LinkDiscovered($ejaculation->link));
|
|
|
|
}
|
|
|
|
|
2018-01-04 16:53:33 +09:00
|
|
|
return redirect()->route('checkin.show', ['id' => $ejaculation->id])->with('status', 'チェックインを修正しました!');
|
2017-08-27 04:44:53 +09:00
|
|
|
}
|
|
|
|
|
2017-11-03 17:48:58 +09:00
|
|
|
public function destroy($id)
|
2017-08-27 04:44:53 +09:00
|
|
|
{
|
2017-11-03 17:48:58 +09:00
|
|
|
$ejaculation = Ejaculation::findOrFail($id);
|
2019-10-14 02:03:57 +09:00
|
|
|
|
2019-10-14 02:24:39 +09:00
|
|
|
$this->authorize('edit', $ejaculation);
|
2019-10-14 02:03:57 +09:00
|
|
|
|
2017-11-03 17:48:58 +09:00
|
|
|
$user = User::findOrFail($ejaculation->user_id);
|
2020-08-10 00:10:06 +09:00
|
|
|
|
|
|
|
DB::transaction(function () use ($ejaculation) {
|
|
|
|
$ejaculation->tags()->detach();
|
|
|
|
$ejaculation->delete();
|
|
|
|
});
|
2019-01-15 00:05:01 +09:00
|
|
|
|
2017-11-03 18:10:50 +09:00
|
|
|
return redirect()->route('user.profile', ['name' => $user->name])->with('status', '削除しました。');
|
2017-08-27 04:44:53 +09:00
|
|
|
}
|
2020-05-23 17:33:58 +09:00
|
|
|
|
|
|
|
public function tools()
|
|
|
|
{
|
|
|
|
return view('ejaculation.tools');
|
|
|
|
}
|
2019-01-15 00:05:01 +09:00
|
|
|
}
|