2017-08-27 04:44:53 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Ejaculation;
|
|
|
|
use App\User;
|
|
|
|
use Carbon\Carbon;
|
2020-05-24 17:27:42 +09:00
|
|
|
use Carbon\CarbonInterface;
|
2017-08-27 04:44:53 +09:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
|
|
class UserController extends Controller
|
|
|
|
{
|
2019-02-26 22:46:40 +09:00
|
|
|
public function redirectMypage()
|
|
|
|
{
|
|
|
|
return redirect()->route('user.profile', ['name' => auth()->user()->name]);
|
|
|
|
}
|
2017-08-27 04:44:53 +09:00
|
|
|
|
|
|
|
public function profile($name)
|
|
|
|
{
|
|
|
|
$user = User::where('name', $name)->first();
|
|
|
|
if (empty($user)) {
|
|
|
|
abort(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
// チェックインの取得
|
2019-01-15 00:05:01 +09:00
|
|
|
$query = Ejaculation::select(DB::raw(
|
|
|
|
<<<'SQL'
|
2017-11-03 17:48:58 +09:00
|
|
|
id,
|
2017-11-05 01:26:52 +09:00
|
|
|
ejaculated_date,
|
2017-08-27 04:44:53 +09:00
|
|
|
note,
|
|
|
|
is_private,
|
2019-09-07 19:57:44 +09:00
|
|
|
is_too_sensitive,
|
2018-01-05 00:26:48 +09:00
|
|
|
link,
|
2020-05-22 00:07:04 +09:00
|
|
|
source,
|
2017-08-27 04:44:53 +09:00
|
|
|
to_char(lead(ejaculated_date, 1, NULL) OVER (ORDER BY ejaculated_date DESC), 'YYYY/MM/DD HH24:MI') AS before_date,
|
|
|
|
to_char(ejaculated_date - (lead(ejaculated_date, 1, NULL) OVER (ORDER BY ejaculated_date DESC)), 'FMDDD日 FMHH24時間 FMMI分') AS ejaculated_span
|
|
|
|
SQL
|
|
|
|
))
|
|
|
|
->where('user_id', $user->id);
|
|
|
|
if (!Auth::check() || $user->id !== Auth::id()) {
|
|
|
|
$query = $query->where('is_private', false);
|
|
|
|
}
|
|
|
|
$ejaculations = $query->orderBy('ejaculated_date', 'desc')
|
2018-01-08 10:28:12 +09:00
|
|
|
->with('tags')
|
2019-04-05 23:10:26 +09:00
|
|
|
->withLikes()
|
2017-08-27 04:44:53 +09:00
|
|
|
->paginate(20);
|
|
|
|
|
2018-06-09 00:30:41 +09:00
|
|
|
// よく使っているタグ
|
|
|
|
$tagsQuery = DB::table('ejaculations')
|
|
|
|
->join('ejaculation_tag', 'ejaculations.id', '=', 'ejaculation_tag.ejaculation_id')
|
|
|
|
->join('tags', 'ejaculation_tag.tag_id', '=', 'tags.id')
|
|
|
|
->selectRaw('tags.name, count(*) as count')
|
|
|
|
->where('ejaculations.user_id', $user->id);
|
|
|
|
if (!Auth::check() || $user->id !== Auth::id()) {
|
|
|
|
$tagsQuery = $tagsQuery->where('ejaculations.is_private', false);
|
|
|
|
}
|
|
|
|
$tags = $tagsQuery->groupBy('tags.name')
|
|
|
|
->orderBy('count', 'desc')
|
|
|
|
->limit(10)
|
|
|
|
->get();
|
|
|
|
|
|
|
|
return view('user.profile')->with(compact('user', 'ejaculations', 'tags'));
|
2017-11-05 01:26:52 +09:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:35:58 +09:00
|
|
|
public function stats($name)
|
2017-11-05 01:26:52 +09:00
|
|
|
{
|
|
|
|
$user = User::where('name', $name)->first();
|
|
|
|
if (empty($user)) {
|
|
|
|
abort(404);
|
2017-08-27 04:44:53 +09:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:27:42 +09:00
|
|
|
$availableMonths = $this->makeStatsAvailableMonths($user);
|
|
|
|
$graphData = $this->makeGraphData($user);
|
2019-03-30 13:29:55 +09:00
|
|
|
|
2020-05-24 17:35:58 +09:00
|
|
|
return view('user.stats.index')->with(compact('user', 'graphData', 'availableMonths'));
|
2020-05-24 17:27:42 +09:00
|
|
|
}
|
2018-01-04 15:52:04 +09:00
|
|
|
|
2020-05-24 17:27:42 +09:00
|
|
|
public function statsYearly($name, $year)
|
|
|
|
{
|
|
|
|
$user = User::where('name', $name)->first();
|
|
|
|
if (empty($user)) {
|
|
|
|
abort(404);
|
2017-11-07 00:22:40 +09:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:27:42 +09:00
|
|
|
$availableMonths = $this->makeStatsAvailableMonths($user);
|
|
|
|
$graphData = $this->makeGraphData(
|
|
|
|
$user,
|
|
|
|
Carbon::createFromDate($year, 1, 1, config('app.timezone'))->startOfDay(),
|
|
|
|
Carbon::createFromDate($year, 1, 1, config('app.timezone'))->addYear()->startOfDay()
|
|
|
|
);
|
2018-01-04 15:52:04 +09:00
|
|
|
|
2020-05-24 17:27:42 +09:00
|
|
|
return view('user.stats.yearly')
|
|
|
|
->with(compact('user', 'graphData', 'availableMonths'))
|
|
|
|
->with('currentYear', (int) $year);
|
|
|
|
}
|
2018-01-04 14:50:14 +09:00
|
|
|
|
2020-05-24 17:27:42 +09:00
|
|
|
public function statsMonthly($name, $year, $month)
|
|
|
|
{
|
|
|
|
$user = User::where('name', $name)->first();
|
|
|
|
if (empty($user)) {
|
|
|
|
abort(404);
|
2019-01-02 14:44:31 +09:00
|
|
|
}
|
2019-08-04 00:57:35 +09:00
|
|
|
|
2020-05-24 17:27:42 +09:00
|
|
|
$availableMonths = $this->makeStatsAvailableMonths($user);
|
|
|
|
$graphData = $this->makeGraphData(
|
|
|
|
$user,
|
|
|
|
Carbon::createFromDate($year, $month, 1, config('app.timezone'))->startOfDay(),
|
|
|
|
Carbon::createFromDate($year, $month, 1, config('app.timezone'))->addMonth()->startOfDay()
|
|
|
|
);
|
2019-03-05 01:08:17 +09:00
|
|
|
|
2020-05-24 17:27:42 +09:00
|
|
|
return view('user.stats.monthly')
|
|
|
|
->with(compact('user', 'graphData', 'availableMonths'))
|
|
|
|
->with('currentYear', (int) $year)
|
|
|
|
->with('currentMonth', (int) $month);
|
2017-11-05 01:26:52 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
public function okazu($name)
|
|
|
|
{
|
|
|
|
$user = User::where('name', $name)->first();
|
|
|
|
if (empty($user)) {
|
|
|
|
abort(404);
|
|
|
|
}
|
2017-08-27 04:44:53 +09:00
|
|
|
|
2018-01-06 00:35:04 +09:00
|
|
|
// チェックインの取得
|
2019-01-15 00:05:01 +09:00
|
|
|
$query = Ejaculation::select(DB::raw(
|
|
|
|
<<<'SQL'
|
2018-01-06 00:35:04 +09:00
|
|
|
id,
|
|
|
|
ejaculated_date,
|
|
|
|
note,
|
|
|
|
is_private,
|
2019-09-07 19:57:44 +09:00
|
|
|
is_too_sensitive,
|
2018-01-06 00:35:04 +09:00
|
|
|
link,
|
2020-05-22 00:07:04 +09:00
|
|
|
source,
|
2018-01-06 00:35:04 +09:00
|
|
|
to_char(lead(ejaculated_date, 1, NULL) OVER (ORDER BY ejaculated_date DESC), 'YYYY/MM/DD HH24:MI') AS before_date,
|
|
|
|
to_char(ejaculated_date - (lead(ejaculated_date, 1, NULL) OVER (ORDER BY ejaculated_date DESC)), 'FMDDD日 FMHH24時間 FMMI分') AS ejaculated_span
|
|
|
|
SQL
|
|
|
|
))
|
|
|
|
->where('user_id', $user->id)
|
|
|
|
->where('link', '<>', '');
|
|
|
|
if (!Auth::check() || $user->id !== Auth::id()) {
|
|
|
|
$query = $query->where('is_private', false);
|
|
|
|
}
|
|
|
|
$ejaculations = $query->orderBy('ejaculated_date', 'desc')
|
2018-01-08 10:28:12 +09:00
|
|
|
->with('tags')
|
2020-05-14 01:18:14 +09:00
|
|
|
->withLikes()
|
2018-01-06 00:35:04 +09:00
|
|
|
->paginate(20);
|
|
|
|
|
|
|
|
return view('user.profile')->with(compact('user', 'ejaculations'));
|
2017-08-27 04:44:53 +09:00
|
|
|
}
|
2019-04-06 00:14:05 +09:00
|
|
|
|
|
|
|
public function likes($name)
|
|
|
|
{
|
|
|
|
$user = User::where('name', $name)->first();
|
|
|
|
if (empty($user)) {
|
|
|
|
abort(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
$likes = $user->likes()
|
|
|
|
->orderBy('created_at', 'desc')
|
|
|
|
->with('ejaculation.user', 'ejaculation.tags')
|
2019-04-21 02:12:42 +09:00
|
|
|
->whereHas('ejaculation', function ($query) {
|
|
|
|
$query->where('user_id', Auth::id())
|
|
|
|
->orWhere('is_private', false);
|
|
|
|
})
|
2019-04-06 00:14:05 +09:00
|
|
|
->paginate(20);
|
|
|
|
|
|
|
|
return view('user.likes')->with(compact('user', 'likes'));
|
|
|
|
}
|
2020-05-24 17:27:42 +09:00
|
|
|
|
|
|
|
private function makeStatsAvailableMonths(User $user): array
|
|
|
|
{
|
|
|
|
$availableMonths = [];
|
|
|
|
$oldest = $user->ejaculations()->orderBy('ejaculated_date')->first();
|
|
|
|
if (isset($oldest)) {
|
|
|
|
$oldestMonth = $oldest->ejaculated_date->startOfMonth();
|
|
|
|
$currentMonth = now()->startOfMonth();
|
|
|
|
for ($month = $currentMonth; $oldestMonth <= $currentMonth; $month = $month->subMonth()) {
|
|
|
|
if (!isset($availableMonths[$month->year])) {
|
|
|
|
$availableMonths[$month->year] = [];
|
|
|
|
}
|
|
|
|
$availableMonths[$month->year][] = $month->month;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $availableMonths;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function makeGraphData(User $user, CarbonInterface $dateSince = null, CarbonInterface $dateUntil = null): array
|
|
|
|
{
|
|
|
|
if ($dateUntil === null) {
|
|
|
|
$dateUntil = now()->addMonth()->startOfMonth();
|
|
|
|
}
|
|
|
|
$dateCondition = [
|
|
|
|
['ejaculated_date', '<', $dateUntil],
|
|
|
|
];
|
|
|
|
if ($dateSince !== null) {
|
|
|
|
$dateCondition[] = ['ejaculated_date', '>=', $dateSince];
|
|
|
|
}
|
|
|
|
|
|
|
|
$groupByDay = Ejaculation::select(DB::raw(
|
|
|
|
<<<'SQL'
|
|
|
|
to_char(ejaculated_date, 'YYYY/MM/DD') AS "date",
|
|
|
|
count(*) AS "count"
|
|
|
|
SQL
|
|
|
|
))
|
|
|
|
->where('user_id', $user->id)
|
|
|
|
->where($dateCondition)
|
|
|
|
->groupBy(DB::raw("to_char(ejaculated_date, 'YYYY/MM/DD')"))
|
|
|
|
->orderBy(DB::raw("to_char(ejaculated_date, 'YYYY/MM/DD')"))
|
|
|
|
->get();
|
|
|
|
|
|
|
|
$groupByHour = Ejaculation::select(DB::raw(
|
|
|
|
<<<'SQL'
|
|
|
|
to_char(ejaculated_date, 'HH24') AS "hour",
|
|
|
|
count(*) AS "count"
|
|
|
|
SQL
|
|
|
|
))
|
|
|
|
->where('user_id', $user->id)
|
|
|
|
->where($dateCondition)
|
|
|
|
->groupBy(DB::raw("to_char(ejaculated_date, 'HH24')"))
|
|
|
|
->orderBy(DB::raw('1'))
|
|
|
|
->get();
|
|
|
|
|
|
|
|
$dailySum = [];
|
|
|
|
$monthlySum = [];
|
|
|
|
$yearlySum = [];
|
|
|
|
$dowSum = array_fill(0, 7, 0);
|
|
|
|
$hourlySum = array_fill(0, 24, 0);
|
|
|
|
|
|
|
|
// 年間グラフ用の配列初期化
|
|
|
|
if ($groupByDay->first() !== null) {
|
|
|
|
$year = Carbon::createFromFormat('Y/m/d', $groupByDay->first()->date)->year;
|
|
|
|
$currentYear = date('Y');
|
|
|
|
for (; $year <= $currentYear; $year++) {
|
|
|
|
$yearlySum[$year] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($groupByDay as $data) {
|
|
|
|
$date = Carbon::createFromFormat('Y/m/d', $data->date);
|
|
|
|
$yearAndMonth = $date->format('Y/m');
|
|
|
|
|
|
|
|
$dailySum[$date->timestamp] = $data->count;
|
|
|
|
$yearlySum[$date->year] += $data->count;
|
|
|
|
$dowSum[$date->dayOfWeek] += $data->count;
|
|
|
|
$monthlySum[$yearAndMonth] = ($monthlySum[$yearAndMonth] ?? 0) + $data->count;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($groupByHour as $data) {
|
|
|
|
$hour = (int)$data->hour;
|
|
|
|
$hourlySum[$hour] += $data->count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'dailySum' => $dailySum,
|
|
|
|
'dowSum' => $dowSum,
|
|
|
|
'monthlySum' => $monthlySum,
|
|
|
|
'yearlyKey' => array_keys($yearlySum),
|
|
|
|
'yearlySum' => array_values($yearlySum),
|
|
|
|
'hourlyKey' => array_keys($hourlySum),
|
|
|
|
'hourlySum' => array_values($hourlySum),
|
|
|
|
];
|
|
|
|
}
|
2017-08-27 04:44:53 +09:00
|
|
|
}
|