AdminController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Http\Traits\CommonTrait;
  5. use App\Models\Visit;
  6. use App\Models\User;
  7. use DateTime;
  8. class AdminController extends Controller
  9. {
  10. use CommonTrait;
  11. /**
  12. * Create a new controller instance.
  13. *
  14. * @return void
  15. */
  16. public function __construct()
  17. {
  18. $this->middleware('auth');
  19. }
  20. /**
  21. * Show the application dashboard.
  22. *
  23. * @return \Illuminate\Contracts\Support\Renderable
  24. */
  25. public function index(Request $request)
  26. {
  27. // 관리자만 접속 가능하다.
  28. if(!$request->user()->is_admin) {
  29. abort(401);
  30. }
  31. $visitStats = $this->_visitStats();
  32. $userStats = $this->_userStats();
  33. return view('admin.index', [
  34. 'visitStats' => $visitStats,
  35. 'userStats' => $userStats
  36. ]);
  37. }
  38. /**
  39. * 방문자 통계
  40. */
  41. private function _visitStats()
  42. {
  43. $visitModel = new Visit();
  44. $monthlyData = $visitModel->getMonthlyData();
  45. $arrDt = array_column($monthlyData, 'dt');
  46. $arrCntNew = array_column($monthlyData, 'cntNew');
  47. $arrCntRe = array_column($monthlyData, 'cntRe');
  48. $arrCntTot = array_column($monthlyData, 'cntTot');
  49. $dates = $this->setArraySingQuote($arrDt);
  50. $cntNews = $this->setArraySingQuote($arrCntNew);
  51. $cntRes = $this->setArraySingQuote($arrCntRe);
  52. $cntTots = $this->setArraySingQuote($arrCntTot);
  53. return [
  54. 'monthlyData' => $visitModel->getMonthlyData(),
  55. 'visitorTodayCount' => $visitModel->todayCount(),
  56. 'visitorYesterdayCount' => $visitModel->yesterdayCount(),
  57. 'visitorTotalCount' => $visitModel->totalCount(),
  58. 'dates' => $dates,
  59. 'cntNews' => $cntNews,
  60. 'cntRes' => $cntRes,
  61. 'cntTots' => $cntTots,
  62. 'totalCntNew' => array_sum($arrCntNew),
  63. 'totalCntRe' => array_sum($arrCntRe),
  64. 'totalCntTo' => array_sum($arrCntTot)
  65. ];
  66. }
  67. /**
  68. * 회원가입 통계
  69. */
  70. private function _userStats()
  71. {
  72. $userModel = new User();
  73. $dailyData = $userModel->dailyStats();
  74. $mapDailyData = array_column($dailyData, 'value', 'date');
  75. $startDt = new DateTime(now()->subDays(14)->toDateString());
  76. $endDt = new DateTime(now()->toDateString());
  77. $dates = [];
  78. for ($i = $startDt; $i <= $endDt; $i->modify('+1 day')) {
  79. $dt = $i->format("Y-m-d");
  80. $dates[$dt] = (array_key_exists($dt, $mapDailyData) ? $mapDailyData[$dt] : 0);
  81. }
  82. $todayCount = $dates[date('Y-m-d')];
  83. $cntNews = $this->setArraySingQuote(array_values($dates));
  84. $dates = $this->setArraySingQuote(array_keys($dates));
  85. return [
  86. 'dailyData' => $dailyData, // 일별 통계
  87. 'dates' => $dates,
  88. 'cntNews' => $cntNews,
  89. 'todayCount' => $todayCount, // 오늘 가입자
  90. 'weeklyUserCount' => $userModel->totalWeeklyCount(), // 주간 회원가입자 수
  91. 'monthlyUserCount' => $userModel->totalMonthlyCount(), // 월별 회원가입자 수
  92. 'totalUserCount' => $userModel->totalUserCount(), // 전체 회원가입자 수
  93. ];
  94. }
  95. }