HomeController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\PartnerDiscount;
  4. use App\Models\UsefulLink;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Hash;
  9. use Illuminate\Support\Facades\Http;
  10. use Illuminate\Support\Facades\Storage;
  11. use Illuminate\Support\Facades\URL;
  12. class HomeController extends Controller
  13. {
  14. const TIME_LEFT_BG_SUCCESS = 'bg-success';
  15. const TIME_LEFT_BG_WARNING = 'bg-warning';
  16. const TIME_LEFT_BG_DANGER = 'bg-danger';
  17. public function __construct()
  18. {
  19. $this->middleware('auth');
  20. }
  21. public function callHome()
  22. {
  23. if (Storage::exists('callHome')) {
  24. return;
  25. }
  26. Http::asForm()->post('https://market.controlpanel.gg/callhome.php', [
  27. 'id' => Hash::make(URL::current()),
  28. ]);
  29. Storage::put('callHome', 'This is only used to count the installations of cpgg.');
  30. }
  31. /**
  32. * @description Get the Background Color for the Days-Left-Box in HomeView
  33. *
  34. * @param float $daysLeft
  35. * @return string
  36. */
  37. public function getTimeLeftBoxBackground(float $daysLeft): string
  38. {
  39. if ($daysLeft >= 15) {
  40. return $this::TIME_LEFT_BG_SUCCESS;
  41. }
  42. if ($daysLeft <= 7) {
  43. return $this::TIME_LEFT_BG_DANGER;
  44. }
  45. return $this::TIME_LEFT_BG_WARNING;
  46. }
  47. /**
  48. * @description Set "hours", "days" or nothing behind the remaining time
  49. *
  50. * @param float $daysLeft
  51. * @param float $hoursLeft
  52. * @return string|void
  53. */
  54. public function getTimeLeftBoxUnit(float $daysLeft, float $hoursLeft)
  55. {
  56. if ($daysLeft > 1) {
  57. return __('days');
  58. }
  59. return $hoursLeft < 1 ? null : __('hours');
  60. }
  61. /**
  62. * @description Get the Text for the Days-Left-Box in HomeView
  63. *
  64. * @param float $daysLeft
  65. * @param float $hoursLeft
  66. * @return string
  67. */
  68. public function getTimeLeftBoxText(float $daysLeft, float $hoursLeft)
  69. {
  70. if ($daysLeft > 1) {
  71. return strval(number_format($daysLeft, 0));
  72. }
  73. return $hoursLeft < 1 ? __('You ran out of Credits') : strval($hoursLeft);
  74. }
  75. /** Show the application dashboard. */
  76. public function index(Request $request)
  77. {
  78. $usage = Auth::user()->creditUsage();
  79. $credits = Auth::user()->Credits();
  80. $bg = '';
  81. $boxText = '';
  82. $unit = '';
  83. /** Build our Time-Left-Box */
  84. if ($credits > 0.01 and $usage > 0) {
  85. $daysLeft = number_format(($credits * 30) / $usage, 2, '.', '');
  86. $hoursLeft = number_format($credits / ($usage / 30 / 24), 2, '.', '');
  87. $bg = $this->getTimeLeftBoxBackground($daysLeft);
  88. $boxText = $this->getTimeLeftBoxText($daysLeft, $hoursLeft);
  89. $unit = $daysLeft < 1 ? ($hoursLeft < 1 ? null : __('hours')) : __('days');
  90. }
  91. $this->callhome();
  92. // RETURN ALL VALUES
  93. return view('home')->with([
  94. 'usage' => $usage,
  95. 'credits' => $credits,
  96. 'useful_links' => UsefulLink::all()->sortBy('id'),
  97. 'bg' => $bg,
  98. 'boxText' => $boxText,
  99. 'unit' => $unit,
  100. 'numberOfReferrals' => DB::table('user_referrals')->where('referral_id', '=', Auth::user()->id)->count(),
  101. 'partnerDiscount' => PartnerDiscount::where('user_id', Auth::user()->id)->first(),
  102. 'myDiscount' => PartnerDiscount::getDiscount(),
  103. ]);
  104. }
  105. }