2017-08-27 04:44:53 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Ejaculation;
|
|
|
|
use App\User;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
|
|
class UserController extends Controller
|
|
|
|
{
|
|
|
|
//
|
|
|
|
|
|
|
|
public function profile($name)
|
|
|
|
{
|
|
|
|
$user = User::where('name', $name)->first();
|
|
|
|
if (empty($user)) {
|
|
|
|
abort(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
// チェックインの取得
|
|
|
|
$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,
|
2018-01-05 00:26:48 +09:00
|
|
|
link,
|
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')
|
2017-08-27 04:44:53 +09:00
|
|
|
->paginate(20);
|
|
|
|
|
2017-11-05 01:26:52 +09:00
|
|
|
return view('user.profile')->with(compact('user', 'ejaculations'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function stats($name)
|
|
|
|
{
|
|
|
|
$user = User::where('name', $name)->first();
|
|
|
|
if (empty($user)) {
|
|
|
|
abort(404);
|
2017-08-27 04:44:53 +09:00
|
|
|
}
|
|
|
|
|
2017-11-07 00:22:40 +09:00
|
|
|
$groupByDay = Ejaculation::select(DB::raw(<<<'SQL'
|
|
|
|
to_char(ejaculated_date, 'YYYY/MM/DD') AS "date",
|
|
|
|
count(*) AS "count"
|
|
|
|
SQL
|
|
|
|
))
|
|
|
|
->where('user_id', $user->id)
|
|
|
|
->groupBy(DB::raw("to_char(ejaculated_date, 'YYYY/MM/DD')"))
|
|
|
|
->orderBy(DB::raw("to_char(ejaculated_date, 'YYYY/MM/DD')"))
|
|
|
|
->get();
|
2018-01-04 15:52:04 +09:00
|
|
|
|
|
|
|
$dailySum = [];
|
|
|
|
$monthlySum = [];
|
|
|
|
$yearlySum = [];
|
|
|
|
|
|
|
|
// 年間グラフ用の配列初期化
|
|
|
|
if ($groupByDay->first() !== null) {
|
|
|
|
$year = Carbon::createFromFormat('Y/m/d', $groupByDay->first()->date)->year;
|
|
|
|
$currentYear = date('Y');
|
|
|
|
for (; $year <= $currentYear; $year++) {
|
|
|
|
$yearlySum[$year] = 0;
|
|
|
|
}
|
2017-11-07 00:22:40 +09:00
|
|
|
}
|
|
|
|
|
2018-01-04 15:52:04 +09:00
|
|
|
// 月間グラフ用の配列初期化
|
|
|
|
$month = Carbon::now()->subMonth(11)->firstOfMonth(); // 直近12ヶ月
|
|
|
|
for ($i = 0; $i < 12; $i++) {
|
|
|
|
$monthlySum[$month->format('Y/m')] = 0;
|
|
|
|
$month->addMonth();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
if (isset($monthlySum[$yearAndMonth])) {
|
|
|
|
$monthlySum[$yearAndMonth] += $data->count;
|
2018-01-04 14:50:14 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 15:52:04 +09:00
|
|
|
return view('user.stats')->with(compact('user', 'dailySum', 'monthlySum', 'yearlySum'));
|
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
|
|
|
// チェックインの取得
|
|
|
|
$query = Ejaculation::select(DB::raw(<<<'SQL'
|
|
|
|
id,
|
|
|
|
ejaculated_date,
|
|
|
|
note,
|
|
|
|
is_private,
|
|
|
|
link,
|
|
|
|
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')
|
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
|
|
|
}
|
|
|
|
}
|